Skip to content

Commit

Permalink
tests: Ignore Textual snapshot mismatches on 3.7
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
godlygeek committed Feb 9, 2024
1 parent 9b54ea4 commit 22aef5d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 64 deletions.
10 changes: 0 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import socket
import sys

import pytest

Expand All @@ -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
66 changes: 17 additions & 49 deletions tests/unit/test_tree_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = (),
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 = [
Expand All @@ -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 = [
Expand Down
23 changes: 18 additions & 5 deletions tests/unit/test_tui_reporter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import datetime
import sys
from io import StringIO
from typing import Awaitable
from typing import Callable
Expand Down Expand Up @@ -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] = (),
Expand All @@ -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
Expand Down

0 comments on commit 22aef5d

Please sign in to comment.