Skip to content

Commit

Permalink
Add unit tests release transpiler argument types
Browse files Browse the repository at this point in the history
Added unit tests to validate the types of the arguments passed to the release transpiler/combiner.
  • Loading branch information
jonathan-eq committed Aug 14, 2023
1 parent 710cdd5 commit 6554b09
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 8 deletions.
2 changes: 1 addition & 1 deletion komodo/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def get_matrix(
current release matrix."""
for product in itertools.product(rhel_versions, py_versions):
rh_ver, py_ver = product
yield (f"rhel{rh_ver}", f"py{py_ver.replace('.', '')}")
yield (f"rhel{rh_ver}", f"py{str(py_ver).replace('.', '')}")


def format_release(base: str, rhel_ver: str, py_ver: str) -> str:
Expand Down
13 changes: 6 additions & 7 deletions komodo/release_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,24 +217,23 @@ def main():

def checkValidPythonVersion(input: str) -> list:
output_list = []
for item in input.split(','):
if (re.match(r"^[2,3](\.\d+)?$", item) == None):
for item in input.split(","):
if re.match(r"^[2,3](\.\d+)?$", item) == None:
raise TypeError(item)
else:
output_list.append(item)
return output_list

matrix_parser.add_argument(
"--py_coords",
help="""Comma delimitated list of python versions to be combined,
for example, "3.6,3.8" (without spaces).
If None, the release files in release-folder will be used to imply
the versions to combine.""",
type=checkValidPythonVersion, #lambda s: [x for x in s.split(',') if (re.match(r"^[2,3](\.\d+)?$", x) != None)], #re.split(",", s),
type=checkValidPythonVersion, # lambda s: [x for x in s.split(',') if (re.match(r"^[2,3](\.\d+)?$", x) != None)], #re.split(",", s),
required=False,
default=None,
)



transpile_parser = subparsers.add_parser(
"transpile",
Expand Down Expand Up @@ -266,14 +265,14 @@ def checkValidPythonVersion(input: str) -> list:


def check_if_valid_file(path: str) -> str:
if (os.path.isfile(path)):
if os.path.isfile(path):
return path
raise TypeError(path)


def dir_path(should_be_valid_path: str) -> str:
if os.path.isdir(should_be_valid_path):
return should_be_valid_path
return should_be_valid_path
raise TypeError(should_be_valid_path)


Expand Down
224 changes: 224 additions & 0 deletions tests/test_release_transpiler_argument_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import os
from os.path import abspath, dirname

import pytest

RELEASE_TRANSPILER_FILE_PATH = abspath(
dirname(dirname(abspath(__file__))) + "/komodo/release_transpiler.py"
)
VALID_RELEASE_FOLDER = abspath(dirname(__file__) + "/data/test_releases")
VALID_RELEASE_BASE = "2020.01.a1"
VALID_OVERRIDE_MAPPING_FILE = abspath(
dirname(dirname(__file__)) + "/examples/stable.yml"
) # "/Users/JONAK/Documents/FMU/SCOUT/komodo/examples/stable.yml"
VALID_PYTHON_COORDS = "3.6,3.8"


# TESTING COMBINER
@pytest.mark.parametrize(
"args, expectedExitCode",
[
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE} --py_coords 3.8,3.6",
0,
),
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE}",
0,
),
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE} --py_coords py3.8",
512,
),
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE} --py_coords test",
512,
),
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE} --py_coords 3.8, 3.6",
512,
),
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE} --py_coords true",
512,
),
],
)
def test_combine_py_coords_type(args, expectedExitCode):
exit_code = os.system(f"{RELEASE_TRANSPILER_FILE_PATH} combine {args}")
assert exit_code == expectedExitCode


@pytest.mark.parametrize(
"args, expectedExitCode",
[
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE} --py_coords 3.8,3.6",
0,
),
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE}",
0,
),
(
f"--release-folder FOLDER/DOES/NOT/EXIST --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE}",
512,
),
(
f"--release-folder true --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE}",
512,
),
(
f"--release-folder null --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE}",
512,
),
(
f"--release-folder random --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE}",
512,
),
],
)
def test_combine_py_release_folder_type(args, expectedExitCode):
exit_code = os.system(f"{RELEASE_TRANSPILER_FILE_PATH} combine {args}")
assert exit_code == expectedExitCode


@pytest.mark.parametrize(
"args, expectedExitCode",
[
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE} --py_coords 3.8,3.6",
0,
),
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE}",
0,
),
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping FAKE/PATH/mapping.yml",
512,
),
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping fake_mapping.yml",
512,
),
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping random_string",
512,
),
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping 0",
512,
),
],
)
def test_combine_py_override_mapping_type(args, expectedExitCode):
exit_code = os.system(f"{RELEASE_TRANSPILER_FILE_PATH} combine {args}")
assert exit_code == expectedExitCode


@pytest.mark.parametrize(
"args, expectedExitCode",
[
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base {VALID_RELEASE_BASE} --override-mapping {VALID_OVERRIDE_MAPPING_FILE} --py_coords 3.8,3.6",
0,
),
(
f"--release-folder {VALID_RELEASE_FOLDER} --release-base INVALID_RELEASE_BASE --override-mapping {VALID_OVERRIDE_MAPPING_FILE} --py_coords 3.8,3.6",
256,
),
],
)
def test_combine_py_release_base_type(args, expectedExitCode):
exit_code = os.system(f"{RELEASE_TRANSPILER_FILE_PATH} combine {args}")
assert exit_code == expectedExitCode


# TESTING TRANSPILER
VALID_MATRIX_FILE = abspath(dirname(__file__) + "/data/test_release_matrix.yml")
VALID_OUTPUT_FOLDER = abspath(dirname(__file__))
VALID_MATRIX_COORDINATES = "{rhel: ['7'], py: ['3.8']}"


@pytest.mark.parametrize(
"args, expectedExitCode",
[
(
f"--matrix-file {VALID_MATRIX_FILE} --output-folder {VALID_RELEASE_FOLDER} --matrix-coordinates '{VALID_MATRIX_COORDINATES}'",
0,
),
(
f"--matrix-file {VALID_MATRIX_FILE} --output-folder {VALID_RELEASE_FOLDER}",
0,
),
(
f"--matrix-file {dirname(VALID_MATRIX_FILE)} --output-folder {VALID_RELEASE_FOLDER}",
512,
),
(
f"--matrix-file {VALID_MATRIX_FILE}/does_not_exist.yaml --output-folder {VALID_RELEASE_FOLDER}",
512,
),
(f"--matrix-file random_string --output-folder {VALID_RELEASE_FOLDER}", 512),
(f"--matrix-file null --output-folder {VALID_RELEASE_FOLDER}", 512),
],
)
def test_transpile_py_matrix_file_type(args, expectedExitCode):
exit_code = os.system(f"{RELEASE_TRANSPILER_FILE_PATH} transpile {args}")
assert exit_code == expectedExitCode


@pytest.mark.parametrize(
"args, expectedExitCode",
[
(
f"--matrix-file {VALID_MATRIX_FILE} --output-folder {VALID_RELEASE_FOLDER} --matrix-coordinates '{VALID_MATRIX_COORDINATES}'",
0,
),
(
f"--matrix-file {VALID_MATRIX_FILE} --output-folder {VALID_RELEASE_FOLDER}",
0,
),
(
f"--matrix-file {VALID_MATRIX_FILE} --output-folder {VALID_RELEASE_FOLDER}/does_not_exist",
512,
),
(f"--matrix-file {VALID_MATRIX_FILE} --output-folder random_string", 512),
(f"--matrix-file {VALID_MATRIX_FILE} --output-folder null", 512),
(f"--matrix-file {VALID_MATRIX_FILE} --output-folder 0", 512),
],
)
def test_transpile_py_output_folder_type(args, expectedExitCode):
exit_code = os.system(f"{RELEASE_TRANSPILER_FILE_PATH} transpile {args}")
assert exit_code == expectedExitCode


@pytest.mark.parametrize(
"args, expectedExitCode",
[
(
f"--matrix-file {VALID_MATRIX_FILE} --output-folder {VALID_RELEASE_FOLDER} --matrix-coordinates '{VALID_MATRIX_COORDINATES}'",
0,
),
(
f"--matrix-file {VALID_MATRIX_FILE} --output-folder {VALID_RELEASE_FOLDER}",
0,
),
(
f"--matrix-file {VALID_MATRIX_FILE} --output-folder {VALID_RELEASE_FOLDER} --matrix-coordinates 0",
256,
),
(
f"--matrix-file {VALID_MATRIX_FILE} --output-folder {VALID_RELEASE_FOLDER} --matrix-coordinates random_string",
256,
),
(
f"--matrix-file {VALID_MATRIX_FILE} --output-folder {VALID_RELEASE_FOLDER} --matrix-coordinates false",
256,
),
],
)
def test_transpile_py_matrix_coordinates_type(args, expectedExitCode):
exit_code = os.system(f"{RELEASE_TRANSPILER_FILE_PATH} transpile {args}")
assert exit_code == expectedExitCode

0 comments on commit 6554b09

Please sign in to comment.