|
| 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