busser-bats 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env bash
2
+ set -e
3
+
4
+ resolve_link() {
5
+ $(type -p greadlink readlink | head -1) "$1"
6
+ }
7
+
8
+ abs_dirname() {
9
+ local cwd="$(pwd)"
10
+ local path="$1"
11
+
12
+ while [ -n "$path" ]; do
13
+ cd "${path%/*}"
14
+ local name="${path##*/}"
15
+ path="$(resolve_link "$name" || true)"
16
+ done
17
+
18
+ pwd
19
+ cd "$cwd"
20
+ }
21
+
22
+ PREFIX="$1"
23
+ if [ -z "$1" ]; then
24
+ { echo "usage: $0 <prefix>"
25
+ echo " e.g. $0 /usr/local"
26
+ } >&2
27
+ exit 1
28
+ fi
29
+
30
+ BATS_ROOT="$(abs_dirname "$0")"
31
+ mkdir -p "$PREFIX"/{bin,libexec}
32
+ cp -R "$BATS_ROOT"/bin/* "$PREFIX"/bin
33
+ cp -R "$BATS_ROOT"/libexec/* "$PREFIX"/libexec
34
+
35
+ echo "Installed Bats to $PREFIX/bin/bats"
@@ -0,0 +1,140 @@
1
+ #!/usr/bin/env bash
2
+ set -e
3
+
4
+ version() {
5
+ echo "Bats 0.3.1"
6
+ }
7
+
8
+ usage() {
9
+ version
10
+ echo "Usage: bats [-c] [-p | -t] <test> [<test> ...]"
11
+ }
12
+
13
+ help() {
14
+ usage
15
+ echo
16
+ echo " <test> is the path to a Bats test file, or the path to a directory"
17
+ echo " containing Bats test files."
18
+ echo
19
+ echo " -c, --count Count the number of test cases without running any tests"
20
+ echo " -h, --help Display this help message"
21
+ echo " -p, --pretty Show results in pretty format (default for terminals)"
22
+ echo " -t, --tap Show results in TAP format"
23
+ echo " -v, --version Display the version number"
24
+ echo
25
+ echo " For more information, see https://github.com/sstephenson/bats"
26
+ echo
27
+ }
28
+
29
+ resolve_link() {
30
+ $(type -p greadlink readlink | head -1) "$1"
31
+ }
32
+
33
+ abs_dirname() {
34
+ local cwd="$(pwd)"
35
+ local path="$1"
36
+
37
+ while [ -n "$path" ]; do
38
+ cd "${path%/*}"
39
+ local name="${path##*/}"
40
+ path="$(resolve_link "$name" || true)"
41
+ done
42
+
43
+ pwd
44
+ cd "$cwd"
45
+ }
46
+
47
+ expand_path() {
48
+ { cd "$(dirname "$1")" 2>/dev/null
49
+ local dirname="$PWD"
50
+ cd "$OLDPWD"
51
+ echo "$dirname/$(basename "$1")"
52
+ } || echo "$1"
53
+ }
54
+
55
+ BATS_LIBEXEC="$(abs_dirname "$0")"
56
+ export BATS_PREFIX="$(abs_dirname "$BATS_LIBEXEC")"
57
+ export PATH="$BATS_LIBEXEC:$PATH"
58
+
59
+ options=()
60
+ arguments=()
61
+ for arg in "$@"; do
62
+ if [ "${arg:0:1}" = "-" ]; then
63
+ if [ "${arg:1:1}" = "-" ]; then
64
+ options[${#options[*]}]="${arg:2}"
65
+ else
66
+ index=1
67
+ while option="${arg:$index:1}"; do
68
+ [ -n "$option" ] || break
69
+ options[${#options[*]}]="$option"
70
+ index=$(($index+1))
71
+ done
72
+ fi
73
+ else
74
+ arguments[${#arguments[*]}]="$arg"
75
+ fi
76
+ done
77
+
78
+ unset count_flag pretty
79
+ [ -t 0 ] && [ -t 1 ] && pretty="1"
80
+
81
+ for option in "${options[@]}"; do
82
+ case "$option" in
83
+ "h" | "help" )
84
+ help
85
+ exit 0
86
+ ;;
87
+ "v" | "version" )
88
+ version
89
+ exit 0
90
+ ;;
91
+ "c" | "count" )
92
+ count_flag="-c"
93
+ ;;
94
+ "t" | "tap" )
95
+ pretty=""
96
+ ;;
97
+ "p" | "pretty" )
98
+ pretty="1"
99
+ ;;
100
+ * )
101
+ usage >&2
102
+ exit 1
103
+ ;;
104
+ esac
105
+ done
106
+
107
+ if [ "${#arguments[@]}" -eq 0 ]; then
108
+ usage >&2
109
+ exit 1
110
+ fi
111
+
112
+ filenames=()
113
+ for filename in "${arguments[@]}"; do
114
+ if [ -d "$filename" ]; then
115
+ shopt -s nullglob
116
+ for suite_filename in "$(expand_path "$filename")"/*.bats; do
117
+ filenames["${#filenames[@]}"]="$suite_filename"
118
+ done
119
+ shopt -u nullglob
120
+ else
121
+ filenames["${#filenames[@]}"]="$(expand_path "$filename")"
122
+ fi
123
+ done
124
+
125
+ if [ "${#filenames[@]}" -eq 1 ]; then
126
+ command="bats-exec-test"
127
+ else
128
+ command="bats-exec-suite"
129
+ fi
130
+
131
+ if [ -n "$pretty" ]; then
132
+ extended_syntax_flag="-x"
133
+ formatter="bats-format-tap-stream"
134
+ else
135
+ extended_syntax_flag=""
136
+ formatter="cat"
137
+ fi
138
+
139
+ set -o pipefail execfail
140
+ exec "$command" $count_flag $extended_syntax_flag "${filenames[@]}" | "$formatter"
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env bash
2
+ set -e
3
+
4
+ count_only_flag=""
5
+ if [ "$1" = "-c" ]; then
6
+ count_only_flag=1
7
+ shift
8
+ fi
9
+
10
+ extended_syntax_flag=""
11
+ if [ "$1" = "-x" ]; then
12
+ extended_syntax_flag="-x"
13
+ shift
14
+ fi
15
+
16
+ trap "kill 0; exit 1" int
17
+
18
+ count=0
19
+ for filename in "$@"; do
20
+ count=$(($count + $(bats-exec-test -c "$filename")))
21
+ done
22
+
23
+ if [ -n "$count_only_flag" ]; then
24
+ echo "$count"
25
+ exit
26
+ fi
27
+
28
+ echo "1..$count"
29
+ status=0
30
+ offset=0
31
+ for filename in "$@"; do
32
+ index=0
33
+ {
34
+ IFS= read -r # 1..n
35
+ while IFS= read -r line; do
36
+ case "$line" in
37
+ "begin "* )
38
+ index=$(($index + 1))
39
+ echo "${line/ $index / $(($offset + $index)) }"
40
+ ;;
41
+ "ok "* | "not ok "* )
42
+ [ -n "$extended_syntax_flag" ] || index=$(($index + 1))
43
+ echo "${line/ $index / $(($offset + $index)) }"
44
+ [ "${line:0:6}" != "not ok" ] || status=1
45
+ ;;
46
+ * )
47
+ echo "$line"
48
+ ;;
49
+ esac
50
+ done
51
+ } < <( bats-exec-test $extended_syntax_flag "$filename" )
52
+ offset=$(($offset + $index))
53
+ done
54
+
55
+ exit "$status"
@@ -0,0 +1,205 @@
1
+ #!/usr/bin/env bash
2
+ set -e
3
+ set -E
4
+ set -T
5
+
6
+ BATS_COUNT_ONLY=""
7
+ if [ "$1" = "-c" ]; then
8
+ BATS_COUNT_ONLY=1
9
+ shift
10
+ fi
11
+
12
+ BATS_EXTENDED_SYNTAX=""
13
+ if [ "$1" = "-x" ]; then
14
+ BATS_EXTENDED_SYNTAX="$1"
15
+ shift
16
+ fi
17
+
18
+ BATS_TEST_FILENAME="$1"
19
+ if [ -z "$BATS_TEST_FILENAME" ]; then
20
+ echo "usage: bats-exec <filename>" >&2
21
+ exit 1
22
+ elif [ ! -f "$BATS_TEST_FILENAME" ]; then
23
+ echo "bats: $BATS_TEST_FILENAME does not exist" >&2
24
+ exit 1
25
+ else
26
+ shift
27
+ fi
28
+
29
+ BATS_TEST_DIRNAME="$(dirname "$BATS_TEST_FILENAME")"
30
+ BATS_TEST_NAMES=()
31
+
32
+ load() {
33
+ local filename="$BATS_TEST_DIRNAME/$1.bash"
34
+ [ -f "$filename" ] || {
35
+ echo "bats: $filename does not exist" >&2
36
+ exit 1
37
+ }
38
+
39
+ source "$BATS_TEST_DIRNAME/$1.bash"
40
+ }
41
+
42
+ run() {
43
+ local e E T
44
+ [[ ! "$-" =~ e ]] || e=1
45
+ [[ ! "$-" =~ E ]] || E=1
46
+ [[ ! "$-" =~ T ]] || T=1
47
+ set +e
48
+ set +E
49
+ set +T
50
+ output="$("$@" 2>&1)"
51
+ status="$?"
52
+ IFS=$'\n' lines=($output)
53
+ [ -z "$e" ] || set -e
54
+ [ -z "$E" ] || set -E
55
+ [ -z "$T" ] || set -T
56
+ }
57
+
58
+ setup() {
59
+ true
60
+ }
61
+
62
+ teardown() {
63
+ true
64
+ }
65
+
66
+ skip() {
67
+ BATS_TEST_SKIPPED=${1:-1}
68
+ BATS_TEST_COMPLETED=1
69
+ exit 0
70
+ }
71
+
72
+ bats_test_begin() {
73
+ BATS_TEST_DESCRIPTION="$1"
74
+ if [ -n "$BATS_EXTENDED_SYNTAX" ]; then
75
+ echo "begin $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
76
+ fi
77
+ setup
78
+ }
79
+
80
+ bats_test_function() {
81
+ local test_name="$1"
82
+ BATS_TEST_NAMES["${#BATS_TEST_NAMES[@]}"]="$test_name"
83
+ }
84
+
85
+ bats_debug_trap() {
86
+ if [ "$BASH_SOURCE" != "$1" ]; then
87
+ BATS_LINE_NUMBER="$BATS_LINE_NUMBER_"
88
+ BATS_LINE_NUMBER_="$2"
89
+ fi
90
+ }
91
+
92
+ bats_error_trap() {
93
+ trap - debug
94
+ }
95
+
96
+ bats_teardown_trap() {
97
+ trap bats_exit_trap exit
98
+ teardown >>"$BATS_OUT" 2>&1
99
+ bats_exit_trap
100
+ }
101
+
102
+ bats_exit_trap() {
103
+ local status
104
+ local skipped
105
+ trap - err exit
106
+
107
+ skipped=""
108
+ if [ -n "$BATS_TEST_SKIPPED" ]; then
109
+ skipped=" # skip"
110
+ if [ "1" != "$BATS_TEST_SKIPPED" ]; then
111
+ skipped+=" ($BATS_TEST_SKIPPED)"
112
+ fi
113
+ fi
114
+
115
+ if [ -z "$BATS_TEST_COMPLETED" ]; then
116
+ echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
117
+ echo "# (in test file $BATS_TEST_FILENAME, line $BATS_LINE_NUMBER)" >&3
118
+ sed -e "s/^/# /" < "$BATS_OUT" >&3
119
+ status=1
120
+ else
121
+ echo "ok ${BATS_TEST_NUMBER}${skipped} ${BATS_TEST_DESCRIPTION}" >&3
122
+ status=0
123
+ fi
124
+
125
+ rm -f "$BATS_OUT"
126
+ exit "$status"
127
+ }
128
+
129
+ bats_perform_tests() {
130
+ echo "1..$#"
131
+ test_number=1
132
+ status=0
133
+ for test_name in "$@"; do
134
+ "$0" $BATS_EXTENDED_SYNTAX "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=1
135
+ test_number=$(($test_number + 1))
136
+ done
137
+ exit "$status"
138
+ }
139
+
140
+ bats_perform_test() {
141
+ BATS_TEST_NAME="$1"
142
+ if [ "$(type -t "$BATS_TEST_NAME" || true)" = "function" ]; then
143
+ BATS_TEST_NUMBER="$2"
144
+ if [ -z "$BATS_TEST_NUMBER" ]; then
145
+ echo "1..1"
146
+ BATS_TEST_NUMBER="1"
147
+ fi
148
+
149
+ BATS_TEST_COMPLETED=""
150
+ BATS_ERROR_LINE=""
151
+ trap "bats_debug_trap \"\$BASH_SOURCE\" \$LINENO" debug
152
+ trap "bats_error_trap" err
153
+ trap "bats_teardown_trap" exit
154
+ "$BATS_TEST_NAME" >>"$BATS_OUT" 2>&1
155
+ BATS_TEST_COMPLETED=1
156
+
157
+ else
158
+ echo "bats: unknown test name \`$BATS_TEST_NAME'" >&2
159
+ exit 1
160
+ fi
161
+ }
162
+
163
+ if [ -z "$TMPDIR" ]; then
164
+ BATS_TMPDIR="/tmp"
165
+ else
166
+ BATS_TMPDIR="${TMPDIR%/}"
167
+ fi
168
+
169
+ BATS_TMPNAME="$BATS_TMPDIR/bats.$$"
170
+ BATS_PARENT_TMPNAME="$BATS_TMPDIR/bats.$PPID"
171
+ BATS_OUT="${BATS_TMPNAME}.out"
172
+
173
+ bats_preprocess_source() {
174
+ BATS_TEST_SOURCE="${BATS_TMPNAME}.src"
175
+ { tr -d '\r' < "$BATS_TEST_FILENAME"; echo; } | bats-preprocess > "$BATS_TEST_SOURCE"
176
+ trap bats_cleanup_preprocessed_source err exit
177
+ trap "bats_cleanup_preprocessed_source; exit 1" int
178
+ }
179
+
180
+ bats_cleanup_preprocessed_source() {
181
+ rm -f "$BATS_TEST_SOURCE"
182
+ }
183
+
184
+ bats_evaluate_preprocessed_source() {
185
+ if [ -z "$BATS_TEST_SOURCE" ]; then
186
+ BATS_TEST_SOURCE="${BATS_PARENT_TMPNAME}.src"
187
+ fi
188
+ source "$BATS_TEST_SOURCE"
189
+ }
190
+
191
+ exec 3<&1
192
+
193
+ if [ "$#" -eq 0 ]; then
194
+ bats_preprocess_source
195
+ bats_evaluate_preprocessed_source
196
+
197
+ if [ -n "$BATS_COUNT_ONLY" ]; then
198
+ echo "${#BATS_TEST_NAMES[@]}"
199
+ else
200
+ bats_perform_tests "${BATS_TEST_NAMES[@]}"
201
+ fi
202
+ else
203
+ bats_evaluate_preprocessed_source
204
+ bats_perform_test "$@"
205
+ fi
@@ -0,0 +1,158 @@
1
+ #!/usr/bin/env bash
2
+ set -e
3
+
4
+ # Just stream the TAP output (sans extended syntax) if tput is missing
5
+ command -v tput >/dev/null || exec grep -v "^begin "
6
+
7
+ header_pattern='[0-9]+\.\.[0-9]+'
8
+ IFS= read -r header
9
+
10
+ if [[ "$header" =~ $header_pattern ]]; then
11
+ count="${header:3}"
12
+ index=0
13
+ failures=0
14
+ name=""
15
+ count_column_width=$(( ${#count} * 2 + 2 ))
16
+ else
17
+ # If the first line isn't a TAP plan, print it and pass the rest through
18
+ printf "%s\n" "$header"
19
+ exec cat
20
+ fi
21
+
22
+ update_screen_width() {
23
+ screen_width="$(tput cols)"
24
+ count_column_left=$(( $screen_width - $count_column_width ))
25
+ }
26
+
27
+ trap update_screen_width WINCH
28
+ update_screen_width
29
+
30
+ begin() {
31
+ go_to_column 0
32
+ printf_with_truncation $(( $count_column_left - 1 )) " %s" "$name"
33
+ clear_to_end_of_line
34
+ go_to_column $count_column_left
35
+ printf "%${#count}s/${count}" "$index"
36
+ go_to_column 1
37
+ }
38
+
39
+ pass() {
40
+ go_to_column 0
41
+ printf " ✓ %s" "$name"
42
+ advance
43
+ }
44
+
45
+ skip() {
46
+ local reason="$1"
47
+ [ -z "$reason" ] || reason=": $reason"
48
+ go_to_column 0
49
+ printf " - %s (skipped%s)" "$name" "$reason"
50
+ advance
51
+ }
52
+
53
+ fail() {
54
+ go_to_column 0
55
+ set_color 1 bold
56
+ printf " ✗ %s" "$name"
57
+ advance
58
+ }
59
+
60
+ log() {
61
+ set_color 1
62
+ printf " %s\n" "$1"
63
+ clear_color
64
+ }
65
+
66
+ summary() {
67
+ printf "\n%d test%s, %d failure%s\n" \
68
+ "$count" "$(plural "$count")" \
69
+ "$failures" "$(plural "$failures")"
70
+ }
71
+
72
+ printf_with_truncation() {
73
+ local width="$1"
74
+ shift
75
+ local string="$(printf "$@")"
76
+
77
+ if [ "${#string}" -gt "$width" ]; then
78
+ printf "%s..." "${string:0:$(( $width - 4 ))}"
79
+ else
80
+ printf "%s" "$string"
81
+ fi
82
+ }
83
+
84
+ go_to_column() {
85
+ local column="$1"
86
+ printf "\x1B[%dG" $(( $column + 1 ))
87
+ }
88
+
89
+ clear_to_end_of_line() {
90
+ printf "\x1B[K"
91
+ }
92
+
93
+ advance() {
94
+ clear_to_end_of_line
95
+ echo
96
+ clear_color
97
+ }
98
+
99
+ set_color() {
100
+ local color="$1"
101
+ local weight="$2"
102
+ printf "\x1B[%d;%dm" $(( 30 + $color )) "$( [ "$weight" = "bold" ] && echo 1 || echo 22 )"
103
+ }
104
+
105
+ clear_color() {
106
+ printf "\x1B[0m"
107
+ }
108
+
109
+ plural() {
110
+ [ "$1" -eq 1 ] || echo "s"
111
+ }
112
+
113
+ _buffer=""
114
+
115
+ buffer() {
116
+ _buffer="${_buffer}$("$@")"
117
+ }
118
+
119
+ flush() {
120
+ printf "%s" "$_buffer"
121
+ _buffer=""
122
+ }
123
+
124
+ finish() {
125
+ flush
126
+ printf "\n"
127
+ }
128
+
129
+ trap finish EXIT
130
+
131
+ while IFS= read -r line; do
132
+ case "$line" in
133
+ "begin "* )
134
+ index=$(( $index + 1 ))
135
+ name="${line#* $index }"
136
+ buffer begin
137
+ flush
138
+ ;;
139
+ "ok "* )
140
+ skip_expr="ok $index # skip (\(([^)]*)\))?"
141
+ if [[ "$line" =~ $skip_expr ]]; then
142
+ buffer skip "${BASH_REMATCH[2]}"
143
+ else
144
+ buffer pass
145
+ fi
146
+ ;;
147
+ "not ok "* )
148
+ failures=$(( $failures + 1 ))
149
+ buffer fail
150
+ ;;
151
+ "# "* )
152
+
153
+ buffer log "${line:2}"
154
+ ;;
155
+ esac
156
+ done
157
+
158
+ buffer summary