nix-gitlab-ci/tests/modules_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

209 lines
6.3 KiB
Nix

{
pkgs,
cilib,
ntlib,
...
}: {
suites."Modules" = {
pos = __curPos;
tests = let
simplePipeline = cilib.mkCI {
pipelines."test" = {
stages = ["test"];
jobs."test" = {
stage = "test";
script = ["echo hello world"];
};
};
};
in [
{
name = "empty pipelines";
expected = {};
actual =
(cilib.mkCI {}).pipelines;
}
{
name = "empty packages";
expected = {};
actual =
(cilib.mkCI {}).packages;
}
{
name = "simple pipeline";
expected = {
stages = [".pre" "test" ".post"];
"test" = {
image = "$NIX_CI_IMAGE";
stage = "test";
before_script = ["source setup_nix_ci \"gitlab-ci:pipeline:test:job-deps:test\""];
script = ["echo hello world"];
after_script = ["finalize_nix_ci"];
};
};
actual = simplePipeline.pipelines."test".finalConfig;
}
{
name = "simple pipeline yaml";
type = "script";
script = let
package = simplePipeline.packages."gitlab-ci:pipeline:test";
in
# sh
''
${ntlib.helpers.path [pkgs.gnugrep]}
${ntlib.helpers.scriptHelpers}
assert_file_contains ${package} 'gitlab-ci:pipeline:test:job-deps:test'
assert_file_contains ${package} 'finalize_nix_ci'
assert_file_contains ${package} 'echo hello world'
'';
}
{
name = "dont fail on store paths";
type = "script";
script = let
package =
(cilib.mkCI {
pipelines."test" = {
variables.EXAMPLE = "${pkgs.hello}";
};
}).packages."gitlab-ci:pipeline:test";
in
# sh
''
${ntlib.helpers.path [pkgs.gnugrep]}
${ntlib.helpers.scriptHelpers}
assert_file_contains ${package} '[".pre",".post"]'
assert_file_contains ${package} '"EXAMPLE":"/nix/store/.*-hello-.*"'
'';
}
{
name = "correctly inject variables containing nix store paths at runtime";
type = "script";
script = let
package =
(cilib.mkCI {
pipelines."test".jobs."test" = {
stage = ".pre";
variables.EXAMPLE = "${pkgs.hello}";
script = [];
};
}).packages."gitlab-ci:pipeline:test:job-deps:test";
in
# sh
''
${ntlib.helpers.path [pkgs.gnugrep]}
${ntlib.helpers.scriptHelpers}
assert_file_contains ${package} 'export PATH=":$PATH";'
assert_file_contains ${package} 'export EXAMPLE="/nix/store/.*-hello-.*"'
'';
}
{
name = "autoChecks with no checks set";
expected = {
stages = [".pre" "test" ".post"];
};
actual =
(cilib.mkCI {
config.autoChecks.enable = true;
pipelines."default".stages = ["test"];
}).pipelines."default".finalConfig;
}
{
name = "autoChecks generates check jobs";
expected = {
stages = [".pre" "check" ".post"];
unit = {
image = "$NIX_CI_IMAGE";
stage = "test";
script = ["nix build .#checks.\"${pkgs.system}\".unit"];
before_script = ["source setup_nix_ci \"gitlab-ci:pipeline:default:job-deps:auto:unit\""];
after_script = ["finalize_nix_ci"];
};
lint = {
image = "$NIX_CI_IMAGE";
stage = "test";
script = ["nix build .#checks.\"${pkgs.system}\".lint"];
before_script = ["source setup_nix_ci \"gitlab-ci:pipeline:default:job-deps:auto:lint\""];
after_script = ["finalize_nix_ci"];
};
};
actual = let
fakeCheck = pkgs.runCommand "fake-check" {} "touch $out";
in
(cilib.mkCI {
config.autoChecks = {
enable = true;
};
checks = {
unit = fakeCheck;
lint = fakeCheck;
};
pipelines."default".stages = ["check"];
}).pipelines."default".finalConfig;
}
{
name = "autoChecks skips existing jobs";
expected = {
stages = [".pre" "check" "build" ".post"];
unit = {
image = "$NIX_CI_IMAGE";
stage = "test";
script = ["nix build .#checks.\"${pkgs.system}\".unit"];
};
custom = {
image = "$NIX_CI_IMAGE";
stage = "build";
script = ["echo custom"];
};
};
actual = let
fakeCheck = pkgs.runCommand "fake-check" {} "touch $out";
in
(cilib.mkCI {
config.autoChecks = {
enable = true;
};
checks = {
unit = fakeCheck;
custom = fakeCheck;
};
pipelines."default" = {
nix.nixJobsByDefault = false;
stages = ["check" "build"];
jobs."custom" = {
stage = "build";
script = ["echo custom"];
};
};
}).pipelines."default".finalConfig;
}
{
name = "autoChecks uses custom stage and tags";
expected = {
stages = [".pre" "quality" ".post"];
lint = {
image = "$NIX_CI_IMAGE";
stage = "quality";
tags = ["docker" "nix"];
script = ["nix build .#checks.\"${pkgs.system}\".lint"];
before_script = ["source setup_nix_ci \"gitlab-ci:pipeline:default:job-deps:auto:lint\""];
after_script = ["finalize_nix_ci"];
};
};
actual = let
fakeCheck = pkgs.runCommand "fake-check" {} "touch $out";
in
(cilib.mkCI {
config.autoChecks = {
enable = true;
stage = "quality";
tags = ["docker" "nix"];
};
checks.lint = fakeCheck;
pipelines."default".stages = ["quality"];
}).pipelines."default".finalConfig;
}
];
};
}