From f6a343d72b425e632db1a02b9b065d07fba93b72 Mon Sep 17 00:00:00 2001 From: Stavros Ntentos <133706+stdedos@users.noreply.github.com> Date: Wed, 18 Oct 2023 21:27:08 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Ignore=20collection=20failures?= =?UTF-8?q?=20in=20non-tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change applies the pre-existing patterns to identify if the files with collection problems are tests. It is then used to eliminate the false-positives of F6401 (cannot-enumerate-pytest-fixtures). As a side effect, this patch also includes precise file paths that may be used to reproduce the problem. Fixes https://github.com/reverbc/pylint-pytest/issues/20 Fixes https://github.com/reverbc/pylint-pytest/issues/21 Signed-off-by: Sviatoslav Sydorenko _Replayed; Source PR: https://github.com/reverbc/pylint-pytest/pull/22_ Additionally, satisfied the https://github.com/pylint-dev/pylint-pytest's `.pre-commit-config.yaml` Signed-off-by: Stavros Ntentos <133706+stdedos@users.noreply.github.com> --- pylint_pytest/checkers/fixture.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/pylint_pytest/checkers/fixture.py b/pylint_pytest/checkers/fixture.py index 32ec894..9af22a7 100644 --- a/pylint_pytest/checkers/fixture.py +++ b/pylint_pytest/checkers/fixture.py @@ -68,8 +68,8 @@ class FixtureChecker(BasePytestChecker): "F6401": ( ( "pylint-pytest plugin cannot enumerate and collect pytest fixtures. " - "Please run `pytest --fixtures --collect-only path/to/current/module.py`" - " and resolve any potential syntax error or package dependency issues" + "Please run `pytest --fixtures --collect-only %s` and resolve " + "any potential syntax error or package dependency issues" ), "cannot-enumerate-pytest-fixtures", "Used when pylint-pytest has been unable to enumerate and collect pytest fixtures.", @@ -143,8 +143,23 @@ def visit_module(self, node): FixtureChecker._pytest_fixtures = fixture_collector.fixtures - if (ret != pytest.ExitCode.OK or fixture_collector.errors) and is_test_module: - self.add_message("cannot-enumerate-pytest-fixtures", node=node) + legitimate_failure_paths = set( + collection_report.nodeid + for collection_report in fixture_collector.errors + if any( + fnmatch.fnmatch( + Path(collection_report.nodeid).name, + pattern, + ) + for pattern in FILE_NAME_PATTERNS + ) + ) + if (ret != pytest.ExitCode.OK or legitimate_failure_paths) and is_test_module: + self.add_message( + "cannot-enumerate-pytest-fixtures", + args=" ".join(legitimate_failure_paths | {node.file}), + node=node, + ) finally: # restore output devices sys.stdout, sys.stderr = stdout, stderr