|
12 | 12 | from typing import cast |
13 | 13 | from typing import Literal |
14 | 14 | from typing import NamedTuple |
15 | | -from unittest.mock import Mock |
16 | | -from unittest.mock import patch |
| 15 | +from unittest import mock |
17 | 16 |
|
18 | 17 | import pluggy |
19 | 18 |
|
@@ -3419,40 +3418,57 @@ def mock_file(self) -> StringIO: |
3419 | 3418 |
|
3420 | 3419 | @pytest.fixture |
3421 | 3420 | def mock_tr(self, mock_file: StringIO) -> pytest.TerminalReporter: |
3422 | | - tr = Mock(spec=pytest.TerminalReporter) |
| 3421 | + tr: pytest.TerminalReporter = mock.create_autospec(pytest.TerminalReporter) |
3423 | 3422 |
|
3424 | | - def write_raw(s: str, *, flush: bool = False) -> None: |
3425 | | - mock_file.write(s) |
| 3423 | + def write_raw(content: str, *, flush: bool = False) -> None: |
| 3424 | + mock_file.write(content) |
3426 | 3425 |
|
3427 | | - tr.write_raw = write_raw |
| 3426 | + tr.write_raw = write_raw # type: ignore[method-assign] |
3428 | 3427 | tr._progress_nodeids_reported = set() |
3429 | 3428 | return tr |
3430 | 3429 |
|
3431 | | - def test_plugin_registration(self, pytester: pytest.Pytester) -> None: |
3432 | | - """Test that the plugin is registered correctly on TTY output.""" |
| 3430 | + @pytest.mark.skipif(sys.platform != "win32", reason="#13896") |
| 3431 | + def test_plugin_registration_enabled_by_default( |
| 3432 | + self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch |
| 3433 | + ) -> None: |
| 3434 | + """Test that the plugin registration is enabled by default. |
| 3435 | +
|
| 3436 | + Currently only on Windows (#13896). |
| 3437 | + """ |
| 3438 | + monkeypatch.setattr(sys.stdout, "isatty", lambda: True) |
3433 | 3439 | # The plugin module should be registered as a default plugin. |
3434 | | - with patch.object(sys.stdout, "isatty", return_value=True): |
3435 | | - config = pytester.parseconfigure() |
3436 | | - plugin = config.pluginmanager.get_plugin("terminalprogress") |
3437 | | - assert plugin is not None |
| 3440 | + config = pytester.parseconfigure() |
| 3441 | + plugin = config.pluginmanager.get_plugin("terminalprogress") |
| 3442 | + assert plugin is not None |
3438 | 3443 |
|
3439 | | - def test_disabled_for_non_tty(self, pytester: pytest.Pytester) -> None: |
| 3444 | + def test_plugin_registred_on_all_platforms_when_explicitly_requested( |
| 3445 | + self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch |
| 3446 | + ) -> None: |
| 3447 | + """Test that the plugin is registered on any platform if explicitly requested.""" |
| 3448 | + monkeypatch.setattr(sys.stdout, "isatty", lambda: True) |
| 3449 | + # The plugin module should be registered as a default plugin. |
| 3450 | + config = pytester.parseconfigure("-p", "terminalprogress") |
| 3451 | + plugin = config.pluginmanager.get_plugin("terminalprogress") |
| 3452 | + assert plugin is not None |
| 3453 | + |
| 3454 | + def test_disabled_for_non_tty( |
| 3455 | + self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch |
| 3456 | + ) -> None: |
3440 | 3457 | """Test that plugin is disabled for non-TTY output.""" |
3441 | | - with patch.object(sys.stdout, "isatty", return_value=False): |
3442 | | - config = pytester.parseconfigure() |
3443 | | - plugin = config.pluginmanager.get_plugin("terminalprogress") |
3444 | | - assert plugin is None |
3445 | | - |
3446 | | - def test_disabled_for_iterm2(self, pytester: pytest.Pytester, monkeypatch) -> None: |
3447 | | - """Should not register the plugin on iTerm2 terminal since it interprets |
3448 | | - OSC 9;4 as desktop notifications, not progress (#13896).""" |
3449 | | - monkeypatch.setenv( |
3450 | | - "ITERM_SESSION_ID", "w0t1p0:3DB6DF06-FE11-40C3-9A66-9E10A193A632" |
3451 | | - ) |
3452 | | - with patch.object(sys.stdout, "isatty", return_value=True): |
3453 | | - config = pytester.parseconfigure() |
3454 | | - plugin = config.pluginmanager.get_plugin("terminalprogress") |
3455 | | - assert plugin is None |
| 3458 | + monkeypatch.setattr(sys.stdout, "isatty", lambda: False) |
| 3459 | + config = pytester.parseconfigure("-p", "terminalprogress") |
| 3460 | + plugin = config.pluginmanager.get_plugin("terminalprogress-plugin") |
| 3461 | + assert plugin is None |
| 3462 | + |
| 3463 | + def test_disabled_for_dumb_terminal( |
| 3464 | + self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch |
| 3465 | + ) -> None: |
| 3466 | + """Test that plugin is disabled when TERM=dumb.""" |
| 3467 | + monkeypatch.setenv("TERM", "dumb") |
| 3468 | + monkeypatch.setattr(sys.stdout, "isatty", lambda: True) |
| 3469 | + config = pytester.parseconfigure("-p", "terminalprogress") |
| 3470 | + plugin = config.pluginmanager.get_plugin("terminalprogress-plugin") |
| 3471 | + assert plugin is None |
3456 | 3472 |
|
3457 | 3473 | @pytest.mark.parametrize( |
3458 | 3474 | ["state", "progress", "expected"], |
@@ -3484,7 +3500,7 @@ def test_session_lifecycle( |
3484 | 3500 | """Test progress updates during session lifecycle.""" |
3485 | 3501 | plugin = TerminalProgressPlugin(mock_tr) |
3486 | 3502 |
|
3487 | | - session = Mock(spec=pytest.Session) |
| 3503 | + session = mock.create_autospec(pytest.Session) |
3488 | 3504 | session.testscollected = 3 |
3489 | 3505 |
|
3490 | 3506 | # Session start - should emit indeterminate progress. |
|
0 commit comments