Skip to content

Commit

Permalink
Allow pass SnkConfig to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Wytamma committed Jan 31, 2024
1 parent bfcc18d commit 287b202
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
13 changes: 8 additions & 5 deletions snk/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion snk/cli/subcommands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 16 additions & 14 deletions tests/test_cli/test_subcommands.py
Original file line number Diff line number Diff line change
@@ -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
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
2 changes: 1 addition & 1 deletion tests/test_dynamic_typer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 287b202

Please sign in to comment.