Skip to content

Commit

Permalink
refactor: cardanoNix.tests is a perSystem module
Browse files Browse the repository at this point in the history
  • Loading branch information
aciceri committed Nov 15, 2023
1 parent 69ed4ec commit 4bc6692
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 58 deletions.
120 changes: 66 additions & 54 deletions checks/testing.nix
Original file line number Diff line number Diff line change
@@ -1,88 +1,100 @@
{
config,
lib,
inputs,
config,
...
}: let
inherit (lib) mkOption types mapAttrs' nameValuePair;
inherit (config.flake) nixosModules;
in {
options.cardanoNix = {
tests = mkOption {
type = types.lazyAttrsOf (types.submodule ({config, ...}: {
options = {
name = mkOption {
type = types.str;
default = config._module.args.name;
internal = true;
};
systems = mkOption {
type = types.listOf types.str;
};
module = mkOption {
type = types.deferredModule;
};
};
}));
};
};

config.perSystem = {
perSystem = {
config,
lib,
system,
pkgs,
...
} @ perSystemArgs: {
options.cardanoNix.runTestScript = mkOption {
type = types.package;
default = pkgs.callPackage ./run-test.nix {inherit (config.cardanoNix) tests;};
description = "A convenience script to run tests";
internal = true;
};

config = let
nixos-lib = import (inputs.nixpkgs.outPath + "/nixos/lib") {};
tests =
lib.filterAttrs
(_: v: lib.elem system v.systems)
config.cardanoNix.tests;

inherit (perSystemArgs.config.cardanoNix) runTestScript;
in {
checks =
mapAttrs'
(name: test:
nameValuePair "testing-${test.name}"
(nixos-lib.runTest {
}: let
cfg = config.cardanoNix;
in {
options.cardanoNix = {
tests = mkOption {
type = types.lazyAttrsOf (types.submodule ({config, ...}: {
options = {
name = mkOption {
type = types.str;
default = config._module.args.name;
internal = true;
};
systems = mkOption {
type = types.listOf types.str;
};
module = mkOption {
type = types.deferredModule;
};
documentation = mkOption {
type = types.bool;
default = false;
};
specialArgs = mkOption {
type = types.attrsOf types.anything;
default = {};
};
};
}));
};
runTestScript = mkOption {
type = types.package;
default = pkgs.callPackage ./run-test.nix {inherit (cfg) tests;};
description = "A convenience script to run tests";
};
_nixosLib = mkOption {
type = types.anything;
default = import (inputs.nixpkgs.outPath + "/nixos/lib") {};
internal = true;
};
_mkCheckFromTest = mkOption {
type = types.functionTo types.package;
internal = true;
default = test:
(cfg._nixosLib.runTest {
hostPkgs = pkgs;

# speed up evaluation by skipping docs
defaults.documentation.enable = lib.mkDefault false;
# false by default, it speeds up evaluation by skipping docs generation
defaults.documentation.enable = test.documentation;

# make self available in test modules and our custom pkgs
node.specialArgs = {
inherit pkgs;
node = {
inherit (test) specialArgs;
};

# import all of our flake nixos modules by default
defaults.imports = [
config.flake.nixosModules.default
nixosModules.default
];

# import the test module
imports = [test.module];
})
.config
.result)
tests;
.result;
};
};

config = {
checks =
mapAttrs'
(name: test: nameValuePair "testing-${test.name}" (cfg._mkCheckFromTest test))
(lib.filterAttrs
(_: v: lib.elem system v.systems)
cfg.tests);

apps.run-test.program = lib.getExe runTestScript;
apps.run-test.program = lib.getExe cfg.runTestScript;

devshells.default.commands = [
{
name = "run-test";
category = "testing";
help = "Run tests";
command = "${lib.getExe runTestScript} $@";
command = "${lib.getExe cfg.runTestScript} $@";
}
];
};
Expand Down
3 changes: 2 additions & 1 deletion tests/cardano-cli.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
_: {
{
cardanoNix.tests = {
dummy = {
systems = ["x86_64-linux"];
Expand All @@ -16,6 +16,7 @@ _: {
cardano-ecosystem.cli.enable = true;
};
};

testScript = ''
# FIXME: check for cardano cli, not git
machine.succeed("git --version")
Expand Down
8 changes: 5 additions & 3 deletions tests/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
imports = [
./cardano-cli.nix
];
perSystem = _: {
imports = [
./cardano-cli.nix
];
};
}

0 comments on commit 4bc6692

Please sign in to comment.