-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating
bats_test
Rule For bats-assert (#11)
* Updating README to use newest tag of bazel-bats. Moving SHA into its own var, similar to the version number. * Updating `bazel_bats_dependencies()`'s default version value (and corresponding SHA) to 1.7.0 (latest bats-core version). Updating WORKSPACE to be more explicit in versions used. Adding param checking to ensure that the SHA was passed in. Updating README to show explicit version passing. * Extending `bazel_bats_dependencies()` to also (optionally) allow for adding in bats extensions, bats-assert and bats-support (bats-support is required for bats-assert). The decision to make these optional additions was made to not force extra dependencies on any downstream library that does not make use of them (dep management is good). The `bats_test()` rule will be updated in a follow-up PR. * Fixing missing comma. * Updating `bats_test()` test target rule to support the bats-assert extension. This was implemented via two separate targets, selected via the `uses_bats_assert` target argument. Using two separate rule definitions is done to allow exclusion of bats-support and/or bats-assert from any downstream library that does not want them. Bazel implicit dependencies are used with bats-assert, thus a separate rule definition must be defined for optional use. Obviously, making use of `uses_bats_assert` without supplying necessary arguments to `bazel_bats_dependencies()` would result in an intended build failure (lacking necessary dependency project). There are other bats extensions not added here. As discussed already, a large focus with this change is to make these extensions optional. If other bats extensions are added, hopefully that optional mindset can persist. Example: [bats-detik](https://github.com/bats-core/bats-detik) is very niche, and I have no interest in including it into my projects -- but maybe others would (opt-in is fine and dandy). The two rule definitions were written to try and reuse as much as possible with regard to common functionality. The extended rule logic copies files from bats-support and bats-assert into the .bats source file path (in bazel runfiles path), for each source file in a given rule. This is necessary for consistency with bats-core layout as defined externally. Bats-core makes use of submodules for extensions, and expects these submodules to be siblings to any/all .bats source test files. This is further backed by the way that bats's `load` system looks in the relative path. Loading of bats-assert is done with `load "test_helper/bats-support/load"`, which means that these extensions must be siblinged to the tests source files (in bazel's runfiles path). These rules allow consistent functionality with any bats tests written externally. This copy logic works for any number of .bats files, whether they are in the same directory or not (over multiple bazel test targets, or within a single target) -- which is actually more flexible, in terms of project structure, than is afforded with bats directly. In addition to the rule changes, other changes in this PR: * Updating README to cover extension support. * Updating WORKSPACE to add in bats-assert. * Adding in new test that validates bats-assert statements. See also: https://github.com/bats-core/bats-assert, https://bats-core.readthedocs.io/en/stable/tutorial.html#quick-installation. * Slight cleanup on `if`s. * Asserting using both `assert_failure` overrides. * More assertions! * * More better words (improving error messages). * * Fixing nit. * * Fixing nit 2. * * Fixing nit 3. * * More nit fix. * * Fixing nit.
- Loading branch information
1 parent
2bfd658
commit caf19fc
Showing
6 changed files
with
180 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
#!/usr/bin/env bash | ||
|
||
if [ ! -z "$2" ]; then | ||
echo "Given for output: $2" | ||
fi | ||
|
||
exit $1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env bats | ||
|
||
PATH_TO_EXIT_WITH_INPUT="tests/exit_with_input" | ||
|
||
setup() { | ||
load "test_helper/bats-support/load" | ||
load "test_helper/bats-assert/load" | ||
} | ||
|
||
@test "can run succeeding executable" { | ||
run "${PATH_TO_EXIT_WITH_INPUT}" 0 | ||
|
||
assert_success | ||
refute_output | ||
} | ||
|
||
@test "can run succeeding executable with output" { | ||
run "${PATH_TO_EXIT_WITH_INPUT}" 0 'some message' | ||
|
||
assert_success | ||
assert_output --partial 'Given for output:' | ||
assert_output --partial 'some message' | ||
assert_output --regexp '^Given for output: some message$' | ||
assert_output 'Given for output: some message' | ||
refute_output 'never printed' | ||
} | ||
|
||
@test "can run failing executable" { | ||
run "${PATH_TO_EXIT_WITH_INPUT}" 1 | ||
|
||
assert_failure | ||
assert_failure 1 | ||
refute_output | ||
} | ||
|
||
@test "can run failing executable with output" { | ||
run "${PATH_TO_EXIT_WITH_INPUT}" 1 'some message' | ||
|
||
assert_failure | ||
assert_failure 1 | ||
assert_output --partial 'Given for output:' | ||
assert_output --partial 'some message' | ||
assert_output --regexp '^Given for output: some message$' | ||
assert_output 'Given for output: some message' | ||
refute_output 'never printed' | ||
} | ||
|
||
@test "can run failing executable with different return code" { | ||
run "${PATH_TO_EXIT_WITH_INPUT}" 2 | ||
|
||
assert_failure | ||
assert_failure 2 | ||
refute_output | ||
} | ||
|
||
@test "can run failing executable with different return code with output" { | ||
run "${PATH_TO_EXIT_WITH_INPUT}" 2 'some message' | ||
|
||
assert_failure | ||
assert_failure 2 | ||
assert_output --partial 'Given for output:' | ||
assert_output --partial 'some message' | ||
assert_output --regexp '^Given for output: some message$' | ||
assert_output 'Given for output: some message' | ||
refute_output 'never printed' | ||
} |