Commit Graph

202 Commits

Author SHA1 Message Date
mrbean-bremen 8248946a55
Do not collect symlinked tests under Windows (#12050)
The check for short paths under Windows via os.path.samefile, introduced in #11936, also found similar tests in symlinked tests in the GH Actions CI.

Fixes #12039.

Co-authored-by: Bruno Oliveira <bruno@soliv.dev>
2024-03-03 09:41:31 -03:00
Ran Benita 31026a2df2 main: only include package parents in collection tree for --pyargs collection arguments
(diff better viewed ignoring whitespace)

In pytest<8, the collection tree for `pyargs` arguments in an invocation
like this:

    pytest --collect-only --pyargs pyflakes.test.test_undefined_names

looked like this:

```
<Package test>
  <Module test_undefined_names.py>
    <UnitTestCase Test>
      <TestCaseFunction test_annotationUndefined>
      ... snipped ...
```

The pytest 8 collection improvements changed it to this:

```
<Dir pytest>
  <Dir .tox>
    <Dir venv>
      <Dir lib>
        <Dir python3.11>
          <Dir site-packages>
            <Package pyflakes>
              <Package test>
                <Module test_undefined_names.py>
                  <UnitTestCase Test>
                    <TestCaseFunction test_annotationUndefined>
                    ... snipped ...
```

Besides being egregious (and potentially even worse than the above,
going all the way to the root, for system-installed packages, as is
apparently common in CI), this also caused permission errors when trying
to probe some of those intermediate directories.

This change makes `--pyargs` arguments no longer try to add parent
directories to the collection tree according to the `--confcutdir` like
they're regular arguments. Instead, only add the parents that are in the
import path. This now looks like this:

```
<Package .tox/venv/lib/python3.11/site-packages/pyflakes>
  <Package test>
    <Module test_undefined_names.py>
      <UnitTestCase Test>
        <TestCaseFunction test_annotationUndefined>
        ... snipped ...
```

Fix #11904.
2024-03-02 20:26:02 +02:00
Bruno Oliveira 8d9b95dcdb
Fix collection of short paths on Windows (#11936)
Passing a short path in the command line was causing the matchparts check to fail, because ``Path(short_path) != Path(long_path)``.

Using ``os.path.samefile`` as fallback ensures the comparsion works on Windows when comparing short/long paths.

Fix #11895
2024-02-23 07:51:15 -03:00
Ran Benita 010ce2ab0f
Add typing to `from_parent` return values (#11916)
Up to now the return values of `from_parent` were untyped, this is an
attempt to make it work with `typing.Self`.
2024-02-22 23:35:57 -08:00
Ran Benita 5bb1363435
Merge pull request #11957 from bluetech/fix-session-collect-order
main: fix reversed collection order in Session
2024-02-13 09:44:13 +02:00
Ran Benita ea57c40c43 main: fix reversed collection order in Session
Since we're working with a stack (last in first out), we need to append
to it in reverse to preserve the order when popped.

Fix #11937.
2024-02-09 15:24:44 +02:00
Ran Benita a182e10b06 Enable lint PGH004 - Use specific rule codes when using noqa 2024-02-09 11:14:36 +02:00
Pierre Sassoulas 4588653b24 Migrate from autoflake, black, isort, pyupgrade, flake8 and pydocstyle, to ruff
ruff is faster and handle everything we had prior.

isort configuration done based on the indication from
https://github.com/astral-sh/ruff/issues/4670, previousely based on
reorder-python-import (#11896)

flake8-docstrings was a wrapper around pydocstyle (now archived) that
explicitly asks to use ruff in https://github.com/PyCQA/pydocstyle/pull/658.

flake8-typing-import is useful mainly for project that support python 3.7
and the one useful check will be implemented in https://github.com/astral-sh/ruff/issues/2302

We need to keep blacken-doc because ruff does not handle detection
of python code inside .md and .rst. The direct link to the repo is
now used to avoid a redirection.

Manual fixes:
- Lines that became too long
- % formatting that was not done automatically
- type: ignore that were moved around
- noqa of hard to fix issues (UP031 generally)
- fmt: off and fmt: on that is not really identical
  between black and ruff
- autofix re-order in pre-commit from faster to slower

Co-authored-by: Ran Benita <ran@unusedvar.com>
2024-02-02 09:27:00 +01:00
Bruno Oliveira 878af85aef
mypy: disallow untyped defs by default (#11862)
Change our mypy configuration to disallow untyped defs by default, which ensures *new* files added to the code base are fully typed.

To avoid having to type-annotate everything now, add `# mypy: allow-untyped-defs` to files which are not fully type annotated yet.

As we fully type annotate those modules, we can then just remove that directive from the top.
2024-01-28 10:12:42 -03:00
Ran Benita d220880924
nodes: fix tracebacks from collection errors are not getting pruned (#11711)
Fix #11710.
2023-12-31 10:14:23 +02:00
Ran Benita 385796ba49 Rework Session and Package collection
Fix #7777.
2023-12-10 17:01:39 +02:00
Ran Benita 0ae02e2165 nodes,python: mark abstract node classes as ABCs
Fixes #11676
2023-12-07 16:41:07 +02:00
Ran Benita b41acaea12 Switch to new-style pluggy hook wrappers
Fix #11122.
2023-07-14 22:47:48 +03:00
Ran Benita c8b1790ee7 python: change `pytest pkg/__init__.py` to only collect the `__init__.py` Module
Previously it would collect the entire package, but this is not what
users expect.

Refs #3749
Fixes #8976
Fixes #9263
Fixes #9313
2023-06-23 20:42:50 +03:00
Zac Hatfield-Dodds 661b938fca Add encoding in more tests 2023-06-20 04:55:40 -07:00
nondescryptid a704605cf1 Fix encoding warnings 2023-06-20 04:55:39 -07:00
Ran Benita 14890329dc config: fix the paths considered for initial conftest discovery
Fixes #11104.

See the issue for a description of the problem.

Now, we use the same logic for initial conftest paths as we do for
deciding the initial args, which was the idea behind checking
`namespace.file_or_dir` and `testpaths` previously.

This fixes the issue of `testpaths` being considered for initial
conftests even when it's not used for the args.

(Another issue in faeb16146b was that the
`testpaths` were not glob-expanded, this is also fixed.)
2023-06-21 09:01:42 +03:00
Bruno Oliveira 2d57d5c32f Do not break on very long command-line options
`_set_initial_conftests` could break on some systems if a very long
option was passed, because the `Path.exists()` call raises an
`OSError` instead of returning `False`.

Fix #10169
2023-05-12 09:34:15 -03:00
Bruno Oliveira faeb16146b Consider testpaths for initial conftests
The 'testpaths' option is meant to be identical to execute
pytest passing the 'testpaths' directories explicitly.

Fix #10987
2023-05-12 09:34:15 -03:00
Marko Pacak 9fbd67dd4b
Class methods can now be discovered as tests (#10552)
Fix #10525
2022-12-02 15:53:04 +00:00
Jon Parise 8ac6dce2c7
Add shell-style wildcard support to 'testpaths' (#9897)
This is especially useful for large repositories (e.g. monorepos) that
use a hierarchical file system organization for nested test paths.

    src/*/tests

The implementation uses the standard `glob` module to perform wildcard
expansion in Config.parse().

The related logic that determines whether or not to include 'testpaths'
in the terminal header was previously relying on a weak heuristic: if
Config.args matched 'testpaths', then its value was printed. That
generally worked, but it could also print when the user explicitly used
the same arguments on the command-line as listed in 'testpaths'. Not a
big deal, but it shows that the check was logically incorrect.

Now that 'testpaths' can contain wildcards, it's no longer possible to
perform this simple comparison, so this change also introduces a public
Config.ArgSource enum and Config.args_source attribute that explicitly
names the "source" of the arguments: the command line, the invocation
directory, or the 'testdata' configuration value.
2022-05-24 10:20:51 +02:00
Bruno Oliveira 3c359455e2 Change directories during some tests in test_collection.py
As discussed in https://github.com/pytest-dev/pytest/pull/9800, this uses
monkeypatch to change directories for two tests in `test_collection.py`, to preserve
their original purpose (even if just removing it doesn't seem to affect the outcome).
2022-03-21 12:38:20 -03:00
Zac Hatfield-Dodds 0ebfa1d76d Fix CI for Python 3.11
Getting the current working directory has no side-effects, so these context managers were no-ops.
Discovered because Path.__enter__ is deprecated in Python 3.11, for removal in 3.13.
2022-03-20 21:01:26 -07:00
Bruno Oliveira 747b8372ea Try to import module before creating dummy modules with 'importmode=importlib'
The dummy modules we introduce in `insert_missing_modules` (due to #7856 and #7859)
would cause problems if the dummy modules actually end up replacing modules
which could be imported normally because they are available in `PYTHONPATH`.

Now we attempt to first import the module via normal mechanisms, and only
introduce the dummy modules if the intermediary modules don't actually exist.

Close #9645
2022-02-14 09:42:05 -03:00
Ran Benita 8713c32462 python: unpacked marks need to be added to keywords on all node types
(except `Instance`)

Currently, `Function` does this manually, but other node types don't get
their markers added to their `keywords`, but they should, if only for
consistency.
2022-01-21 14:34:27 +02:00
Ran Benita f08a77de77 python: add back `instance` accessor to all python nodes, not just Function
Regressed in 062d91ab4 (pytest 7.0.0rc1 only).

Fix #9486.
2022-01-09 12:14:46 +02:00
Bruno Oliveira a335ade1f5
Rename pathlib hook parameters (#9363)
* Rename pytest_ignore_collect fspath parameter to collection_path

* Rename pytest_collect_file fspath parameter to file_path

* Rename pytest_pycollect_makemodule fspath parameter to module_path

* Rename pytest_report_header startpath parameter to start_path

* Rename pytest_report_collectionfinish startpath parameter to start_path

* Update docs with the renamed parameters

* Use pytest-flakes fork temporarily to prove it works

* Use pytest-flakes 4.0.5
2021-12-03 13:14:09 +01:00
Ran Benita 062d91ab47
python: remove the `Instance` collector node 2021-11-13 14:03:44 +02:00
Ran Benita 1df28a4450 Move testdir to legacypath plugin 2021-10-28 21:51:51 +03:00
Ran Benita cf4495ffe1 mark/structures: fix NodeKeywords.{__iter__,__len__} given duplicates
Regressed in c4b9b14a4 -- the `set` stuff had a reason which I had
missed.
2021-10-06 11:24:28 +03:00
Bruno Oliveira 2c4c57e135 Revert fspath deprecation
It is not clear yet how we should proceed with this deprecation
because `pytest.Item.reportinfo` is public API and returns a `py.path` object,
and is not clear how plugins and our examples should handle that.

Reverting just the deprecation aspect of #8251 so we can get a 7.0.0 release out.

We will reintroduce the deprecation later once we have a clear path moving forward with replacing `reportinfo`.

Closes #8445
Closes #8821
2021-07-13 08:24:39 -03:00
Florian Bruhin 61be48b485 Fix test_collect_symlink_dir on Windows 2021-05-04 17:25:08 +02:00
Ran Benita f0c7043138 Remove/replace some more unnecessary uses of py.path 2021-03-15 10:39:44 +02:00
Ran Benita a03ee02817 config: make `collect_ignore` accept any PathLike
The main reason is to remove a reference to `py.path`.
2021-03-15 10:39:32 +02:00
Ran Benita 59251e8a2a Remove/replace some unneeded usages of py.path 2021-03-14 14:12:28 +02:00
Ronny Pfannschmidt 77cb110258 drop usage of py.path.local calls
Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2021-03-06 21:32:03 +01:00
Ronny Pfannschmidt 22dad53a24 implement Node.path as pathlib.Path
* reorganize lastfailed node sort

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
2021-03-06 21:32:03 +01:00
Ran Benita 6e7dc8bac8
Merge pull request #8357 from matthewhughes934/remove-tmpdir-from-tests
Remove tmpdir from tests
2021-03-04 18:34:19 +02:00
Matthew Hughes 514f8e0680 fixup! Rename variables 'tmpdir'->'tmp_path' 2021-02-24 20:55:35 +00:00
pre-commit-ci[bot] 3b7fc2c9c8 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2021-02-23 17:02:50 +00:00
Florian Bruhin 54a154c86f Allow Class.from_parent to forward custom parameters to the constructor
Similarly to #7143, at work we have a project with a custom pytest.Class
subclass, adding an additional argument to the constructor.

All from_parent implementations in pytest accept and forward *kw, except
Class (before this change) and DoctestItem - since I'm not familiar with
doctest support, I've left the latter as-is.
2021-02-23 18:00:56 +01:00
Matthew Hughes 09d4c5e30a Rename variables 'tmpdir'->'tmp_path'
Rename this variables reflecting the migrations made with 3bcd316f0 and
ed658d682
2021-02-20 18:24:58 +00:00
Ran Benita ca4effc822 Convert most of the collection code from py.path to pathlib 2020-12-22 21:09:36 +02:00
Ran Benita 2c05a7babb config: let main() accept any os.PathLike instead of just py.path.local
Really it ought to only take the List[str], but for backward
compatibility, at least get rid of the explicit py.path.local check.
2020-12-19 14:52:40 +02:00
antonblr 15156e94c4 tests: Migrate to pytester - final update 2020-12-18 11:02:38 -08:00
Ran Benita ed658d6829 Some py.path.local -> pathlib.Path
- Some conftest related functions
- _confcutdir
- Allow arbitrary os.PathLike[str] in gethookproxy.
2020-12-12 17:33:28 +02:00
Sanket Duthade 3bcd316f07
test_collection.py migrate from testdir to Pytester (#8003) 2020-11-07 16:56:00 +02:00
Ran Benita 7fb0ea3f68
Merge pull request #7956 from csernazs/fix-7951
Fix handling recursive symlinks
2020-10-31 18:59:50 +02:00
Cserna Zsolt 8a38e7a6e8 Fix handling recursive symlinks
When pytest was run on a directory containing a recursive symlink it failed
with ELOOP as the library was not able to determine the type of the
direntry:

src/_pytest/main.py:685: in collect
    if not direntry.is_file():
E   OSError: [Errno 40] Too many levels of symbolic links: '/home/florian/proj/pytest/tests/recursive'

This is fixed by handling ELOOP and other errors in the visit function in
pathlib.py, so the entries whose is_file() call raises an OSError with the
pre-defined list of error numbers will be exluded from the result.

The _ignore_errors function was copied from Lib/pathlib.py of cpython 3.9.

Fixes #7951
2020-10-31 17:40:56 +01:00
Ran Benita 6cdae8ed40 pathlib: fix symlinked directories not followed during collection 2020-10-31 14:22:15 +02:00