-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: better array parsing, small testing framework, asdf list fix (#561)
Fixes the `docker.sh` array parsing to properly handle an array not being set. Extracts aforementioned logic to be in a generic `yaml.sh` library in hopes it can be more usable in the future, since moving to Go isn't happening yet. As part of that, I felt we needed some tests here. So, I wrote a _very_ simple testing framework to call `*_test.sh` scripts, much like Go. If the text returns a non-zero exit code, it is considered failed. Added some tests for the new logic. I will add some documentation for this in the PR shortly, but I wanted to get it out for comments (since it is simple and described here). Fixed an issue where newer asdf versions seem to put `*` in front of versions as well as a space, so our original `^` regex no longer works.
- Loading branch information
1 parent
327de32
commit b96f4c0
Showing
8 changed files
with
127 additions
and
15 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 was deleted.
Oops, something went wrong.
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,16 @@ | ||
#!/usr/bin/env bash | ||
# Runs all files with _test.sh at the end of the filename | ||
set -euo pipefail | ||
|
||
# Find all files with _test.sh at the end of the filename | ||
# and run them | ||
mapfile -t test_files < <(find . -name "*_test.sh" | sort | grep -v "./node_modules") | ||
|
||
for test_file in "${test_files[@]}"; do | ||
echo "Running tests in $test_file" | ||
# shellcheck disable=SC1090 | ||
bash "$test_file" || { | ||
echo "Tests failed in '$test_file'" | ||
exit 1 | ||
} | ||
done |
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,5 @@ | ||
#!/usr/bin/env bash | ||
# Calls extra test functions | ||
set -euo pipefail | ||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" | ||
exec "${DIR}/bash-test-runner.sh" |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env bash | ||
# yaml is a general purpose bash yaml parsing library | ||
|
||
# yaml_get_array returns a newline separated list of values | ||
# from a yaml array. If a value is not set, it will return | ||
# an empty string. | ||
# | ||
# $1 yq filter | ||
# $2 yaml file | ||
yaml_get_array() { | ||
local filter="$1" | ||
local file="$2" | ||
yq -r "$filter | .[]?" "$file" | ||
} | ||
|
||
# yaml_construct_object_filter creates a yq filter for all | ||
# arguments passed to access a field on an object. For example, | ||
# if you have a yaml file with the following contents: | ||
# | ||
# foo: | ||
# bar: | ||
# baz: 1 | ||
# | ||
# yaml_construct_object_filter foo bar baz will return | ||
# .["foo"]["bar"]["baz"] | ||
yaml_construct_object_filter() { | ||
local filter="." | ||
for arg in "$@"; do | ||
filter+="[\"$arg\"]" | ||
done | ||
echo "$filter" | ||
} |
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,48 @@ | ||
#!/usr/bin/env bash | ||
# Tests the shell/lib/yaml.sh library | ||
# | ||
# Not needed for tests: | ||
# - SC2155: Declare and assign separately to avoid masking return values. | ||
# shellcheck disable=SC2155 | ||
|
||
# No -e because we want to handle errors to make them | ||
# obvious. | ||
set -uo pipefail | ||
|
||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" | ||
|
||
# shellcheck source=../lib/yaml.sh | ||
source "${DIR}/../lib/yaml.sh" | ||
|
||
test_yaml_get_array() { | ||
# should be able to get values from an array | ||
local yaml_file=$(mktemp) | ||
{ | ||
echo "set:" | ||
echo " - bar" | ||
echo " - baz" | ||
} >"$yaml_file" | ||
|
||
echo "Should be able to get values from an array" | ||
local got=$(yaml_get_array ".set" "$yaml_file") | ||
local expected=$({ | ||
echo "bar" | ||
echo "baz" | ||
}) | ||
if [[ $got != "$expected" ]]; then | ||
echo "Expected '$expected', got '$got'" | ||
exit 1 | ||
fi | ||
|
||
echo "Should not error if the field is not set" | ||
got=$(yaml_get_array ".not_set" "$yaml_file" 2>&1) | ||
if [[ $got != "" ]]; then | ||
echo "Expected empty value for unset field, got: $got" >&2 | ||
exit 1 | ||
fi | ||
|
||
rm -f "$yaml_file" | ||
return 0 | ||
} | ||
|
||
test_yaml_get_array |