From 51236d9b14104fb656bb61f6758a892d979198eb Mon Sep 17 00:00:00 2001 From: DriesSchaumont <5946712+DriesSchaumont@users.noreply.github.com> Date: Fri, 19 Jan 2024 17:11:22 +0100 Subject: [PATCH] Fix bugs and update CI --- _viash.yaml | 1 + .../test_run_component/dummy_config.vsh.yaml | 10 ++- .../test_run_component/test_script.py | 89 +++++++++++++++---- .../unittests/fixtures/test_run_component.py | 8 +- tox.ini | 2 +- viashpy/_run.py | 5 +- viashpy/testing.py | 5 +- 7 files changed, 94 insertions(+), 26 deletions(-) create mode 100644 _viash.yaml diff --git a/_viash.yaml b/_viash.yaml new file mode 100644 index 0000000..e33b498 --- /dev/null +++ b/_viash.yaml @@ -0,0 +1 @@ +viash_version: 0.8.2 \ No newline at end of file diff --git a/tests/integration/test_run_component/dummy_config.vsh.yaml b/tests/integration/test_run_component/dummy_config.vsh.yaml index 3ddd7e1..373bfa7 100644 --- a/tests/integration/test_run_component/dummy_config.vsh.yaml +++ b/tests/integration/test_run_component/dummy_config.vsh.yaml @@ -23,7 +23,9 @@ functionality: test_resources: - type: python_script path: test_script.py + - path: /tests/integration/test_run_component/dummy_config.vsh.yaml platforms: + - type: native - type: docker image: python:3.10 test_setup: @@ -32,5 +34,11 @@ platforms: - type: python packages: - /viashpy - - type: native + - ruamel.yaml + - type: apt + packages: + - default-jdk + - type: docker + run: | + curl -fsSL dl.viash.io | bash; mv viash /usr/local/bin/ - type: nextflow diff --git a/tests/integration/test_run_component/test_script.py b/tests/integration/test_run_component/test_script.py index 32bc299..96b4496 100644 --- a/tests/integration/test_run_component/test_script.py +++ b/tests/integration/test_run_component/test_script.py @@ -1,7 +1,10 @@ import pytest import sys +from pathlib import Path from io import StringIO -from contextlib import redirect_stdout +from ruamel.yaml import YAML +from contextlib import redirect_stdout, redirect_stderr +import uuid ### VIASH START # noqa: E266 meta = { @@ -10,29 +13,83 @@ ### VIASH END # noqa: E266 -def test_run_component(run_component): - captured_output = run_component(["--output", "bar.txt"]) - with open("bar.txt", "r") as open_output_file: - contents = open_output_file.read() - assert contents == "foo!" - assert ( - captured_output is not None - ), "Some output from stdout or stderr should have been captured" - assert b"This is a logging statement" in captured_output +@pytest.fixture() +def random_config_path(tmp_path): + def wrapper(): + unique_filename = f"{str(uuid.uuid4())}.vsh.yaml" + temp_file = tmp_path / unique_filename + return temp_file + return wrapper -if __name__ == "__main__": + +class TestRunExecutable: + meta = meta + + def test_run_component(self, run_component): + captured_output = run_component(["--output", "bar.txt"]) + with open("bar.txt", "r") as open_output_file: + contents = open_output_file.read() + assert contents == "foo!" + assert ( + captured_output is not None + ), "Some output from stdout or stderr should have been captured" + assert b"This is a logging statement" in captured_output + + +class TestRunUsingConfig: + meta = meta + + @pytest.fixture() + def viash_source_config_path(self, random_config_path): + yaml_interface = YAML(typ="safe", pure=True) + yaml_interface.default_flow_style = False + original_config = yaml_interface.load( + Path(f"{meta['resources_dir']}/dummy_config.vsh.yaml") + ) + del original_config["functionality"]["test_resources"] + del original_config["functionality"]["resources"][1] + result_path = random_config_path() + yaml_interface.dump(original_config, result_path) + return result_path + + def test_run_component_check_correct_memory_syntax(self, run_component): + captured_output = run_component(["--output", "bar.txt"]) + with open("bar.txt", "r") as open_output_file: + contents = open_output_file.read() + assert contents == "foo!" + assert ( + captured_output is not None + ), "Some output from stdout or stderr should have been captured" + assert ( + b"--memory looks like a parameter but is not a defined parameter and will instead be treated as a positional argument" + not in captured_output + ) + + +def run_pytest_and_capture_output(): temp_stdout = StringIO() + temp_stderr = StringIO() print(f"meta: {meta}") + with redirect_stdout(temp_stdout), redirect_stderr(temp_stderr): + exit_code = pytest.main( + [__file__, "--log-cli-level", "DEBUG"], plugins=["viashpy"] + ) + stdout_str = temp_stdout.getvalue() + stderr_str = temp_stderr.getvalue() + print(f"stdout: {stdout_str}\nstderr: {stderr_str}\nexit_code: {exit_code}") + return stdout_str, stderr_str, exit_code + + +if __name__ == "__main__": assert ( meta["memory_gb"] is not None ), "This test should be executed with some --memory set." - with redirect_stdout(temp_stdout): - result = pytest.main([__file__], plugins=["viashpy"]) - stdout_str = temp_stdout.getvalue() - print(stdout_str) + + stdout_str, stderr_str, exit_code = run_pytest_and_capture_output() assert ( "Different values were defined in the 'meta' dictionary that limit memory, choosing the one with the smallest unit" not in stdout_str ) - sys.exit(result) + + sys.exit(exit_code) diff --git a/tests/unittests/fixtures/test_run_component.py b/tests/unittests/fixtures/test_run_component.py index 354018b..a40487f 100644 --- a/tests/unittests/fixtures/test_run_component.py +++ b/tests/unittests/fixtures/test_run_component.py @@ -109,7 +109,7 @@ def test_run_component_different_memory_specification_warnings( expected_bytes, expected_warning, ): - expected_memory_args = "" + expected_memory_args = ", " memory_specifiers = [ memory_pb, memory_tb, @@ -122,9 +122,9 @@ def test_run_component_different_memory_specification_warnings( specifier for specifier in memory_specifiers if specifier != "None" ] if any(memory_specifiers): - expected_memory_args = f', "--memory", "{expected_bytes}B"' + expected_memory_args = f', "--memory", "{expected_bytes}B", ' expected = ( - '["viash", "run", Path(meta["config"]), "--", "bar"%s]' % expected_memory_args + '["viash", "run", Path(meta["config"])%s"--", "bar"]' % expected_memory_args ) makepyfile_and_add_meta( f""" @@ -170,7 +170,7 @@ def test_loading_run_component(mocker, run_component): [ ( "dummy_config", - '["viash", "run", Path(meta["config"]), "--", "bar"%s%s]', + '["viash", "run", Path(meta["config"])%s%s, "--", "bar"]', "--", ), ("dummy_config_with_info", '[Path("foo"), "bar"%s%s]', "---"), diff --git a/tox.ini b/tox.ini index ee4233f..7a2213b 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ # For more information about tox, see https://tox.readthedocs.io/en/latest/ [tox] -envlist = py3.9,py3.10,py3.11,flake8 +envlist = py3.9,py3.10,py3.11,py3.12flake8 isolated_build = True [testenv] diff --git a/viashpy/_run.py b/viashpy/_run.py index 904bda5..12c573e 100644 --- a/viashpy/_run.py +++ b/viashpy/_run.py @@ -79,8 +79,9 @@ def viash_run( if not config.is_file(): raise FileNotFoundError(f"{config} does not exist or is not a file.") return check_output( - [viash_location, "run", config, "--"] - + _add_cpu_and_memory(args, cpus, memory, "--"), + _add_cpu_and_memory([viash_location, "run", config], cpus, memory, "--") + + ["--"] + + args, stderr=stderr, **popen_kwargs, ) diff --git a/viashpy/testing.py b/viashpy/testing.py index 6879ac8..523d89b 100644 --- a/viashpy/testing.py +++ b/viashpy/testing.py @@ -7,7 +7,8 @@ from subprocess import CalledProcessError import warnings -logger = logging.Logger(__name__) +logger = logging.getLogger(__name__) +logger.propagate = True @pytest.fixture @@ -196,7 +197,7 @@ def wrapper(args_as_list): return wrapper logger.info( - "Could not find the original viash config source. " + f"Could not find the original viash config source at '{viash_source_config_path}'. " "Assuming test script is run from 'viash test' or 'viash_test'." )