File: help.bats

package info (click to toggle)
golang-github-containers-buildah 1.39.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,724 kB
  • sloc: sh: 2,398; makefile: 236; perl: 187; asm: 16; awk: 12; ansic: 1
file content (95 lines) | stat: -rw-r--r-- 3,390 bytes parent folder | download
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
#!/usr/bin/env bats

load helpers

# run 'buildah help', parse the output looking for 'Available Commands';
# return that list.
function buildah_commands() {
    run_buildah help "$@" |\
        awk '/^Available Commands:/{ok=1;next}/^Flags:/{ok=0}ok { print $1 }' |\
        grep .
}

function check_help() {
    local count=0
    local -A found

    for cmd in $(buildah_commands "$@"); do
        # Human-readable buildah command string, with multiple spaces collapsed
        command_string="buildah $* $cmd"
        command_string=${command_string//  / }  # 'buildah  x' -> 'buildah x'

        # help command and --help flag have the same output
        run_buildah help "$@" $cmd
        local full_help=$output

        # The line immediately after 'Usage:' gives us a 1-line synopsis
        usage=$(echo "$output" | grep -A1 '^Usage:' | tail -1)
        [ -n "$usage" ] || die "$command_string: no Usage message found"
        expr "$usage" : "^  $command_string" > /dev/null || die "$command_string: Usage string doesn't match command"

        # If usage ends in '[command]', recurse into subcommands
        if expr "$usage" : '.*\[command\]$' >/dev/null; then
            found[subcommands]=1
            check_help "$@" $cmd
            continue
        fi

        # Cross-check: if usage includes '[flags]', there must be a
        # longer 'Flags:' section in the full --help output; vice-versa,
        # if 'Flags:' is in full output, usage line must have '[flags]'.
        if expr "$usage" : '.*\[flags' >/dev/null; then
            if ! expr "$full_help" : ".*Flags:" >/dev/null; then
                die "$command_string: Usage includes '[flags]' but has no 'Flags:' subsection"
            fi
        elif expr "$full_help" : ".*Flags:" >/dev/null; then
            die "$command_string: --help has 'Flags:' section but no '[flags]' in synopsis"
        fi

        count=$(expr $count + 1)

    done

    run_buildah "$@" --help
    full_usage=$output

    # Any command that takes subcommands, must show usage if called without one.
    run_buildah "$@"
    expect_output "$full_usage"

    # 'NoSuchCommand' subcommand shows usage unless the command is root 'buildah' command.
    if [ -n "$*" ]; then
        run_buildah "$@" NoSuchCommand
        expect_output "$full_usage"
    else
        run_buildah 125 "$@" NoSuchCommand
        expect_output --substring "unknown command"
    fi

    # This can happen if the output of --help changes, such as between
    # the old command parser and cobra.
    assert "$count" -gt 0 \
           "Internal error: no commands found in 'buildah help $@' list"

    # Sanity check: make sure the special loops above triggered at least once.
    # (We've had situations where a typo makes the conditional never run in podman)
    if [ -z "$*" ]; then
        # This loop is copied from podman test and redundant for buildah now.
        # But this is kept for future extension.
        for i in subcommands; do
            if [[ -z ${found[$i]} ]]; then
                die "Internal error: '$i' subtest did not trigger"
            fi
        done
    fi

    # This can happen if the output of --help changes, such as between
    # the old command parser and cobra.
    assert "$count" -gt 0 \
           "Internal error: no commands found in 'buildah help list"

}

@test "buildah help - basic tests" {
    check_help
}