Skip to content

Commit

Permalink
cacheprovider: make file-skipping work with any File, not just Modules
Browse files Browse the repository at this point in the history
No reason for `--lf`'s whole-file-skipping feature to not for for
non-Python files.

Fix #11068.
  • Loading branch information
bluetech committed Jun 3, 2023
1 parent 4b823a4 commit fda8024
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/11068.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the ``--last-failed`` whole-file skipping functionality ("skipped N files") for :ref:`non-python test files <non-python tests>`.
8 changes: 4 additions & 4 deletions src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from _pytest.fixtures import fixture
from _pytest.fixtures import FixtureRequest
from _pytest.main import Session
from _pytest.python import Module
from _pytest.nodes import File
from _pytest.python import Package
from _pytest.reports import TestReport

Expand Down Expand Up @@ -242,7 +242,7 @@ def sort_key(node: Union[nodes.Item, nodes.Collector]) -> bool:
)
return

elif isinstance(collector, Module):
elif isinstance(collector, File):
if collector.path in self.lfplugin._last_failed_paths:
out = yield
res = out.get_result()
Expand Down Expand Up @@ -280,9 +280,9 @@ def __init__(self, lfplugin: "LFPlugin") -> None:
def pytest_make_collect_report(
self, collector: nodes.Collector
) -> Optional[CollectReport]:
# Packages are Modules, but we only want to skip test-bearing Modules,
# Packages are Files, but we only want to skip test-bearing Files,
# so don't filter Packages.
if isinstance(collector, Module) and not isinstance(collector, Package):
if isinstance(collector, File) and not isinstance(collector, Package):
if collector.path not in self.lfplugin._last_failed_paths:
self.lfplugin._skipped_files += 1

Expand Down
2 changes: 1 addition & 1 deletion testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def get_write_msg(self, idx):


@pytest.fixture
def dummy_yaml_custom_test(pytester: Pytester):
def dummy_yaml_custom_test(pytester: Pytester) -> None:
"""Writes a conftest file that collects and executes a dummy yaml test.
Taken from the docs, but stripped down to the bare minimum, useful for
Expand Down
22 changes: 22 additions & 0 deletions testing/test_cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,28 @@ def test_packages(self, pytester: Pytester) -> None:
result = pytester.runpytest("--lf")
result.assert_outcomes(failed=3)

def test_non_python_file_skipped(
self,
pytester: Pytester,
dummy_yaml_custom_test: None,
) -> None:
pytester.makepyfile(
**{
"test_bad.py": """def test_bad(): assert False""",
},
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(["collected 2 items", "* 1 failed, 1 passed in *"])

result = pytester.runpytest("--lf")
result.stdout.fnmatch_lines(
[
"collected 1 item",
"run-last-failure: rerun previous 1 failure (skipped 1 file)",
"* 1 failed in *",
]
)


class TestNewFirst:
def test_newfirst_usecase(self, pytester: Pytester) -> None:
Expand Down

0 comments on commit fda8024

Please sign in to comment.