From 46b7b48c3c4bb8e0e179a2d3db6491b4ffb74f43 Mon Sep 17 00:00:00 2001 From: helly25 Date: Sat, 29 Mar 2025 11:58:25 +0000 Subject: [PATCH 1/2] * Added convenient target to print `bashtest` help `bazel run //bashtest:bashtest_help` which can be used with `|pandoc -s -t man|man -l -`. * Make release script automatically add the `strip_prefix` attribute for workspace installs. --- .github/workflows/release_prep.sh | 1 + CHANGELOG.md | 2 + README.md | 8 ++-- bashtest/BUILD.bazel | 24 +++++++++++ bashtest/README.md | 2 +- bashtest/bashtest.sh | 71 +++++++++++++++++-------------- 6 files changed, 70 insertions(+), 38 deletions(-) diff --git a/.github/workflows/release_prep.sh b/.github/workflows/release_prep.sh index d118dbe..1ef18f5 100755 --- a/.github/workflows/release_prep.sh +++ b/.github/workflows/release_prep.sh @@ -104,6 +104,7 @@ http_archive( name = "${WORKSPACE_NAME}", url = "https://github.com/helly25/${PACKAGE_NAME}/releases/download/${TAG}/${ARCHIVE}", sha256 = "${SHA256}", + strip_prefix = "${PREFIX}", ) \`\`\` diff --git a/CHANGELOG.md b/CHANGELOG.md index 9db77ce..27f7697 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # 0.1.1 +* Added convenient target to print `bashtest` help `bazel run //bashtest:bashtest_help` which can be used with `|pandoc -s -t man|man -l -`. + # 0.1.0 * Initial version moved from helly25_mbo//testing/bashtest* diff --git a/README.md b/README.md index b68eb2f..05c927e 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,11 @@ The library is tested with continuous integration: [![Test](https://github.com/h ## Bashtest -Run the following command to get detailed information on the actual bashtest.sh script: +Run one of the following commands to get detailed information on the actual bashtest.sh script: -```sh -(. testing/bashtest.sh --help)` -``` +* `bazel run //bashtest:bashtest_help` +* `bazel run //bashtest:bashtest_help | pandoc -s -t man | man -l -` +* `bazel run //bashtest:bashtest_help | pandoc | lynx -stdin` The flags can be used on the `bazel run` and `bazel test` commands (the latter requiring `--test_arg=...`). diff --git a/bashtest/BUILD.bazel b/bashtest/BUILD.bazel index eaf3df1..9d615f4 100644 --- a/bashtest/BUILD.bazel +++ b/bashtest/BUILD.bazel @@ -33,3 +33,27 @@ alias( actual = ":bashtest_sh", visibility = ["//visibility:public"], ) + +genrule( + name = "bashtest_help", + testonly = 1, + srcs = ["bashtest.sh"], + outs = ["bashtest_help.sh"], + cmd = """ + echo "#!/usr/bin/env bash" >$@ + echo "bash $< --help || true" >>$@ + """, + executable = 1, + tags = ["manual"], + visibility = ["//visibility:private"], +) + +genrule( + name = "bashtest_help_md", + testonly = 1, + srcs = ["bashtest.sh"], + outs = ["bashtest_help.md"], + cmd = """bash $< --help >$@ || true""", + tags = ["manual"], + visibility = ["//visibility:private"], +) diff --git a/bashtest/README.md b/bashtest/README.md index 114c2aa..0af027c 100644 --- a/bashtest/README.md +++ b/bashtest/README.md @@ -4,7 +4,7 @@ Bashtest provides `sh_test` wrapper that simplifies the creation of shell tests. * bashtest:bashtest, bashtest/bashtest.sh - * sh_library `bashtest.sh` which provides a test runner for complex shell tests involving golden files that provides built-in golden update functionality (see `(. bashtest/bashtest.sh --help)`). + * sh_library `bashtest.sh` which provides a test runner for complex shell tests involving golden files that provides built-in golden update functionality (see `bazel run //bashtest:bashtest_help`). * status helper `test_has_erro`: Returns whether a test function has had an error. * status helper `test_has_failed_tests`: Returns whether a test program had previous failing test functions. * expectation `expect_eq` "\${LHS}" "\${RHS}": Asserts that two strings are the same. diff --git a/bashtest/bashtest.sh b/bashtest/bashtest.sh index a1b61f4..7996c40 100644 --- a/bashtest/bashtest.sh +++ b/bashtest/bashtest.sh @@ -1,5 +1,4 @@ -#!/usr/bin/env bash - +# shellcheck disable=SC2148 # This is not executable # SPDX-FileCopyrightText: Copyright (c) The helly25 authors (helly25.com) # SPDX-License-Identifier: Apache-2.0 # @@ -15,62 +14,68 @@ # See the License for the specific language governing permissions and # limitations under the License. -# BashTest: see `bashtest.sh -h` +# BashTest: see `bazel run //bashtest:bashtest_help` set -euo pipefail function die() { echo >&2 "ERROR: ${*}"; exit 1; } _BASHTEST_USAGE=$(cat <<'EOF' -bashtest.sh - A Bazel shell test runner. - -- All declared functions that start with "test::" are considered as tests. -- Each test should `return 0` to indicate success or `return 1` for failure. -- Call `test_runner` at the end of the test program. -- Provides '${BASHTEST_TMPDIR}' which is a test scratch directory. -- Tests can be filtered (skipping non matching) using flag '--test_filter '. -- Tests that use diff functionality 'expect_files_eq' can use '-u="${PWD}"' to +# bashtest.sh - A Bazel shell test runner. + +* All declared functions that start with "test::" are considered as tests. +* Each test should use `return 0` to indicate success or `return 1` for failure. +* Call `test_runner` at the end of the test program. +* Provides '${BASHTEST_TMPDIR}' which is a test scratch directory. +* Tests can be filtered (skipping non matching) using flag '--test_filter ``'. +* Tests that use diff functionality 'expect_files_eq' can use '-u="${PWD}"' to update their golden files. -Usage: - bazel test [ --test_arg=-f= ] [ -v ] [ -u="${PWD}" ] - bazel run ] [ -v ][ -u="${PWD}" ] +## Usage: + +* bazel test `` [ --test_arg=-f=`` ] [ -v ] [ -u="${PWD}" ] +* bazel run `` -- [ -f=`` ] [ -v ][ -u="${PWD}" ] + +## Flags: --f --test-filter A glob pattern for tests to run (skip others). The - test is considered failing of no test was run. --u --update Update golden files, assuming the . --v --verbose Show additional output while tests succeed or fail. - Prints all test calls and file diffs. +* -f --test-filter `` A glob pattern for tests to run (skip others). The + test is considered failing of no test was run. +* -u --update `` Update golden files, assuming the ``. +* -v --verbose Show additional output while tests succeed or fail. + Prints all test calls and file diffs. -Assertions: - expect_eq "${LHS}" "${RHS}" +## Assertions: + +* expect_eq "${LHS}" "${RHS}" Asserts that two strings are the same. - expect_ne "${LHS}" "${RHS}" +* expect_ne "${LHS}" "${RHS}" Asserts that two strings are different. - expect_files_eq "${LHS}" "${RHS}" +* expect_files_eq "${LHS}" "${RHS}" Asserts that two file are the same (supports golden updates). - expect_contains "${EXPECTED}" "${ARRAY[@]}" +* expect_contains "${EXPECTED}" "${ARRAY[@]}" Assert that one string is present in an array. - expect_not_contains "${EXPECTED}" "${ARRAY[@]}" +* expect_not_contains "${EXPECTED}" "${ARRAY[@]}" Assert that one string is NOT present in an array. -Status: - test_has_error Returns whether a test function has had an error. - test_has_failed_tests Returns whether a test program had previous +## Status: + +* test_has_error Returns whether a test function has had an error. +* test_has_failed_tests Returns whether a test program had previous failing test functions. -Setup/Shutdown: - test::test_init If present, then this function runs first! +## Setup/Shutdown: + +* test::test_init If present, then this function runs first! Test will only be executed if it succeeds. - test::test_done If present, then this function runs last! +* test::test_done If present, then this function runs last! -Example: +## Example: ```sh # shellcheck disable=SC2317 # Functions are called bashtest @@ -125,7 +130,7 @@ while getopts -- '-:f:hu:v' OPTION; do fi case "${OPTION}" in f|test[-_]filter) _BASHTEST_FILTER="${OPTARG}" ;; - h|help) echo "${_BASHTEST_USAGE}"; return 2 ;; + h|help) echo "${_BASHTEST_USAGE}"; exit 2 ;; u|update[-_]golden) _BASHTEST_UPDATE_GOLDEN="${OPTARG}" ;; v|verbose) _BASHTEST_VERBOSE=1 ;; *) die "Unknown flag '${OPTERR}'." ;; From 587f546bd25636867cbbee5789efd2fe98835bf8 Mon Sep 17 00:00:00 2001 From: helly25 Date: Sat, 29 Mar 2025 12:12:05 +0000 Subject: [PATCH 2/2] Update release script --- tools/trigger_release.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/trigger_release.sh b/tools/trigger_release.sh index 8cf6f6e..b8c1036 100755 --- a/tools/trigger_release.sh +++ b/tools/trigger_release.sh @@ -54,8 +54,9 @@ fi grep "${VERSION}" < <(git tag -l) && die "Version tag is already in use." -# Should the tag have the changelog parts? -git tag -s -a "${VERSION}" -m "New release tag version: '${VERSION}'." +git tag -s -a "${VERSION}" \ + -m "New release tag version: '${VERSION}'." \ + -m "$(awk '/^#/{if(NR>1)exit}/^[^#]/{print}'