busser-bats 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1 +1 @@
1
- https://github.com/sstephenson/bats/archive/v0.3.1.tar.gz
1
+ https://github.com/sstephenson/bats/archive/v0.4.0.tar.gz
@@ -2,7 +2,7 @@
2
2
  set -e
3
3
 
4
4
  version() {
5
- echo "Bats 0.3.1"
5
+ echo "Bats 0.4.0"
6
6
  }
7
7
 
8
8
  usage() {
@@ -54,6 +54,7 @@ expand_path() {
54
54
 
55
55
  BATS_LIBEXEC="$(abs_dirname "$0")"
56
56
  export BATS_PREFIX="$(abs_dirname "$BATS_LIBEXEC")"
57
+ export BATS_CWD="$(abs_dirname .)"
57
58
  export PATH="$BATS_LIBEXEC:$PATH"
58
59
 
59
60
  options=()
@@ -67,7 +68,7 @@ for arg in "$@"; do
67
68
  while option="${arg:$index:1}"; do
68
69
  [ -n "$option" ] || break
69
70
  options[${#options[*]}]="$option"
70
- index=$(($index+1))
71
+ let index+=1
71
72
  done
72
73
  fi
73
74
  else
@@ -77,6 +78,7 @@ done
77
78
 
78
79
  unset count_flag pretty
79
80
  [ -t 0 ] && [ -t 1 ] && pretty="1"
81
+ [ -n "$CI" ] && pretty=""
80
82
 
81
83
  for option in "${options[@]}"; do
82
84
  case "$option" in
@@ -28,8 +28,10 @@ if [ -z "$1" ]; then
28
28
  fi
29
29
 
30
30
  BATS_ROOT="$(abs_dirname "$0")"
31
- mkdir -p "$PREFIX"/{bin,libexec}
31
+ mkdir -p "$PREFIX"/{bin,libexec,share/man/man{1,7}}
32
32
  cp -R "$BATS_ROOT"/bin/* "$PREFIX"/bin
33
33
  cp -R "$BATS_ROOT"/libexec/* "$PREFIX"/libexec
34
+ cp "$BATS_ROOT"/man/bats.1 "$PREFIX"/share/man/man1
35
+ cp "$BATS_ROOT"/man/bats.7 "$PREFIX"/share/man/man7
34
36
 
35
37
  echo "Installed Bats to $PREFIX/bin/bats"
@@ -2,7 +2,7 @@
2
2
  set -e
3
3
 
4
4
  version() {
5
- echo "Bats 0.3.1"
5
+ echo "Bats 0.4.0"
6
6
  }
7
7
 
8
8
  usage() {
@@ -54,6 +54,7 @@ expand_path() {
54
54
 
55
55
  BATS_LIBEXEC="$(abs_dirname "$0")"
56
56
  export BATS_PREFIX="$(abs_dirname "$BATS_LIBEXEC")"
57
+ export BATS_CWD="$(abs_dirname .)"
57
58
  export PATH="$BATS_LIBEXEC:$PATH"
58
59
 
59
60
  options=()
@@ -67,7 +68,7 @@ for arg in "$@"; do
67
68
  while option="${arg:$index:1}"; do
68
69
  [ -n "$option" ] || break
69
70
  options[${#options[*]}]="$option"
70
- index=$(($index+1))
71
+ let index+=1
71
72
  done
72
73
  fi
73
74
  else
@@ -77,6 +78,7 @@ done
77
78
 
78
79
  unset count_flag pretty
79
80
  [ -t 0 ] && [ -t 1 ] && pretty="1"
81
+ [ -n "$CI" ] && pretty=""
80
82
 
81
83
  for option in "${options[@]}"; do
82
84
  case "$option" in
@@ -17,7 +17,7 @@ trap "kill 0; exit 1" int
17
17
 
18
18
  count=0
19
19
  for filename in "$@"; do
20
- count=$(($count + $(bats-exec-test -c "$filename")))
20
+ let count+="$(bats-exec-test -c "$filename")"
21
21
  done
22
22
 
23
23
  if [ -n "$count_only_flag" ]; then
@@ -35,11 +35,11 @@ for filename in "$@"; do
35
35
  while IFS= read -r line; do
36
36
  case "$line" in
37
37
  "begin "* )
38
- index=$(($index + 1))
38
+ let index+=1
39
39
  echo "${line/ $index / $(($offset + $index)) }"
40
40
  ;;
41
41
  "ok "* | "not ok "* )
42
- [ -n "$extended_syntax_flag" ] || index=$(($index + 1))
42
+ [ -n "$extended_syntax_flag" ] || let index+=1
43
43
  echo "${line/ $index / $(($offset + $index)) }"
44
44
  [ "${line:0:6}" != "not ok" ] || status=1
45
45
  ;;
@@ -30,13 +30,21 @@ BATS_TEST_DIRNAME="$(dirname "$BATS_TEST_FILENAME")"
30
30
  BATS_TEST_NAMES=()
31
31
 
32
32
  load() {
33
- local filename="$BATS_TEST_DIRNAME/$1.bash"
33
+ local name="$1"
34
+ local filename
35
+
36
+ if [ "${name:0:1}" = "/" ]; then
37
+ filename="${name}"
38
+ else
39
+ filename="$BATS_TEST_DIRNAME/${name}.bash"
40
+ fi
41
+
34
42
  [ -f "$filename" ] || {
35
43
  echo "bats: $filename does not exist" >&2
36
44
  exit 1
37
45
  }
38
46
 
39
- source "$BATS_TEST_DIRNAME/$1.bash"
47
+ source "${filename}"
40
48
  }
41
49
 
42
50
  run() {
@@ -82,20 +90,150 @@ bats_test_function() {
82
90
  BATS_TEST_NAMES["${#BATS_TEST_NAMES[@]}"]="$test_name"
83
91
  }
84
92
 
93
+ bats_capture_stack_trace() {
94
+ BATS_PREVIOUS_STACK_TRACE=( "${BATS_CURRENT_STACK_TRACE[@]}" )
95
+ BATS_CURRENT_STACK_TRACE=()
96
+
97
+ local test_pattern=" $BATS_TEST_NAME $BATS_TEST_SOURCE"
98
+ local setup_pattern=" setup $BATS_TEST_SOURCE"
99
+ local teardown_pattern=" teardown $BATS_TEST_SOURCE"
100
+
101
+ local frame
102
+ local index=1
103
+
104
+ while frame="$(caller "$index")"; do
105
+ BATS_CURRENT_STACK_TRACE["${#BATS_CURRENT_STACK_TRACE[@]}"]="$frame"
106
+ if [[ "$frame" = *"$test_pattern" || \
107
+ "$frame" = *"$setup_pattern" || \
108
+ "$frame" = *"$teardown_pattern" ]]; then
109
+ break
110
+ else
111
+ let index+=1
112
+ fi
113
+ done
114
+
115
+ BATS_SOURCE="$(bats_frame_filename "${BATS_CURRENT_STACK_TRACE[0]}")"
116
+ BATS_LINENO="$(bats_frame_lineno "${BATS_CURRENT_STACK_TRACE[0]}")"
117
+ }
118
+
119
+ bats_print_stack_trace() {
120
+ local frame
121
+ local index=1
122
+ local count="${#@}"
123
+
124
+ for frame in "$@"; do
125
+ local filename="$(bats_trim_filename "$(bats_frame_filename "$frame")")"
126
+ local lineno="$(bats_frame_lineno "$frame")"
127
+
128
+ if [ $index -eq 1 ]; then
129
+ echo -n "# ("
130
+ else
131
+ echo -n "# "
132
+ fi
133
+
134
+ local fn="$(bats_frame_function "$frame")"
135
+ if [ "$fn" != "$BATS_TEST_NAME" ]; then
136
+ echo -n "from function \`$fn' "
137
+ fi
138
+
139
+ if [ $index -eq $count ]; then
140
+ echo "in test file $filename, line $lineno)"
141
+ else
142
+ echo "in file $filename, line $lineno,"
143
+ fi
144
+
145
+ let index+=1
146
+ done
147
+ }
148
+
149
+ bats_print_failed_command() {
150
+ local frame="$1"
151
+ local status="$2"
152
+ local filename="$(bats_frame_filename "$frame")"
153
+ local lineno="$(bats_frame_lineno "$frame")"
154
+
155
+ local failed_line="$(bats_extract_line "$filename" "$lineno")"
156
+ local failed_command="$(bats_strip_string "$failed_line")"
157
+ echo -n "# \`${failed_command}' "
158
+
159
+ if [ $status -eq 1 ]; then
160
+ echo "failed"
161
+ else
162
+ echo "failed with status $status"
163
+ fi
164
+ }
165
+
166
+ bats_frame_lineno() {
167
+ local frame="$1"
168
+ local lineno="${frame%% *}"
169
+ echo "$lineno"
170
+ }
171
+
172
+ bats_frame_function() {
173
+ local frame="$1"
174
+ local rest="${frame#* }"
175
+ local fn="${rest%% *}"
176
+ echo "$fn"
177
+ }
178
+
179
+ bats_frame_filename() {
180
+ local frame="$1"
181
+ local rest="${frame#* }"
182
+ local filename="${rest#* }"
183
+
184
+ if [ "$filename" = "$BATS_TEST_SOURCE" ]; then
185
+ echo "$BATS_TEST_FILENAME"
186
+ else
187
+ echo "$filename"
188
+ fi
189
+ }
190
+
191
+ bats_extract_line() {
192
+ local filename="$1"
193
+ local lineno="$2"
194
+ sed -n "${lineno}p" "$filename"
195
+ }
196
+
197
+ bats_strip_string() {
198
+ local string="$1"
199
+ printf "%s" "$string" | sed -e "s/^[ "$'\t'"]*//" -e "s/[ "$'\t'"]*$//"
200
+ }
201
+
202
+ bats_trim_filename() {
203
+ local filename="$1"
204
+ local length="${#BATS_CWD}"
205
+
206
+ if [ "${filename:0:length+1}" = "${BATS_CWD}/" ]; then
207
+ echo "${filename:length+1}"
208
+ else
209
+ echo "$filename"
210
+ fi
211
+ }
212
+
85
213
  bats_debug_trap() {
86
214
  if [ "$BASH_SOURCE" != "$1" ]; then
87
- BATS_LINE_NUMBER="$BATS_LINE_NUMBER_"
88
- BATS_LINE_NUMBER_="$2"
215
+ bats_capture_stack_trace
89
216
  fi
90
217
  }
91
218
 
92
219
  bats_error_trap() {
220
+ BATS_ERROR_STATUS="$?"
221
+ BATS_ERROR_STACK_TRACE=( "${BATS_PREVIOUS_STACK_TRACE[@]}" )
93
222
  trap - debug
94
223
  }
95
224
 
96
225
  bats_teardown_trap() {
97
- trap bats_exit_trap exit
98
- teardown >>"$BATS_OUT" 2>&1
226
+ trap "bats_exit_trap" exit
227
+ local status=0
228
+ teardown >>"$BATS_OUT" 2>&1 || status="$?"
229
+
230
+ if [ $status -eq 0 ]; then
231
+ BATS_TEARDOWN_COMPLETED=1
232
+ elif [ -n "$BATS_TEST_COMPLETED" ]; then
233
+ BATS_ERROR_STATUS="$status"
234
+ BATS_ERROR_STACK_TRACE=( "${BATS_CURRENT_STACK_TRACE[@]}" )
235
+ fi
236
+
99
237
  bats_exit_trap
100
238
  }
101
239
 
@@ -112,9 +250,10 @@ bats_exit_trap() {
112
250
  fi
113
251
  fi
114
252
 
115
- if [ -z "$BATS_TEST_COMPLETED" ]; then
253
+ if [ -z "$BATS_TEST_COMPLETED" ] || [ -z "$BATS_TEARDOWN_COMPLETED" ]; then
116
254
  echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3
117
- echo "# (in test file $BATS_TEST_FILENAME, line $BATS_LINE_NUMBER)" >&3
255
+ bats_print_stack_trace "${BATS_ERROR_STACK_TRACE[@]}" >&3
256
+ bats_print_failed_command "${BATS_ERROR_STACK_TRACE[${#BATS_ERROR_STACK_TRACE[@]}-1]}" "$BATS_ERROR_STATUS" >&3
118
257
  sed -e "s/^/# /" < "$BATS_OUT" >&3
119
258
  status=1
120
259
  else
@@ -132,7 +271,7 @@ bats_perform_tests() {
132
271
  status=0
133
272
  for test_name in "$@"; do
134
273
  "$0" $BATS_EXTENDED_SYNTAX "$BATS_TEST_FILENAME" "$test_name" "$test_number" || status=1
135
- test_number=$(($test_number + 1))
274
+ let test_number+=1
136
275
  done
137
276
  exit "$status"
138
277
  }
@@ -147,8 +286,8 @@ bats_perform_test() {
147
286
  fi
148
287
 
149
288
  BATS_TEST_COMPLETED=""
150
- BATS_ERROR_LINE=""
151
- trap "bats_debug_trap \"\$BASH_SOURCE\" \$LINENO" debug
289
+ BATS_TEARDOWN_COMPLETED=""
290
+ trap "bats_debug_trap \"\$BASH_SOURCE\"" debug
152
291
  trap "bats_error_trap" err
153
292
  trap "bats_teardown_trap" exit
154
293
  "$BATS_TEST_NAME" >>"$BATS_OUT" 2>&1
@@ -173,7 +312,7 @@ BATS_OUT="${BATS_TMPNAME}.out"
173
312
  bats_preprocess_source() {
174
313
  BATS_TEST_SOURCE="${BATS_TMPNAME}.src"
175
314
  { tr -d '\r' < "$BATS_TEST_FILENAME"; echo; } | bats-preprocess > "$BATS_TEST_SOURCE"
176
- trap bats_cleanup_preprocessed_source err exit
315
+ trap "bats_cleanup_preprocessed_source" err exit
177
316
  trap "bats_cleanup_preprocessed_source; exit 1" int
178
317
  }
179
318
 
@@ -11,6 +11,7 @@ if [[ "$header" =~ $header_pattern ]]; then
11
11
  count="${header:3}"
12
12
  index=0
13
13
  failures=0
14
+ skipped=0
14
15
  name=""
15
16
  count_column_width=$(( ${#count} * 2 + 2 ))
16
17
  else
@@ -64,9 +65,15 @@ log() {
64
65
  }
65
66
 
66
67
  summary() {
67
- printf "\n%d test%s, %d failure%s\n" \
68
- "$count" "$(plural "$count")" \
69
- "$failures" "$(plural "$failures")"
68
+ printf "\n%d test%s" "$count" "$(plural "$count")"
69
+
70
+ printf ", %d failure%s" "$failures" "$(plural "$failures")"
71
+
72
+ if [ "$skipped" -gt 0 ]; then
73
+ printf ", %d skipped" "$skipped"
74
+ fi
75
+
76
+ printf "\n"
70
77
  }
71
78
 
72
79
  printf_with_truncation() {
@@ -131,7 +138,7 @@ trap finish EXIT
131
138
  while IFS= read -r line; do
132
139
  case "$line" in
133
140
  "begin "* )
134
- index=$(( $index + 1 ))
141
+ let index+=1
135
142
  name="${line#* $index }"
136
143
  buffer begin
137
144
  flush
@@ -139,17 +146,17 @@ while IFS= read -r line; do
139
146
  "ok "* )
140
147
  skip_expr="ok $index # skip (\(([^)]*)\))?"
141
148
  if [[ "$line" =~ $skip_expr ]]; then
149
+ let skipped+=1
142
150
  buffer skip "${BASH_REMATCH[2]}"
143
151
  else
144
152
  buffer pass
145
153
  fi
146
154
  ;;
147
155
  "not ok "* )
148
- failures=$(( $failures + 1 ))
156
+ let failures+=1
149
157
  buffer fail
150
158
  ;;
151
159
  "# "* )
152
-
153
160
  buffer log "${line:2}"
154
161
  ;;
155
162
  esac
@@ -31,16 +31,17 @@ encode_name() {
31
31
 
32
32
  tests=()
33
33
  index=0
34
+ pattern='^ *@test *([^ ].*) *\{ *(.*)$'
34
35
 
35
36
  while IFS= read -r line; do
36
- index=$(($index + 1))
37
- quoted_name="$(expr "$line" : ' *@test *\([^ ].*\) *{ *$' || true)"
38
-
39
- if [ -n "$quoted_name" ]; then
37
+ let index+=1
38
+ if [[ "$line" =~ $pattern ]]; then
39
+ quoted_name="${BASH_REMATCH[1]}"
40
+ body="${BASH_REMATCH[2]}"
40
41
  name="$(eval echo "$quoted_name")"
41
42
  encoded_name="$(encode_name "$name")"
42
43
  tests["${#tests[@]}"]="$encoded_name"
43
- echo "${encoded_name}() { bats_test_begin ${quoted_name} ${index}"
44
+ echo "${encoded_name}() { bats_test_begin ${quoted_name} ${index}; ${body}"
44
45
  else
45
46
  printf "%s\n" "$line"
46
47
  fi
@@ -0,0 +1,10 @@
1
+ RONN := ronn
2
+ PAGES := bats.1 bats.7
3
+
4
+ all: $(PAGES)
5
+
6
+ bats.1: bats.1.ronn
7
+ $(RONN) -r $<
8
+
9
+ bats.7: bats.7.ronn
10
+ $(RONN) -r $<
@@ -0,0 +1,5 @@
1
+ Bats man pages are generated with [Ronn](http://rtomayko.github.io/ronn/).
2
+
3
+ After making changes to `bats.1.ronn` or `bats.7.ronn`, run `make` in
4
+ this directory to generate `bats.1` and `bats.7`. **Do not edit the
5
+ `bats.1` or `bats.7` files directly.**
@@ -0,0 +1,101 @@
1
+ .\" generated with Ronn/v0.7.3
2
+ .\" http://github.com/rtomayko/ronn/tree/0.7.3
3
+ .
4
+ .TH "BATS" "1" "August 2014" "" ""
5
+ .
6
+ .SH "NAME"
7
+ \fBbats\fR \- Bash Automated Testing System
8
+ .
9
+ .SH "SYNOPSIS"
10
+ bats [\-c] [\-p | \-t] \fItest\fR [\fItest\fR \.\.\.]
11
+ .
12
+ .P
13
+ \fItest\fR is the path to a Bats test file, or the path to a directory containing Bats test files\.
14
+ .
15
+ .SH "DESCRIPTION"
16
+ Bats is a TAP\-compliant testing framework for Bash\. It provides a simple way to verify that the UNIX programs you write behave as expected\.
17
+ .
18
+ .P
19
+ A Bats test file is a Bash script with special syntax for defining test cases\. Under the hood, each test case is just a function with a description\.
20
+ .
21
+ .P
22
+ Test cases consist of standard shell commands\. Bats makes use of Bash\'s \fBerrexit\fR (\fBset \-e\fR) option when running test cases\. If every command in the test case exits with a \fB0\fR status code (success), the test passes\. In this way, each line is an assertion of truth\.
23
+ .
24
+ .P
25
+ See \fBbats\fR(7) for more information on writing Bats tests\.
26
+ .
27
+ .SH "RUNNING TESTS"
28
+ To run your tests, invoke the \fBbats\fR interpreter with a path to a test file\. The file\'s test cases are run sequentially and in isolation\. If all the test cases pass, \fBbats\fR exits with a \fB0\fR status code\. If there are any failures, \fBbats\fR exits with a \fB1\fR status code\.
29
+ .
30
+ .P
31
+ You can invoke the \fBbats\fR interpreter with multiple test file arguments, or with a path to a directory containing multiple \fB\.bats\fR files\. Bats will run each test file individually and aggregate the results\. If any test case fails, \fBbats\fR exits with a \fB1\fR status code\.
32
+ .
33
+ .SH "OPTIONS"
34
+ .
35
+ .TP
36
+ \fB\-c\fR, \fB\-\-count\fR
37
+ Count the number of test cases without running any tests
38
+ .
39
+ .TP
40
+ \fB\-h\fR, \fB\-\-help\fR
41
+ Display help message
42
+ .
43
+ .TP
44
+ \fB\-p\fR, \fB\-\-pretty\fR
45
+ Show results in pretty format (default for terminals)
46
+ .
47
+ .TP
48
+ \fB\-t\fR, \fB\-\-tap\fR
49
+ Show results in TAP format
50
+ .
51
+ .TP
52
+ \fB\-v\fR, \fB\-\-version\fR
53
+ Display the version number
54
+ .
55
+ .SH "OUTPUT"
56
+ When you run Bats from a terminal, you\'ll see output as each test is performed, with a check\-mark next to the test\'s name if it passes or an "X" if it fails\.
57
+ .
58
+ .IP "" 4
59
+ .
60
+ .nf
61
+
62
+ $ bats addition\.bats
63
+ ✓ addition using bc
64
+ ✓ addition using dc
65
+
66
+ 2 tests, 0 failures
67
+ .
68
+ .fi
69
+ .
70
+ .IP "" 0
71
+ .
72
+ .P
73
+ If Bats is not connected to a terminal\-\-in other words, if you run it from a continuous integration system or redirect its output to a file\-\-the results are displayed in human\-readable, machine\-parsable TAP format\. You can force TAP output from a terminal by invoking Bats with the \fB\-\-tap\fR option\.
74
+ .
75
+ .IP "" 4
76
+ .
77
+ .nf
78
+
79
+ $ bats \-\-tap addition\.bats
80
+ 1\.\.2
81
+ ok 1 addition using bc
82
+ ok 2 addition using dc
83
+ .
84
+ .fi
85
+ .
86
+ .IP "" 0
87
+ .
88
+ .SH "EXIT STATUS"
89
+ The \fBbats\fR interpreter exits with a value of \fB0\fR if all test cases pass, or \fB1\fR if one or more test cases fail\.
90
+ .
91
+ .SH "SEE ALSO"
92
+ Bats wiki: \fIhttps://github\.com/sstephenson/bats/wiki/\fR
93
+ .
94
+ .P
95
+ \fBbash\fR(1), \fBbats\fR(7)
96
+ .
97
+ .SH "COPYRIGHT"
98
+ (c) 2014 Sam Stephenson
99
+ .
100
+ .P
101
+ Bats is released under the terms of an MIT\-style license\.