Skip to content

Commit b7017d7

Browse files
committed
tests: Add tests for #119 the optional import feature (and the subcommands feature)
Signed-off-by: Drew Robinson <drew.robinson@gmail.com>
1 parent 85e5a2e commit b7017d7

7 files changed

+178
-10
lines changed

testdata/missing-imports.ahoy.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ahoyapi: v2
22
commands:
33
missing-imports:
4-
usage: "This item has imports set, but it doesn't exit."
4+
usage: "This item has imports set, but it doesn't exist."
55
imports:
66
- "bogus.ahoy.yml"
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ahoyapi: v2
2+
commands:
3+
non-optional-cmd:
4+
usage: This command is not optional
5+
imports:
6+
- non-existent-file.yml

testdata/optional-command.ahoy.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ahoyapi: v2
2+
commands:
3+
optional-cmd:
4+
usage: These imports are optional
5+
imports:
6+
- testdata/non-existent-file.yml
7+
optional: true
8+
regular-cmd:
9+
usage: This is a regular command
10+
cmd: echo "This is a regular command"

tests/no-ahoy-file.bats

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ teardown() {
88
mv tmp.ahoy.yml .ahoy.yml
99
}
1010

11-
@test "run ahoy without a command and without a .ahoy.yml file" {
11+
@test "Run ahoy without a command and without an .ahoy.yml file" {
1212
run ./ahoy
1313
[ $status -eq 1 ]
1414
[ "${lines[-2]}" == "[error] No .ahoy.yml found. You can use 'ahoy init' to download an example." ]
15-
[ "${lines[-1]}" == "[fatal] Missing flag or argument." ]
15+
[ "${lines[-1]}" == "[warn] Missing flag or argument." ]
1616
}
1717

18-
@test "run an ahoy command without a .ahoy.yml file" {
18+
@test "Run an ahoy command without an .ahoy.yml file" {
1919
run ./ahoy something
2020
[ "$output" == "[fatal] Command not found for 'something'" ]
2121
}
2222

23-
@test "run ahoy init without a .ahoy.yml file" {
23+
@test "Run ahoy init without an .ahoy.yml file" {
2424
run ./ahoy init
2525
[ "${lines[-1]}" == "Example .ahoy.yml downloaded to the current directory. You can customize it to suit your needs!" ]
2626
}
2727

28-
@test "run ahoy init with a existing .ahoy.yml file in the current directory" {
28+
@test "Run ahoy init with an existing .ahoy.yml file in the current directory" {
2929
cp tmp.ahoy.yml .ahoy.yml
3030
run ./ahoy init --force
3131
[ "${lines[0]}" == "Warning: '--force' parameter passed, overwriting .ahoy.yml in current directory." ]

tests/optional-commands.bats

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env bats
2+
3+
load 'test_helpers/bats-support/load'
4+
load 'test_helpers/bats-assert/load'
5+
6+
@test "Optional import not found doesn't cause error" {
7+
# Run ahoy without arguments (which typically lists available commands)
8+
run ./ahoy -f testdata/optional-command.ahoy.yml
9+
10+
# Check that the optional command is not listed
11+
[[ ! "$output" =~ "optional-cmd" ]]
12+
13+
# Check that the regular command is listed
14+
[[ "$output" =~ "regular-cmd" ]]
15+
16+
# Check that a standard "Missing argument" error is shown
17+
[[ "$output" =~ "Missing flag or argument" ]]
18+
19+
# Check that the command exited with an error
20+
[ $status -eq 1 ]
21+
22+
# Try to run the optional command (it should fail gracefully)
23+
run ./ahoy -f testdata/optional-command.ahoy.yml optional-cmd
24+
[ $status -eq 1 ]
25+
[[ "$output" =~ "Command not found for 'optional-cmd'" ]]
26+
27+
# Run the regular command (it should work)
28+
run ./ahoy -f testdata/optional-command.ahoy.yml regular-cmd
29+
[ $status -eq 0 ]
30+
[[ "$output" = "This is a regular command" ]]
31+
}
32+
33+
@test "Non-optional command with missing imports causes error" {
34+
# Run ahoy without arguments
35+
run ./ahoy -f testdata/non-optional-command.ahoy.yml
36+
37+
# Check that the command failed
38+
[ $status -eq 1 ]
39+
40+
# Check for the appropriate error message
41+
[[ "$output" =~ "Command [non-optional-cmd] has 'imports' set, but no commands were found" ]]
42+
}

tests/simple.bats

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#!/usr/bin/env bats
22

3-
@test "display help text and fatal error when no arguments are passed." {
3+
@test "Display help text and fatal error when no arguments are passed." {
44
run ./ahoy -f testdata/simple.ahoy.yml
55
# Should throw an error.
66
[ $status -ne 0 ]
77
echo "$output"
88
[ "${#lines[@]}" -gt 10 ]
9-
[ "${lines[-1]}" == "[fatal] Missing flag or argument." ]
9+
[ "${lines[-1]}" == "[warn] Missing flag or argument." ]
1010
}
1111

12-
@test "run a simple ahoy command: echo" {
12+
@test "Run a simple ahoy command: echo" {
1313
result="$(./ahoy -f testdata/simple.ahoy.yml echo something)"
1414
[ "$result" == "something" ]
1515
}
1616

17-
@test "run a simple ahoy command (ls -a) with an extra parameter (-l)" {
17+
@test "Run a simple ahoy command (ls -a) with an extra parameter (-l)" {
1818
run ./ahoy -f testdata/simple.ahoy.yml list -- -l
1919
[ "${#lines[@]}" -gt 13 ]
2020
}

tests/sub-commands.bats

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/usr/bin/env bats
2+
3+
load 'test_helpers/bats-support/load'
4+
load 'test_helpers/bats-assert/load'
5+
6+
setup() {
7+
# Create a temporary directory for our test files
8+
TEST_DIR="$(mktemp -d)"
9+
10+
# Create main .ahoy.yml file
11+
cat > "${TEST_DIR}/.ahoy.yml" <<EOF
12+
ahoyapi: v2
13+
commands:
14+
main-cmd:
15+
usage: Main command
16+
imports:
17+
- sub1.yml
18+
- sub2.yml
19+
- non-existent.yml
20+
EOF
21+
22+
# Create sub1.yml
23+
cat > "${TEST_DIR}/sub1.yml" <<EOF
24+
ahoyapi: v2
25+
commands:
26+
sub1-cmd:
27+
usage: Subcommand 1
28+
cmd: echo "Subcommand 1"
29+
EOF
30+
31+
# Create sub2.yml
32+
cat > "${TEST_DIR}/sub2.yml" <<EOF
33+
ahoyapi: v2
34+
commands:
35+
sub2-cmd:
36+
usage: Subcommand 2
37+
cmd: echo "Subcommand 2"
38+
EOF
39+
40+
# Copy freshly built ahoy binary to the test directory
41+
cp ahoy "${TEST_DIR}"
42+
43+
# Change to the test directory
44+
cd "${TEST_DIR}"
45+
}
46+
47+
teardown() {
48+
# Remove the temporary directory
49+
rm -rf "${TEST_DIR}"
50+
}
51+
52+
@test "getSubCommands() loads existing subcommands" {
53+
run ./ahoy
54+
55+
# Check that main-cmd is listed
56+
[[ "$output" =~ "Main command" ]]
57+
58+
# Check that the command executed with an expected error
59+
[ "$status" -eq 1 ]
60+
61+
# Check that sub1-cmd and sub2-cmd are listed under main-cmd
62+
run ahoy main-cmd --help
63+
[ "$status" -eq 0 ]
64+
[[ "$output" =~ "sub1-cmd" ]]
65+
[[ "$output" =~ "sub2-cmd" ]]
66+
67+
# Check that non-existent.yml is ignored without error
68+
[[ ! "$output" =~ "non-existent.yml" ]]
69+
70+
# Check that the command executed successfully
71+
[ "$status" -eq 0 ]
72+
}
73+
74+
@test "getSubCommands() executes subcommands correctly" {
75+
# Test sub1-cmd
76+
run ./ahoy main-cmd sub1-cmd
77+
[ "$status" -eq 0 ]
78+
[ "$output" = "Subcommand 1" ]
79+
80+
# Test sub2-cmd
81+
run ./ahoy main-cmd sub2-cmd
82+
[ "$status" -eq 0 ]
83+
[ "$output" = "Subcommand 2" ]
84+
}
85+
86+
@test "getSubCommands() handles empty imports" {
87+
# Create an empty.ahoy.yml with empty imports
88+
cat > empty.ahoy.yml <<EOF
89+
ahoyapi: v2
90+
commands:
91+
empty-import:
92+
usage: Command with empty imports
93+
imports: []
94+
EOF
95+
96+
run ./ahoy -f empty.ahoy.yml
97+
[[ "$output" =~ "empty-import" ]]
98+
99+
# Check that the command executed with an expected error
100+
[ "$status" -eq 1 ]
101+
102+
run ./ahoy -f empty.ahoy.yml empty-import
103+
104+
[[ "$output" =~ "empty-import" ]]
105+
106+
[[ "$output" =~ "but it is empty" ]]
107+
108+
# Check that the command executed with an expected error
109+
[ "$status" -eq 1 ]
110+
}

0 commit comments

Comments
 (0)