nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix

pytest: new hook to delete pytest bytecode

In https://github.com/NixOS/nixpkgs/issues/139292 we found out that
pytest leaves pytest-generated bytecode in install directory
(as tests are ran after install). That bytecode is:
- non-deterministic (was copied from cpython before cpython got
support for deterministic bytecode)
- unneeded after test run

The change cleans bytecode up and provides a hook variable to avoid it
if needed.

Tested on `python39Packages.pytest-xdist` and `python27Packages.flaky` as:

$ nix build -f. python39Packages.pytest-xdist
$ nix build -f. python39Packages.pytest-xdist --rebuild

$ nix build -f. python27Packages.flaky
$ nix build -f. python27Packages.flaky --rebuild

Closes: https://github.com/NixOS/nixpkgs/issues/139292

authored by

Sergei Trofimovich and committed by
Jonathan Ringer
f0bb568b 062a1496

+39
+13
pkgs/development/python-modules/pytest/4.nix
··· 43 43 } 44 44 45 45 preDistPhases+=" pytestcachePhase" 46 + 47 + # pytest generates it's own bytecode files to improve assertion messages. 48 + # These files similar to cpython's bytecode files but are never laoded 49 + # by python interpreter directly. We remove them for a few reasons: 50 + # - files are non-deterministic: https://github.com/NixOS/nixpkgs/issues/139292 51 + # (file headers are generatedt by pytest directly and contain timestamps) 52 + # - files are not needed after tests are finished 53 + pytestRemoveBytecodePhase () { 54 + # suffix is defined at: 55 + # https://github.com/pytest-dev/pytest/blob/4.6.11/src/_pytest/assertion/rewrite.py#L32-L47 56 + find $out -name "*-PYTEST.py[co]" -delete 57 + } 58 + preDistPhases+=" pytestRemoveBytecodePhase" 46 59 ''; 47 60 48 61 meta = with lib; {
+13
pkgs/development/python-modules/pytest/5.nix
··· 70 70 find $out -name .pytest_cache -type d -exec rm -rf {} + 71 71 } 72 72 preDistPhases+=" pytestcachePhase" 73 + 74 + # pytest generates it's own bytecode files to improve assertion messages. 75 + # These files similar to cpython's bytecode files but are never laoded 76 + # by python interpreter directly. We remove them for a few reasons: 77 + # - files are non-deterministic: https://github.com/NixOS/nixpkgs/issues/139292 78 + # (file headers are generatedt by pytest directly and contain timestamps) 79 + # - files are not needed after tests are finished 80 + pytestRemoveBytecodePhase () { 81 + # suffix is defined at: 82 + # https://github.com/pytest-dev/pytest/blob/5.4.3/src/_pytest/assertion/rewrite.py#L42-L45 83 + find $out -name "*-pytest-*.py[co]" -delete 84 + } 85 + preDistPhases+=" pytestRemoveBytecodePhase" 73 86 ''; 74 87 75 88 pythonImportsCheck = [
+13
pkgs/development/python-modules/pytest/default.nix
··· 82 82 find $out -name .pytest_cache -type d -exec rm -rf {} + 83 83 } 84 84 preDistPhases+=" pytestcachePhase" 85 + 86 + # pytest generates it's own bytecode files to improve assertion messages. 87 + # These files similar to cpython's bytecode files but are never laoded 88 + # by python interpreter directly. We remove them for a few reasons: 89 + # - files are non-deterministic: https://github.com/NixOS/nixpkgs/issues/139292 90 + # (file headers are generatedt by pytest directly and contain timestamps) 91 + # - files are not needed after tests are finished 92 + pytestRemoveBytecodePhase () { 93 + # suffix is defined at: 94 + # https://github.com/pytest-dev/pytest/blob/6.2.5/src/_pytest/assertion/rewrite.py#L51-L53 95 + find $out -name "*-pytest-*.py[co]" -delete 96 + } 97 + preDistPhases+=" pytestRemoveBytecodePhase" 85 98 ''; 86 99 87 100 pythonImportsCheck = [