From cc212014840c94377516f7251e1675f60660e8e5 Mon Sep 17 00:00:00 2001 From: Denis Angilella Date: Tue, 9 Mar 2021 17:34:17 +0100 Subject: [PATCH] [exec] Use --config-file override in test command --- exec/src/klio_exec/cli.py | 5 ++- exec/tests/unit/test_cli.py | 85 ++++++++++++++++++++++++++++++++----- 2 files changed, 78 insertions(+), 12 deletions(-) diff --git a/exec/src/klio_exec/cli.py b/exec/src/klio_exec/cli.py index 38609704..345eb9ca 100644 --- a/exec/src/klio_exec/cli.py +++ b/exec/src/klio_exec/cli.py @@ -121,14 +121,15 @@ def stop_job(config_file): @main.command("test", context_settings=dict(ignore_unknown_options=True)) +@options.config_file @click.argument("pytest_args", nargs=-1, type=click.UNPROCESSED) -def test_job(pytest_args): +def test_job(config_file, pytest_args): """Thin wrapper around pytest. Any arguments after -- are passed through. """ import os import pytest - config_path = "klio-job.yaml" + config_path = config_file or "klio-job.yaml" config_data = _get_config(config_path) conf_obj = config.KlioConfig(config_data) diff --git a/exec/tests/unit/test_cli.py b/exec/tests/unit/test_cli.py index 43d42ae7..6e4804bc 100644 --- a/exec/tests/unit/test_cli.py +++ b/exec/tests/unit/test_cli.py @@ -141,6 +141,27 @@ def mock_compare_runtime_to_buildtime_config(mocker, monkeypatch): return mock +def _create_tmp_config_file( + temp_dir_str: str, config_file: str, config +) -> str: + """ + Creates a temporary config file + + Args: + temp_dir_str: Temporary directory where to create file + config_file: config file name + config: config to write + + Returns: + Temp config file path + """ + # create a tmp file else click will complain it doesn't exist + tmp_config_file = os.path.join(temp_dir_str, config_file) + with open(tmp_config_file, "w") as f: + yaml.dump(config, f) + return tmp_config_file + + def test_get_config(tmpdir, config): tmp_config = tmpdir.mkdir("klio-exec-testing").join("klio-job.yaml") tmp_config.write(yaml.dump(config)) @@ -298,15 +319,12 @@ def test_run_pipeline_conf_override( temp_dir_str = str(temp_dir) monkeypatch.setattr(os, "getcwd", lambda: temp_dir_str) - exp_conf_file = "klio-job.yaml" if config_file_override: - exp_conf_file = os.path.join(temp_dir_str, config_file_override) - cli_inputs.extend(["--config-file", exp_conf_file]) - # create a tmp file else click will complain it doesn't exist - with open(exp_conf_file, "w") as f: - yaml.dump(config, f) - + exp_conf_file = _create_tmp_config_file( + temp_dir_str, config_file_override, config + ) + cli_inputs.extend(["--config-file", exp_conf_file]) mock_klio_config.setup(_config(), "klio-job.yaml", exp_conf_file) else: mock_klio_config.setup(_config(), "klio-job.yaml") @@ -354,10 +372,10 @@ def test_stop_job( exp_file = os.path.join(temp_dir_str, "klio-job.yaml") if config_file_override: # create a tmp file else click will complain it doesn't exist - exp_file = os.path.join(temp_dir_str, config_file_override) + exp_file = _create_tmp_config_file( + temp_dir_str, config_file_override, config + ) cli_inputs.extend(["--config-file", exp_file]) - with open(exp_file, "w") as f: - yaml.dump(config, f) result = cli_runner.invoke(cli.stop_job, cli_inputs) assert 0 == result.exit_code @@ -395,6 +413,53 @@ def test_test_job( mock_test.assert_called_once_with(pytest_args) +@pytest.mark.parametrize( + "config_file_override,pytest_args", + [(None, []), ("klio-job2.yaml", ["test_file.py::test_foo"])], +) +def test_test_job_conf_override( + config_file_override, + pytest_args, + mocker, + monkeypatch, + config, + cli_runner, + patch_klio_config, + tmpdir, +): + mock_get_config = mocker.Mock() + monkeypatch.setattr(cli, "_get_config", mock_get_config) + mock_get_config.return_value = config + + mock_test = mocker.Mock() + monkeypatch.setattr(pytest, "main", mock_test) + mock_test.return_value = 0 + + cli_inputs = [] + + temp_dir = tmpdir.mkdir("testing") + temp_dir_str = str(temp_dir) + monkeypatch.setattr(os, "getcwd", lambda: temp_dir_str) + + config_file = "klio-job.yaml" + if config_file_override: + # create a tmp file else click will complain it doesn't exist + config_file = _create_tmp_config_file( + temp_dir_str, config_file_override, config + ) + cli_inputs.extend(["--config-file", config_file]) + + cli_inputs.extend(pytest_args) + + result = cli_runner.invoke(cli.test_job, cli_inputs) + + core_testing.assert_execution_success(result) + assert "true" == os.environ["KLIO_TEST_MODE"] + + mock_test.assert_called_once_with(pytest_args) + mock_get_config.assert_called_once_with(config_file) + + @pytest.mark.parametrize( "pytest_args", [