Skip to content

Instantly share code, notes, and snippets.

@rbuckland
Created November 16, 2022 10:40
Show Gist options
  • Save rbuckland/19a073e7708e93a8c74d764d41166321 to your computer and use it in GitHub Desktop.
Save rbuckland/19a073e7708e93a8c74d764d41166321 to your computer and use it in GitHub Desktop.
shellspec and bazel

shellspec and bazel

  • 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.
  1. 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 = "",
)

...
  1. 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/**"]),
)
  1. 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
  1. .shellspec - ref: https://github.com/shellspec/shellspec#shellspec---project-options-file
--require spec_helper
--fail-no-examples
  1. 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
)
  1. 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() {
  :
}
  1. 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/**"]),
)
  1. spec/hello_spec.sh
Describe 'hello.sh'
  It 'says hello'
    When call hello ShellSpec
    The output should equal 'Hello ShellSpec!'
  End
End
  1. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment