From c8a5103fef2778fd9e4fb9383499b5a94f871aef Mon Sep 17 00:00:00 2001 From: Yousef Moazzam Date: Fri, 21 Mar 2025 11:53:51 +0000 Subject: [PATCH 1/5] Replace subprocess-launching in CLI version test --- tests/test_cli.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 621154777..b53dd1aa7 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,15 +4,17 @@ import pytest from pytest_mock import MockerFixture +from click.testing import CliRunner import httomo from httomo import __version__ -from httomo.cli import set_global_constants, transform_limit_str_to_bytes +from httomo.cli import set_global_constants, transform_limit_str_to_bytes, main def test_cli_version_shows_version(): - cmd = [sys.executable, "-m", "httomo", "--version"] - assert __version__ == subprocess.check_output(cmd).decode().strip() + runner = CliRunner() + result = runner.invoke(main, ["--version"]) + assert __version__ == result.stdout.strip() def test_cli_help_shows_help(): From 83271bcf64c573715fed0b499d6d00c41ee13806 Mon Sep 17 00:00:00 2001 From: Yousef Moazzam Date: Fri, 21 Mar 2025 11:55:05 +0000 Subject: [PATCH 2/5] Replace subprocess-launching in CLI help test --- tests/test_cli.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index b53dd1aa7..c9f4dde6c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -18,13 +18,9 @@ def test_cli_version_shows_version(): def test_cli_help_shows_help(): - cmd = [sys.executable, "-m", "httomo", "--help"] - assert ( - subprocess.check_output(cmd) - .decode() - .strip() - .startswith("Usage: python -m httomo") - ) + runner = CliRunner() + result = runner.invoke(main, ["--help"]) + assert result.stdout.strip().startswith("Usage: ") def test_cli_noargs_raises_error(): From 2d99fea91b25e274c7ef04080863c1c1537c9454 Mon Sep 17 00:00:00 2001 From: Yousef Moazzam Date: Fri, 21 Mar 2025 12:00:15 +0000 Subject: [PATCH 3/5] Replace CLI no-args test to check correct behaviour Prior to this change the test was checking that, when running the CLI with no args, if an error was raised, to ensure a specific exit code was returned. However, when the CLI is given no args, an error is not expected to be raised; instead, the help message is expected to be displayed. Therefore, the test was checking for behaviour that the CLI never exhibited. Also, this change replaces the subprocess-launching in the test with testing functionality provided by `click` to run the CLI. --- tests/test_cli.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index c9f4dde6c..3e515efad 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -23,12 +23,11 @@ def test_cli_help_shows_help(): assert result.stdout.strip().startswith("Usage: ") -def test_cli_noargs_raises_error(): - cmd = [sys.executable, "-m", "httomo"] - try: - subprocess.check_output(cmd) - except subprocess.CalledProcessError as e: - assert e.returncode == 2 +def test_cli_noargs_shows_help(): + runner = CliRunner() + result = runner.invoke(main) + assert result.exit_code == 0 + assert result.stdout.strip().startswith("Usage: ") def test_cli_check_pass_data_file(standard_loader, standard_data): From d3fcf8615047844e06956bb422e77d408ac81cbd Mon Sep 17 00:00:00 2001 From: Yousef Moazzam Date: Fri, 21 Mar 2025 12:02:02 +0000 Subject: [PATCH 4/5] Replace subprocess-launching in CLI check command test --- tests/test_cli.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 3e515efad..0803a5082 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -31,12 +31,13 @@ def test_cli_noargs_shows_help(): def test_cli_check_pass_data_file(standard_loader, standard_data): - cmd = [sys.executable, "-m", "httomo", "check", standard_loader, standard_data] + runner = CliRunner() + result = runner.invoke(main, ["check", standard_loader, standard_data]) check_data_str = ( "Checking that the paths to the data and keys in the YAML_CONFIG file " "match the paths and keys in the input file (IN_DATA)..." ) - assert check_data_str in subprocess.check_output(cmd).decode().strip() + assert check_data_str in result.stdout @pytest.mark.cupy From d93ec955b4e0a0dc8372cf1e68b960c374fd0292 Mon Sep 17 00:00:00 2001 From: Yousef Moazzam Date: Fri, 21 Mar 2025 12:03:40 +0000 Subject: [PATCH 5/5] Replace subprocess-launching in CLI GPU ID flag test --- tests/test_cli.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 0803a5082..ad47625c9 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,5 +1,3 @@ -import subprocess -import sys from pathlib import Path import pytest @@ -41,17 +39,12 @@ def test_cli_check_pass_data_file(standard_loader, standard_data): @pytest.mark.cupy -def test_cli_pass_gpu_id(cmd, standard_data, standard_loader, output_folder): - cmd.insert(4, standard_data) - cmd.insert(5, standard_loader) - cmd.insert(6, output_folder) - cmd.insert(7, "--gpu-id") - cmd.insert(8, "100") - - result = subprocess.run( - cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True +def test_cli_pass_gpu_id(standard_data, standard_loader, output_folder): + runner = CliRunner() + result = runner.invoke( + main, ["run", standard_data, standard_loader, output_folder, "--gpu-id", "100"] ) - assert "GPU Device not available for access." in result.stderr + assert "GPU Device not available for access." in str(result.exception) @pytest.mark.parametrize(