tests/functional2: provide a way to easily access often used assets

Change-Id: I461ee08d9752d972e7485e15186426f98e68ba13
This commit is contained in:
Commentator2.0
2025-08-24 10:38:39 +02:00
committed by Commentator2.0
parent e2641cb890
commit 7b6a85982b
4 changed files with 122 additions and 0 deletions
@@ -0,0 +1,28 @@
let
contentAddressedByDefault = builtins.getEnv "NIX_TESTS_CA_BY_DEFAULT" == "1";
caArgs = if contentAddressedByDefault then {
__contentAddressed = true;
outputHashMode = "recursive";
outputHashAlgo = "sha256";
} else {};
in
rec {
path = "@path@";
system = "@system@";
shared = builtins.getEnv "_NIX_TEST_SHARED";
mkDerivation = args:
derivation ({
inherit system;
builder = "/bin/sh";
args = ["-e" args.builder or (builtins.toFile "builder-${args.name}.sh" ''
if [ -e "$NIX_ATTRS_SH_FILE" ]; then source $NIX_ATTRS_SH_FILE; fi;
eval "$buildCommand"
'')];
PATH = path;
} // caArgs // removeAttrs args ["builder" "meta"])
// { meta = args.meta or {}; };
}
@@ -0,0 +1,17 @@
[ "${input1: -2}" = /. ]
[ "${input2: -2}" = /. ]
echo $PATH
mkdir $out
echo $(cat $input1/foo)$(cat $input2/bar) > $out/foobar
ln -s $input2 $out/reference-to-input-2
# Self-reference.
ln -s $out $out/self
# Executable.
echo program > $out/program
chmod +x $out/program
echo FOO
@@ -0,0 +1,48 @@
{ hashInvalidator ? "" }:
with import ./config.nix;
let
input0 = mkDerivation {
name = "dependencies-input-0";
buildCommand = "mkdir $out; echo foo > $out/bar";
};
input1 = mkDerivation {
name = "dependencies-input-1";
buildCommand = "mkdir $out; echo FOO > $out/foo";
};
input2 = mkDerivation {
name = "dependencies-input-2";
buildCommand = ''
mkdir $out
# Space-filler to test GC stats reporting
head -c 100k /dev/zero > $out/filler
echo BAR > $out/bar
echo ${input0} > $out/input0
'';
};
fod_input = mkDerivation {
name = "fod-input";
buildCommand = ''
echo ${hashInvalidator}
echo FOD > $out
'';
outputHashMode = "flat";
outputHashAlgo = "sha256";
outputHash = "1dq9p0hnm1y75q2x40fws5887bq1r840hzdxak0a9djbwvx0b16d";
};
in
mkDerivation {
name = "dependencies-top";
builder = ./dependencies.builder0.sh + "/FOOBAR/../.";
input1 = input1 + "/.";
input2 = "${input2}/.";
input1_drv = input1;
input2_drv = input2;
input0_drv = input0;
fod_input_drv = fod_input;
meta.description = "Random test package";
}
+29
View File
@@ -1,4 +1,5 @@
import builtins
import os
import types
import typing
from pathlib import Path
@@ -10,6 +11,8 @@ from functional2.testlib.fixtures.file_helper import (
CopyTree,
FileDeclaration,
merge_file_declaration,
Fileish,
CopyTemplate,
)
# Things have to be resolved from top to bottom, because otherwise the tests behave flakey
@@ -131,3 +134,29 @@ def is_value_of_type(value: Any, expected_type: type[Any] | UnionType) -> bool:
case _:
msg = f"Unsupported expected_type. Must be a non-generic or one of {supported_origin_types!r}"
raise ValueError(msg)
def get_global_asset(name: str) -> Fileish:
if name == "config.nix":
return CopyTemplate(
functional2_base_folder / "testlib" / "global_assets" / "config.nix.template",
{
"system": os.environ.get("system"), # noqa: SIM112 # system is actually lowercase here
# Either just the build shell or entire global path if we are darwin
"path": os.environ.get("BUILD_TEST_SHELL") or os.environ.get("PATH"),
},
)
return CopyFile(functional2_base_folder / "testlib" / "global_assets" / name)
def get_global_asset_pack(name: Literal["dependencies"]) -> FileDeclaration:
match name:
case "dependencies":
return {
"config.nix": get_global_asset("config.nix"),
"dependencies.nix": get_global_asset("dependencies.nix"),
"dependencies.builder0.sh": get_global_asset("dependencies.builder0.sh"),
}
case _:
msg = f"invalid pack name {name!r}"
raise ValueError(msg)