diff --git a/tests/functional2/testlib/global_assets/config.nix.template b/tests/functional2/testlib/global_assets/config.nix.template new file mode 100644 index 000000000..bb6b331f2 --- /dev/null +++ b/tests/functional2/testlib/global_assets/config.nix.template @@ -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 {}; }; +} diff --git a/tests/functional2/testlib/global_assets/dependencies.builder0.sh b/tests/functional2/testlib/global_assets/dependencies.builder0.sh new file mode 100644 index 000000000..762f2be49 --- /dev/null +++ b/tests/functional2/testlib/global_assets/dependencies.builder0.sh @@ -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 diff --git a/tests/functional2/testlib/global_assets/dependencies.nix b/tests/functional2/testlib/global_assets/dependencies.nix new file mode 100644 index 000000000..6463bcb32 --- /dev/null +++ b/tests/functional2/testlib/global_assets/dependencies.nix @@ -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"; +} diff --git a/tests/functional2/testlib/utils.py b/tests/functional2/testlib/utils.py index 7a8253673..07343c60a 100644 --- a/tests/functional2/testlib/utils.py +++ b/tests/functional2/testlib/utils.py @@ -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)