Merge pull request #204462 from ShamrockLee/test-references

tests.trivial-builders.references: refactor and move expressions into trivial-builders/test/references
This commit is contained in:
Robert Hensing 2023-12-09 14:45:20 +01:00 committed by GitHub
commit 6ac78f1174
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 225 additions and 159 deletions

View File

@ -14,21 +14,24 @@
{ callPackage, lib, stdenv }:
let
inherit (lib) recurseIntoAttrs;
references = callPackage ./references {};
in
recurseIntoAttrs {
concat = callPackage ./concat-test.nix {};
linkFarm = callPackage ./link-farm.nix {};
overriding = callPackage ../test-overriding.nix {};
# VM test not supported beyond linux yet
references =
# VM test not supported beyond linux yet
if stdenv.hostPlatform.isLinux
then callPackage ./references.nix {}
else null;
then references
else {};
writeCBin = callPackage ./writeCBin.nix {};
writeShellApplication = callPackage ./writeShellApplication.nix {};
writeScriptBin = callPackage ./writeScriptBin.nix {};
writeShellScript = callPackage ./write-shell-script.nix {};
writeShellScriptBin = callPackage ./writeShellScriptBin.nix {};
writeStringReferencesToFile = callPackage ./writeStringReferencesToFile.nix {};
writeStringReferencesToFile = callPackage ./writeStringReferencesToFile.nix {
inherit (references) samples;
};
writeTextFile = callPackage ./write-text-file.nix {};
}

View File

@ -1,4 +0,0 @@
{ pkgs ? import ../../../.. { config = {}; overlays = []; } }:
pkgs.lib.mapAttrs
(k: v: pkgs.writeDirectReferencesToFile v)
(import ./sample.nix { inherit pkgs; })

View File

@ -1,4 +0,0 @@
{ pkgs ? import ../../../.. { config = {}; overlays = []; } }:
pkgs.lib.mapAttrs
(k: v: pkgs.writeReferencesToFile v)
(import ./sample.nix { inherit pkgs; })

View File

@ -1,62 +0,0 @@
#!/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!'

View File

@ -1,52 +0,0 @@
{ lib, testers, 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
testers.nixosTest {
name = "nixpkgs-trivial-builders";
nodes.machine = { ... }: {
virtualisation.writableStore = true;
# Test runs without network, so we don't substitute and prepare our deps
nix.settings.substituters = 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 = {
maintainers = with lib.maintainers; [
roberth
];
};
}

View File

@ -0,0 +1,124 @@
{ lib
, stdenvNoCC
, testers
, callPackage
, writeText
# nativeBuildInputs
, shellcheck-minimal
# Samples
, samples ? cleanSamples (callPackage ./samples.nix { })
# Filter out the non-string-like attributes such as <pkg>.override added by
# callPackage.
, cleanSamples ? lib.filterAttrs (n: lib.isStringLike)
# Test targets
, writeDirectReferencesToFile
, writeReferencesToFile
}:
# -------------------------------------------------------------------------- #
#
# trivial-builders test
#
# -------------------------------------------------------------------------- #
#
# Execute this build script directly (quick):
#
# * Classic
# $ NIX_PATH="nixpkgs=$PWD" nix-shell -p tests.trivial-builders.references.testScriptBin --run references-test
#
# * Flake-based
# $ nix run .#tests.trivial-builders.references.testScriptBin
#
# or in the build sandbox with a ~20s VM overhead:
#
# * Classic
# $ nix-build --no-out-link -A tests.trivial-builders.references
#
# * Flake-based
# $ nix build -L --no-link .#tests.trivial-builders.references
#
# -------------------------------------------------------------------------- #
let
# Map each attribute to an element specification of Bash associative arrary
# and concatenate them with white spaces, to be used to define a
# one-line Bash associative array.
samplesToString = attrs:
lib.concatMapStringsSep " " (name: "[${name}]=${lib.escapeShellArg "${attrs.${name}}"}") (builtins.attrNames attrs);
references = lib.mapAttrs (n: v: writeReferencesToFile v) samples;
directReferences = lib.mapAttrs (n: v: writeDirectReferencesToFile v) samples;
testScriptBin = stdenvNoCC.mkDerivation (finalAttrs: {
name = "references-test";
src = ./references-test.sh;
dontUnpack = true;
dontBuild = true;
installPhase = ''
runHook preInstall
mkdir -p "$out/bin"
substitute "$src" "$out/bin/${finalAttrs.meta.mainProgram}" \
--replace "@SAMPLES@" ${lib.escapeShellArg (samplesToString samples)} \
--replace "@REFERENCES@" ${lib.escapeShellArg (samplesToString references)} \
--replace "@DIRECT_REFS@" ${lib.escapeShellArg (samplesToString directReferences)}
runHook postInstall
chmod +x "$out/bin/${finalAttrs.meta.mainProgram}"
'';
doInstallCheck = true;
nativeInstallCheckInputs = [
shellcheck-minimal
];
installCheckPhase = ''
runHook preInstallCheck
shellcheck "$out/bin/${finalAttrs.meta.mainProgram}"
runHook postInstallCheck
'';
passthru = {
inherit
directReferences
references
samples
;
};
meta = with lib; {
mainProgram = "references-test";
};
});
in
testers.nixosTest {
name = "nixpkgs-trivial-builders";
nodes.machine = { ... }: {
virtualisation.writableStore = true;
# Test runs without network, so we don't substitute and prepare our deps
nix.settings.substituters = lib.mkForce [ ];
environment.etc."pre-built-paths".source = writeText "pre-built-paths" (
builtins.toJSON [ testScriptBin ]
);
};
testScript =
''
machine.succeed("""
${lib.getExe testScriptBin} 2>/dev/console
""")
'';
passthru = {
inherit
directReferences
references
samples
testScriptBin
;
};
meta = {
maintainers = with lib.maintainers; [
roberth
ShamrockLee
];
};
}

View File

@ -0,0 +1,61 @@
#!/usr/bin/env bash
# -------------------------------------------------------------------------- #
#
# trivial-builders test
#
# -------------------------------------------------------------------------- #
#
# Execute this build script directly (quick):
#
# * Classic
# $ NIX_PATH="nixpkgs=$PWD" nix-shell -p tests.trivial-builders.references.testScriptBin --run references-test
#
# * Flake-based
# $ nix run .#tests.trivial-builders.references.testScriptBin
#
# or in the build sandbox with a ~20s VM overhead:
#
# * Classic
# $ nix-build --no-out-link -A tests.trivial-builders.references
#
# * Flake-based
# $ nix build -L --no-link .#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
# Injected by Nix (to avoid evaluating in a derivation)
# turn them into arrays
# shellcheck disable=SC2206 # deliberately unquoted
declare -A samples=( @SAMPLES@ )
# shellcheck disable=SC2206 # deliberately unquoted
declare -A directRefs=( @DIRECT_REFS@ )
# shellcheck disable=SC2206 # deliberately unquoted
declare -A references=( @REFERENCES@ )
echo >&2 Testing direct references...
for i in "${!samples[@]}"; do
echo >&2 Checking "$i" "${samples[$i]}" "${directRefs[$i]}"
diff -U3 \
<(sort <"${directRefs[$i]}") \
<(nix-store -q --references "${samples[$i]}" | sort)
done
echo >&2 Testing closure...
for i in "${!samples[@]}"; do
echo >&2 Checking "$i" "${samples[$i]}" "${references[$i]}"
diff -U3 \
<(sort <"${references[$i]}") \
<(nix-store -q --requisites "${samples[$i]}" | sort)
done
echo 'OK!'

View File

@ -0,0 +1,30 @@
{ lib
, runCommand
, writeText
, emptyFile
, emptyDirectory
, figlet
, hello
, zlib
}:
{
inherit
figlet
hello
zlib
;
zlib-dev = zlib.dev;
norefs = writeText "hi" "hello";
norefsDup = writeText "hi" "hello";
helloRef = writeText "hi" "hello ${hello}";
helloRefDup = writeText "hi" "hello ${hello}";
path = ./samples.nix;
pathLike.outPath = ./samples.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
emptyFile
emptyDirectory
;
}

View File

@ -1,29 +0,0 @@
{ pkgs ? import ../../../.. { config = { }; overlays = [ ]; } }:
let
inherit (pkgs)
figlet
zlib
hello
writeText
runCommand
;
in
{
hello = hello;
figlet = figlet;
zlib = zlib;
zlib-dev = zlib.dev;
norefs = writeText "hi" "hello";
norefsDup = writeText "hi" "hello";
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
;
}

View File

@ -1,14 +1,13 @@
{ callPackage, lib, pkgs, runCommand, writeText, writeStringReferencesToFile }:
{ callPackage, lib, pkgs, runCommand, samples, writeText, writeStringReferencesToFile }:
let
sample = import ./sample.nix { inherit pkgs; };
samplePaths = lib.unique (lib.attrValues sample);
samplePaths = lib.unique (lib.attrValues samples);
stri = x: "${x}";
sampleText = writeText "sample-text" (lib.concatStringsSep "\n" (lib.unique (map stri samplePaths)));
stringReferencesText =
writeStringReferencesToFile
((lib.concatMapStringsSep "fillertext"
stri
(lib.attrValues sample)) + ''
(lib.attrValues samples)) + ''
STORE=${builtins.storeDir};\nsystemctl start bar-foo.service
'');
in