diff --git a/approvaltests/reporters/__init__.py b/approvaltests/reporters/__init__.py index aae863d0..22eed696 100644 --- a/approvaltests/reporters/__init__.py +++ b/approvaltests/reporters/__init__.py @@ -12,4 +12,5 @@ from .received_file_launcher_reporter import * from .report_all_to_clipboard import * from .report_by_creating_diff_file import * +from .report_with_opening_file import * from .reporter_that_automatically_approves import * diff --git a/approvaltests/reporters/report_with_opening_file.py b/approvaltests/reporters/report_with_opening_file.py new file mode 100644 index 00000000..1e904a73 --- /dev/null +++ b/approvaltests/reporters/report_with_opening_file.py @@ -0,0 +1,38 @@ +import platform +from subprocess import call +from typing import List + +from typing_extensions import override + +from approvaltests.core.reporter import Reporter + + +class ReportWithOpeningFile(Reporter): + """ + A reporter that opens the received file using the + system default file viewer. + + Uses platform-specific commands: + - macOS: open + - Windows: start + - Linux/Unix: xdg-open + + Depending on the file viewer being launched, + the test suite execution may halt until the + user has closed the new process. + """ + + @staticmethod + def get_command(received_path: str) -> List[str]: + system = platform.system() + if system == "Darwin": + return ["open", received_path] + if system == "Windows": + return ["start", received_path] + return ["xdg-open", received_path] + + @override + def report(self, received_path: str, approved_path: str) -> bool: + command_array = self.get_command(received_path) + call(command_array) + return True diff --git a/tests/reporters/test_report_with_opening_file.py b/tests/reporters/test_report_with_opening_file.py new file mode 100644 index 00000000..a3102974 --- /dev/null +++ b/tests/reporters/test_report_with_opening_file.py @@ -0,0 +1,36 @@ +from unittest.mock import MagicMock, patch + +from approvaltests.reporters.report_with_opening_file import ReportWithOpeningFile + + +def test_get_command_darwin() -> None: + with patch("platform.system", return_value="Darwin"): + command = ReportWithOpeningFile.get_command("test.txt") + assert command == ["open", "test.txt"] + + +def test_get_command_windows() -> None: + with patch("platform.system", return_value="Windows"): + command = ReportWithOpeningFile.get_command("test.txt") + assert command == ["start", "test.txt"] + + +def test_get_command_linux() -> None: + with patch("platform.system", return_value="Linux"): + command = ReportWithOpeningFile.get_command("test.txt") + assert command == ["xdg-open", "test.txt"] + + +def test_get_command_unknown_system() -> None: + with patch("platform.system", return_value="UnknownOS"): + command = ReportWithOpeningFile.get_command("test.txt") + assert command == ["xdg-open", "test.txt"] + + +@patch("approvaltests.reporters.report_with_opening_file.call") +def test_report_calls_command(mock_call: MagicMock) -> None: + reporter = ReportWithOpeningFile() + with patch("platform.system", return_value="Darwin"): + result = reporter.report("received.txt", "approved.txt") + mock_call.assert_called_once_with(["open", "received.txt"]) + assert result is True