Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytestCheckHook: proof of concept of a solution to recursive imports in some modules #255530

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkgs/development/interpreters/python/hooks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ in {
inherit (pythonForBuild.pkgs) installer;
};

pytestCheckHook = callPackage ({ makePythonHook, pytest }:
pytestCheckHook = callPackage ({ makePythonHook, pytest, which }:
makePythonHook {
name = "pytest-check-hook";
propagatedBuildInputs = [ pytest ];
propagatedBuildInputs = [ pytest which ];
substitutions = {
inherit pythonCheckInterpreter;
pytestPython = pytest.pythonModule;
};
} ./pytest-check-hook.sh) {};

Expand Down
32 changes: 29 additions & 3 deletions pkgs/development/interpreters/python/hooks/pytest-check-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,38 @@ function _pytestComputeDisabledTestsString () {
echo "$result"
}

function pytestBinaryPatchPhase() {
# This patch of code is necessary because `python -m pytest` is known to
# create circular import errors in libraries that have native extensions
# such as libraries with Cython modules
#
# The problem of calling pytest directly is that not necessarily the pytest
# command will use the @pythonCheckInterpreter@ python so we copy, patch
# and add the pytest binary before all others using PATH prepend
echo "Executing pytestBinaryPatchPhase"
local actualEntrypoint=$(which pytest)
local pytestBinaryDirectory=$(mktemp -d)

PATH="$pytestBinaryDirectory:$PATH"

if (( "${NIX_DEBUG:-0}" >= 1 )); then
echo "pytestBinaryPatchPhase: actualEntrypoint: $actualEntrypoint"
echo "pytestBinaryPatchPhase: originalPythonInterpreterBin: $originalPythonInterpreterBin"
echo "pytestBinaryPatchPhase: pytestBinaryDirectory: $pytestBinaryDirectory"
echo "pytestBinaryPatchPhase: patched PATH: $PATH"
echo "pytestBinaryPatchPhase: which pytest: $(which pytest)"
fi
install -m 755 "$(dirname $actualEntrypoint)"/* $pytestBinaryDirectory
substituteInPlace "$pytestBinaryDirectory"/* \
--replace "@pytestPython@" "$(dirname "$(dirname "@pythonCheckInterpreter@")")"
}

function pytestCheckPhase() {
echo "Executing pytestCheckPhase"
runHook preCheck

# Compose arguments
args=" -m pytest"
args=""
if [ -n "$disabledTests" ]; then
disabledTestsString=$(_pytestComputeDisabledTestsString "${disabledTests[@]}")
args+=" -k \""$disabledTestsString"\""
Expand All @@ -50,15 +76,15 @@ function pytestCheckPhase() {
args+=" --ignore=\"$path\""
done
args+=" ${pytestFlagsArray[@]}"
eval "@pythonCheckInterpreter@ $args"
eval "pytest $args"

runHook postCheck
echo "Finished executing pytestCheckPhase"
}

if [ -z "${dontUsePytestCheck-}" ] && [ -z "${installCheckPhase-}" ]; then
echo "Using pytestCheckPhase"
preDistPhases+=" pytestCheckPhase"
preDistPhases+=" pytestBinaryPatchPhase pytestCheckPhase"

# It's almost always the case that setuptoolsCheckPhase should not be ran
# when the pytestCheckHook is being ran
Expand Down
10 changes: 4 additions & 6 deletions pkgs/development/python-modules/itypes/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
lib,
fetchFromGitHub,
buildPythonPackage,
pytest,
pytestCheckHook,
}:

buildPythonPackage rec {
Expand All @@ -16,11 +16,9 @@ buildPythonPackage rec {
sha256 = "1ljhjp9pacbrv2phs58vppz1dlxix01p98kfhyclvbml6dgjcr52";
};

nativeCheckInputs = [ pytest ];
checkPhase = ''
mv itypes.py itypes.py.hidden
pytest tests.py
'';
pytestFlagsArray = [ "tests.py" ];

nativeCheckInputs = [ pytestCheckHook ];

meta = with lib; {
description = "Simple immutable types for python";
Expand Down