nix-gitlab-ci/tests/auto_checks_test.nix
Jairo Llopis ab41bc24ec
feat: auto-generate CI jobs from flake checks
Add `checks` option and `config.autoChecks` to auto-generate a CI job
for each flake check derivation. Auto-generated jobs go through
`mkJobPatched` for proper Nix setup, cache, and environment handling.

The `checks` option is automatically wired to `config.checks` in
flake-parts. System is inferred from `pkgs.system` and set via the new
`system` option. User-defined jobs with the same name always take
precedence.

New tests cover both flake-parts and direct API entry points.

MT-14138
2026-05-11 08:02:44 +01:00

65 lines
2.3 KiB
Nix

{
pkgs,
ntlib,
cilib,
...
}: {
suites."autoChecks" = {
pos = __curPos;
tests = [
{
name = "flakeModule";
type = "script";
script =
# sh
''
${ntlib.helpers.scriptHelpers}
${ntlib.helpers.path (with pkgs; [coreutils nix gnused gnugrep jq])}
export SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
export NIX_SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt
repo_path=${../.}
cp ${./fixtures/flake_parts_autochecks}/* .
sed -i -e "s|@repo_path@|$repo_path|" flake.nix
nix build --impure .#gitlab-ci:pipeline:default
assert "-f result" "should exist"
system=$(nix eval --impure --expr "builtins.currentSystem" --raw)
jq -e '."my-check" | .stage == "test"' result > /dev/null
jq -e '."my-check" | .script[0] | contains("nix build")' result > /dev/null
jq -e '."my-check" | .script[0] | contains("'"$system"'")' result > /dev/null
jq -e '."my-check" | has("before_script")' result > /dev/null
jq -e '."my-check" | has("after_script")' result > /dev/null
'';
}
{
name = "direct";
type = "script";
script = let
system = pkgs.system;
fakeCheck = pkgs.runCommand "fake-check" {} "touch $out";
ci = cilib.mkCI {
checks.my-check = fakeCheck;
config.autoChecks.enable = true;
pipelines.default.stages = [ "test" ];
};
pkg = ci.packages."gitlab-ci:pipeline:default";
in
# sh
''
${ntlib.helpers.scriptHelpers}
${ntlib.helpers.path (with pkgs; [coreutils gnugrep jq])}
json=$(cat ${pkg})
echo "$json" | jq -e '.stages == [".pre", "test", ".post"]' > /dev/null
echo "$json" | jq -e '."my-check" | .stage == "test"' > /dev/null
echo "$json" | jq -e '."my-check" | .script[0] | contains("nix build")' > /dev/null
echo "$json" | jq -e '."my-check" | .script[0] | contains("${system}")' > /dev/null
echo "$json" | jq -e '."my-check" | has("before_script")' > /dev/null
echo "$json" | jq -e '."my-check" | has("after_script")' > /dev/null
'';
}
];
};
}