about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--doc/languages-frameworks/python.section.md11
-rw-r--r--pkgs/development/interpreters/python/hooks/default.nix8
-rw-r--r--pkgs/development/interpreters/python/hooks/unittest-check-hook.sh29
-rw-r--r--pkgs/top-level/python-packages.nix1
4 files changed, 49 insertions, 0 deletions
diff --git a/doc/languages-frameworks/python.section.md b/doc/languages-frameworks/python.section.md
index 7fb8ba2e7c27..8de523e89dfd 100644
--- a/doc/languages-frameworks/python.section.md
+++ b/doc/languages-frameworks/python.section.md
@@ -734,6 +734,16 @@ work in any of the formats supported by `buildPythonPackage` currently,
 with the exception of `other` (see `format` in
 [`buildPythonPackage` parameters](#buildpythonpackage-parameters) for more details).
 
+### Using unittestCheckHook {#using-unittestcheckhook}
+
+`unittestCheckHook` is a hook which will substitute the setuptools `test` command for a `checkPhase` which runs `python -m unittest discover`:
+
+```
+  checkInputs = [ unittestCheckHook ];
+
+  unittestFlags = [ "-s" "tests" "-v" ];
+```
+
 ### Develop local package {#develop-local-package}
 
 As a Python developer you're likely aware of [development mode](http://setuptools.readthedocs.io/en/latest/setuptools.html#development-mode)
@@ -1270,6 +1280,7 @@ are used in `buildPythonPackage`.
   with the `pipInstallHook`.
 - `pythonRelaxDepsHook` will relax Python dependencies restrictions for the package.
   See [example usage](#using-pythonrelaxdepshook).
+- `unittestCheckHook` will run tests with `python -m unittest discover`. See [example usage](#using-unittestcheckhook).
 
 ### Development mode {#development-mode}
 
diff --git a/pkgs/development/interpreters/python/hooks/default.nix b/pkgs/development/interpreters/python/hooks/default.nix
index 34c6a72662d7..0f175c90920e 100644
--- a/pkgs/development/interpreters/python/hooks/default.nix
+++ b/pkgs/development/interpreters/python/hooks/default.nix
@@ -164,6 +164,14 @@ in rec {
       };
     } ./setuptools-check-hook.sh) {};
 
+  unittestCheckHook = callPackage ({ }:
+    makeSetupHook {
+      name = "unittest-check-hook";
+      substitutions = {
+        inherit pythonCheckInterpreter;
+      };
+    } ./unittest-check-hook.sh) {};
+
   venvShellHook = disabledIf (!isPy3k) (callPackage ({ }:
     makeSetupHook {
       name = "venv-shell-hook";
diff --git a/pkgs/development/interpreters/python/hooks/unittest-check-hook.sh b/pkgs/development/interpreters/python/hooks/unittest-check-hook.sh
new file mode 100644
index 000000000000..3485fcc79452
--- /dev/null
+++ b/pkgs/development/interpreters/python/hooks/unittest-check-hook.sh
@@ -0,0 +1,29 @@
+# Setup hook for unittest.
+echo "Sourcing unittest-check-hook"
+
+unittestCheckPhase() {
+    echo "Executing unittestCheckPhase"
+    runHook preCheck
+
+    eval "@pythonCheckInterpreter@ -m unittest discover $unittestFlagsArray"
+
+    runHook postCheck
+    echo "Finished executing unittestCheckPhase"
+}
+
+if [ -z "${dontUseUnittestCheck-}" ] && [ -z "${installCheckPhase-}" ]; then
+    echo "Using unittestCheckPhase"
+    preDistPhases+=" unittestCheckPhase"
+
+    # It's almost always the case that setuptoolsCheckPhase should not be ran
+    # when the unittestCheckHook is being ran
+    if [ -z "${useSetuptoolsCheck-}" ]; then
+        dontUseSetuptoolsCheck=1
+
+        # Remove command if already injected into preDistPhases
+        if [[ "$preDistPhases" =~ "setuptoolsCheckPhase" ]]; then
+            echo "Removing setuptoolsCheckPhase"
+            preDistPhases=${preDistPhases/setuptoolsCheckPhase/}
+        fi
+    fi
+fi
diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix
index b3baadb27cdb..1975a0937ccb 100644
--- a/pkgs/top-level/python-packages.nix
+++ b/pkgs/top-level/python-packages.nix
@@ -125,6 +125,7 @@ in {
     pythonRemoveTestsDirHook
     setuptoolsBuildHook
     setuptoolsCheckHook
+    unittestCheckHook
     venvShellHook
     wheelUnpackHook;