-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
testing.nix
137 lines (123 loc) · 3.87 KB
/
testing.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
{
self,
inputs,
...
}: {
perSystem = {
lib,
config,
system,
...
}: let
# create a custom nixpkgs with our flake packages available
pkgs = import inputs.nixpkgs {
inherit system;
overlays = [
self.overlays.default
];
};
in {
########################################
## Interface
########################################
options.testing.checks = with lib;
mkOption {
type = types.attrsOf types.package;
default = {};
};
########################################
## Implementation
########################################
config.testing.checks = with lib; let
# import the testing framework
nixos-lib = import (pkgs.path + "/nixos/lib") {};
# traverse the filesystem and capture any files with `.test.nix` suffix
eachTest =
filterAttrs
(_: (hasSuffix ".test.nix"))
(fs.flattenTree {
tree = fs.rakeLeaves ./.;
separator = "-";
});
# examine the `systems` attribute of each test, filtering out any that do not support the current system
eachTestForSystem = with lib;
filterAttrs
(_: v: elem system v.systems)
(mapAttrs (_: import) eachTest);
in
mapAttrs'
(name: test:
nameValuePair "testing-${removeSuffix ".test" name}"
(nixos-lib.runTest {
hostPkgs = pkgs;
# speed up evaluation by skipping docs
defaults.documentation.enable = lib.mkDefault false;
# make self available in test modules and our custom pkgs
node.specialArgs = {inherit self pkgs;};
# import all of our flake nixos modules by default
defaults.imports = [
self.nixosModules.default
];
# import the test module
imports = [test.module];
})
.config
.result)
eachTestForSystem;
########################################
## Commands
########################################
config.devshells.default.commands = [
{
name = "tests";
category = "Testing";
help = "Build and run a test";
command = ''
Help() {
# Display Help
echo " Build and run a test"
echo
echo " Usage:"
echo " , test <name>"
echo " , test <name> --interactive"
echo " , test -s <system> <name>"
echo
echo " Arguments:"
echo " <name> If a test package is called 'testing-nethermind-basic' then <name> should be 'nethermind-basic'."
echo
echo " Options:"
echo " -h --help Show this screen."
echo " -s --system Specify the target platform [default: x84_64-linux]."
echo " -i --interactive Run the test interactively."
echo
}
ARGS=$(getopt -o ihs: --long interactive,help,system: -n ', test' -- "$@")
eval set -- "$ARGS"
SYSTEM="x86_64-linux"
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;;
-- ) shift; break;;
* ) break;;
esac
done
if [ $# -eq 0 ]; then
# No test name has been provided
Help
exit 1
fi
NAME="$1"
shift
# build the test driver
nix build ".#checks.$SYSTEM.testing-$NAME.driver"
# run the test driver, passing any remaining arguments
set -x
./result/bin/nixos-test-driver "''${DRIVER_ARGS[@]}"
'';
}
];
};
}