From 588282424d725468037d25809eff581b0c58c96b Mon Sep 17 00:00:00 2001 From: Torsten Long Date: Thu, 28 Mar 2024 10:38:51 +0100 Subject: [PATCH] Patch release 0.8.1 Signed-off-by: Torsten Long --- Makefile | 6 +++--- README.md | 5 ++++- bin/mock_exe.sh | 3 +-- go/.gitignore | 4 +++- go/go.mod | 5 +---- go/main.go | 14 ++++++++++---- tests/extended.bats | 11 ++++++++++- 7 files changed, 32 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 5a72bd2..0fe5868 100644 --- a/Makefile +++ b/Makefile @@ -104,15 +104,15 @@ coverage: test # Analyse output of coverage reports and fail if not all files have been # covered of if coverage is not high enough. gawk \ - -v min_cov="92" \ - -v tot_num_files="1" \ + -v min_cov="91" \ + -v tot_num_files="2" \ 'BEGIN{num_files=0; cov=0;} \ $$1 ~ /"file":/{num_files++} \ $$1 ~ /"covered_lines":/{cov=$$2} \ $$1 ~ /"total_lines":/{tot_cov=$$2} \ END{ \ if(num_files!=tot_num_files){ \ - printf("Not all files covered: %d < %d\n", num_files, tot_num_files); exit 1 \ + printf("Not all files covered: %d != %d\n", num_files, tot_num_files); exit 1 \ } \ } \ END{ \ diff --git a/README.md b/README.md index fb8ef13..33500fc 100644 --- a/README.md +++ b/README.md @@ -48,15 +48,18 @@ The following tools are needed to use `shellmock`: - `base32` - `base64` +- `basename` - `bash` (at least version 4.4) - `cat` - `chmod` - `env` - `find` +- `flock` - `gawk` - `grep` - `mkdir` - `mktemp` +- `ps` - `rm` - `sed` - `sort` @@ -67,7 +70,7 @@ The following tools are needed to use `shellmock`: On Debian-based systems, they can be installed via: ```bash -sudo apt install -yqq bash coreutils findutils gawk grep sed +sudo apt install -yqq bash coreutils findutils gawk grep procps sed util-linux ``` You also need the [bats-core] testing framework that diff --git a/bin/mock_exe.sh b/bin/mock_exe.sh index 1f0e63d..6b38a68 100755 --- a/bin/mock_exe.sh +++ b/bin/mock_exe.sh @@ -26,8 +26,6 @@ # stored this way can be used by the main shellmock library to assert # user-defined expectations. -set -euo pipefail - # Check whether required environment variables are set. env_var_check() { local var @@ -315,6 +313,7 @@ main() { # Run if executed directly. If sourced from a bash shell, don't do anything, # which simplifies testing. if [[ -z ${BASH_SOURCE[0]-} ]] || [[ ${BASH_SOURCE[0]} == "${0}" ]]; then + set -euo pipefail main "$@" else : diff --git a/go/.gitignore b/go/.gitignore index 6c2c294..46f3567 100644 --- a/go/.gitignore +++ b/go/.gitignore @@ -1,2 +1,4 @@ # Always ignore the sumfile because users won't have one either. -go.sum +/go.sum +# Ignore the build artefact. +/main diff --git a/go/go.mod b/go/go.mod index 8e20742..5fefca6 100644 --- a/go/go.mod +++ b/go/go.mod @@ -18,7 +18,4 @@ module main go 1.22.1 -require ( - golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 - mvdan.cc/sh/v3 v3.8.0 -) +require mvdan.cc/sh/v3 v3.8.0 diff --git a/go/main.go b/go/main.go index 5e5dca7..6e8ccb2 100644 --- a/go/main.go +++ b/go/main.go @@ -25,10 +25,18 @@ import ( "os" "slices" - "golang.org/x/exp/maps" shell "mvdan.cc/sh/v3/syntax" ) +func sortedKeys(data map[string]int) []string { + result := make([]string, 0, len(data)) + for key := range data { + result = append(result, key) + } + slices.Sort(result) + return result +} + // Determine all commands executed by a script. func findCommands(shellCode shell.Node) map[string]int { result := map[string]int{} @@ -66,9 +74,7 @@ func main() { log.Fatalf("failed to parse shell code: %s", err.Error()) } commands := findCommands(parsed) - keys := maps.Keys(commands) - slices.Sort(keys) - for _, cmd := range keys { + for _, cmd := range sortedKeys(commands) { count := commands[cmd] fmt.Printf("%s:%d\n", cmd, count) } diff --git a/tests/extended.bats b/tests/extended.bats index 3028779..513ea9a 100644 --- a/tests/extended.bats +++ b/tests/extended.bats @@ -149,19 +149,28 @@ EOF } @test "that we know which executables shellmock uses" { - run -0 --separate-stderr shellmock commands < "${root}/shellmock.bash" + # Source mock executable to know functions defined therein. Shellmock itself + # has already been sourced, which means we can filter its internal functions. + source "${root}/bin/mock_exe.sh" + + code=$(cat "${root}/shellmock.bash" "${root}/bin/mock_exe.sh") + run -0 --separate-stderr shellmock commands <<< "${code}" exes=( base32 base64 + basename cat chmod env find + flock + gawk go grep mkdir mktemp + ps rm sed sort