Skip to content

Commit df14f9a

Browse files
committed
removed some redundancies in standalone tests
1 parent 81047e7 commit df14f9a

File tree

4 files changed

+254
-286
lines changed

4 files changed

+254
-286
lines changed

graalpython/com.oracle.graal.python.test/src/tests/standalone/micronaut/hello/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<exec.mainClass>example.micronaut.Application</exec.mainClass>
2424

2525
<!-- graalpy -->
26-
<graalpy.version>24.1.0</graalpy.version>
26+
<graalpy.version>{graalpy-maven-plugin-version}</graalpy.version>
2727
<graalpy.edition>python-community</graalpy.edition>
2828

2929
</properties>

graalpython/com.oracle.graal.python.test/src/tests/standalone/test_micronaut.py

Lines changed: 12 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -38,93 +38,14 @@
3838
# SOFTWARE.
3939

4040
import os
41-
import subprocess
4241
import unittest
4342
import shutil
4443
import difflib
4544
import tempfile
46-
import sys
45+
import util
4746

4847
is_enabled = 'ENABLE_MICRONAUT_UNITTESTS' in os.environ and os.environ['ENABLE_MICRONAUT_UNITTESTS'] == "true"
4948

50-
MAVEN_VERSION = "3.9.6"
51-
52-
def run_cmd(cmd, env, cwd=None):
53-
out = []
54-
out.append(f"Executing:\n {cmd=}\n")
55-
process = subprocess.Popen(cmd, env=env, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, text=True, errors='backslashreplace')
56-
for line in iter(process.stdout.readline, ""):
57-
out.append(line)
58-
return "".join(out), process.wait()
59-
60-
def patch_pom(pom):
61-
if custom_repos := os.environ.get("MAVEN_REPO_OVERRIDE"):
62-
repos = []
63-
pluginRepos = []
64-
for idx, custom_repo in enumerate(custom_repos.split(",")):
65-
repos.append(f"""
66-
<repository>
67-
<id>myrepo{idx}</id>
68-
<url>{custom_repo}</url>
69-
<releases>
70-
<enabled>true</enabled>
71-
<updatePolicy>never</updatePolicy>
72-
</releases>
73-
<snapshots>
74-
<enabled>true</enabled>
75-
<updatePolicy>never</updatePolicy>
76-
</snapshots>
77-
</repository>
78-
""")
79-
pluginRepos.append(f"""
80-
<pluginRepository>
81-
<id>myrepo{idx}</id>
82-
<url>{custom_repo}</url>
83-
<releases>
84-
<enabled>true</enabled>
85-
</releases>
86-
<snapshots>
87-
<enabled>true</enabled>
88-
</snapshots>
89-
</pluginRepository>
90-
""")
91-
92-
with open(pom, "r") as f:
93-
contents = f.read()
94-
with open(pom, "w") as f:
95-
f.write(contents.replace("</project>", """
96-
<repositories>
97-
""" + '\n'.join(repos) + """
98-
</repositories>
99-
<pluginRepositories>
100-
""" + '\n'.join(pluginRepos) + """
101-
</pluginRepositories>
102-
</project>
103-
"""))
104-
105-
def get_gp():
106-
graalpy_home = os.environ["PYTHON_STANDALONE_HOME"]
107-
graalpy = get_executable(os.path.join(graalpy_home, "bin", "graalpy"))
108-
109-
if not os.path.isfile(graalpy):
110-
print(
111-
"Micronaut extension tests require a GraalPy standalone.",
112-
"Please point the JAVA_HOME and PYTHON_STANDALONE_HOME environment variables properly.",
113-
f"{graalpy_home=}",
114-
"graalpy exits: " + str(os.path.exists(graalpy)),
115-
sep="\n",
116-
)
117-
assert False
118-
return graalpy
119-
120-
def get_graalvm_version():
121-
graalvmVersion, _ = run_cmd([get_gp(), "-c", "print(__graalpython__.get_graalvm_version(), end='')"], os.environ.copy())
122-
# when JLine is cannot detect a terminal, it prints logging info
123-
graalvmVersion = graalvmVersion.split("\n")[-1]
124-
# we never test -dev versions here, we always pretend to use release versions
125-
graalvmVersion = graalvmVersion.split("-dev")[0]
126-
return graalvmVersion
127-
12849
def create_hello_app(hello_app_dir, target_dir):
12950
for root, dirs, files in os.walk(hello_app_dir):
13051
for file in files:
@@ -149,7 +70,7 @@ def create_hello_app(hello_app_dir, target_dir):
14970
with open(pom, 'w') as f:
15071
for line in lines:
15172
if "{graalpy-maven-plugin-version}" in line:
152-
line = line.replace("{graalpy-maven-plugin-version}", get_graalvm_version())
73+
line = line.replace("{graalpy-maven-plugin-version}", util.get_graalvm_version())
15374
f.write(line)
15475

15576
def diff_texts(a, b, a_filename, b_filename):
@@ -174,39 +95,6 @@ def check_golden_file(file, golden):
17495

17596
return found_diff
17697

177-
def print_output(out):
178-
print("============== output =============")
179-
for line in out:
180-
print(line, end="")
181-
print("\n========== end of output ==========")
182-
183-
def check_ouput(txt, out, contains=True):
184-
if contains and txt not in out:
185-
print_output(out)
186-
assert False, f"expected '{txt}' in output"
187-
elif not contains and txt in out:
188-
print_output(out)
189-
assert False, f"did not expect '{txt}' in output"
190-
191-
def get_mvn_wrapper(project_dir, env):
192-
if 'win32' != sys.platform:
193-
cmd = [shutil.which('mvn'), "--batch-mode", "wrapper:wrapper", f"-Dmaven={MAVEN_VERSION}"]
194-
out, return_code = run_cmd(cmd, env, cwd=project_dir)
195-
check_ouput("BUILD SUCCESS", out)
196-
mvn_cmd = [os.path.abspath(os.path.join(project_dir, "mvnw")), "--batch-mode"]
197-
else:
198-
# TODO installing mvn wrapper with the current mvn 3.3.9 on gates does not work
199-
# we have to provide the mvnw.bat script
200-
mvnw_dir = os.path.join(os.path.dirname(__file__), "mvnw")
201-
mvn_cmd = [os.path.abspath(os.path.join(mvnw_dir, "mvnw.cmd")), "--batch-mode"]
202-
203-
print("mvn --version ...")
204-
cmd = mvn_cmd + ["--version"]
205-
out, return_code = run_cmd(cmd, env, cwd=project_dir)
206-
check_ouput("3.9.6", out)
207-
print_output(out)
208-
return mvn_cmd
209-
21098
class MicronautAppTest(unittest.TestCase):
21199
def setUpClass(self):
212100
if not is_enabled:
@@ -222,23 +110,23 @@ def test_hello_app(self):
222110
create_hello_app(hello_app_dir, target_dir)
223111

224112
pom_file = os.path.join(target_dir, "pom.xml")
225-
patch_pom(pom_file)
113+
util.patch_pom_repositories(pom_file)
226114

227-
mvn_cmd = get_mvn_wrapper(target_dir, self.env)
115+
mvn_cmd = util.get_mvn_wrapper(target_dir, self.env)
228116

229117
# clean
230118
print("clean micronaut hello app ...")
231119
cmd = mvn_cmd + ["clean"]
232-
out, return_code = run_cmd(cmd, self.env, cwd=target_dir)
233-
check_ouput("BUILD SUCCESS", out)
120+
out, return_code = util.run_cmd(cmd, self.env, cwd=target_dir)
121+
util.check_ouput("BUILD SUCCESS", out)
234122

235123
# build
236124
# java unittests are executed during the build
237125
print("build micronaut hello app ...")
238126
cmd = mvn_cmd + ["package"]
239-
out, return_code = run_cmd(cmd, self.env, cwd=target_dir)
240-
check_ouput("BUILD SUCCESS", out)
241-
check_ouput("=== CREATED REPLACE CONTEXT ===", out, False)
127+
out, return_code = util.run_cmd(cmd, self.env, cwd=target_dir)
128+
util.check_ouput("BUILD SUCCESS", out)
129+
util.check_ouput("=== CREATED REPLACE CONTEXT ===", out, False)
242130

243131
# build and execute tests with a custom graalpy context factory
244132
source_file = os.path.join(hello_app_dir, "src/main/java/example/micronaut/ContextFactory.j")
@@ -247,8 +135,8 @@ def test_hello_app(self):
247135

248136
print("build micronaut hello app with custom context factory ...")
249137
cmd = mvn_cmd + ["package"]
250-
out, return_code = run_cmd(cmd, self.env, cwd=target_dir)
251-
check_ouput("BUILD SUCCESS", out)
252-
check_ouput("=== CREATED REPLACE CONTEXT ===", out)
138+
out, return_code = util.run_cmd(cmd, self.env, cwd=target_dir)
139+
util.check_ouput("BUILD SUCCESS", out)
140+
util.check_ouput("=== CREATED REPLACE CONTEXT ===", out)
253141

254142
unittest.skip_deselected_test_functions(globals())

0 commit comments

Comments
 (0)