Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions checks/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{inputs, ...}: {
imports = [
./testing.nix
];
perSystem = {
pkgs,
config,
Expand Down
69 changes: 69 additions & 0 deletions checks/run-test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
writeShellApplication,
lib,
tests,
stdenv,
...
}:
writeShellApplication {
name = "run-test";

runtimeInputs = [];

text = ''
cmd_name=$(basename "$0")

help() {
echo " build and run a test"
echo
echo " usage:"
echo " $cmd_name <name>"
echo " $cmd_name <name> --interactive"
echo " $cmd_name -s <system> <name>"
echo
echo " arguments:"
echo " <name>"
echo
echo " options:"
echo " -h --help show this screen."
echo " -l --list show available tests."
echo " -s --system specify the target platform [default: ${stdenv.system}]."
echo " -i --interactive run the test interactively."
echo
}

list() {
echo " list of available tests:"
echo
echo "${lib.concatMapStrings (s: " - " + s + "\n") (lib.mapAttrsToList (_: test: test.name) tests)}"
}

args=$(getopt -o lihs: --long list,interactive,help,system: -n 'tests' -- "$@")
eval set -- "$args"

system="${stdenv.system}"
driver_args=()

while [ $# -gt 0 ]; do
case "$1" in
-i | --interactive) driver_args+=("--interactive"); shift;;
-s | --system) system="$2"; shift 2;;
-h | --help) help; exit 0;;
-l | --list) list; exit 0;;
-- ) shift; break;;
* ) break;;
esac
done

if [ $# -eq 0 ]; then
help
exit 1
fi

name="$1"
shift

# build/run the test driver, passing any remaining arguments
nix run ".#checks.$system.testing-$name.driver" -- "''${driver_args[@]}"
'';
}
102 changes: 102 additions & 0 deletions checks/testing.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
lib,
inputs,
config,
...
}: let
inherit (lib) mkOption types mapAttrs' nameValuePair;
inherit (config.flake) nixosModules;
in {
perSystem = {
config,
lib,
system,
pkgs,
...
}: 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to create a cleaner type consider using lib.systems.flakeExposed which gives you a list of all systems used in nixpkgs.

};
module = mkOption {
type = types.deferredModule;
};
documentation = mkOption {
type = types.bool;
default = false;
};
specialArgs = mkOption {
type = types.attrsOf types.anything;
default = {};
};
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lacks a name option that by default is equal to config._module.args.name.

}));
};
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;

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

node = {
inherit (test) specialArgs;
};

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

# import the test module
imports = [test.module];
})
.config
.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 cfg.runTestScript;

devshells.default.commands = [
{
name = "run-test";
category = "testing";
help = "Run tests";
command = "${lib.getExe cfg.runTestScript} $@";
}
];
};
};
}
5 changes: 3 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
outputs = inputs @ {flake-parts, ...}:
flake-parts.lib.mkFlake {
inherit inputs;
}
{
} {
debug = true;
imports = [
./lib
./checks
./ci
./formatter
./modules
./shell
./tests
];
systems = [
"x86_64-linux"
Expand Down
2 changes: 1 addition & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{lib, ...}: let
lib' = import ./functions.nix lib;
in {
flake.lib = lib';
config.flake.lib = lib';
}
2 changes: 1 addition & 1 deletion modules/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{config, ...}: {
imports = [
];

Expand Down
27 changes: 27 additions & 0 deletions tests/cardano-cli.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
cardanoNix.tests = {
dummy = {
systems = ["x86_64-linux"];

module = {
name = "cli-test";

nodes = {
machine = {
virtualisation = {
cores = 2;
memorySize = 1024;
writableStore = true;
};
cardano-ecosystem.cli.enable = true;
};
};

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