Skip to content

Commit

Permalink
add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
robzr committed Feb 22, 2024
1 parent 1a1b3a4 commit 7cf7edd
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 76 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Semantic Version parsing and utility script and function library in pure bash
`sver` is a self contained cli tool and function library implementing a Semantic
Versioning 2 [Semantic Versioning 2](https://semver.org) compliant parser and
utilities. Written in optimized, portable, pure bash (v3+) for simplicity & speed.
"

### Features
- bump or get version identifiers (major, minor, patch, prerelease, build_metadata)
Expand All @@ -17,6 +16,7 @@ utilities. Written in optimized, portable, pure bash (v3+) for simplicity & spee
- bash command line completion function & injector built in
- uses bash primitives and builtins exclusively for speed & portability
- single script usable as a CLI or mixin bash function library (documentation in source)
- comprehensive [test](tests) coverage

# License
Permissive [Creative Commons - CC BY 3.0](https://creativecommons.org/licenses/by/3.0/)
Expand Down
38 changes: 38 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# sver/tests
Unit tests for sver

## Coverage
Test data is in [tests.yaml](tests.yaml), references to keys below are in this file.

### `bump (major|minor|patch)`
See top level `.bump`.

### Comparisons (`equals`, `greater_than` & `less_than`)
Indirectly tested during `bump` and `sort` tests.

### `complete`
No test coverage.

### `constraint`
TODO

### `get (major|minor|patch|prerelease|build)`
See top level `.get`.

### `json`
Tested against valid and invalid example versions, see top level `.examples`.

### `sort`
Tested against valid versions, see top level `.examples.sorted`. Pre-sorted
input is unsorted both with a fixed reverse sort, as well as 5 random "sorts",
then sorted by sver, and the output is then compared to the pre-sorted input.

### `filter`

### `validate`
Tested against valid and invalid example versions, see top level `.examples`.

### `yaml`
Tested against valid and invalid example versions, see top level `.examples`.


123 changes: 82 additions & 41 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,69 @@
#!/bin/bash -eo pipefail
#!/bin/bash
#set -x

SVER_BIN="$(git rev-parse --show-toplevel)/sver"
TESTS_JSON=$(mktemp)
TESTS_YAML=tests/tests.yaml

_get_sha() {
local sha=$(shasum)
echo "${sha:0:6}"
}

_get_split_json() {
jq --arg key "$2" \
--arg key2 "$3" \
-r '
.[$key][$key2] |
split("\n")[] |
select(
test("^\\s*$") or test("^\\s*#") |
not
)
' "$1"
}

test_sort() {
local presorted presorted_sha sorted sorted_sha unsorted unsorted_sha
local random=$([ "$1" = "-r" ] && echo true || echo false)
local sort_status=0

presorted=$(sed 's/\+.*//') # filter out build metadata as it is not sorted
presorted_sha=$(_get_sha <<< "$presorted")
cat > "sort-presorted-${presorted_sha}.txt" <<< "$presorted"

if $random ; then
unsorted=$(sort -R -t. -k1 -k2 -k3 <<< "$presorted")
else
unsorted=$(sort -rn -t. -k1 -k2 -k3 <<< "$presorted")
fi

unsorted_sha=$(_get_sha <<< "$unsorted")
sorted=$($SVER_BIN sort <<< "$unsorted")
sorted_sha=$(_get_sha <<< "$sorted")
cat > "sort-sorted-${sorted_sha}.txt" <<< "$sorted"

if [ "$sorted_sha" != "$presorted_sha" ] ; then
sort_status=1
cat > "sort-unsorted-${unsorted_sha}.txt" <<< "$unsorted"
cat > "sort-sorted-${sorted_sha}.txt" <<< "$sorted"
fi

printf -- \
'- checking sort %s (%s) - %s\n' \
"$($random && echo "random" || echo "fixed")" \
"$unsorted_sha" \
"$([ "$sort_status" -eq 0 ] && echo 'passed.' || echo "failed ($sorted_sha)!")"

return $sort_status
}

TESTS_JSON=$(mktemp)
TESTS_YAML_FULL="$(git rev-parse --show-toplevel)/${TESTS_YAML}"
yq @json "$TESTS_YAML_FULL" > "$TESTS_JSON"

EXAMPLES_VALID=$(
jq -r '
.examples.valid |
split("\n")[] |
select(
test("^\\s*$") or test("^\\s*#") |
not
)
' "$TESTS_JSON"
)

EXAMPLES_INVALID=$(
jq -r '
.examples.invalid |
split("\n")[] |
select(
test("^\\s*$") or test("^\\s*#") |
not
)
' "$TESTS_JSON"
)
EXAMPLES_SORTED=$(_get_split_json "$TESTS_JSON" examples sorted)
EXAMPLES_VALID=$(_get_split_json "$TESTS_JSON" examples valid)
EXAMPLES_INVALID=$(_get_split_json "$TESTS_JSON" examples invalid)

TESTS_FAILED=0

Expand All @@ -43,24 +79,41 @@ else
((TESTS_FAILED++))
fi

echo -n 'Testing sort '
echo 'Testing sorts'
sort_output=$(mktemp)
test_sort <<< "$EXAMPLES_SORTED" &
for ((x=0; x < 5; x++)) ; do
( test_sort -r <<< "$EXAMPLES_SORTED"
echo $? > "${sort_output}.${x}"
) &
done
wait
for ((x=0; x < 5; x++)) ; do
REPLY=$(cat "${sort_output}.${x}")
if [ "$REPLY" != 0 ] ; then ((TESTS_FAILED++)); fi
done

echo 'Testing valid example versions'
while read -r line ; do
TESTS_FAILED_BEFORE=$TESTS_FAILED
echo -n "- checking \"${line}\" - validate"
if ! $SVER_BIN validate "$line" ; then
((TESTS_FAILED++))
echo ' - failed!'
else
echo ' - passed.'
fi
echo -n ', yaml'

echo -n "- checking \"${line}\" - yaml"
if ! $SVER_BIN yaml "$line" | yq . >/dev/null; then
((TESTS_FAILED++))
echo ' - failed!'
else
echo ' - passed.'
fi
echo -n ', json'

echo -n "- checking \"${line}\" - json"
if ! $SVER_BIN json "$line" | jq . >/dev/null; then
((TESTS_FAILED++))
fi
if [ $TESTS_FAILED -gt $TESTS_FAILED_BEFORE ] ; then
echo ' - failed!'
else
echo ' - passed.'
Expand All @@ -69,20 +122,9 @@ done <<< "$EXAMPLES_VALID"

echo 'Testing invalid example versions'
while read -r line ; do
TESTS_FAILED_BEFORE=$TESTS_FAILED
echo -n "- checking \"${line}\" - validate"
if $SVER_BIN validate "$line" 2>/dev/null ; then
((TESTS_FAILED++))
fi
echo -n ', yaml'
if $SVER_BIN yaml "$line" 2>/dev/null | yq . >/dev/null; then
((TESTS_FAILED++))
fi
echo -n ', json'
if $SVER_BIN json "$line" 2>/dev/null | jq . >/dev/null; then
((TESTS_FAILED++))
fi
if [ $TESTS_FAILED -gt $TESTS_FAILED_BEFORE ] ; then
echo ' - failed!'
else
echo ' - passed.'
Expand Down Expand Up @@ -116,4 +158,3 @@ else
echo "All tests passed."
exit 0
fi

100 changes: 66 additions & 34 deletions tests/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Test file for tests/run_tests.sh
#

# Format is (generally):
# <command>:
# <subcommand>:
Expand Down Expand Up @@ -41,41 +41,9 @@ get:
v5.4.3-pre.v1.2.3: ""

# valid and invalid example versions from https://regex101.com/r/Ly7O1x/3/ as per semver.org
#
examples:
valid: |
0.0.4
1.2.3
10.20.30
1.1.2-prerelease+meta
1.1.2+meta
1.1.2+meta-valid
1.0.0-alpha
1.0.0-beta
1.0.0-alpha.beta
1.0.0-alpha.beta.1
1.0.0-alpha.1
1.0.0-alpha0.valid
1.0.0-alpha.0valid
1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay
1.0.0-rc.1+build.1
2.0.0-rc.1+build.123
1.2.3-beta
10.2.3-DEV-SNAPSHOT
1.2.3-SNAPSHOT-123
1.0.0
2.0.0
1.1.7
2.0.0+build.1848
2.0.1-alpha.1227
1.0.0-alpha+beta
1.2.3----RC-SNAPSHOT.12.9.1--.12+788
1.2.3----R-S.12.9.1--.12+meta
1.2.3----RC-SNAPSHOT.12.9.1--.12
1.0.0+0.build.1-rc.10000aaa-kk-0.1
99999999999999999999999.999999999999999999.99999999999999999
1.0.0-0A.is.legal
invalid: |
# from https://regex101.com/r/Ly7O1x/3/ as per semver.org
1
1.2
1.2.3-0123
Expand Down Expand Up @@ -116,3 +84,67 @@ examples:
9.8.7+meta+meta
9.8.7-whatever+meta+meta
99999999999999999999999.999999999999999999.99999999999999999----RC-SNAPSHOT.12.09.1--------------------------------..12
sorted: |
0.0.4
1.0.0-0A.is.legal
1.0.0-alpha+beta
1.0.0-alpha
1.0.0-alpha.0valid
1.0.0-alpha.1
1.0.0-alpha.beta
1.0.0-alpha.beta.1
1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay
1.0.0-alpha0.valid
1.0.0-beta
1.0.0-rc.1+build.1
1.0.0+0.build.1-rc.10000aaa-kk-0.1
1.0.0
1.1.2-prerelease+meta
1.1.2+meta-valid
1.1.2+meta
1.1.7
1.2.3----R-S.12.9.1--.12+meta
1.2.3----RC-SNAPSHOT.12.9.1--.12+788
1.2.3----RC-SNAPSHOT.12.9.1--.12
1.2.3-SNAPSHOT-123
1.2.3-beta
1.2.3
2.0.0-rc.1+build.123
2.0.0+build.1848
2.0.0
2.0.1-alpha.1227
10.2.3-DEV-SNAPSHOT
10.20.30
99999999999999999999999.999999999999999999.99999999999999999
valid: |
0.0.4
1.2.3
10.20.30
1.1.2-prerelease+meta
1.1.2+meta
1.1.2+meta-valid
1.0.0-alpha
1.0.0-beta
1.0.0-alpha.beta
1.0.0-alpha.beta.1
1.0.0-alpha.1
1.0.0-alpha0.valid
1.0.0-alpha.0valid
1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay
1.0.0-rc.1+build.1
2.0.0-rc.1+build.123
1.2.3-beta
10.2.3-DEV-SNAPSHOT
1.2.3-SNAPSHOT-123
1.0.0
2.0.0
1.1.7
2.0.0+build.1848
2.0.1-alpha.1227
1.0.0-alpha+beta
1.2.3----RC-SNAPSHOT.12.9.1--.12+788
1.2.3----R-S.12.9.1--.12+meta
1.2.3----RC-SNAPSHOT.12.9.1--.12
1.0.0+0.build.1-rc.10000aaa-kk-0.1
99999999999999999999999.999999999999999999.99999999999999999
1.0.0-0A.is.legal

0 comments on commit 7cf7edd

Please sign in to comment.