From 287b202cfcbca10e5c430844e7350b089f75be4b Mon Sep 17 00:00:00 2001 From: Wytamma Wirth Date: Wed, 31 Jan 2024 23:39:32 +1100 Subject: [PATCH] Allow pass SnkConfig to CLI --- snk/cli/cli.py | 13 ++++++++----- snk/cli/subcommands/run.py | 2 +- tests/test_cli/test_subcommands.py | 30 ++++++++++++++++-------------- tests/test_dynamic_typer.py | 2 +- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/snk/cli/cli.py b/snk/cli/cli.py index 4ee8c74..9f49c0b 100644 --- a/snk/cli/cli.py +++ b/snk/cli/cli.py @@ -32,8 +32,8 @@ class CLI(DynamicTyper): >>> CLI(Path('/path/to/workflow')) """ - def __init__(self, workflow_dir_path: Path = None) -> None: - if not workflow_dir_path: + def __init__(self, workflow_dir_path: Path = None, snk_config: SnkConfig = None) -> None: + if workflow_dir_path is None: # get the calling frame (the frame of the function that called this function) calling_frame = inspect.currentframe().f_back # get the file path from the calling frame @@ -42,9 +42,12 @@ def __init__(self, workflow_dir_path: Path = None) -> None: workflow_dir_path = workflow_dir_path.parent self.workflow = Workflow(path=workflow_dir_path) self.snakemake_config = load_workflow_snakemake_config(workflow_dir_path) - self.snk_config = SnkConfig.from_workflow_dir( - workflow_dir_path, create_if_not_exists=True - ) + if snk_config is None: + self.snk_config = SnkConfig.from_workflow_dir( + workflow_dir_path, create_if_not_exists=True + ) + else: + self.snk_config = snk_config if self.snk_config.version: self.version = self.snk_config.version else: diff --git a/snk/cli/subcommands/run.py b/snk/cli/subcommands/run.py index 93c9a5c..02679d6 100644 --- a/snk/cli/subcommands/run.py +++ b/snk/cli/subcommands/run.py @@ -256,7 +256,7 @@ def run( if configs: args.extend(["--config", *configs]) if verbose: - typer.secho(f"snakemake {' '.join(args)}\n", fg=typer.colors.MAGENTA, secho=True) + typer.secho(f"snakemake {' '.join(args)}\n", fg=typer.colors.MAGENTA, err=True) if not keep_snakemake and Path(".snakemake").exists(): keep_snakemake = True try: diff --git a/tests/test_cli/test_subcommands.py b/tests/test_cli/test_subcommands.py index 0d82ddb..8fa5fe0 100644 --- a/tests/test_cli/test_subcommands.py +++ b/tests/test_cli/test_subcommands.py @@ -1,19 +1,21 @@ import pytest -@pytest.mark.parametrize("cmd,expected_in_response", [ - (["script", "run", "hello.py"], ["hello world"]), - (["script", "list"], ["hello.py"]), - (["script", "show", "hello.py"], ["print('hello world')"]), - (["env", "list"], ["python"]), - (["env", "show", "python"], ["python"]), - (["env", "create"], ["All conda environments created!"]), - (["env", "create", "python"], ["Created environment python!"]), - (["env", "run", "-v", "python", "which python"], [".snakemake"]), - (["env", "activate", "python"], ["Activating python environment...", "Exiting python environment..."]), - (["env", "remove", "-f"], ["Deleted"]), +@pytest.mark.parametrize("cmd,expected_in_stdout,expected_in_stderr", [ + (["script", "run", "hello.py"], ["hello world"], []), + (["script", "list"], ["hello.py"], []), + (["script", "show", "hello.py"], ["print('hello world')"], []), + (["env", "list"], ["python"], []), + (["env", "show", "python"], ["python"], []), + (["env", "create"], ["All conda environments created!"], []), + (["env", "create", "python"], ["Created environment python!"], []), + (["env", "run", "-v", "python", "which python"], [".snakemake"], []), + (["env", "activate", "python"], [], ["Activating python environment...", "Exiting python environment..."]), + (["env", "remove", "-f"], ["Deleted"], []), ]) -def test_snk_cli_command(local_runner, cmd, expected_in_response): +def test_snk_cli_command(local_runner, cmd, expected_in_stdout, expected_in_stderr): res = local_runner(cmd) assert res.code == 0, res.stderr - for expected in expected_in_response: - assert expected in res.stdout, res.stderr \ No newline at end of file + for expected in expected_in_stdout: + assert expected in res.stdout, res.stderr + for expected in expected_in_stderr: + assert expected in res.stderr, res.stderr \ No newline at end of file diff --git a/tests/test_dynamic_typer.py b/tests/test_dynamic_typer.py index 98d4490..562d3ef 100644 --- a/tests/test_dynamic_typer.py +++ b/tests/test_dynamic_typer.py @@ -126,6 +126,6 @@ def test_error(dynamic_typer, cli_runner): def test_log(dynamic_typer, cli_runner, capsys): - dynamic_typer.log("Log message") + dynamic_typer.log("Log message", stderr=False) captured = capsys.readouterr() assert "Log message" in captured.out