lib/modules: fixup paths

This commit is contained in:
Johannes Kirschbauer
2025-10-21 20:31:21 +02:00
parent 346e3d816a
commit c838e08d77
8 changed files with 52 additions and 54 deletions

108
lib/dir_test.nix Normal file
View File

@@ -0,0 +1,108 @@
{
lib ? import <nixpkgs/lib>,
}:
let
clanLibOrig = (import ./. { inherit lib; }).__unfix__;
clanLibWithFs =
{ virtual_fs }:
lib.fix (
lib.extends (
final: _:
let
clan-core = {
clanLib = final;
modules.clan.default = lib.modules.importApply ../modules/clan { inherit clan-core; };
# Note: Can add other things to "clan-core"
# ... Not needed for this test
};
in
{
clan = import ./clan {
inherit lib clan-core;
};
# Override clanLib.fs for unit-testing against a virtual filesystem
fs = import ./clanTest/virtual-fs.nix { inherit lib; } {
inherit rootPath virtual_fs;
# Example of a passthru
# passthru = [
# ".*inventory\.json$"
# ];
};
}
) clanLibOrig
);
rootPath = ./.;
in
{
test_autoload_directories =
let
vclan =
(clanLibWithFs {
virtual_fs = {
"machines" = {
type = "directory";
};
"machines/foo-machine" = {
type = "directory";
};
"machines/bar-machine" = {
type = "directory";
};
};
}).clan
{ config.directory = rootPath; };
in
{
inherit vclan;
expr = {
machines = lib.attrNames vclan.config.inventory.machines;
definedInMachinesDir = map (
p: lib.hasInfix "/machines/" p
) vclan.options.inventory.valueMeta.configuration.options.machines.files;
};
expected = {
machines = [
"bar-machine"
"foo-machine"
];
definedInMachinesDir = [
true # /machines/foo-machine
true # /machines/bar-machine
false # <clan-core>/module.nix defines "machines" without members
];
};
};
# Could probably be unified with the previous test
# This is here for the sake to show that 'virtual_fs' is a test parameter
test_files_are_not_machines =
let
vclan =
(clanLibWithFs {
virtual_fs = {
"machines" = {
type = "directory";
};
"machines/foo.nix" = {
type = "file";
};
"machines/bar.nix" = {
type = "file";
};
};
}).clan
{ config.directory = rootPath; };
in
{
inherit vclan;
expr = {
machines = lib.attrNames vclan.config.inventory.machines;
};
expected = {
machines = [ ];
};
};
}

View File

@@ -17,9 +17,9 @@ lib.evalModules {
specialArgs._ctx = prefix;
modules = [
# Base module
./service-module.nix
./inventory/distributed-service/service-module.nix
# Feature modules
(lib.modules.importApply ./api-feature.nix {
(lib.modules.importApply ./inventory/distributed-service/api-feature.nix {
inherit clanLib prefix;
})
]

View File

@@ -100,6 +100,35 @@ rec {
touch $out
'';
};
# Run: nix-unit --extra-experimental-features flakes --flake .#legacyPackages.x86_64-linux.evalTests-build-clan
legacyPackages.evalTests-build-clan = import ./tests.nix {
inherit lib;
clan-core = self;
};
checks = {
eval-lib-build-clan = pkgs.runCommand "tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } ''
export HOME="$(realpath .)"
nix-unit --eval-store "$HOME" \
--extra-experimental-features flakes \
--show-trace \
${inputOverrides} \
--flake ${
self.filter {
include = [
"flakeModules"
"inventory.json"
"lib"
"machines"
"nixosModules"
];
}
}#legacyPackages.${system}.evalTests-build-clan
touch $out
'';
};
};
}

View File

@@ -13,16 +13,17 @@ in
let
# Common filtered source for inventory tests
inventoryTestsSrc = lib.fileset.toSource {
root = ../../../..;
root = ../../..;
fileset = lib.fileset.unions [
../../../../flake.nix
../../../../flake.lock
(lib.fileset.fileFilter (file: file.name == "flake-module.nix") ../../../..)
../../../../flakeModules
../../../../lib
../../../../nixosModules/clanCore
../../../../machines
../../../../inventory.json
../../../flake.nix
../../../flake.lock
(lib.fileset.fileFilter (file: file.name == "flake-module.nix") ../../..)
../../../flakeModules
../../../lib
../../../nixosModules/clanCore
../../../machines
../../../inventory.json
../../../modules
];
};
in

232
lib/tests.nix Normal file
View File

@@ -0,0 +1,232 @@
{
lib,
clan-core,
}:
let
# Shallowly force all attribute values to be evaluated.
shallowForceAllAttributes = lib.foldlAttrs (
_acc: _name: value:
lib.seq value true
) true;
inherit (clan-core.clanLib) clan;
in
#######
{
autoloading = import ./dir_test.nix { inherit lib; };
test_missing_self =
let
eval = clan {
meta.name = "test";
directory = ./.;
};
in
{
expr = shallowForceAllAttributes eval.config;
expected = true;
};
test_only_required =
let
eval = clan {
self = {
inputs = { };
outPath = ./.;
};
meta.name = "test";
};
in
{
expr = shallowForceAllAttributes eval.config;
expected = true;
};
test_all_simple =
let
eval = clan {
self = {
inputs = { };
};
directory = ./.;
machines = { };
inventory = {
meta.name = "test";
};
};
in
{
expr = eval.config ? inventory;
expected = true;
};
test_outputs_clanInternals =
let
eval = clan {
self = {
inputs = { };
};
directory = ./.;
imports = [
# What the user needs to specify
{
directory = ./.;
inventory.meta.name = "test";
}
];
};
in
{
inherit eval;
expr = eval.config.clanInternals.inventoryClass.inventory.meta;
expected = {
description = null;
icon = null;
name = "test";
};
};
test_fn_simple =
let
eval = clan {
self = {
inputs = { };
};
directory = ./.;
meta.name = "test";
};
in
{
expr = lib.isAttrs eval.config.clanInternals;
expected = true;
};
test_fn_clan_core =
let
eval = clan {
self = {
inputs = { };
};
directory = ../.;
meta.name = "test-clan-core";
};
in
{
expr = builtins.attrNames eval.config.nixosConfigurations;
expected = [
"test-backup"
"test-inventory-machine"
];
};
test_machines_are_modules =
let
eval = clan {
self = {
inputs = { };
};
directory = ../.;
meta.name = "test-clan-core";
};
in
{
expr = builtins.attrNames eval.config.nixosModules;
expected = [
"clan-machine-test-backup"
"clan-machine-test-inventory-machine"
];
};
test_clan_all_machines =
let
eval = clan {
self = {
inputs = { };
};
directory = ./.;
meta.name = "test";
inventory.machines.machine1 = { };
machines.machine2 = { };
};
in
{
expr = builtins.attrNames eval.config.nixosConfigurations;
expected = [
"machine1"
"machine2"
];
};
test_clan_specialArgs =
let
eval = clan {
self = {
inputs = { };
};
directory = ./.;
meta.name = "test";
specialArgs.foo = "dream2nix";
machines.machine2 =
{ foo, ... }:
{
networking.hostName = foo;
nixpkgs.hostPlatform = "x86_64-linux";
};
};
in
{
expr = eval.config.nixosConfigurations.machine2.config.networking.hostName;
expected = "dream2nix";
};
test_clan_darwin_machines =
let
eval = clan {
self = {
inputs = { };
};
directory = ./.;
meta.name = "test";
machines.machine1 = { };
inventory.machines.machine2 = {
machineClass = "darwin";
};
inventory.machines.machine3 = {
machineClass = "nixos";
};
};
in
{
result = eval;
expr = {
nixos = builtins.attrNames eval.config.nixosConfigurations;
darwin = builtins.attrNames eval.config.darwinConfigurations;
};
expected = {
nixos = [
"machine1"
"machine3"
];
darwin = [ "machine2" ];
};
};
test_clan_all_machines_laziness =
let
eval = clan {
self = {
inputs = { };
};
directory = ./.;
meta.name = "test";
machines.machine1.non_existent_option = throw "eval error";
};
in
{
expr = builtins.attrNames eval.config.nixosConfigurations;
expected = [
"machine1"
];
};
}