From 208f21c20ac45556cd65bb0900f670b6c4a54d83 Mon Sep 17 00:00:00 2001 From: Altynbek Orumbayev Date: Mon, 14 Aug 2023 12:55:08 +0200 Subject: [PATCH] refactor: fixing ruff, mypy errors, improving fixtures --- tests/goal/test_goal.py | 24 ++++++++++++------- ...l_simple_args_with_file_error.approved.txt | 4 ++++ ...ndbox_with_out_of_date_config.approved.txt | 4 +--- ...eset_without_existing_sandbox.approved.txt | 4 +--- ...net_start.test_localnet_start.approved.txt | 4 +--- ...alnet_start_health_bad_status.approved.txt | 4 +--- ...localnet_start_health_failure.approved.txt | 4 +--- tests/utils/proc_mock.py | 14 ++++++++--- 8 files changed, 36 insertions(+), 26 deletions(-) create mode 100644 tests/goal/test_goal.test_goal_simple_args_with_file_error.approved.txt diff --git a/tests/goal/test_goal.py b/tests/goal/test_goal.py index bfd3c571..d62e049f 100644 --- a/tests/goal/test_goal.py +++ b/tests/goal/test_goal.py @@ -24,7 +24,7 @@ def _mocked_goal_mount_path(cwd: Path, monkeypatch: pytest.MonkeyPatch) -> None: @pytest.fixture() -def _setup_input_files(cwd: Path, request) -> None: +def _setup_input_files(cwd: Path, request: pytest.FixtureRequest) -> None: file_names = request.param for file_name in file_names: (cwd / file_name).write_text( @@ -147,13 +147,13 @@ def test_goal_start_without_docker_engine_running(proc_mock: ProcMock) -> None: verify(result.output) -@pytest.mark.usefixtures("proc_mock", "cwd", "_mocked_goal_mount_path", "_setup_input_files") +@pytest.mark.usefixtures("proc_mock", "_mocked_goal_mount_path", "_setup_input_files") @pytest.mark.parametrize("_setup_input_files", [["approval.teal"]], indirect=True) def test_goal_simple_args_with_input_file( proc_mock: ProcMock, cwd: Path, ) -> None: - (cwd / "/root/goal_mount/approval.teal").exists() + assert (cwd / "approval.teal").exists() expected_arguments = [ "docker", "exec", @@ -168,7 +168,7 @@ def test_goal_simple_args_with_input_file( ] proc_mock.set_output(expected_arguments, output=["File compiled"]) - result = invoke("goal clerk group approval.teal") + result = invoke("goal clerk group approval.teal", cwd=cwd) assert proc_mock.called[1].command[9] == "/root/goal_mount/approval.teal" assert result.exit_code == 0 @@ -191,7 +191,12 @@ def test_goal_simple_args_with_output_file(proc_mock: ProcMock, cwd: Path) -> No "/root/goal_mount/balance_record.json", ] - proc_mock.set_output(expected_arguments, output=["File compiled"], side_effect=dump_json_file(cwd)) + proc_mock.set_output( + expected_arguments, + output=["File compiled"], + side_effect=dump_json_file, + side_effect_args={"cwd": cwd}, + ) result = invoke("goal account dump -o balance_record.json") assert proc_mock.called[1].command[10] == "/root/goal_mount/balance_record.json" @@ -223,7 +228,9 @@ def test_goal_simple_args_with_input_output_files( "/root/goal_mount/approval.compiled", ] - proc_mock.set_output(expected_arguments, output=["File compiled"], side_effect=dump_file(cwd)) + proc_mock.set_output( + expected_arguments, output=["File compiled"], side_effect=dump_file, side_effect_args={"cwd": cwd} + ) result = invoke("goal clerk compile approval.teal -o approval.compiled", cwd=cwd) @@ -258,7 +265,9 @@ def test_goal_simple_args_with_multiple_input_output_files( "/root/goal_mount/approval.compiled", ] - proc_mock.set_output(expected_arguments, output=["File compiled"], side_effect=dump_file(cwd)) + proc_mock.set_output( + expected_arguments, output=["File compiled"], side_effect=dump_file, side_effect_args={"cwd": cwd} + ) result = invoke("goal clerk compile approval1.teal approval2.teal -o approval.compiled", cwd=cwd) assert proc_mock.called[1].command[9] == "/root/goal_mount/approval1.teal" @@ -273,7 +282,6 @@ def test_goal_simple_args_with_multiple_input_output_files( @pytest.mark.usefixtures("proc_mock", "cwd", "_mocked_goal_mount_path") def test_goal_simple_args_with_file_error( - proc_mock: ProcMock, cwd: Path, ) -> None: assert not (cwd / "approval.teal").exists() diff --git a/tests/goal/test_goal.test_goal_simple_args_with_file_error.approved.txt b/tests/goal/test_goal.test_goal_simple_args_with_file_error.approved.txt new file mode 100644 index 00000000..b198e4d5 --- /dev/null +++ b/tests/goal/test_goal.test_goal_simple_args_with_file_error.approved.txt @@ -0,0 +1,4 @@ +DEBUG: Running 'docker version' in '{current_working_directory}' +DEBUG: docker: STDOUT +DEBUG: docker: STDERR +ERROR: approval.teal does not exist. diff --git a/tests/localnet/test_localnet_reset.test_localnet_reset_with_existing_sandbox_with_out_of_date_config.approved.txt b/tests/localnet/test_localnet_reset.test_localnet_reset_with_existing_sandbox_with_out_of_date_config.approved.txt index 1ece9836..4d70de9d 100644 --- a/tests/localnet/test_localnet_reset.test_localnet_reset_with_existing_sandbox_with_out_of_date_config.approved.txt +++ b/tests/localnet/test_localnet_reset.test_localnet_reset_with_existing_sandbox_with_out_of_date_config.approved.txt @@ -38,9 +38,7 @@ services: - type: bind source: ./algod_config.json target: /etc/algorand/config.json - - type: volume - source: /Users/negarabbasi/algokit_local - target: /Users/negarabbasi/goal + - ./goal_mount:/root/goal_mount indexer: container_name: algokit_indexer diff --git a/tests/localnet/test_localnet_reset.test_localnet_reset_without_existing_sandbox.approved.txt b/tests/localnet/test_localnet_reset.test_localnet_reset_without_existing_sandbox.approved.txt index b5903e3b..88576589 100644 --- a/tests/localnet/test_localnet_reset.test_localnet_reset_without_existing_sandbox.approved.txt +++ b/tests/localnet/test_localnet_reset.test_localnet_reset_without_existing_sandbox.approved.txt @@ -36,9 +36,7 @@ services: - type: bind source: ./algod_config.json target: /etc/algorand/config.json - - type: volume - source: /Users/negarabbasi/algokit_local - target: /Users/negarabbasi/goal + - ./goal_mount:/root/goal_mount indexer: container_name: algokit_indexer diff --git a/tests/localnet/test_localnet_start.test_localnet_start.approved.txt b/tests/localnet/test_localnet_start.test_localnet_start.approved.txt index 0b0155af..06be61b5 100644 --- a/tests/localnet/test_localnet_start.test_localnet_start.approved.txt +++ b/tests/localnet/test_localnet_start.test_localnet_start.approved.txt @@ -36,9 +36,7 @@ services: - type: bind source: ./algod_config.json target: /etc/algorand/config.json - - type: volume - source: /Users/negarabbasi/algokit_local - target: /Users/negarabbasi/goal + - ./goal_mount:/root/goal_mount indexer: container_name: algokit_indexer diff --git a/tests/localnet/test_localnet_start.test_localnet_start_health_bad_status.approved.txt b/tests/localnet/test_localnet_start.test_localnet_start_health_bad_status.approved.txt index 5d5fcb25..504fde81 100644 --- a/tests/localnet/test_localnet_start.test_localnet_start_health_bad_status.approved.txt +++ b/tests/localnet/test_localnet_start.test_localnet_start_health_bad_status.approved.txt @@ -36,9 +36,7 @@ services: - type: bind source: ./algod_config.json target: /etc/algorand/config.json - - type: volume - source: /Users/negarabbasi/algokit_local - target: /Users/negarabbasi/goal + - ./goal_mount:/root/goal_mount indexer: container_name: algokit_indexer diff --git a/tests/localnet/test_localnet_start.test_localnet_start_health_failure.approved.txt b/tests/localnet/test_localnet_start.test_localnet_start_health_failure.approved.txt index 767e530a..d91c786d 100644 --- a/tests/localnet/test_localnet_start.test_localnet_start_health_failure.approved.txt +++ b/tests/localnet/test_localnet_start.test_localnet_start_health_failure.approved.txt @@ -35,9 +35,7 @@ services: - type: bind source: ./algod_config.json target: /etc/algorand/config.json - - type: volume - source: /Users/negarabbasi/algokit_local - target: /Users/negarabbasi/goal + - ./goal_mount:/root/goal_mount indexer: container_name: algokit_indexer diff --git a/tests/utils/proc_mock.py b/tests/utils/proc_mock.py index b508d040..65c74745 100644 --- a/tests/utils/proc_mock.py +++ b/tests/utils/proc_mock.py @@ -43,6 +43,7 @@ class CommandMockData: exit_code: int = 0 output_lines: list[str] = dataclasses.field(default_factory=lambda: ["STDOUT", "STDERR"]) side_effect: Any | None = None + side_effect_args: dict[str, Any] | None = None @dataclasses.dataclass(kw_only=True) @@ -90,7 +91,11 @@ def should_bad_exit_on(self, cmd: list[str] | str, exit_code: int = -1, output: self._add_mock_data(cmd, mock_data) def set_output( - self, cmd: list[str] | str, output: list[str], side_effect: Callable[[], None] | None = None + self, + cmd: list[str] | str, + output: list[str], + side_effect: Callable[[Any], None] | None = None, + side_effect_args: dict[str, Any] | None = None, ) -> None: """ Set the output of a command, and optionally a side effect to be called when the command is run. @@ -103,7 +108,9 @@ def set_output( side_effect: A callable to be called when the command is run """ - self._add_mock_data(cmd, CommandMockData(output_lines=output, side_effect=side_effect)) + self._add_mock_data( + cmd, CommandMockData(output_lines=output, side_effect=side_effect, side_effect_args=side_effect_args) + ) def popen(self, cmd: list[str], env: dict[str, str] | None = None, *_args: Any, **_kwargs: Any) -> PopenMock: self.called.append(PopenArgs(command=cmd, env=env)) @@ -126,7 +133,8 @@ def popen(self, cmd: list[str], env: dict[str, str] | None = None, *_args: Any, output = "\n".join(mock_data.output_lines) if mock_data.side_effect: - mock_data.side_effect() + side_effect_args = mock_data.side_effect_args or {} + mock_data.side_effect(**side_effect_args) return PopenMock(output, exit_code)