Skip to content

Commit

Permalink
Fix bugs and update CI
Browse files Browse the repository at this point in the history
  • Loading branch information
DriesSchaumont committed Jan 19, 2024
1 parent bef8d81 commit 51236d9
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 26 deletions.
1 change: 1 addition & 0 deletions _viash.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
viash_version: 0.8.2
10 changes: 9 additions & 1 deletion tests/integration/test_run_component/dummy_config.vsh.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
89 changes: 73 additions & 16 deletions tests/integration/test_run_component/test_script.py
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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)
8 changes: 4 additions & 4 deletions tests/unittests/fixtures/test_run_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"""
Expand Down Expand Up @@ -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]', "---"),
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
5 changes: 3 additions & 2 deletions viashpy/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
5 changes: 3 additions & 2 deletions viashpy/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from subprocess import CalledProcessError
import warnings

logger = logging.Logger(__name__)
logger = logging.getLogger(__name__)
logger.propagate = True


@pytest.fixture
Expand Down Expand Up @@ -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'."
)

Expand Down

0 comments on commit 51236d9

Please sign in to comment.