@bats-hardened/bats 1.14.1-alpha02

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of @bats-hardened/bats might be problematic. Click here for more details.

@@ -0,0 +1,404 @@
1
+ bats(7) -- Bats test file format
2
+ ================================
3
+
4
+
5
+ DESCRIPTION
6
+ -----------
7
+
8
+ A Bats test file is a Bash script with special syntax for defining
9
+ test cases. Under the hood, each test case is just a function with a
10
+ description.
11
+
12
+ #!/usr/bin/env bats
13
+
14
+ @test "addition using bc" {
15
+ result="$(echo 2+2 | bc)"
16
+ [ "$result" -eq 4 ]
17
+ }
18
+
19
+ @test "addition using dc" {
20
+ result="$(echo 2 2+p | dc)"
21
+ [ "$result" -eq 4 ]
22
+ }
23
+
24
+
25
+ Each Bats test file is evaluated n+1 times, where _n_ is the number of
26
+ test cases in the file. The first run counts the number of test cases,
27
+ then iterates over the test cases and executes each one in its own
28
+ process.
29
+
30
+
31
+ Tagging tests
32
+ -------------
33
+
34
+ Each test has a list of tags attached to it. Without specification, this list is empty.
35
+ Tags can be defined in two ways. The first being `# bats test_tags=`:
36
+
37
+ # bats test_tags=tag:1, tag:2, tag:3
38
+ @test "second test" {
39
+ # ...
40
+ }
41
+
42
+ @test "second test" {
43
+ # ...
44
+ }
45
+
46
+ These tags (`tag:1`, `tag:2`, `tag:3`) will be attached to the test `first test`.
47
+ The second test will have no tags attached. Values defined in the `# bats test_tags=`
48
+ directive will be assigned to the next `@test` that is being encountered in the
49
+ file and forgotten after that. Only the value of the last `# bats test_tags=` directive
50
+ before a given test will be used.
51
+
52
+ Sometimes, we want to give all tests in a file a set of the same tags. This can
53
+ be achieved via `# bats file_tags=`. They will be added to all tests in the file
54
+ after that directive. An additional `# bats file_tags=` directive will override
55
+ the previously defined values:
56
+
57
+ @test "Zeroth test" {
58
+ # will have no tags
59
+ }
60
+
61
+ # bats file_tags=a:b
62
+ # bats test_tags=c:d
63
+
64
+ @test "First test" {
65
+ # will be tagged a:b, c:d
66
+ }
67
+
68
+ # bats file_tags=
69
+
70
+ @test "Second test" {
71
+ # will have no tags
72
+ }
73
+
74
+ Tags are case sensitive and must only consist of alphanumeric characters and `_`,
75
+ `-`, or `:`. They must not contain whitespaces!
76
+ The colon is intended as a separator for (recursive) namespacing.
77
+
78
+ Tag lists must be separated by commas and are allowed to contain whitespace.
79
+ They must not contain empty tags like `test_tags=,b` (first tag is empty),
80
+ `test_tags=a,,c`, `test_tags=a, ,c` (second tag is only whitespace/empty),
81
+ `test_tags=a,b,` (third tag is empty).
82
+
83
+ Every tag starting with `bats:` (case insensitive!) is reserved for Bats'
84
+ internal use:
85
+
86
+ * `bats:focus`:
87
+ If any test with the tag `bats:focus` is encountered in a test suite, only those tagged with this tag will be executed.
88
+
89
+ In focus mode, the exit code of successful runs will be overridden to 1 to prevent CI from silently running on a subset
90
+ of tests due to an accidentally committed `bats:focus` tag.
91
+ Should you require the true exit code, e.g. for a `git bisect` operation, you can disable this behavior by setting
92
+ `BATS_NO_FAIL_FOCUS_RUN=1` when running `bats`, but make sure not to commit this to CI!
93
+
94
+ THE RUN HELPER
95
+ --------------
96
+
97
+ Usage: run [OPTIONS] [--] <command...>
98
+ Options:
99
+ ! check for non zero exit code
100
+ -<N> check that exit code is <N>
101
+ --separate-stderr
102
+ split stderr and stdout
103
+ --keep-empty-lines
104
+ retain empty lines in `${lines[@]}`/`${stderr_lines[@]}`
105
+
106
+ Many Bats tests need to run a command and then make assertions about
107
+ its exit status and output. Bats includes a `run` helper that invokes
108
+ its arguments as a command, saves the exit status and output into
109
+ special global variables, and (optionally) checks exit status against
110
+ a given expected value. If successful, `run` returns with a `0` status
111
+ code so you can continue to make assertions in your test case.
112
+
113
+ For example, let's say you're testing that the `foo` command, when
114
+ passed a nonexistent filename, exits with a `1` status code and prints
115
+ an error message.
116
+
117
+ @test "invoking foo with a nonexistent file prints an error" {
118
+ run -1 foo nonexistent_filename
119
+ [ "$output" = "foo: no such file 'nonexistent_filename'" ]
120
+ }
121
+
122
+ The `-1` as first argument tells `run` to expect 1 as an exit
123
+ status, and to fail if the command exits with any other value.
124
+ On failure, both actual and expected values will be displayed,
125
+ along with the invoked command and its output:
126
+
127
+ (in test file test.bats, line 2)
128
+ `run -1 foo nonexistent_filename' failed, expected exit code 1, got 127
129
+
130
+ This error indicates a possible problem with the installation or
131
+ configuration of `foo`; note that a simple `[ $status != 0 ]`
132
+ test would not have caught this kind of failure.
133
+
134
+ The `$status` variable contains the status code of the command, and
135
+ the `$output` variable contains the combined contents of the command's
136
+ standard output and standard error streams.
137
+
138
+ A third special variable, the `$lines` array, is available for easily
139
+ accessing individual lines of output. For example, if you want to test
140
+ that invoking `foo` without any arguments prints usage information on
141
+ the first line:
142
+
143
+ @test "invoking foo without arguments prints usage" {
144
+ run -1 foo
145
+ [ "${lines[0]}" = "usage: foo <filename>" ]
146
+ }
147
+
148
+ By default `run` leaves out empty lines in `${lines[@]}`. Use `run --keep-empty-lines` to retain them.
149
+
150
+ Additionally, you can use `--separate-stderr` to split stdout and stderr
151
+ into `$output`/`$stderr` and `${lines[@]}`/`${stderr_lines[@]}`.
152
+
153
+ All additional parameters to run should come before the command.
154
+ If you want to run a command that starts with `-`, prefix it with `--` to
155
+ prevent `run` from parsing it as an option.
156
+
157
+ THE BATS_PIPE HELPER
158
+ --------------
159
+
160
+ Usage: bats_pipe [OPTIONS] [--] <command0...> [ \| <command1...> [ \| <command2...> [...] ] ]
161
+ Options:
162
+ -<N> return the exit code from the <N>th command in the chain
163
+ of piped commands, instead of default behavior of "the last
164
+ non-zero status".
165
+
166
+ The bats_pipe helper command is meant to handle piping between commands. Its
167
+ main purpose is to aide the `run` helper command (which cannot handle pipes,
168
+ due to bash parsing priority). `run command0 | command1` will parse `|` before
169
+ `run`, which is commonly not intended by test authors.
170
+
171
+ Running `run bats_pipe command0 \| command1` will have the piped commands run
172
+ within the context of the `run` command, and thus have the output and status
173
+ variables properly contained within the normal `output` and `status` variables.
174
+
175
+ Note that this requires the usage of `\|`, not `|`. This is to avoid bash
176
+ parsing out `|` first, instead sending `\|` to the bats_pipe command for it to
177
+ parse and set up intended piping. Running bats_pipe with no instances of `\|`
178
+ will always fail; this is intended to catch typos (accidentally using `|`) by
179
+ the test author.
180
+
181
+ The bats_pipe command will also properly propagate exit status from the piped
182
+ commands. The default behavior mimics `set -o pipefail`, returning the status
183
+ of the last (rightmost) command that exits with a non-zero status. This ensures
184
+ that usage of pipes do not mask the exit statuses of earlier commands.
185
+
186
+ @test "invoking foo piped to bar" {
187
+ run bats_pipe foo \| bar
188
+ # asserting foo or bar would return 17 (from foo if bar returns 0).
189
+ [ "$status" -eq 17 ]
190
+ [ "$output" = "bar output." ]
191
+ }
192
+
193
+ Alternatively, if the test always cares about the status of a specific command,
194
+ the -<N> option can be given (e.g. -0) to always return the status of the
195
+ command of interest.
196
+
197
+ @test "invoking foo piped to bar always return foo status" {
198
+ run bats_pipe -0 foo \| bar
199
+ # status of bar is ignored, status is always from foo.
200
+ [ "$status" -eq 2 ]
201
+ [ "$output" = "bar output." ]
202
+ }
203
+
204
+ Similarly, --returned-status N (or --returned-status=N) can be used for similar
205
+ functionality. This option supports negative values, which always return the
206
+ status of the command starting from the end and in reverse order.
207
+
208
+ @test "invoking foo piped to bar always return foo status" {
209
+ run bats_pipe --returned-status -2 foo \| bar
210
+ # status of bar is ignored, status is always from foo.
211
+ [ "$status" -eq 2 ]
212
+ [ "$output" = "bar output." ]
213
+ }
214
+
215
+ Piping of command output is especially helpful when the output needs to be
216
+ modified in some way (e.g. the command outputs binary data into stdout, which
217
+ cannot be stored as-is in an environment variable).
218
+
219
+ @test "invoking foo that returns binary data" {
220
+ run bats_pipe foo \| hexdump -v -e "1/1 \"0x%02X \""
221
+ [ "$status" -eq 17 ]
222
+ [[ "$output" =~ 0xDE\ 0xAD ]]
223
+ }
224
+
225
+ Any number of pipes can be used in conjunction to chain output between some set
226
+ of running commands.
227
+
228
+ THE LOAD COMMAND
229
+ ----------------
230
+
231
+ You may want to share common code across multiple test files. Bats
232
+ includes a convenient `load` command for sourcing a Bash source file
233
+ relative to the location of the current test file. For example, if you
234
+ have a Bats test in `test/foo.bats`, the command
235
+
236
+ load test_helper
237
+
238
+ will source the script `test/test_helper.bash` in your test file. This
239
+ can be useful for sharing functions to set up your environment or load
240
+ fixtures.
241
+
242
+ THE BATS_LOAD_LIBRARY COMMAND
243
+ -----------------------------
244
+
245
+ Some libraries are installed on the system, e.g. by `npm` or `brew`.
246
+ These should not be `load`ed, as their path depends on the installation method.
247
+ Instead, one should use `bats_load_library` together with setting
248
+ `BATS_LIB_PATH`, a `PATH`-like colon-delimited variable.
249
+
250
+ `bats_load_library` has two modes of resolving requests:
251
+
252
+ 1. by relative path from the `BATS_LIB_PATH` to a file in the library
253
+ 2. by library name, expecting libraries to have a `load.bash` entrypoint
254
+
255
+ For example if your `BATS_LIB_PATH` is set to
256
+ `~/.bats/libs:/usr/lib/bats`, then `bats_load_library test_helper`
257
+ would look for existing files with the following paths:
258
+
259
+ - `~/.bats/libs/test_helper`
260
+ - `~/.bats/libs/test_helper/load.bash`
261
+ - `/usr/lib/bats/test_helper`
262
+ - `/usr/lib/bats/test_helper/load.bash`
263
+
264
+ The first existing file in this list will be sourced.
265
+
266
+ If you want to load only part of a library or the entry point is not named `load.bash`,
267
+ you have to include it in the argument:
268
+ `bats_load_library library_name/file_to_load` will try
269
+
270
+ - `~/.bats/libs/library_name/file_to_load`
271
+ - `~/.bats/libs/library_name/file_to_load/load.bash`
272
+ - `/usr/lib/bats/library_name/file_to_load`
273
+ - `/usr/lib/bats/library_name/file_to_load/load.bash`
274
+
275
+ Apart from the changed lookup rules, `bats_load_library` behaves like `load`.
276
+
277
+ **Note**: As seen above `load.bash` is the entry point for libraries and
278
+ meant to load more files from its directory or other libraries.
279
+
280
+ **Note**: Obviously, the actual `BATS_LIB_PATH` is highly dependent on the environment.
281
+ To maintain a uniform location across systems, (distribution) package maintainers
282
+ are encouraged to use `/usr/lib/bats/` as the install path for libraries where possible.
283
+ However, if the package manager has another preferred location, like `npm` or `brew`,
284
+ you should use this instead.
285
+
286
+ THE SKIP COMMAND
287
+ ----------------
288
+
289
+ Tests can be skipped by using the `skip` command at the point in a
290
+ test you wish to skip.
291
+
292
+ @test "A test I don't want to execute for now" {
293
+ skip
294
+ run -0 foo
295
+ }
296
+
297
+ Optionally, you may include a reason for skipping:
298
+
299
+ @test "A test I don't want to execute for now" {
300
+ skip "This command will return zero soon, but not now"
301
+ run -0 foo
302
+ }
303
+
304
+ Or you can skip conditionally:
305
+
306
+ @test "A test which should run" {
307
+ if [ foo != bar ]; then
308
+ skip "foo isn't bar"
309
+ fi
310
+
311
+ run -0 foo
312
+ }
313
+
314
+
315
+ THE BATS_REQUIRE_MINIMUM_VERSION COMMAND
316
+ ----------------------------------------
317
+
318
+ Code for newer versions of Bats can be incompatible with older versions.
319
+ In the best case this will lead to an error message and a failed test suite.
320
+ In the worst case, the tests will pass erroneously, potentially masking a failure.
321
+
322
+ Use `bats_require_minimum_version <Bats version number>` to avoid this.
323
+ It communicates in a concise manner, that you intend the following code to be run
324
+ under the given Bats version or higher.
325
+
326
+ Additionally, this function will communicate the current Bats version floor to
327
+ subsequent code, allowing e.g. Bats' internal warning to give more informed warnings.
328
+
329
+ **Note**: By default, calling `bats_require_minimum_version` with versions before
330
+ Bats 1.7.0 will fail regardless of the required version as the function is not
331
+ available. However, you can use the
332
+ bats-backports plugin (https://github.com/bats-core/bats-backports) to make
333
+ your code usable with older versions, e.g. during migration while your CI system
334
+ is not yet upgraded.
335
+
336
+ SETUP AND TEARDOWN FUNCTIONS
337
+ ----------------------------
338
+
339
+ You can define special `setup` and `teardown` functions which run
340
+ before and after each test case, respectively. Use these to load
341
+ fixtures, set up your environment, and clean up when you're done.
342
+
343
+
344
+ CODE OUTSIDE OF TEST CASES
345
+ --------------------------
346
+
347
+ You can include code in your test file outside of `@test` functions.
348
+ For example, this may be useful if you want to check for dependencies
349
+ and fail immediately if they're not present. However, any output that
350
+ you print in code outside of `@test`, `setup` or `teardown` functions
351
+ must be redirected to `stderr` (`>&2`). Otherwise, the output may
352
+ cause Bats to fail by polluting the TAP stream on `stdout`.
353
+
354
+
355
+ SPECIAL VARIABLES
356
+ -----------------
357
+
358
+ There are several global variables you can use to introspect on Bats
359
+ tests:
360
+
361
+ * `$BATS_TEST_FILENAME` is the fully expanded path to the Bats test
362
+ file.
363
+ * `$BATS_TEST_DIRNAME` is the directory in which the Bats test file is
364
+ located.
365
+ * `$BATS_TEST_NAMES` is an array of function names for each test case.
366
+ * `$BATS_TEST_NAME` is the name of the function containing the current
367
+ test case.
368
+ * `BATS_TEST_NAME_PREFIX` will be prepended to the description of each test
369
+ on stdout and in reports.
370
+ * `$BATS_TEST_DESCRIPTION` is the description of the current test
371
+ case.
372
+ * `BATS_TEST_RETRIES` is the maximum number of additional attempts that will be
373
+ made on a failed test before it is finally considered failed.
374
+ The default of 0 means the test must pass on the first attempt.
375
+ * `BATS_TEST_TIMEOUT` is the number of seconds after which a test (including setup)
376
+ will be aborted and marked as failed. Updates to this value in `setup()` or `@test`
377
+ cannot change the running timeout countdown, so the latest useful update location is `setup_file()`.
378
+ * `$BATS_TEST_NUMBER` is the (1-based) index of the current test case
379
+ in the test file.
380
+ * `$BATS_SUITE_TEST_NUMBER` is the (1-based) index of the current test
381
+ case in the test suite (over all files).
382
+ * `$BATS_TMPDIR` is the base temporary directory used by bats to create its
383
+ temporary files / directories.
384
+ (default: `$TMPDIR`. If `$TMPDIR` is not set, `/tmp` is used.)
385
+ * `$BATS_RUN_TMPDIR` is the location to the temporary directory used by
386
+ bats to store all its internal temporary files during the tests.
387
+ (default: `$BATS_TMPDIR/bats-run-$BATS_ROOT_PID-XXXXXX`)
388
+ * `$BATS_FILE_EXTENSION` (default: `bats`) specifies the extension of
389
+ test files that should be found when running a suite (via
390
+ `bats [-r] suite_folder/`)
391
+ * `$BATS_TEST_TAGS` the tags of the current test.
392
+ * `$BATS_SUITE_TMPDIR` is a temporary directory common to all tests of a suite.
393
+ Could be used to create files required by multiple tests.
394
+ * `$BATS_FILE_TMPDIR` is a temporary directory common to all tests of a test file.
395
+ Could be used to create files required by multiple tests in the same test file.
396
+ * `$BATS_TEST_TMPDIR` is a temporary directory unique for each test.
397
+ Could be used to create files required only for specific tests.
398
+ * `$BATS_VERSION` is the version of Bats running the test.
399
+
400
+
401
+ SEE ALSO
402
+ --------
403
+
404
+ `bash`(1), `bats`(1)
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@bats-hardened/bats",
3
+ "version": "1.14.1-alpha02",
4
+ "description": "Bash Automated Testing System",
5
+ "homepage": "https://github.com/bats-core/bats-core#readme",
6
+ "license": "MIT",
7
+ "author": "Sam Stephenson <sstephenson@gmail.com> (http://sstephenson.us/)",
8
+ "repository": "github:bats-core/bats-core",
9
+ "bugs": "https://github.com/bats-core/bats-core/issues",
10
+ "files": [
11
+ "bin",
12
+ "libexec",
13
+ "lib",
14
+ "man",
15
+ "install.sh",
16
+ "uninstall.sh"
17
+ ],
18
+ "directories": {
19
+ "doc": "docs",
20
+ "man": "man",
21
+ "test": "test"
22
+ },
23
+ "bin": {
24
+ "bats": "bin/bats"
25
+ },
26
+ "scripts": {
27
+ "test": "bin/bats test"
28
+ },
29
+ "keywords": [
30
+ "bats",
31
+ "bash",
32
+ "shell",
33
+ "test",
34
+ "unit"
35
+ ]
36
+ }
package/uninstall.sh ADDED
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -e
4
+
5
+ BATS_ROOT="${0%/*}"
6
+ PREFIX="${1%/}"
7
+
8
+ if [[ -z "$PREFIX" ]]; then
9
+ printf '%s\n' \
10
+ "usage: $0 <prefix> [base_libdir]" \
11
+ " e.g. $0 /usr/local" \
12
+ " $0 /usr/local lib64" >&2
13
+ exit 1
14
+ fi
15
+
16
+ if [[ ! -d "$PREFIX" ]]; then
17
+ printf "No valid installation in directory %s.\n" "$PREFIX"
18
+ exit 2
19
+ fi
20
+
21
+ if [ -e "$PREFIX/bin/bats" ]; then
22
+ LIBDIR=$(grep -e '^BATS_BASE_LIBDIR=' "$PREFIX/bin/bats")
23
+ eval "$LIBDIR"
24
+ fi
25
+ LIBDIR="${BATS_BASE_LIBDIR:-lib}"
26
+
27
+ remove_file() { # <file>
28
+ echo "Removing $1"
29
+ rm -f "$1"
30
+ }
31
+
32
+ remove_directory() { # <directory>
33
+ local directory=$1
34
+ if [[ -d "$directory" ]]; then
35
+ echo "Removing $directory"
36
+ rmdir "$directory"
37
+ fi
38
+ }
39
+
40
+ d="$PREFIX/bin"
41
+ for elt in "$BATS_ROOT/bin"/*; do
42
+ elt=${elt##*/}
43
+ remove_file "$d/$elt"
44
+ done
45
+
46
+ d="$PREFIX/libexec/bats-core"
47
+ for elt in "$BATS_ROOT/libexec/bats-core"/*; do
48
+ elt=${elt##*/}
49
+ remove_file "$d/$elt"
50
+ done
51
+ remove_directory "$d"
52
+
53
+ d="$PREFIX/${LIBDIR}/bats-core"
54
+ for elt in "$BATS_ROOT/lib/bats-core"/*; do
55
+ elt=${elt##*/}
56
+ remove_file "$d/$elt"
57
+ done
58
+ remove_directory "$d"
59
+
60
+ remove_file "$PREFIX"/share/man/man1/bats.1
61
+ remove_file "$PREFIX"/share/man/man7/bats.7
62
+
63
+ echo "Uninstalled Bats from $PREFIX/bin/bats"