From c5cf740496577cf42a085deb0e7bcfcdb8cb0a2c Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Fri, 8 Mar 2024 16:15:34 +0000 Subject: [PATCH] complete testing Settings class maybe a few more edge cases to look at later Signed-off-by: Grant Ramsay --- tests/test_settings.py | 73 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/tests/test_settings.py b/tests/test_settings.py index 73943e95..b5ab06e2 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -67,6 +67,21 @@ def setting_file_macos( ) return fs + @pytest.fixture() + def setting_file_linux( + self, + fs: FakeFilesystem, + ) -> FakeFilesystem: + """Create a settings file.""" + fs.os = OSType.LINUX + config_dir = Path.home() / self.CONFIG_FOLDER + fs.create_dir(config_dir) + fs.create_file( + str(config_dir / self.CONGIG_FILE), + contents=self.FAKE_TOML, + ) + return fs + def test_settings(self, setting_file: FakeFilesystem) -> None: """Test the settings module creates properly.""" settings = get_settings() @@ -126,10 +141,64 @@ def test_edit_config_macos( ) # ---------------------------- unfinished test --------------------------- # - def test_edit_config_linux( - self, mocker: MockerFixture, setting_file: FakeFilesystem + def test_edit_config_linux_first_match( + self, mocker: MockerFixture, setting_file_linux: FakeFilesystem ) -> None: """Test the edit config function on linux.""" + assert setting_file_linux.is_linux + mocker.patch("platform.system", return_value="Linux") + mock_call = mocker.patch("subprocess.call") + settings = Settings("pymaker") + settings.edit_config() + + mock_call.assert_called_once_with( + ["xdg-open", str(Path.home() / ".pymaker/config.toml")] + ) + + def test_edit_config_linux_third_match( + self, mocker: MockerFixture, setting_file_linux: FakeFilesystem + ) -> None: + """Test the edit config function on linux.""" + call_count = [0] + + def side_effect(_) -> int: + call_count[0] += 1 + if call_count[0] <= 2: # noqa: PLR2004 + raise FileNotFoundError + return 0 + + assert setting_file_linux.is_linux + mocker.patch("platform.system", return_value="Linux") + mock_call = mocker.patch("subprocess.call", side_effect=side_effect) + settings = Settings("pymaker") + settings.edit_config() + + assert mock_call.call_count == 3 # noqa: PLR2004 + + def test_edit_config_linux_no_editor( + self, mocker: MockerFixture, setting_file_linux: FakeFilesystem, capsys + ) -> None: + """Test the edit config function on linux when editor not found.""" + assert setting_file_linux.is_linux + mocker.patch("platform.system", return_value="Linux") + mock_call = mocker.patch( + "subprocess.call", side_effect=FileNotFoundError + ) + settings = Settings("pymaker") + settings.edit_config() + + output = capsys.readouterr().out + + call_args_expected = ["xdg-open", "sensible-editor", "nano", "vi"] + call_args_processed = [ + call.args[0][0] for call in mock_call.call_args_list + ] + + assert call_args_processed == call_args_expected + assert mock_call.call_count == 4 # noqa: PLR2004 + assert ( + "No editor found. Please edit the settings file manually" in output + ) # -------------------------- end unfinished test ------------------------- #