-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This was caused by unintended escape where word splitting was intended. Rewrite the logic with more robust implementation with clear word splitting using `read`. Add tests for the bug, automate test run and reuse some of test logic in tests. Finally, introduce introduce `tests/run.sh` to automatically discover and run tests.
- Loading branch information
1 parent
51e11bf
commit 1bdaeef
Showing
7 changed files
with
178 additions
and
47 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 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,61 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Import dependencies | ||
readonly SELF_DIRECTORY=$(dirname "$0") | ||
# shellcheck source=tests/test-utilities.sh | ||
source "$SELF_DIRECTORY/test-utilities.sh" | ||
|
||
main() { | ||
run_test test_increase_patch_version | ||
} | ||
|
||
test_increase_patch_version() { | ||
local -i result=0 | ||
|
||
expect_version '1.5.3' '1.5.4' | ||
((result+=$?)) | ||
|
||
expect_version '0.0.0' '0.0.1' | ||
((result+=$?)) | ||
|
||
expect_version '1.22.333' '1.22.334' | ||
((result+=$?)) | ||
|
||
expect_version '31.31.31' '31.31.32' | ||
((result+=$?)) | ||
|
||
return "$result" | ||
} | ||
|
||
expect_version() { | ||
# Prepare | ||
local -r given="$1" | ||
local -r expected="$2" | ||
local actual | ||
|
||
rm -rf .git | ||
git init -b master --quiet || { echo '😢 Could not initialize git repository'; return 1; } | ||
git remote add origin "$(pwd)" || { echo "😢 Could not add fake origin: $(pwd)"; return 1; } | ||
git config --local user.email "[email protected]" || { echo '😢 Could not set user e-mail'; return 1; } | ||
git config --local user.name "Test User" || { echo '😢 Could not set user name'; return 1; } | ||
git commit --message "Tagged commit" --allow-empty || { echo '😢 Cannot do first commit'; return 1; } | ||
git tag "$given" || { echo "😢 Cannot tag $given"; return 1; } | ||
git commit --message "Empty commit" --allow-empty || { echo '😢 Cannot do second commit'; return 1; } | ||
|
||
# Act | ||
local -r sut=$(get_absolute_sut_path bump-and-tag-version.sh) || { echo '😢 Could not locate sut'; return 1; } | ||
echo "Sut: $sut" | ||
bash "$sut" | ||
|
||
# Assert | ||
local -r actual="$(git describe --tags --abbrev=0)" | ||
if [[ "$actual" == "$expected" ]]; then | ||
echo -e "Success: $given returned $actual as expected." | ||
return 0 | ||
else | ||
echo "Fail: Given $given, $expected (expected) != $actual (actual)." | ||
return 1 | ||
fi | ||
} | ||
|
||
main |
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,50 @@ | ||
#!/usr/bin/env bash | ||
|
||
readonly SELF_DIRECTORY=$(dirname "$0") | ||
|
||
|
||
main() { | ||
local succeeded_tests=() | ||
local failed_tests=() | ||
local -i test_index=0 | ||
|
||
# Run | ||
files=$(find "$SELF_DIRECTORY" -name "*.test.sh") | ||
for file in $files; do | ||
((test_index++)) | ||
echo "$test_index. Testing: $file" | ||
local output | ||
if output=$(bash "$file" 2>&1); then | ||
succeeded_tests+=("$file") | ||
echo $'\t'"🟢 Succeeded." | ||
else | ||
failed_tests+=("$file") | ||
echo $'\t'"🔴 Failed." | ||
fi | ||
# shellcheck disable=SC2001 | ||
echo "$output" | sed 's/^/\t/' # Tab indent output | ||
done | ||
|
||
# Report | ||
echo "-----------" | ||
echo "Total test: $test_index" | ||
local -ir total_failed="${#failed_tests[@]}" | ||
if [ "$total_failed" -gt 0 ]; then | ||
echo "Failed tests ($total_failed):" | ||
printf '\t- %s\n' "${failed_tests[@]}" | ||
else | ||
echo "🎉 No tests are failed!" | ||
fi | ||
local -ir total_succeeded="${#succeeded_tests[@]}" | ||
if [ "$total_succeeded" -gt 0 ]; then | ||
echo "Succeeded tests ($total_succeeded):" | ||
printf '\t- %s\n' "${succeeded_tests[@]}" | ||
else | ||
echo "😢 No tests are succeeded!" | ||
fi | ||
|
||
exit "$total_failed" | ||
} | ||
|
||
|
||
main |
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,43 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Globals | ||
readonly ABSOLUTE_SELF_DIRECTORY=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
|
||
|
||
get_absolute_sut_path() | ||
{ | ||
local -r path_from_scripts="$1" | ||
local -r script_dir="$ABSOLUTE_SELF_DIRECTORY/../scripts" | ||
local normalized | ||
if ! normalized=$(cd "${script_dir}" || return 1;pwd; return 0); then | ||
echo "Dir does not exist: ${script_dir}" | ||
return 1 | ||
fi | ||
local -r script_path="$normalized/$path_from_scripts" | ||
echo "$script_path" | ||
} | ||
|
||
# Takes test function delegate as argument and then: | ||
# 1. Creates a temporary directory, navigates to it | ||
# 2. Runs the test | ||
# 3. Cleans temporary directory, returns the test result | ||
run_test() { | ||
# Beging | ||
local -r temp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'bumpeverywheretmpdir') | ||
echo "Test dir: \"$temp_dir\"" | ||
if ! cd "$temp_dir"; then | ||
echo "😢 Could not navigate to $temp_dir" | ||
return 1 | ||
fi | ||
|
||
# Test | ||
$1 | ||
local -i -r test_exit_code="$?" | ||
echo "Test exit code: $test_exit_code" | ||
|
||
# Clean | ||
rm -rf "$temp_dir" | ||
echo "Cleaned \"$temp_dir\"" | ||
exit "$test_exit_code" | ||
} | ||
|