Skip to content

Commit

Permalink
Merge pull request #61 from jmolina4/increase_readibility_for_java_ve…
Browse files Browse the repository at this point in the history
…rsion_test

Refactoring java version test
  • Loading branch information
lauris-tw authored Jul 8, 2024
2 parents baafd69 + 54ceb47 commit 585676d
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions tests/integration/test_validate_spark_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,34 @@ def test_java_home_is_set():
assert java_home is not None, "Environment variable 'JAVA_HOME' is not set but is required by pySpark to work."


def test_java_version_is_greater_or_equal_11():
java_version_output = get_java_version_output()
print(f"\n`java -version` returned\n{java_version_output}")

version_line = extract_version_line(java_version_output)
if not version_line:
pytest.fail("Couldn't find version information in `java -version` output.")

major_version = parse_major_version(version_line)
if major_version is None:
pytest.fail(f"Couldn't parse Java version from {version_line}.")

expected_major_version = 11
def test_java_version_minimum_requirement(expected_major_version=11):
version_line = __extract_version_line(__java_version_output())
major_version = __parse_major_version(version_line)
assert major_version >= expected_major_version, (f"Major version {major_version} is not recent enough, "
f"we need at least version {expected_major_version}.")


def get_java_version_output():
return subprocess.check_output(['java', '-version'], stderr=subprocess.STDOUT).decode("utf-8")
def __java_version_output():
java_version = subprocess.check_output(['java', '-version'], stderr=subprocess.STDOUT).decode("utf-8")
print(f"\n`java -version` returned\n{java_version}")
return java_version


def extract_version_line(java_version_output):
for line in java_version_output.splitlines():
if "version" in line:
return line
return None
def __extract_version_line(java_version_output):
version_line = next((line for line in java_version_output.splitlines() if "version" in line), None)
if not version_line:
pytest.fail("Couldn't find version information in `java -version` output.")
return version_line


def parse_major_version(version_line):
def __parse_major_version(version_line):
version_regex = re.compile(r'version "(?P<major>\d+)\.(?P<minor>\d+)\.\d+"')
match = version_regex.search(version_line)
if not match:
return None
major_version = int(match.group("major"))
if major_version == 1:
major_version = int(match.group("minor"))
if major_version is None:
pytest.fail(f"Couldn't parse Java version from {version_line}.")
return major_version

0 comments on commit 585676d

Please sign in to comment.