1# Tests for the Python interpreters, package sets and environments.
2#
3# Each Python interpreter has a `passthru.tests` which is the attribute set
4# returned by this function. For example, for Python 3 the tests are run with
5#
6# $ nix-build -A python3.tests
7#
8{ stdenv
9, python
10, runCommand
11, lib
12, callPackage
13, pkgs
14}:
15
16let
17 # Test whether the interpreter behaves in the different types of environments
18 # we aim to support.
19 environmentTests = let
20 envs = let
21 inherit python;
22 pythonEnv = python.withPackages(ps: with ps; [ ]);
23 pythonVirtualEnv = if python.isPy3k
24 then
25 python.withPackages(ps: with ps; [ virtualenv ])
26 else
27 python.buildEnv.override {
28 extraLibs = with python.pkgs; [ virtualenv ];
29 # Collisions because of namespaces __init__.py
30 ignoreCollisions = true;
31 };
32 in {
33 # Plain Python interpreter
34 plain = rec {
35 env = python;
36 interpreter = env.interpreter;
37 is_venv = "False";
38 is_nixenv = "False";
39 is_virtualenv = "False";
40 };
41 } // lib.optionalAttrs (!python.isPyPy && !stdenv.isDarwin) {
42 # Use virtualenv from a Nix env.
43 # Fails on darwin with
44 # virtualenv: error: argument dest: the destination . is not write-able at /nix/store
45 nixenv-virtualenv = rec {
46 env = runCommand "${python.name}-virtualenv" {} ''
47 ${pythonVirtualEnv.interpreter} -m virtualenv venv
48 mv venv $out
49 '';
50 interpreter = "${env}/bin/${python.executable}";
51 is_venv = "False";
52 is_nixenv = "True";
53 is_virtualenv = "True";
54 };
55 } // lib.optionalAttrs (python.implementation != "graal") {
56 # Python Nix environment (python.buildEnv)
57 nixenv = rec {
58 env = pythonEnv;
59 interpreter = env.interpreter;
60 is_venv = "False";
61 is_nixenv = "True";
62 is_virtualenv = "False";
63 };
64 } // lib.optionalAttrs (python.isPy3k && (!python.isPyPy)) {
65 # Venv built using plain Python
66 # Python 2 does not support venv
67 # TODO: PyPy executable name is incorrect, it should be pypy-c or pypy-3c instead of pypy and pypy3.
68 plain-venv = rec {
69 env = runCommand "${python.name}-venv" {} ''
70 ${python.interpreter} -m venv $out
71 '';
72 interpreter = "${env}/bin/${python.executable}";
73 is_venv = "True";
74 is_nixenv = "False";
75 is_virtualenv = "False";
76 };
77
78 } // lib.optionalAttrs (python.pythonAtLeast "3.8") {
79 # Venv built using Python Nix environment (python.buildEnv)
80 # TODO: Cannot create venv from a nix env
81 # Error: Command '['/nix/store/ddc8nqx73pda86ibvhzdmvdsqmwnbjf7-python3-3.7.6-venv/bin/python3.7', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.
82 nixenv-venv = rec {
83 env = runCommand "${python.name}-venv" {} ''
84 ${pythonEnv.interpreter} -m venv $out
85 '';
86 interpreter = "${env}/bin/${pythonEnv.executable}";
87 is_venv = "True";
88 is_nixenv = "True";
89 is_virtualenv = "False";
90 };
91 };
92
93 testfun = name: attrs: runCommand "${python.name}-tests-${name}" ({
94 inherit (python) pythonVersion;
95 } // attrs) ''
96 cp -r ${./tests/test_environments} tests
97 chmod -R +w tests
98 substituteAllInPlace tests/test_python.py
99 ${attrs.interpreter} -m unittest discover --verbose tests #/test_python.py
100 mkdir $out
101 touch $out/success
102 '';
103
104 in lib.mapAttrs testfun envs;
105
106 # Integration tests involving the package set.
107 # All PyPy package builds are broken at the moment
108 integrationTests = lib.optionalAttrs (!python.isPyPy) (
109 lib.optionalAttrs (python.isPy3k && !stdenv.isDarwin) { # darwin has no split-debug
110 cpython-gdb = callPackage ./tests/test_cpython_gdb {
111 interpreter = python;
112 };
113 } // lib.optionalAttrs (python.pythonAtLeast "3.7") {
114 # Before the addition of NIX_PYTHONPREFIX mypy was broken with typed packages
115 nix-pythonprefix-mypy = callPackage ./tests/test_nix_pythonprefix {
116 interpreter = python;
117 };
118 # Make sure tkinter is importable. See https://github.com/NixOS/nixpkgs/issues/238990
119 tkinter = callPackage ./tests/test_tkinter {
120 interpreter = python;
121 };
122 }
123 );
124
125 # Tests to ensure overriding works as expected.
126 overrideTests = let
127 extension = self: super: {
128 foobar = super.numpy;
129 };
130 # `pythonInterpreters.pypy39_prebuilt` does not expose an attribute
131 # name (is not present in top-level `pkgs`).
132 is_prebuilt = python: python.pythonAttr == null;
133 in lib.optionalAttrs (python.isPy3k) ({
134 test-packageOverrides = let
135 myPython = let
136 self = python.override {
137 packageOverrides = extension;
138 inherit self;
139 };
140 in self;
141 in assert myPython.pkgs.foobar == myPython.pkgs.numpy; myPython.withPackages(ps: with ps; [ foobar ]);
142 # overrideScope is broken currently
143 # test-overrideScope = let
144 # myPackages = python.pkgs.overrideScope extension;
145 # in assert myPackages.foobar == myPackages.numpy; myPackages.python.withPackages(ps: with ps; [ foobar ]);
146 #
147 # Have to skip prebuilt python as it's not present in top-level
148 # `pkgs` as an attribute.
149 } // lib.optionalAttrs (python ? pythonAttr && !is_prebuilt python) {
150 # Test applying overrides using pythonPackagesOverlays.
151 test-pythonPackagesExtensions = let
152 pkgs_ = pkgs.extend(final: prev: {
153 pythonPackagesExtensions = prev.pythonPackagesExtensions ++ [
154 (python-final: python-prev: {
155 foo = python-prev.setuptools;
156 })
157 ];
158 });
159 in pkgs_.${python.pythonAttr}.pkgs.foo;
160 });
161
162 condaTests = let
163 requests = callPackage ({
164 autoPatchelfHook,
165 fetchurl,
166 pythonCondaPackages,
167 }:
168 python.pkgs.buildPythonPackage {
169 pname = "requests";
170 version = "2.24.0";
171 format = "other";
172 src = fetchurl {
173 url = "https://repo.anaconda.com/pkgs/main/noarch/requests-2.24.0-py_0.tar.bz2";
174 sha256 = "02qzaf6gwsqbcs69pix1fnjxzgnngwzvrsy65h1d521g750mjvvp";
175 };
176 nativeBuildInputs = [ autoPatchelfHook ] ++ (with python.pkgs; [
177 condaUnpackHook condaInstallHook
178 ]);
179 buildInputs = [
180 pythonCondaPackages.condaPatchelfLibs
181 ];
182 propagatedBuildInputs = with python.pkgs; [
183 chardet idna urllib3 certifi
184 ];
185 }
186 ) {};
187 pythonWithRequests = requests.pythonModule.withPackages (ps: [ requests ]);
188 in lib.optionalAttrs (python.isPy3k && stdenv.isLinux)
189 {
190 condaExamplePackage = runCommand "import-requests" {} ''
191 ${pythonWithRequests.interpreter} -c "import requests" > $out
192 '';
193 };
194
195in lib.optionalAttrs (stdenv.hostPlatform == stdenv.buildPlatform ) (environmentTests // integrationTests // overrideTests // condaTests)