- goal have bazel run some shell spec testing using
shellspec
- caveat won't run on windows (
sh_test(..)
is not supported there)
- caveat this is not
rules_shellspec
- though this is close.
- observation
shellspec
was fussy, but it ran in the end.
- Add the WORKSPACE entry
#WORKSPACE
...
#----------------------------------------
# Shell Testing using shellspec
#----------------------------------------
http_archive(
name = "shellspec_lib",
urls = [ "https://github.com/shellspec/shellspec/archive/refs/tags/0.28.1.zip" ],
sha256 = "755d66d245ef35fffda3dff6a26af71569b93e9948d6b506a37076b4a9387e17",
strip_prefix = "shellspec-0.28.1",
build_file = "//lib/3rdparty/sh:shellspec.BUILD",
workspace_file_content = "",
)
...
- Add the drop in
BUILD
file used above
# mkdir -p lib/3rdparty/sh/shellspec.BUILD
# touch lib/3rdparty/sh/BUILD
# add following to lib/3rdparty/sh/shellspec.BUILD
package(default_visibility=[ "//visibility:public" ])
sh_library(
name = "bundle",
srcs = glob(["shellspec", "lib/**", "libexec/**", "helper/**", "contrib/**"]),
)
- create this type of structure
# some/sub/dir/projectthing/BUILD # contents below
# some/sub/dir/projectthing/tests
# /.shellspec # file see below
# /shellspec_wrapper.sh # file see below
# /spec/spec_helper.sh # file see below
# /spec/hello_spec.sh # file see below
.shellspec
- ref: https://github.com/shellspec/shellspec#shellspec---project-options-file
--require spec_helper
--fail-no-examples
shellspec_wrapper.sh
needed to tweak a few things before shellspec is run
#!/usr/bin/env bash
# -- helpful debug BEGIN
tree -a
env
# -- helpful debug END
export HOME=$PWD
cur=$PWD
cd $1 && (
export SHELL_TYPE=bash
$cur/external/shellspec_lib/shellspec \
-I $cur/external/shellspec_lib/helper \
-I $cur/external/shellspec_lib/contrib \
-I $cur/external/shellspec_lib/lib \
-I $cur/external/shellspec_lib/libexec
)
spec/spec_helper.sh
- ref https://github.com/shellspec/shellspec#spec_helper
# Filename: spec/spec_helper.sh
set -eu
spec_helper_precheck() {
minimum_version "0.28.0"
}
spec_helper_loaded() {
: # In most cases, you won't use it.
}
spec_helper_configure() {
import 'support/custom_matcher'
before_each "global_before_each_hook"
}
# User-defined global function
global_before_each_hook() {
:
}
some/sub/dir/projectthing/BUILD
sh_test(
name = "my_tests_for_sh",
size = "small",
srcs = ["tests/shellspec_wrapper.sh"],
args = [
package_name() + "/tests"
],
data = [
":files-for-testing",
"@shellspec_lib//:bundle",
] + glob(["tests/**"]),
)
spec/hello_spec.sh
Describe 'hello.sh'
It 'says hello'
When call hello ShellSpec
The output should equal 'Hello ShellSpec!'
End
End
- run it
(some/sub/dir/projectthing)
> bazel test :my_tests_for_sh
Running: /bin/sh [sh]
F
Examples:
1) hello.sh says hello
When call hello ShellSpec
1.1) The output should equal Hello ShellSpec!
expected: "Hello ShellSpec!"
got: ""
# spec/hello_spec.sh:4
1.2) WARNING: It exits with status non-zero but not found expectation
status: 127
# spec/hello_spec.sh:2-5
1.3) WARNING: There was output to stderr but not found expectation
stderr: /bin/sh: 153: hello: not found
# spec/hello_spec.sh:2-5
Finished in 0.05 seconds (user 0.06 seconds, sys 0.00 seconds)
1 example, 1 failure
Failure examples / Errors: (Listed here affect your suite's status)
shellspec spec/hello_spec.sh:2 # 1) hello.sh says hello FAILED