38
38
# SOFTWARE.
39
39
40
40
import os
41
- import subprocess
42
41
import unittest
43
42
import shutil
44
43
import difflib
45
44
import tempfile
46
- import sys
45
+ import util
47
46
48
47
is_enabled = 'ENABLE_MICRONAUT_UNITTESTS' in os .environ and os .environ ['ENABLE_MICRONAUT_UNITTESTS' ] == "true"
49
48
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
-
128
49
def create_hello_app (hello_app_dir , target_dir ):
129
50
for root , dirs , files in os .walk (hello_app_dir ):
130
51
for file in files :
@@ -149,7 +70,7 @@ def create_hello_app(hello_app_dir, target_dir):
149
70
with open (pom , 'w' ) as f :
150
71
for line in lines :
151
72
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 ())
153
74
f .write (line )
154
75
155
76
def diff_texts (a , b , a_filename , b_filename ):
@@ -174,39 +95,6 @@ def check_golden_file(file, golden):
174
95
175
96
return found_diff
176
97
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
-
210
98
class MicronautAppTest (unittest .TestCase ):
211
99
def setUpClass (self ):
212
100
if not is_enabled :
@@ -222,23 +110,23 @@ def test_hello_app(self):
222
110
create_hello_app (hello_app_dir , target_dir )
223
111
224
112
pom_file = os .path .join (target_dir , "pom.xml" )
225
- patch_pom (pom_file )
113
+ util . patch_pom_repositories (pom_file )
226
114
227
- mvn_cmd = get_mvn_wrapper (target_dir , self .env )
115
+ mvn_cmd = util . get_mvn_wrapper (target_dir , self .env )
228
116
229
117
# clean
230
118
print ("clean micronaut hello app ..." )
231
119
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 )
234
122
235
123
# build
236
124
# java unittests are executed during the build
237
125
print ("build micronaut hello app ..." )
238
126
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 )
242
130
243
131
# build and execute tests with a custom graalpy context factory
244
132
source_file = os .path .join (hello_app_dir , "src/main/java/example/micronaut/ContextFactory.j" )
@@ -247,8 +135,8 @@ def test_hello_app(self):
247
135
248
136
print ("build micronaut hello app with custom context factory ..." )
249
137
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 )
253
141
254
142
unittest .skip_deselected_test_functions (globals ())
0 commit comments