biome: 1.9.2 -> 1.9.3
[NixPkgs.git] / pkgs / build-support / trivial-builders / test / references / default.nix
blob85df57ea210c233a2bf43d92f9c2240257c71eb5
1 { lib
2 , stdenvNoCC
3 , testers
4 , callPackage
5 , writeText
6   # nativeBuildInputs
7 , shellcheck-minimal
8   # Samples
9 , samples ? cleanSamples (callPackage ./samples.nix { })
10   # Filter out the non-string-like attributes such as <pkg>.override added by
11   # callPackage.
12 , cleanSamples ? lib.filterAttrs (n: lib.isStringLike)
13   # Test targets
14 , writeDirectReferencesToFile
15 , writeClosure
18 # -------------------------------------------------------------------------- #
20 #                         trivial-builders test
22 # -------------------------------------------------------------------------- #
24 # Execute this build script directly (quick):
26 # * Classic
27 #   $ NIX_PATH="nixpkgs=$PWD" nix-shell -p tests.trivial-builders.references.testScriptBin --run references-test
29 # * Flake-based
30 #   $ nix run .#tests.trivial-builders.references.testScriptBin
32 # or in the build sandbox with a ~20s VM overhead:
34 # * Classic
35 #   $ nix-build --no-out-link -A tests.trivial-builders.references
37 # * Flake-based
38 #   $ nix build -L --no-link .#tests.trivial-builders.references
40 # -------------------------------------------------------------------------- #
42 let
43   # Map each attribute to an element specification of Bash associative arrary
44   # and concatenate them with white spaces, to be used to define a
45   # one-line Bash associative array.
46   samplesToString = attrs:
47     lib.concatMapStringsSep " " (name: "[${name}]=${lib.escapeShellArg "${attrs.${name}}"}") (builtins.attrNames attrs);
49   closures = lib.mapAttrs (n: v: writeClosure [ v ]) samples;
50   directReferences = lib.mapAttrs (n: v: writeDirectReferencesToFile v) samples;
51   collectiveClosure = writeClosure (lib.attrValues samples);
53   testScriptBin = stdenvNoCC.mkDerivation (finalAttrs: {
54     name = "references-test";
56     src = ./references-test.sh;
57     dontUnpack = true;
58     dontBuild = true;
60     installPhase = ''
61       runHook preInstall
62       mkdir -p "$out/bin"
63       substitute "$src" "$out/bin/${finalAttrs.meta.mainProgram}" \
64         --replace "@SAMPLES@" ${lib.escapeShellArg (samplesToString samples)} \
65         --replace "@CLOSURES@" ${lib.escapeShellArg (samplesToString closures)} \
66         --replace "@DIRECT_REFS@" ${lib.escapeShellArg (samplesToString directReferences)} \
67         --replace "@COLLECTIVE_CLOSURE@" ${lib.escapeShellArg collectiveClosure}
68       runHook postInstall
69       chmod +x "$out/bin/${finalAttrs.meta.mainProgram}"
70     '';
72     doInstallCheck = true;
73     nativeInstallCheckInputs = [
74       shellcheck-minimal
75     ];
76     installCheckPhase = ''
77       runHook preInstallCheck
78       shellcheck "$out/bin/${finalAttrs.meta.mainProgram}"
79       runHook postInstallCheck
80     '';
82     passthru = {
83       inherit
84         collectiveClosure
85         directReferences
86         closures
87         samples
88         ;
89     };
91     meta = with lib; {
92       mainProgram = "references-test";
93     };
94   });
96 testers.runNixOSTest ({ config, lib, ... }:
97 let
98   # Use the testScriptBin from guest pkgs.
99   # The attribute path to access the guest version of testScriptBin is
100   # tests.trivial-builders.references.config.node.pkgs.tests.trivial-builders.references.testScriptBin
101   # which is why passthru.guestTestScriptBin is provided.
102   guestTestScriptBin = config.node.pkgs.tests.trivial-builders.references.testScriptBin;
105   name = "nixpkgs-trivial-builders-references";
106   nodes.machine = { config, lib, pkgs, ... }: {
107     virtualisation.writableStore = true;
109     # Test runs without network, so we don't substitute and prepare our deps
110     nix.settings.substituters = lib.mkForce [ ];
111     system.extraDependencies = [ guestTestScriptBin ];
112   };
113   testScript =
114     ''
115       machine.succeed("""
116         ${lib.getExe guestTestScriptBin} 2>/dev/console
117       """)
118     '';
119   passthru = {
120     inherit
121       collectiveClosure
122       directReferences
123       closures
124       samples
125       testScriptBin
126       ;
127     inherit guestTestScriptBin;
128   };
129   meta = {
130     maintainers = with lib.maintainers; [
131       roberth
132       ShamrockLee
133     ];
134   };