Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
F6401 cannot-enumerate-pytest-fixtures (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
reverbc committed Apr 11, 2021
1 parent 5bde71e commit c581816
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
## Added
- W6402 `useless-pytest-mark-decorator`: add warning for [using pytest.mark on fixtures](https://docs.pytest.org/en/stable/reference.html#marks) (thanks to @DKorytkin)
- W6403 `deprecated-positional-argument-for-pytest-fixture`: add warning for [positional arguments to pytest.fixture()](https://docs.pytest.org/en/stable/deprecations.html#pytest-fixture-arguments-are-keyword-only) (thanks to @DKorytkin)
- F6401 `cannot-enumerate-pytest-fixtures`: add fatal error when the plugin cannot enumerate and collect pytest fixtures for analysis (#27)

## [1.0.3] - 2021-03-13
## Fixed
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ def awesome_fixture():
...
```

### F6401 `cannot-enumerate-pytest-fixtures`

Raise when the plugin cannot enumerate and collect pytest fixtures for analysis

```python
import no_such_package # <- pylint-pytest plugin cannot enumerate and collect pytest fixtures
```

## Changelog

See [CHANGELOG](CHANGELOG.md).
Expand Down
17 changes: 16 additions & 1 deletion pylint_pytest/checkers/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@

class FixtureCollector:
fixtures = {}
errors = set()

def pytest_sessionfinish(self, session):
# pylint: disable=protected-access
self.fixtures = session._fixturemanager._arg2fixturedefs

def pytest_collectreport(self, report):
if report.failed:
self.errors.add(report)


class FixtureChecker(BasePytestChecker):
__implements__ = IAstroidChecker
Expand All @@ -44,6 +49,13 @@ class FixtureChecker(BasePytestChecker):
'deprecated-positional-argument-for-pytest-fixture',
'Pass scope as a kwarg, not positional arg, which is deprecated in future pytest.'
'Take a look at: https://docs.pytest.org/en/stable/deprecations.html#pytest-fixture-arguments-are-keyword-only',
'F6401': (
(
'pylint-pytest plugin cannot enumerate and collect pytest fixtures. '
'Please run `pytest --fixtures --collect-only` 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.',
),
}

Expand Down Expand Up @@ -95,7 +107,7 @@ def visit_module(self, node):
# save and restore sys.path to prevent pytest.main from altering it
sys_path = sys.path.copy()

pytest.main(
ret = pytest.main(
[
node.file, '--fixtures', '--collect-only',
'--pythonwarnings=ignore:Module already imported:pytest.PytestWarning',
Expand All @@ -107,6 +119,9 @@ def visit_module(self, node):
sys.path = sys_path

FixtureChecker._pytest_fixtures = fixture_collector.fixtures

if ret != pytest.ExitCode.OK or fixture_collector.errors:

This comment has been minimized.

Copy link
@webknjaz

webknjaz Aug 18, 2021

This check is buggy and should be fixed by #22.

self.add_message('cannot-enumerate-pytest-fixtures', node=node)
finally:
# restore output devices
sys.stdout, sys.stderr = stdout, stderr
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from no_such_package import fixture


def test_something(fixture):
pass
10 changes: 10 additions & 0 deletions tests/input/cannot-enumerate-pytest-fixtures/no_such_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest
import this_is_invalid # makes pytest fail


@pytest.fixture
def fixture():
pass

def test_something(fixture):
pass
19 changes: 19 additions & 0 deletions tests/test_cannot_enumerate_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest
from pylint.checkers.variables import VariablesChecker
from base_tester import BasePytestTester
from pylint_pytest.checkers.fixture import FixtureChecker


class TestCannotEnumerateFixtures(BasePytestTester):
CHECKER_CLASS = FixtureChecker
MSG_ID = 'cannot-enumerate-pytest-fixtures'

@pytest.mark.parametrize('enable_plugin', [True, False])
def test_no_such_package(self, enable_plugin):
self.run_linter(enable_plugin)
self.verify_messages(1 if enable_plugin else 0)

@pytest.mark.parametrize('enable_plugin', [True, False])
def test_import_corrupted_module(self, enable_plugin):
self.run_linter(enable_plugin)
self.verify_messages(1 if enable_plugin else 0)

0 comments on commit c581816

Please sign in to comment.