Skip to content

Add ReportWithOpeningFile reporter for issue #205 #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions approvaltests/reporters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
38 changes: 38 additions & 0 deletions approvaltests/reporters/report_with_opening_file.py
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions tests/reporters/test_report_with_opening_file.py
Original file line number Diff line number Diff line change
@@ -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
Loading