From e074d716c43189d440c9a6ea3a39e7f7bc77cbe6 Mon Sep 17 00:00:00 2001 From: technofab Date: Mon, 1 Sep 2025 15:46:07 +0200 Subject: [PATCH] test: add basic test for flakeModule functionality --- flake.nix | 4 +- nix/repo/flake.lock | 18 +++++++++ nix/repo/flake.nix | 2 + nix/repo/tests.nix | 10 +++++ .../fixtures/flake_parts}/flake.lock | 0 .../fixtures/flake_parts}/flake.nix | 6 +-- tests/flake_parts_test.nix | 39 +++++++++++++++++++ 7 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 nix/repo/tests.nix rename {flakeModuleTest => tests/fixtures/flake_parts}/flake.lock (100%) rename {flakeModuleTest => tests/fixtures/flake_parts}/flake.nix (92%) create mode 100644 tests/flake_parts_test.nix diff --git a/flake.nix b/flake.nix index ea47bb8..b507b22 100644 --- a/flake.nix +++ b/flake.nix @@ -21,14 +21,14 @@ cellBlocks = with ren.blocks; [ (simple "devShells") (simple "pkgs") - # (simple "tests") + (simple "tests") # (simple "docs") (simple "ci") ]; } { packages = ren.select self [ - # ["repo" "tests"] + ["repo" "tests"] # ["repo" "docs"] ["repo" "ci" "packages"] ["packages" "pkgs"] diff --git a/nix/repo/flake.lock b/nix/repo/flake.lock index 6b16df1..1b7c64c 100644 --- a/nix/repo/flake.lock +++ b/nix/repo/flake.lock @@ -17,9 +17,27 @@ "type": "gitlab" } }, + "nixtest-lib": { + "locked": { + "dir": "lib", + "lastModified": 1753957623, + "narHash": "sha256-kdImwKx57N0QL8HPUUb5ADwXFgSjaNOk39b/eKlzyTo=", + "owner": "TECHNOFAB", + "repo": "nixtest", + "rev": "22b43c9fe83be73c3f0648bbb54bc3c1cf7f96df", + "type": "gitlab" + }, + "original": { + "dir": "lib", + "owner": "TECHNOFAB", + "repo": "nixtest", + "type": "gitlab" + } + }, "root": { "inputs": { "devshell-lib": "devshell-lib", + "nixtest-lib": "nixtest-lib", "treefmt-nix": "treefmt-nix" } }, diff --git a/nix/repo/flake.nix b/nix/repo/flake.nix index 7d5e657..42077d0 100644 --- a/nix/repo/flake.nix +++ b/nix/repo/flake.nix @@ -1,6 +1,7 @@ { inputs = { devshell-lib.url = "gitlab:rensa-nix/devshell?dir=lib"; + nixtest-lib.url = "gitlab:TECHNOFAB/nixtest?dir=lib"; treefmt-nix = { url = "github:numtide/treefmt-nix"; flake = false; @@ -10,6 +11,7 @@ i // { devshell = i.devshell-lib.lib {inherit (i.parent) pkgs;}; + ntlib = i.nixtest-lib.lib {inherit (i.parent) pkgs;}; cilib = import "${i.parent.self}/lib" {inherit (i.parent) pkgs;}; treefmt = import i.treefmt-nix; }; diff --git a/nix/repo/tests.nix b/nix/repo/tests.nix new file mode 100644 index 0000000..62c0833 --- /dev/null +++ b/nix/repo/tests.nix @@ -0,0 +1,10 @@ +{inputs, ...}: let + inherit (inputs) pkgs ntlib cilib; +in { + tests = ntlib.mkNixtest { + modules = ntlib.autodiscover {dir = "${inputs.self}/tests";}; + args = { + inherit pkgs ntlib cilib; + }; + }; +} diff --git a/flakeModuleTest/flake.lock b/tests/fixtures/flake_parts/flake.lock similarity index 100% rename from flakeModuleTest/flake.lock rename to tests/fixtures/flake_parts/flake.lock diff --git a/flakeModuleTest/flake.nix b/tests/fixtures/flake_parts/flake.nix similarity index 92% rename from flakeModuleTest/flake.nix rename to tests/fixtures/flake_parts/flake.nix index 92d5c87..cca696f 100644 --- a/flakeModuleTest/flake.nix +++ b/tests/fixtures/flake_parts/flake.nix @@ -6,13 +6,11 @@ } @ inputs: flake-parts.lib.mkFlake {inherit inputs;} { imports = [ - ../lib/flakeModule.nix + "@repo_path@/lib/flakeModule.nix" ]; systems = import systems; flake = {}; - perSystem = { - ... - }: { + perSystem = _: { ci = { config = { # true is already default, just for testing diff --git a/tests/flake_parts_test.nix b/tests/flake_parts_test.nix new file mode 100644 index 0000000..f192420 --- /dev/null +++ b/tests/flake_parts_test.nix @@ -0,0 +1,39 @@ +{ + pkgs, + ntlib, + ... +}: { + suites."flake-parts" = { + pos = __curPos; + tests = [ + { + name = "flakeModule"; + type = "script"; + script = + # sh + '' + ${ntlib.helpers.scriptHelpers} + ${ntlib.helpers.path (with pkgs; [coreutils nix gnused gnugrep jq])} + repo_path=${../.} + + dir=$(mktemp -d) + trap "rm -rf $dir" EXIT + cd $dir + cp ${./fixtures/flake_parts}/* . + # import from the absolute path above, is easier than trying to figure out the repo path etc. + sed -i -e "s|@repo_path@|$repo_path|" flake.nix + + # NOTE: --impure is required since importing modules from absolute paths is not allowed in pure mode + nix build --impure .#gitlab-ci:pipeline:default + assert "-f result" "should exist" + assert_file_contains "result" "finalize_nix_ci" + jq '.' result # check if valid json just to be sure + + nix build --impure .#gitlab-ci:pipeline:default:pretty + assert "-f result" "should exist" + assert_file_contains "result" " - finalize_nix_ci" + ''; + } + ]; + }; +}