Skip to content

pytestCheckHook: support inclusion and exclusion of path globs, test items, keywords, and markers#386513

Merged
ShamrockLee merged 8 commits intoNixOS:stagingfrom
ShamrockLee:build-python-include-tests
Mar 23, 2025
Merged

pytestCheckHook: support inclusion and exclusion of path globs, test items, keywords, and markers#386513
ShamrockLee merged 8 commits intoNixOS:stagingfrom
ShamrockLee:build-python-include-tests

Conversation

@ShamrockLee
Copy link
Contributor

@ShamrockLee ShamrockLee commented Mar 3, 2025

If applied, pytestCheckHook will accept

  • enabledTestPaths and disabledTestPaths to include/exclude either path globs or test items (<file path>::<specifier>)
  • enabledTests and disabledTests to include/exclude keywords
  • enabledTestMarks and disabledTestMarks to include/exclude markers

The inclusion corresponds to the positional arguments of pytest, as described by the PyTest documentation.

The path globs inside enabledTestPaths is expanded using an in-line Python script powered by the glob module from the Python Standard Library. enabledTestPaths is crucial for deprecating pytestFlagsArray, or we'll have to specify globbed paths inside preCheck. Other attributes also enhance the structural selection of the test cases.

This enhancement is essential to the tree-wide transition to deprecate the non __structuredAttrs-compatible pytestFlagsArray flags. See the previous discussion: #347194 (comment)

The keyword and markers expressions shares the same form ((inc1) or (inc2)) and not (exc1) and not (exc2), and are passed to -k and -m respectively.

I have considered renaming them to includeTests and excludeTests, but I'm unsure if tree-wide renaming from disabledTests to excludeTests would be worthwhile.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added the 6.topic: python Python is a high-level, general-purpose programming language. label Mar 3, 2025
@ofborg ofborg bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Mar 3, 2025
@ShamrockLee ShamrockLee force-pushed the build-python-include-tests branch from 4501097 to c5c9dab Compare March 7, 2025 08:58
@ofborg ofborg bot removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Mar 7, 2025
@github-actions github-actions bot added 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. labels Mar 7, 2025
@ShamrockLee ShamrockLee force-pushed the build-python-include-tests branch 2 times, most recently from d9e5f0f to 80b35f2 Compare March 7, 2025 10:48
…edTestPaths

Pass disabledTestPaths elements containing double colons (::)
to --deselect instead of --ignore-glob.
@ShamrockLee ShamrockLee force-pushed the build-python-include-tests branch from 80b35f2 to a238b9d Compare March 9, 2025 14:20
@ShamrockLee
Copy link
Contributor Author

Do you think we should rename from disabledTests to excludeTests and disabledTestPaths to excludeTestPaths?

This way, we can call the whole set of arguments:

  • includeTestPaths, excludeTestPaths
  • includeTests, excludeTests
  • includeTestMarks, excludeTestMarks

Ping @wolfgangwalther

@ShamrockLee ShamrockLee force-pushed the build-python-include-tests branch from a238b9d to 8c64725 Compare March 9, 2025 16:07
@github-actions github-actions bot added the 8.has: documentation This PR adds or changes documentation label Mar 9, 2025
@ShamrockLee ShamrockLee force-pushed the build-python-include-tests branch from 8c64725 to 8c0090b Compare March 9, 2025 16:08
@ShamrockLee ShamrockLee marked this pull request as ready for review March 9, 2025 16:12
Copy link
Member

@mweinelt mweinelt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool.

@mweinelt mweinelt requested a review from fabaff March 9, 2025 16:35
@ShamrockLee
Copy link
Contributor Author

I improved the example based on pytest's default test discovery documented in its reference manual.

See the discussion comment for detail: #386513 (comment)

Copy link
Contributor

@MattSturgeon MattSturgeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor suggestions, nothing blocking

@wegank wegank added the 12.approvals: 1 This PR was reviewed and approved by one person. label Mar 16, 2025
@ShamrockLee ShamrockLee force-pushed the build-python-include-tests branch from 75b99b9 to 9417ba1 Compare March 23, 2025 09:43
@ShamrockLee
Copy link
Contributor Author

@MattSturgeon, I addressed the suggestions and adopted the definition-list syntax.

I also add pytestFlags attribute description to the list, and merge its pytestFlags example into the main example. I moved the __structuredAttrs part to a new paragraph below the main example.

@ShamrockLee ShamrockLee force-pushed the build-python-include-tests branch from 9417ba1 to 05a9d04 Compare March 23, 2025 15:09
…ssions

Wrap each elements of disabledTests with parenthesis
so that when __structuredAttrs = true,
people could use sub-expressions an element.

E.g.
```nix
{
  disabledTests = [
    "ClassFoo and test_foo"
    "test_bar"
  ];
}
@ShamrockLee ShamrockLee force-pushed the build-python-include-tests branch from 05a9d04 to 8ba0944 Compare March 23, 2025 15:10
Copy link
Contributor

@wolfgangwalther wolfgangwalther left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not do any builds or tests, but code LGTM.

(short of the current CI failure for nixfmt)

@ShamrockLee ShamrockLee force-pushed the build-python-include-tests branch from 8ba0944 to bb76d74 Compare March 23, 2025 15:51
@ShamrockLee
Copy link
Contributor Author

ShamrockLee commented Mar 23, 2025

BTW, the current enable* not-an-empty-list assertions only guard the buildPythonPackage (and overridePythonAttrs) arguments but not the overridden attributes by overrideAttrs, since the latter is tricky to implement.

@ShamrockLee
Copy link
Contributor Author

Did not do any builds or tests, but code LGTM.

Let's see if python3Packages.pytestCheckHook.tests and python3Packages.xyzservices build.

@ofborg build nixpkgs-manual

@ShamrockLee
Copy link
Contributor Author

(short of the current CI failure for nixfmt)

Addressed.

@ShamrockLee
Copy link
Contributor Author

The OfBorg CI test building xyzservices on aarch64-linux fails because an inline test of perl5 doesn't pass. This is unrelated to this PR, but might hint that it's a bit unrealistic to expect python3Packages.xyzservices to build smoothly on the staging branch.

The tests should be lighter:
@ofborg build python3Packages.pytestCheckHook.tests

@wegank wegank removed the 12.approvals: 1 This PR was reviewed and approved by one person. label Mar 23, 2025
@MattSturgeon MattSturgeon added the 12.approvals: 2 This PR was reviewed and approved by two persons. label Mar 23, 2025
@ShamrockLee
Copy link
Contributor Author

I cherry-picked the documentation changes onto nixos-unstable-small and the Nixpkgs Manual builds fine.

OfBorg's failures to build nixpkgs-manual on aarch64-linux seem to caused by a Perl plugin failing to pass three of its tests. That's not related to the issue.

Partial log:

error: builder for '/nix/store/bpl8lqqw67kpqd7jcqy0km6fn3rs67i9-perl5.40.0-XML-Parser-2.46.drv' failed with exit code 2;
       last 25 log lines:
       > PERL_DL_NONLAZY=1 "/nix/store/ds9x5fnzhspvcndr32i9qf5d95a3xd57-perl-5.40.0/bin/perl" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
       > t/astress.t ........... ok
       > t/cdata.t ............. ok
       > t/decl.t .............. ok
       > t/defaulted.t ......... ok
       > t/encoding.t .......... ok
       > t/external_ent.t ...... ok
       > t/file.t .............. ok
       > t/file_open_scalar.t .. ok
       > t/finish.t ............ ok
       > t/namespaces.t ........ ok
       > t/parament.t .......... ok
       > t/partial.t ........... Failed 1/3 subtests
       > t/skip.t .............. ok
       > t/stream.t ............ ok
       > t/styles.t ............ ok
       >
       > Test Summary Report
       > -------------------
       > t/partial.t         (Wstat: 0 Tests: 3 Failed: 1)
       >   Failed test:  3
       > Files=15, Tests=140,  2 wallclock secs ( 0.06 usr  0.03 sys +  0.73 cusr  0.49 csys =  1.31 CPU)
       > Result: FAIL
       > Failed 1/15 test programs. 1/140 subtests failed.
       > make: *** [Makefile:981: test_dynamic] Error 255

That is unrelated to this issue.

Given that python3Packages.pytestCheckHook.tests builds on aarch64-linux, and that the functionality is platform-agnostic, I'm going to merge this PR.

@ShamrockLee ShamrockLee merged commit d3a452a into NixOS:staging Mar 23, 2025
31 of 36 checks passed
else
# The `|| kill "$$"` trick propagates the errors from the process substitutiton subshell,
# which is suggested by a StackOverflow answer: https://unix.stackexchange.com/a/217643
readarray -t -O"${#flagsArray[@]}" flagsArray < <(@pythonCheckInterpreter@ - "$path" <<EOF || kill "$$")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid this doesn't work as expected,

In a random hydra log, I found this error:

/nix/store/l3797z8ybpgk14ara7z0ld4hsnrpnvyv-pytest-check-hook/nix-support/setup-hook: line 41: warning: command substitution: 1 unterminated here-document

This makes sense - the here doc starts in the subshell, but ends outside.

What I am confused about even more is the fact that this doesn't break the build at this stage, yet.

Copy link
Contributor Author

@ShamrockLee ShamrockLee May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid this doesn't work as expected,

In a random hydra log, I found this error:

/nix/store/l3797z8ybpgk14ara7z0ld4hsnrpnvyv-pytest-check-hook/nix-support/setup-hook: line 41: warning: command substitution: 1 unterminated here-document

This makes sense - the here doc starts in the subshell, but ends outside.

I'll fix it this weekend.

What I am confused about even more is the fact that this doesn't break the build at this stage, yet.

I guess it's because the syntax error require a missing EOF for the heredoc, which is only valid when taking account the whole line. Since it the doesn't happen before the || operator, it isn't caught by the kill command.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's because the syntax error require a missing EOF for the heredoc, which is only valid when taking account the whole line. Since it the doesn't happen before the || operator, it isn't caught by the kill command.

Ah, ok! So it's a demonstration of why we need the kill:) (for other, non-syntax errors, that is)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wolfgangwalther Here is the fix: #403973.

@ShamrockLee
Copy link
Contributor Author

ShamrockLee commented Jul 14, 2025

I proposed in #425191 to add a default value enabledTestPaths = [ "." ] to simplify overriding (making it always specified as a non-empty list). What do you think about the change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: python Python is a high-level, general-purpose programming language. 8.has: documentation This PR adds or changes documentation 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 12.approvals: 2 This PR was reviewed and approved by two persons.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants