Skip to content

Commit

Permalink
feat: tests for nested commands
Browse files Browse the repository at this point in the history
  • Loading branch information
deemp committed Jan 13, 2024
1 parent 053b792 commit 0d4e6e4
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 5 deletions.
6 changes: 1 addition & 5 deletions nix/commands/examples.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ let inherit (pkgs) lib; in
"category 1" = [
{
prefix = "nix run .#";
prefixes = {
a.b = {
d = "nix run ../#";
};
};
prefixes.a.b.yq-1 = "nix run ../#";
packages = {
a.b = {
jq-1 = [ "[package] jq description" pkgs.jq ];
Expand Down
52 changes: 52 additions & 0 deletions tests/extra/commands.examples.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{ pkgs, devshell, runTest }:
{
nested =
let
shell = devshell.mkShell {
devshell.name = "nested-commands-test";
commands = (import ../../nix/commands/examples.nix { inherit pkgs; }).nested;
};
in
runTest "nested" { } ''
# Load the devshell
source ${shell}/env.bash
type -p python3
# Has hyperfine
# Has no yq
if [[ -z "$(type -p hyperfine)" ]]; then
echo "OK"
else
echo "Error! Has hyperfine"
fi
# Has no yq
if [[ -z "$(type -p yq)" ]]; then
echo "OK"
else
echo "Error! Has yq"
fi
'';

flat =
let
shell = devshell.mkShell {
devshell.name = "flat-commands-test";
commands = (import ../../nix/commands/examples.nix { inherit pkgs; }).flat;
};
in
runTest "flat" { } ''
# Load the devshell
source ${shell}/env.bash
# Has yarn
type -p yarn
# Has hello
type -p hello
# Has black
type -p black
'';
}
188 changes: 188 additions & 0 deletions tests/extra/commands.lib.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
{ pkgs, devshell, runTest }:
let inherit (pkgs) lib; in
{
normalizeCommandsNested =
let
commands = (import ../../nix/commands/examples.nix { inherit pkgs; }).nested;
check = (import ../../nix/commands/lib.nix { inherit pkgs; }).normalizeCommandsNested commands == [
{
category = "category 1";
command = null;
expose = false;
help = "[package] jq description";
name = "a.b.jq-1";
package = pkgs.jq;
prefix = "nix run .#";
}
{
category = "category 1";
command = null;
expose = false;
help = "[package] yq description";
name = "a.b.yq-1";
package = pkgs.yq-go;
prefix = "nix run ../#";
}
{
category = "category 1";
command = null;
expose = false;
help = "Portable command-line YAML processor";
name = "a.b.yq-2";
package = pkgs.yq-go;
prefix = "nix run .#";
}
{
category = "category 1";
command = null;
expose = false;
help = "a package manager for JavaScript";
name = "npm";
package = pkgs.nodePackages.npm;
prefix = "nix run .#";
}
{
category = "category 1";
command = null;
expose = true;
help = "GNU Find Utilities, the basic directory searching utilities of the GNU operating system";
name = "a.b.findutils";
package = pkgs.findutils;
prefix = "";
}
{
category = "category 1";
command = null;
expose = false;
help = "Command-line benchmarking tool";
name = "a.b.hyperfine";
package = pkgs.hyperfine;
prefix = "";
}
{
category = "category 1";
command = "${lib.getExe pkgs.gawk} $@";
expose = false;
help = "[command] run awk";
name = "a.b.awk";
package = null;
prefix = "";
}
{
category = "category 1";
command = "${lib.getExe pkgs.jq} $@";
expose = false;
help = "[command] run jq";
name = "a.b.jq-2";
package = null;
prefix = "";
}
{
category = "category 1";
command = ''printf "hello\n"'';
expose = false;
help = ''[command] print "hello"'';
name = "command with spaces";
package = null;
prefix = "";
}
{
category = "category 1";
command = null;
expose = true;
help = null;
name = pkgs.python3.name;
package = pkgs.python3;
prefix = "";
}
{
category = "category 1";
command = null;
expose = true;
help = "[package] vercel description";
name = pkgs.nodePackages.vercel.name;
package = pkgs.nodePackages.vercel;
prefix = "";
}
{
category = "category 1";
command = null;
expose = true;
help = null;
name = pkgs.nodePackages.yarn.name;
package = pkgs.nodePackages.yarn;
prefix = "";
}
{
category = "category 1";
command = null;
expose = true;
help = null;
name = null;
package = pkgs.gnugrep;
prefix = "";
}
{
category = "category 1";
command = null;
expose = true;
help = "run hello";
name = "run cowsay";
package = pkgs.cowsay;
prefix = "";
}
{
category = "category 1";
command = "${lib.getExe pkgs.perl} $@";
expose = true;
help = "run perl";
name = "run perl";
package = null;
prefix = "";
}
{
category = "category 1";
command = null;
expose = true;
help = "format Nix files";
name = "nix fmt";
package = null;
prefix = "";
}
{
category = "category-2";
command = null;
expose = true;
help = null;
name = null;
package = pkgs.go;
prefix = "";
}
{
category = "category-2";
command = null;
expose = true;
help = "[package] run hello ";
name = pkgs.hello.name;
package = pkgs.hello;
prefix = "";
}
{
category = "category-2";
command = null;
expose = true;
help = null;
name = pkgs.nixpkgs-fmt.name;
package = pkgs.nixpkgs-fmt;
prefix = "";
}
];
in
runTest "simple" { } ''
${
if check
then ''printf "OK"''
else ''printf "Not OK"; exit 1''
}
'';
}

0 comments on commit 0d4e6e4

Please sign in to comment.