From 91d70162d8835eca5c9a48d485a2b98551a9cbb5 Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Thu, 8 Feb 2024 14:12:55 -0500 Subject: [PATCH] tests: Ignore Textual snapshot mismatches on 3.7 The snapshots we've generated using current versions of Textual aren't expected to match anymore on Python 3.7, as Textual dropped support for Python 3.7 in the 0.44 release. However, we'd still like to run our snapshot tests on Python 3.7, to confirm that no unexpected exceptions occur and that the app doesn't crash. So, allow `snap_compare()` to drive the application, but always return `True` on Python 3.7 as long as no exception was raised. Signed-off-by: Matt Wozniski --- tests/conftest.py | 10 ----- tests/unit/test_tree_reporter.py | 66 ++++++++------------------------ tests/unit/test_tui_reporter.py | 23 ++++++++--- 3 files changed, 35 insertions(+), 64 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b708e04a05..91f1283aa2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,4 @@ import socket -import sys import pytest @@ -11,12 +10,3 @@ def free_port(): port_number = s.getsockname()[1] s.close() return port_number - - -def pytest_configure(config): - # Several of the tree reporter tests require Textual 0.48, which does not - # support Python 3.7, but skipping those tests causes the test suite to - # fail due to unused snapshots. Override the configuration for Python 3.7 - # so that unused snapshots are a warning, not an error. - if sys.version_info < (3, 8): - config.option.warn_unused_snapshots = True diff --git a/tests/unit/test_tree_reporter.py b/tests/unit/test_tree_reporter.py index 030436e1de..3e450116cb 100644 --- a/tests/unit/test_tree_reporter.py +++ b/tests/unit/test_tree_reporter.py @@ -1532,6 +1532,15 @@ def test_render_runs_the_app(self): @pytest.fixture def compare(monkeypatch, tmp_path, snap_compare): + # The snapshots we've generated using current versions of Textual aren't + # expected to match anymore on Python 3.7, as Textual dropped support for + # Python 3.7 in the 0.44 release. However, we'd still like to run our + # snapshot tests on Python 3.7, to confirm that no unexpected exceptions + # occur and that the app doesn't crash. So, allow `snap_compare()` to drive + # the application, but always return `True` on Python 3.7 as long as no + # exception was raised. + succeed_even_if_mismatched = sys.version_info < (3, 8) + def compare_impl( allocations: Iterator[AllocationRecord], press: Iterable[str] = (), @@ -1550,21 +1559,20 @@ def compare_impl( with monkeypatch.context() as app_patch: app_patch.setitem(globals(), app_global, app) tmp_main.write_text(f"from {__name__} import {app_global} as app") - return snap_compare( - str(tmp_main), - press=press, - terminal_size=terminal_size, - run_before=run_before, + return ( + snap_compare( + str(tmp_main), + press=press, + terminal_size=terminal_size, + run_before=run_before, + ) + or succeed_even_if_mismatched ) yield compare_impl class TestTUILooks: - @pytest.mark.skipif( - sys.version_info < (3, 8), - reason="This test requires Textual 0.49 or higher, which doesn't support 3.7", - ) def test_basic(self, compare): # GIVEN code = dedent( @@ -1599,10 +1607,6 @@ def generate_primes(): getlines.return_value = code.splitlines() assert compare(peak_allocations, press=[]) - @pytest.mark.skipif( - sys.version_info < (3, 8), - reason="This test requires Textual 0.48 or higher, which doesn't support 3.7", - ) def test_basic_node_selected_not_leaf(self, compare): # GIVEN code = dedent( @@ -1637,10 +1641,6 @@ def generate_primes(): getlines.return_value = code.splitlines() assert compare(peak_allocations, press=[*["down"] * 2]) - @pytest.mark.skipif( - sys.version_info < (3, 8), - reason="This test requires Textual 0.49 or higher, which doesn't support 3.7", - ) def test_basic_node_selected_leaf(self, compare): # GIVEN code = dedent( @@ -1675,10 +1675,6 @@ def generate_primes(): getlines.return_value = code.splitlines() assert compare(peak_allocations, press=[*["down"] * 3]) - @pytest.mark.skipif( - sys.version_info < (3, 8), - reason="This test requires Textual 0.49 or higher, which doesn't support 3.7", - ) def test_two_chains(self, compare): # GIVEN code = dedent( @@ -1726,10 +1722,6 @@ def generate_primes(): getlines.return_value = code.splitlines() assert compare(peak_allocations, press=[]) - @pytest.mark.skipif( - sys.version_info < (3, 8), - reason="This test requires Textual 0.48 or higher, which doesn't support 3.7", - ) def test_two_chains_after_expanding_second(self, compare): # GIVEN code = dedent( @@ -1779,10 +1771,6 @@ def generate_primes(): getlines.return_value = code.splitlines() assert compare(peak_allocations, press=[*["down"] * 4, "e"]) - @pytest.mark.skipif( - sys.version_info < (3, 8), - reason="This test requires Textual 0.49 or higher, which doesn't support 3.7", - ) def test_hide_import_system(self, compare): # GIVEN code = dedent( @@ -1833,10 +1821,6 @@ def generate_primes(): getlines.return_value = code.splitlines() assert compare(peak_allocations, press=["i"]) - @pytest.mark.skipif( - sys.version_info < (3, 8), - reason="This test requires Textual 0.49 or higher, which doesn't support 3.7", - ) def test_show_uninteresting(self, compare): # GIVEN code = dedent( @@ -1887,10 +1871,6 @@ def generate_primes(): getlines.return_value = code.splitlines() assert compare(peak_allocations, press=["u"]) - @pytest.mark.skipif( - sys.version_info < (3, 8), - reason="This test requires Textual 0.49 or higher, which doesn't support 3.7", - ) def test_show_uninteresting_and_hide_import_system(self, compare): # GIVEN code = dedent( @@ -1942,10 +1922,6 @@ def generate_primes(): getlines.return_value = code.splitlines() assert compare(peak_allocations, press=["u", "i"]) - @pytest.mark.skipif( - sys.version_info < (3, 8), - reason="This test requires Textual 0.48 or higher, which doesn't support 3.7", - ) def test_select_screen(self, tmp_path, compare): # GIVEN code = dedent( @@ -1979,10 +1955,6 @@ def generate_primes(): getlines.return_value = code.splitlines() assert compare(peak_allocations, press=[*["down"] * 3]) - @pytest.mark.skipif( - sys.version_info < (3, 8), - reason="This test requires Textual 0.49 or higher, which doesn't support 3.7", - ) def test_allocations_of_different_sizes(self, compare): # GIVEN peak_allocations = [ @@ -2003,10 +1975,6 @@ def test_allocations_of_different_sizes(self, compare): getlines.return_value = [] assert compare(peak_allocations, press=[], terminal_size=(350, 100)) - @pytest.mark.skipif( - sys.version_info < (3, 8), - reason="This test requires Textual 0.49 or higher, which doesn't support 3.7", - ) def test_biggest_allocations(self, compare): # GIVEN peak_allocations = [ diff --git a/tests/unit/test_tui_reporter.py b/tests/unit/test_tui_reporter.py index 83b46cfef0..88aa747e3e 100644 --- a/tests/unit/test_tui_reporter.py +++ b/tests/unit/test_tui_reporter.py @@ -1,5 +1,6 @@ import asyncio import datetime +import sys from io import StringIO from typing import Awaitable from typing import Callable @@ -102,6 +103,15 @@ def get_current_snapshot( def compare(monkeypatch, tmp_path, snap_compare): monkeypatch.setattr(memray.reporters.tui, "datetime", FakeDatetime) + # The snapshots we've generated using current versions of Textual aren't + # expected to match anymore on Python 3.7, as Textual dropped support for + # Python 3.7 in the 0.44 release. However, we'd still like to run our + # snapshot tests on Python 3.7, to confirm that no unexpected exceptions + # occur and that the app doesn't crash. So, allow `snap_compare()` to drive + # the application, but always return `True` on Python 3.7 as long as no + # exception was raised. + succeed_even_if_mismatched = sys.version_info < (3, 8) + def compare_impl( cmdline_override: Optional[str] = None, press: Iterable[str] = (), @@ -128,11 +138,14 @@ async def run_before_wrapper(pilot) -> None: with monkeypatch.context() as app_patch: app_patch.setitem(globals(), app_global, app) tmp_main.write_text(f"from {__name__} import {app_global} as app") - return snap_compare( - str(tmp_main), - press=press, - terminal_size=terminal_size, - run_before=run_before_wrapper, + return ( + snap_compare( + str(tmp_main), + press=press, + terminal_size=terminal_size, + run_before=run_before_wrapper, + ) + or succeed_even_if_mismatched ) yield compare_impl