@@ -11,23 +11,39 @@ def test_java_home_is_set():
1111
1212
1313def test_java_version_is_greater_or_equal_11 ():
14- version_regex = re .compile (r'(?P<major>\d+)\.(?P<minor>\d+)\.\w+' )
15-
16- java_version_output = subprocess .check_output (['java' , '-version' ], stderr = subprocess .STDOUT ).decode ("utf-8" )
14+ java_version_output = get_java_version_output ()
1715 print (f"\n `java -version` returned\n { java_version_output } " )
1816
19- version_number = java_version_output .splitlines ()[0 ].split ('"' )[1 ].strip ('"' )
20- print (f"Assuming { version_number = } is the version to check." )
21-
22- regex_match = version_regex .search (version_number )
23- if not regex_match :
24- pytest .fail (f"Couldn't parse Java version from { version_number = } using { version_regex = } ." )
25- if regex_match ["major" ] == "1" :
26- # we need to jump this hoop due to Java version naming conventions - it's fun:
27- # https://softwareengineering.stackexchange.com/questions/175075/why-is-java-version-1-x-referred-to-as-java-x
28- actual_major_version = int (regex_match ["minor" ])
29- else :
30- actual_major_version = int (regex_match ["major" ])
17+ version_line = extract_version_line (java_version_output )
18+ if not version_line :
19+ pytest .fail ("Couldn't find version information in `java -version` output." )
20+
21+ major_version = parse_major_version (version_line )
22+ if major_version is None :
23+ pytest .fail (f"Couldn't parse Java version from { version_line } ." )
24+
3125 expected_major_version = 11
32- assert actual_major_version >= expected_major_version , (f"Major version { actual_major_version } is not recent "
33- f"enough, we need at least version { expected_major_version } ." )
26+ assert major_version >= expected_major_version , (f"Major version { major_version } is not recent enough, "
27+ f"we need at least version { expected_major_version } ." )
28+
29+
30+ def get_java_version_output ():
31+ return subprocess .check_output (['java' , '-version' ], stderr = subprocess .STDOUT ).decode ("utf-8" )
32+
33+
34+ def extract_version_line (java_version_output ):
35+ for line in java_version_output .splitlines ():
36+ if "version" in line :
37+ return line
38+ return None
39+
40+
41+ def parse_major_version (version_line ):
42+ version_regex = re .compile (r'version "(?P<major>\d+)\.(?P<minor>\d+)\.\d+"' )
43+ match = version_regex .search (version_line )
44+ if not match :
45+ return None
46+ major_version = int (match .group ("major" ))
47+ if major_version == 1 :
48+ major_version = int (match .group ("minor" ))
49+ return major_version
0 commit comments