Merge pull request #144052 from hercules-ci/improve-trivial-builders-test

test.trivial-builders: Add test cases, fix test runner, rename

Changed files
+144 -128
pkgs
-69
pkgs/build-support/trivial-builders/test.nix
···
-
{ lib, nixosTest, pkgs, writeText, hello, figlet, stdenvNoCC }:
-
-
# -------------------------------------------------------------------------- #
-
#
-
# trivial-builders test
-
#
-
# -------------------------------------------------------------------------- #
-
#
-
# This file can be run independently (quick):
-
#
-
# $ pkgs/build-support/trivial-builders/test.sh
-
#
-
# or in the build sandbox with a ~20s VM overhead
-
#
-
# $ nix-build -A tests.trivial-builders
-
#
-
# -------------------------------------------------------------------------- #
-
-
let
-
invokeSamples = file:
-
lib.concatStringsSep " " (
-
lib.attrValues (import file { inherit pkgs; })
-
);
-
in
-
nixosTest {
-
name = "nixpkgs-trivial-builders";
-
nodes.machine = { ... }: {
-
virtualisation.writableStore = true;
-
-
# Test runs without network, so we don't substitute and prepare our deps
-
nix.binaryCaches = lib.mkForce [];
-
environment.etc."pre-built-paths".source = writeText "pre-built-paths" (
-
builtins.toJSON [hello figlet stdenvNoCC]
-
);
-
environment.variables = {
-
SAMPLE = invokeSamples ./test/sample.nix;
-
REFERENCES = invokeSamples ./test/invoke-writeReferencesToFile.nix;
-
DIRECT_REFS = invokeSamples ./test/invoke-writeDirectReferencesToFile.nix;
-
};
-
};
-
testScript =
-
let
-
sample = import ./test/sample.nix { inherit pkgs; };
-
samplePaths = lib.unique (lib.attrValues sample);
-
sampleText = pkgs.writeText "sample-text" (lib.concatStringsSep "\n" samplePaths);
-
stringReferencesText =
-
pkgs.writeStringReferencesToFile
-
((lib.concatMapStringsSep "fillertext"
-
(d: "${d}")
-
(lib.attrValues sample)) + ''
-
STORE=${builtins.storeDir};\nsystemctl start bar-foo.service
-
'');
-
in ''
-
machine.succeed("""
-
${./test.sh} 2>/dev/console
-
""")
-
machine.succeed("""
-
echo >&2 Testing string references...
-
diff -U3 <(sort ${stringReferencesText}) <(sort ${sampleText})
-
""")
-
'';
-
meta = {
-
license = lib.licenses.mit; # nixpkgs license
-
maintainers = with lib.maintainers; [
-
roberth
-
];
-
description = "Run the Nixpkgs trivial builders tests";
-
};
-
}
-56
pkgs/build-support/trivial-builders/test.sh
···
-
#!/usr/bin/env bash
-
-
# -------------------------------------------------------------------------- #
-
#
-
# trivial-builders test
-
#
-
# -------------------------------------------------------------------------- #
-
#
-
# This file can be run independently (quick):
-
#
-
# $ pkgs/build-support/trivial-builders/test.sh
-
#
-
# or in the build sandbox with a ~20s VM overhead
-
#
-
# $ nix-build -A tests.trivial-builders
-
#
-
# -------------------------------------------------------------------------- #
-
-
# strict bash
-
set -euo pipefail
-
-
# debug
-
# set -x
-
# PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
-
-
cd "$(dirname ${BASH_SOURCE[0]})" # nixpkgs root
-
-
if [[ -z ${SAMPLE:-} ]]; then
-
sample=( `nix-build test/sample.nix` )
-
directRefs=( `nix-build test/invoke-writeDirectReferencesToFile.nix` )
-
references=( `nix-build test/invoke-writeReferencesToFile.nix` )
-
else
-
# Injected by Nix (to avoid evaluating in a derivation)
-
# turn them into arrays
-
sample=($SAMPLE)
-
directRefs=($DIRECT_REFS)
-
references=($REFERENCES)
-
fi
-
-
echo >&2 Testing direct references...
-
for i in "${!sample[@]}"; do
-
echo >&2 Checking '#'$i ${sample[$i]} ${directRefs[$i]}
-
diff -U3 \
-
<(sort <${directRefs[$i]}) \
-
<(nix-store -q --references ${sample[$i]} | sort)
-
done
-
-
echo >&2 Testing closure...
-
for i in "${!sample[@]}"; do
-
echo >&2 Checking '#'$i ${sample[$i]} ${references[$i]}
-
diff -U3 \
-
<(sort <${references[$i]}) \
-
<(nix-store -q --requisites ${sample[$i]} | sort)
-
done
-
-
echo 'OK!'
+62
pkgs/build-support/trivial-builders/test/references-test.sh
···
+
#!/usr/bin/env bash
+
+
# -------------------------------------------------------------------------- #
+
#
+
# trivial-builders test
+
#
+
# -------------------------------------------------------------------------- #
+
#
+
# This file can be run independently (quick):
+
#
+
# $ pkgs/build-support/trivial-builders/references-test.sh
+
#
+
# or in the build sandbox with a ~20s VM overhead
+
#
+
# $ nix-build -A tests.trivial-builders.references
+
#
+
# -------------------------------------------------------------------------- #
+
+
# strict bash
+
set -euo pipefail
+
+
# debug
+
# set -x
+
# PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
+
+
cd "$(dirname ${BASH_SOURCE[0]})" # nixpkgs root
+
+
if [[ -z ${SAMPLE:-} ]]; then
+
echo "Running the script directly is currently not supported."
+
echo "If you need to iterate, remove the raw path, which is not returned by nix-build."
+
exit 1
+
# sample=( `nix-build --no-out-link sample.nix` )
+
# directRefs=( `nix-build --no-out-link invoke-writeDirectReferencesToFile.nix` )
+
# references=( `nix-build --no-out-link invoke-writeReferencesToFile.nix` )
+
# echo "sample: ${#sample[@]}"
+
# echo "direct: ${#directRefs[@]}"
+
# echo "indirect: ${#references[@]}"
+
else
+
# Injected by Nix (to avoid evaluating in a derivation)
+
# turn them into arrays
+
sample=($SAMPLE)
+
directRefs=($DIRECT_REFS)
+
references=($REFERENCES)
+
fi
+
+
echo >&2 Testing direct references...
+
for i in "${!sample[@]}"; do
+
echo >&2 Checking '#'$i ${sample[$i]} ${directRefs[$i]}
+
diff -U3 \
+
<(sort <${directRefs[$i]}) \
+
<(nix-store -q --references ${sample[$i]} | sort)
+
done
+
+
echo >&2 Testing closure...
+
for i in "${!sample[@]}"; do
+
echo >&2 Checking '#'$i ${sample[$i]} ${references[$i]}
+
diff -U3 \
+
<(sort <${references[$i]}) \
+
<(nix-store -q --requisites ${sample[$i]} | sort)
+
done
+
+
echo 'OK!'
+54
pkgs/build-support/trivial-builders/test/references.nix
···
+
{ lib, nixosTest, pkgs, writeText, hello, figlet, stdenvNoCC }:
+
+
# -------------------------------------------------------------------------- #
+
#
+
# trivial-builders test
+
#
+
# -------------------------------------------------------------------------- #
+
#
+
# This file can be run independently (quick):
+
#
+
# $ pkgs/build-support/trivial-builders/references-test.sh
+
#
+
# or in the build sandbox with a ~20s VM overhead
+
#
+
# $ nix-build -A tests.trivial-builders.references
+
#
+
# -------------------------------------------------------------------------- #
+
+
let
+
invokeSamples = file:
+
lib.concatStringsSep " " (
+
lib.attrValues (import file { inherit pkgs; })
+
);
+
in
+
nixosTest {
+
name = "nixpkgs-trivial-builders";
+
nodes.machine = { ... }: {
+
virtualisation.writableStore = true;
+
+
# Test runs without network, so we don't substitute and prepare our deps
+
nix.binaryCaches = lib.mkForce [];
+
environment.etc."pre-built-paths".source = writeText "pre-built-paths" (
+
builtins.toJSON [hello figlet stdenvNoCC]
+
);
+
environment.variables = {
+
SAMPLE = invokeSamples ./sample.nix;
+
REFERENCES = invokeSamples ./invoke-writeReferencesToFile.nix;
+
DIRECT_REFS = invokeSamples ./invoke-writeDirectReferencesToFile.nix;
+
};
+
};
+
testScript =
+
''
+
machine.succeed("""
+
${./references-test.sh} 2>/dev/console
+
""")
+
'';
+
meta = {
+
license = lib.licenses.mit; # nixpkgs license
+
maintainers = with lib.maintainers; [
+
roberth
+
];
+
description = "Run the Nixpkgs trivial builders tests";
+
};
+
}
+5 -1
pkgs/build-support/trivial-builders/test/sample.nix
···
-
{ pkgs ? import ../../../.. { config = {}; overlays = []; } }:
+
{ pkgs ? import ../../../.. { config = { }; overlays = [ ]; } }:
let
inherit (pkgs)
figlet
zlib
hello
writeText
+
runCommand
;
in
{
···
helloRef = writeText "hi" "hello ${hello}";
helloRefDup = writeText "hi" "hello ${hello}";
path = ./invoke-writeReferencesToFile.nix;
+
pathLike.outPath = ./invoke-writeReferencesToFile.nix;
helloFigletRef = writeText "hi" "hello ${hello} ${figlet}";
+
selfRef = runCommand "self-ref-1" {} "echo $out >$out";
+
selfRef2 = runCommand "self-ref-2" {} ''echo "${figlet}, $out" >$out'';
inherit (pkgs)
emptyFile
emptyDirectory
+18
pkgs/build-support/trivial-builders/test/writeStringReferencesToFile.nix
···
+
{ callPackage, lib, pkgs, runCommand, writeText, writeStringReferencesToFile }:
+
let
+
sample = import ./sample.nix { inherit pkgs; };
+
samplePaths = lib.unique (lib.attrValues sample);
+
stri = x: "${x}";
+
sampleText = writeText "sample-text" (lib.concatStringsSep "\n" (lib.unique (map stri samplePaths)));
+
stringReferencesText =
+
writeStringReferencesToFile
+
((lib.concatMapStringsSep "fillertext"
+
stri
+
(lib.attrValues sample)) + ''
+
STORE=${builtins.storeDir};\nsystemctl start bar-foo.service
+
'');
+
in
+
runCommand "test-writeStringReferencesToFile" { } ''
+
diff -U3 <(sort ${stringReferencesText}) <(sort ${sampleText})
+
touch $out
+
''
+5 -2
pkgs/test/default.nix
···
cuda = callPackage ./cuda { };
-
trivial = callPackage ../build-support/trivial-builders/test.nix {};
-
trivial-overriding = callPackage ../build-support/trivial-builders/test-overriding.nix {};
+
trivial-builders = recurseIntoAttrs {
+
writeStringReferencesToFile = callPackage ../build-support/trivial-builders/test/writeStringReferencesToFile.nix {};
+
references = callPackage ../build-support/trivial-builders/test/references.nix {};
+
overriding = callPackage ../build-support/trivial-builders/test-overriding.nix {};
+
};
writers = callPackage ../build-support/writers/test.nix {};
}