Load initial conftest when an option value is an existing file path#14696
Open
breidenbach0 wants to merge 1 commit into
Open
Conversation
…ytest-dev#13913) Since the switch to intermixed argument parsing (115b870), the *value* of a command line option that is a path to an existing file (e.g. ``--write-idents idents.txt``) ends up among the positional arguments during pre-parsing. _set_initial_conftests() then treated it as a test path anchor. Because the anchor was a file, the ``test*`` sub-directory discovery was skipped, and since the anchor list was non-empty the invocation-directory fallback was skipped too. As a result a conftest.py that registers the option (typically living under a ``test*`` directory) was not loaded as an initial conftest, so the option was reported as "unrecognized". Resolve file anchors to their containing directory (via _get_directory) so both the conftest loading and the ``test*`` sub-directory discovery consider the right location. Fixes pytest-dev#13913.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #13913.
Since pytest 9 (commit
115b8703, switch to intermixed argument parsing), a conftest.py that registers command line options is sometimes not loaded as an initial conftest, so its options are reported asunrecognized arguments. Reported by SQLAlchemy's lead (@zzzeek) and confirmed by at least one other project.Reproducer (minimal):
Reproduced on current
main.Cause
(as diagnosed by @bluetech in the issue)
With intermixed parsing, the value of an option that is a path to an existing file ends up among the positional args during pre-parsing:
_set_initial_conftests()then treatsidents.txtas a test-path anchor. Because it is a file:anchor.is_dir()is false → thetest*sub-directory glob is skipped, andtest*fallback is skipped too.So the
test*/conftest.pythat registers--write-identsis never discovered.Fix
Resolve a file anchor to its containing directory (
_get_directory) so both the conftest loading and thetest*sub-directory discovery consider the right location. This follows the fix direction @bluetech sketched in the issue.The only behavioral change is that a file argument also triggers
test*discovery relative to its directory — consistent with the existingtest*auto-discovery feature (#14608) and the documented behavior; the full test suite is unaffected.Tests
test_initial_conftest_loaded_when_option_value_is_existing_file(passes with the fix, fails without).test_conftests_in_invocation_dir_tests_is_initial(pytest 9.1.0: conftest.pyaddoptionregressed in nested directories #14608) still passes.testing/test_conftest.py,test_collection.py,test_main.py,test_config.py, and a broad sweep (test_capture,test_terminal,test_session,test_runner,test_debugging,test_assertion,python/collect,python/fixtures) show no new failures versus baseline (identical pass/error counts; the only errors/failures are pre-existing and unrelated — the devminversion/pytest.toml-parse environment issues).News fragment
changelog/13913.bugfix.rstadded.cc @bluetech @zzzeek