nixos/testing: Set up scope for testScript linter

Our test driver exposes a bunch of variables and functions, which
pyflakes doesn't recognise by default because it assumes that the test
script is executed standalone. In reality however the test driver script
is using exec() on the testScript.

Fortunately pyflakes has $PYFLAKES_BUILTINS, which are the attributes
that are globally available on all modules to be checked. Since we only
have one module, using this environment variable is fine as opposed to
my first approach to this, which tried to use the unstable internal API
of pyflakes.

The attributes are gathered by the main derivation of the test driver,
because we don't want to end up defining a new attribute in the test
driver module just to being confused why using it in a test will result
in an error.

Another way we could have gathered these attributes would be in
mkDriver, which is where the linting takes place. However, we do have a
different set of Python dependencies in scope and duplicating these will
again just cause confusion over having it at one location only.

Signed-off-by: aszlig <aszlig@nix.build>
Co-Authored-By: aszlig <aszlig@nix.build>
This commit is contained in:
Robert Hensing 2021-05-08 17:52:22 +02:00 committed by aszlig
parent 71087b2bc4
commit 56d9637119
No known key found for this signature in database
GPG key ID: 684089CE67EBB691

View file

@ -32,6 +32,14 @@ rec {
preferLocalBuild = true;
buildPhase = ''
python <<EOF
from pydoc import importfile
with open('driver-exports', 'w') as fp:
fp.write(','.join(dir(importfile('${testDriverScript}'))))
EOF
'';
doCheck = true;
checkPhase = ''
mypy --disallow-untyped-defs \
@ -50,6 +58,8 @@ rec {
wrapProgram $out/bin/nixos-test-driver \
--prefix PATH : "${lib.makeBinPath [ qemu_pkg vde2 netpbm coreutils ]}" \
install -m 0644 -vD driver-exports $out/nix-support/driver-exports
'';
};
@ -161,7 +171,10 @@ rec {
echo -n "$testScript" > $out/test-script
${lib.optionalString (!skipLint) ''
${python3Packages.pyflakes}/bin/pyflakes $out/test-script
PYFLAKES_BUILTINS="$(
echo -n ${lib.escapeShellArg (lib.concatStringsSep "," nodeHostNames)},
< ${lib.escapeShellArg "${testDriver}/nix-support/driver-exports"}
)" ${python3Packages.pyflakes}/bin/pyflakes $out/test-script
''}
ln -s ${testDriver}/bin/nixos-test-driver $out/bin/
@ -195,6 +208,8 @@ rec {
(node: builtins.match "^[A-z_]([A-z0-9_]+)?$" node == null)
nodeNames;
nodeHostNames = map (c: c.config.system.name) (lib.attrValues driver.nodes);
in
if lib.length invalidNodeNames > 0 then
throw ''