From 2e8ce6036852d366c64c2f5a6597a8052dc3609a Mon Sep 17 00:00:00 2001 From: Owen Jones Date: Tue, 11 Jul 2023 13:30:10 +0100 Subject: [PATCH 1/4] Amend MD --- e2e_tests/e2e_test.md | 1 + 1 file changed, 1 insertion(+) diff --git a/e2e_tests/e2e_test.md b/e2e_tests/e2e_test.md index 045cef1ad..5d98af56c 100644 --- a/e2e_tests/e2e_test.md +++ b/e2e_tests/e2e_test.md @@ -2,3 +2,4 @@ This module is setup in `pytest.ini` to _not run_. If you want to run the E2E tests, you will need to run `pytest e2e_tests` rather than `pytest`. + From fbf32e65b11d64dcf2117ba03d2691933d36ca54 Mon Sep 17 00:00:00 2001 From: Owen Jones Date: Tue, 11 Jul 2023 15:19:47 +0100 Subject: [PATCH 2/4] Tests for cli command --- e2e_tests/__init__.py | 42 +++++++++++++++++++ tests/e2e_test_internals/__init__.py | 0 .../test_run_cli_command.py | 35 ++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 tests/e2e_test_internals/__init__.py create mode 100644 tests/e2e_test_internals/test_run_cli_command.py diff --git a/e2e_tests/__init__.py b/e2e_tests/__init__.py index e69de29bb..8c62420f5 100644 --- a/e2e_tests/__init__.py +++ b/e2e_tests/__init__.py @@ -0,0 +1,42 @@ +from subprocess import run +from typing import Optional, Tuple + +from darwin.exceptions import DarwinException + + +def run_cli_command(command: str, working_directory: Optional[str] = None) -> Tuple[int, str, str]: + """ + Run a CLI command and return the return code, stdout, and stderr. + + Parameters + ---------- + command : str + The command to run. + working_directory : str, optional + The working directory to run the command in. + + Returns + ------- + Tuple[int, str, str] + The return code, stdout, and stderr. + """ + + # Do ot allow directory traversal + if ".." in command or (working_directory and ".." in working_directory): + raise DarwinException("Cannot pass directory traversal to 'run_cli_command'.") + + result = ( + run( + command, + capture_output=True, + cwd=working_directory, + shell=True, + ) + if working_directory + else run( + command, + capture_output=True, + shell=True, + ) + ) + return result.returncode, result.stdout.decode("utf-8"), result.stderr.decode("utf-8") diff --git a/tests/e2e_test_internals/__init__.py b/tests/e2e_test_internals/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/e2e_test_internals/test_run_cli_command.py b/tests/e2e_test_internals/test_run_cli_command.py new file mode 100644 index 000000000..8d1e5b17b --- /dev/null +++ b/tests/e2e_test_internals/test_run_cli_command.py @@ -0,0 +1,35 @@ +from unittest import mock + +import pytest + +from darwin.exceptions import DarwinException +from e2e_tests import run_cli_command + + +def test_does_not_allow_directory_traversal() -> None: + with pytest.raises(DarwinException) as excinfo: + run_cli_command("darwin --help; ls ..") + + assert excinfo.value == "Cannot pass directory traversal to 'run_cli_command'." + + with pytest.raises(DarwinException) as excinfo: + run_cli_command("darwin --help", working_directory="/usr/bin/../") + assert excinfo.value == "Cannot pass directory traversal to 'run_cli_command'." + + +@mock.patch("") +def test_passes_working_directory_to_run_cli_command(mock_subprocess_run: mock.Mock) -> None: + mock_subprocess_run.reset_mock() + run_cli_command("darwin --help", working_directory="/usr/bin") + mock_subprocess_run.assert_called_with("darwin --help", working_directory="/usr/bin") + + +@mock.patch("") +def test_does_not_pass_working_directory_to_run_cli_command(mock_subprocess_run: mock.Mock) -> None: + mock_subprocess_run.reset_mock() + run_cli_command("darwin --help") + mock_subprocess_run.assert_called_with("darwin --help") + + +if __name__ == "__main__": + pytest.main() From 689b13bbf29a77e3f77b6475a835bf041cd45c24 Mon Sep 17 00:00:00 2001 From: Owen Jones Date: Thu, 13 Jul 2023 09:50:12 +0100 Subject: [PATCH 3/4] Restructure and working tests --- e2e_tests/__init__.py | 42 ------------------- e2e_tests/helpers.py | 42 +++++++++++++++++++ .../test_run_cli_command.py | 18 +++++--- 3 files changed, 54 insertions(+), 48 deletions(-) create mode 100644 e2e_tests/helpers.py diff --git a/e2e_tests/__init__.py b/e2e_tests/__init__.py index 8c62420f5..e69de29bb 100644 --- a/e2e_tests/__init__.py +++ b/e2e_tests/__init__.py @@ -1,42 +0,0 @@ -from subprocess import run -from typing import Optional, Tuple - -from darwin.exceptions import DarwinException - - -def run_cli_command(command: str, working_directory: Optional[str] = None) -> Tuple[int, str, str]: - """ - Run a CLI command and return the return code, stdout, and stderr. - - Parameters - ---------- - command : str - The command to run. - working_directory : str, optional - The working directory to run the command in. - - Returns - ------- - Tuple[int, str, str] - The return code, stdout, and stderr. - """ - - # Do ot allow directory traversal - if ".." in command or (working_directory and ".." in working_directory): - raise DarwinException("Cannot pass directory traversal to 'run_cli_command'.") - - result = ( - run( - command, - capture_output=True, - cwd=working_directory, - shell=True, - ) - if working_directory - else run( - command, - capture_output=True, - shell=True, - ) - ) - return result.returncode, result.stdout.decode("utf-8"), result.stderr.decode("utf-8") diff --git a/e2e_tests/helpers.py b/e2e_tests/helpers.py new file mode 100644 index 000000000..f3959a322 --- /dev/null +++ b/e2e_tests/helpers.py @@ -0,0 +1,42 @@ +from subprocess import run +from typing import Optional, Tuple + +from darwin.exceptions import DarwinException + + +def run_cli_command(command: str, working_directory: Optional[str] = None) -> Tuple[int, str, str]: + """ + Run a CLI command and return the return code, stdout, and stderr. + + Parameters + ---------- + command : str + The command to run. + working_directory : str, optional + The working directory to run the command in. + + Returns + ------- + Tuple[int, str, str] + The return code, stdout, and stderr. + """ + + # Do ot allow directory traversal + if ".." in command or (working_directory and ".." in working_directory): + raise DarwinException("Cannot pass directory traversal to 'run_cli_command'.") + + if working_directory: + result = run( + command, + cwd=working_directory, + capture_output=True, + shell=True, + ) + else: + result = run( + command, + capture_output=True, + shell=True, + ) + + return result.returncode, result.stdout.decode("utf-8"), result.stderr.decode("utf-8") diff --git a/tests/e2e_test_internals/test_run_cli_command.py b/tests/e2e_test_internals/test_run_cli_command.py index 8d1e5b17b..46838d871 100644 --- a/tests/e2e_test_internals/test_run_cli_command.py +++ b/tests/e2e_test_internals/test_run_cli_command.py @@ -3,7 +3,7 @@ import pytest from darwin.exceptions import DarwinException -from e2e_tests import run_cli_command +from e2e_tests.helpers import run_cli_command def test_does_not_allow_directory_traversal() -> None: @@ -17,18 +17,24 @@ def test_does_not_allow_directory_traversal() -> None: assert excinfo.value == "Cannot pass directory traversal to 'run_cli_command'." -@mock.patch("") +@mock.patch("e2e_tests.run_cli_command.run") def test_passes_working_directory_to_run_cli_command(mock_subprocess_run: mock.Mock) -> None: mock_subprocess_run.reset_mock() - run_cli_command("darwin --help", working_directory="/usr/bin") - mock_subprocess_run.assert_called_with("darwin --help", working_directory="/usr/bin") + run_cli_command("darwin --help", "/usr/bin") + mock_subprocess_run.assert_called_once() + assert mock_subprocess_run.call_args[0][0] == "darwin --help" + assert mock_subprocess_run.call_args[1]["cwd"] == "/usr/bin" -@mock.patch("") + +@mock.patch("e2e_tests.run_cli_command.run") def test_does_not_pass_working_directory_to_run_cli_command(mock_subprocess_run: mock.Mock) -> None: mock_subprocess_run.reset_mock() run_cli_command("darwin --help") - mock_subprocess_run.assert_called_with("darwin --help") + + mock_subprocess_run.assert_called_once() + assert mock_subprocess_run.call_args[0][0] == "darwin --help" + assert "cwd" not in mock_subprocess_run.call_args[1] if __name__ == "__main__": From c93654ffe607377fbd29b80a379fd7caa5288bec Mon Sep 17 00:00:00 2001 From: Owen Jones Date: Thu, 13 Jul 2023 10:07:12 +0100 Subject: [PATCH 4/4] Test change --- .../test_run_cli_command.py | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tests/e2e_test_internals/test_run_cli_command.py b/tests/e2e_test_internals/test_run_cli_command.py index 46838d871..f439f962c 100644 --- a/tests/e2e_test_internals/test_run_cli_command.py +++ b/tests/e2e_test_internals/test_run_cli_command.py @@ -1,3 +1,4 @@ +from collections import namedtuple from unittest import mock import pytest @@ -17,7 +18,7 @@ def test_does_not_allow_directory_traversal() -> None: assert excinfo.value == "Cannot pass directory traversal to 'run_cli_command'." -@mock.patch("e2e_tests.run_cli_command.run") +@mock.patch("e2e_tests.helpers.run") def test_passes_working_directory_to_run_cli_command(mock_subprocess_run: mock.Mock) -> None: mock_subprocess_run.reset_mock() run_cli_command("darwin --help", "/usr/bin") @@ -27,7 +28,23 @@ def test_passes_working_directory_to_run_cli_command(mock_subprocess_run: mock.M assert mock_subprocess_run.call_args[1]["cwd"] == "/usr/bin" -@mock.patch("e2e_tests.run_cli_command.run") +@mock.patch("e2e_tests.helpers.run") +def test_passes_back_returncode_stdout_and_stderr(mock_subprocess_run: mock.Mock) -> None: + CompletedProcess = namedtuple("CompletedProcess", ["returncode", "stdout", "stderr"]) + mocked_output = CompletedProcess(returncode=137, stdout=b"stdout", stderr=b"stderr") + + mock_subprocess_run.return_value = mocked_output + + return_code, std_out, std_err = run_cli_command("darwin --help", "/usr/bin") + + mock_subprocess_run.assert_called_once() + + assert return_code == 137 + assert std_out == "stdout" + assert std_err == "stderr" + + +@mock.patch("e2e_tests.helpers.run") def test_does_not_pass_working_directory_to_run_cli_command(mock_subprocess_run: mock.Mock) -> None: mock_subprocess_run.reset_mock() run_cli_command("darwin --help")