Skip to content

Commit

Permalink
dev: Make the tests command able to select which test to launch
Browse files Browse the repository at this point in the history
The `tests` command can either launch all tests (without any arguments),
a specific test with `-t/--test` or choose a test with `-i/--interactive`
  • Loading branch information
traxys committed Jul 21, 2024
1 parent b157636 commit 7908729
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 5 deletions.
1 change: 1 addition & 0 deletions flake-modules/dev/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
};
statix.enable = true;
stylua.enable = true;
shfmt.enable = true;
taplo.enable = true;
};

Expand Down
18 changes: 14 additions & 4 deletions flake-modules/dev/devshell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@
{
name = "tests";
help = "Run nixvim tests";
command = ''
echo "=> Running nixvim tests for the '${system}' architecture..."
command =
let
launchTest = pkgs.writeShellApplication {
name = "launch-tests";
runtimeInputs = with pkgs; [
jq
fzf
];

${nix} build .#checks.${system}.tests "$@"
'';
text = builtins.readFile ./launch-test.sh;
};
in
''
NIXVIM_SYSTEM=${system} NIXVIM_NIX_COMMAND=${nix} ${lib.getExe launchTest} "$@"
'';
}
{
name = "test-lib";
Expand Down
83 changes: 83 additions & 0 deletions flake-modules/dev/launch-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env bash

: "${NIXVIM_NIX_COMMAND:=nix}"
if [[ -z ${NIXVIM_SYSTEM+x} ]]; then
NIXVIM_SYSTEM=$(nix eval --raw --impure --expr "builtins.currentSystem")
fi

help() {
cat <<EOF
Usage: tests [OPTIONS] [tests...]
If tests are passed on the command line only these will be launched
Options:
-h, --help: Display this help message and exit
-l, --list: Display the list of tests and exit
-i, --interactive: Pick interactively the tests. Can't be supplied if tests where passed.
EOF
}

if ! OPTS=$(getopt -o "hli" -l "help,list,interactive" -- "$@"); then
echo "Invalid options" >&2
help
exit 1
fi

eval set -- "$OPTS"

specified_tests=()
interactive=false

mk_test_list() {
nix eval ".#checks.${NIXVIM_SYSTEM}" --apply builtins.attrNames --json |
jq -r 'map(select(startswith("test-")))[]'
}

while true; do
case "$1" in
-h | --help)
help
exit 0
;;
-l | --list)
mk_test_list
exit 0
;;
-i | --interactive)
interactive=true
shift
;;
--)
shift
specified_tests=("$@")
break
;;
esac
done

run_tests() {
# Add the prefix "checks.${system}." to each argument
if ! "${NIXVIM_NIX_COMMAND}" build --no-link --file . "${@/#/checks.${NIXVIM_SYSTEM}.}"; then
echo "Test failure" >&2
exit 1
fi
}

if [[ $interactive = true && ${#specified_tests[@]} -ne 0 ]]; then
echo "Can't use --interactive with tests on the command line" >&2
exit 1
fi

if [[ $interactive = true ]]; then
test_name=$(mk_test_list | fzf) || exit $?
specified_tests+=("$test_name")
fi

if [[ ${#specified_tests[@]} -eq 0 ]]; then
readarray -t complete_test_list < <(mk_test_list)
run_tests "${complete_test_list[@]}"
else
echo "Running ${#specified_tests[@]} tests: ${specified_tests[*]}" >&2
run_tests "${specified_tests[@]}"
fi
2 changes: 1 addition & 1 deletion tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ builtins.listToAttrs (
dontRunModule = case: case.tests.dontRun or false;
in
{
inherit name;
name = "test-${name}";
value = mkTestDerivationFromNixvimModule {
inherit name;
tests = builtins.map (
Expand Down

0 comments on commit 7908729

Please sign in to comment.