|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | +import pytest |
| 3 | + |
| 4 | + |
| 5 | +from logplot.plot import Plot |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def mock_pyplot(monkeypatch): |
| 10 | + monkeypatch.setattr("logplot.plot.plt", MagicMock()) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def no_plot_initialise(monkeypatch): |
| 15 | + monkeypatch.setattr("logplot.plot.Plot._initialise", MagicMock()) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.usefixtures("mock_pyplot", "no_plot_initialise") |
| 19 | +@patch("logplot.plot.sys") |
1 | 20 | class TestOpenLogViewer: |
2 | | - # TODO |
3 | | - pass |
| 21 | + # fmt: off |
| 22 | + @pytest.mark.parametrize( |
| 23 | + "open_cmd, shell, platform, log_path, expected", |
| 24 | + [ |
| 25 | + ("", False, "linux", "foo/bar/log.txt", ["xdg-open", "foo/bar/log.txt"]), |
| 26 | + ("", False, "darwin", "foo/bar/log.txt", ["open", "foo/bar/log.txt"]), |
| 27 | + ("custom_cmd '{path}'", True, "darwin", "/foo/bar space/log.txt", |
| 28 | + ["custom_cmd", "/foo/bar space/log.txt"]), |
| 29 | + ("custom_cmd '{path}':{line_number} ", True, "win32", "somewhere/log.txt", |
| 30 | + ["custom_cmd", "somewhere/log.txt:99"]) |
| 31 | + ] |
| 32 | + ) |
| 33 | + # fmt: on |
| 34 | + @patch("logplot.plot.subprocess.Popen") |
| 35 | + def test_it_passes_correct_params_to_popen( |
| 36 | + self, popen, sys_mock, open_cmd, shell, platform, log_path, expected |
| 37 | + ): |
| 38 | + conf = MagicMock() |
| 39 | + conf.general.shell = shell |
| 40 | + conf.general.log_open_cmd = open_cmd |
| 41 | + sys_mock.platform = platform |
| 42 | + plot = Plot(entries=[], special_entries=[], log_path=log_path, conf=conf) |
| 43 | + |
| 44 | + plot._open_log_viewer(line_number=99) |
| 45 | + |
| 46 | + args, kwargs = popen.call_args |
| 47 | + assert args[0] == expected |
| 48 | + assert kwargs["shell"] == shell |
| 49 | + |
| 50 | + @patch("logplot.plot.os") |
| 51 | + def test_it_calls_startfile_on_windows_as_default(self, os_mock, sys_mock): |
| 52 | + conf = MagicMock() |
| 53 | + conf.general.log_open_cmd = '' |
| 54 | + sys_mock.platform = "win32" |
| 55 | + plot = Plot(entries=[], special_entries=[], log_path="foo", conf=conf) |
| 56 | + |
| 57 | + plot._open_log_viewer(line_number=99) |
| 58 | + os_mock.startfile.assert_called_with("foo") |