Skip to content

Commit

Permalink
chore: Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bennetrr committed Mar 15, 2024
1 parent e97e56b commit 92bcd09
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def get_new_version(prefix: str, suffix: str, only_increase_suffix: bool) -> tup

next_version, has_changes = get_next_version(repo, current_version_tag, current_version)

logger.debug(
logger.info(
'current_version=%s, next_version=%s, has_changes=%s',
current_version, next_version, has_changes
)
Expand Down
62 changes: 38 additions & 24 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
#! /bin/bash
# Arg1: Test case name, Arg 2: Expected; Arg 3: Actual
function assert-equal() {

# assert-equal(test case name, expected value, actual value)
# Assert that both values are equal.
assert-equal() {
if [[ $3 != "$2" ]]; then
printf "\e[41mTest failed: %s, expected %s, got %s\e[0m\n\n" "$1" "$2" "$3"
else
printf "\e[42mTest successful: %s, expected %s\e[0m\n\n" "$1" "$2"
printf "\e[42mTest successful: %s, expected %s, got %s\e[0m\n\n" "$1" "$2" "$3"
fi
}

# Arg1: Test case name, Arg 2: Expected; Arg 3: Output to search in
# assert-contains(test case name, value, searching string)
# Assert that the searching string contains a value.
function assert-contains() {
if [[ $3 != *$2* ]]; then
printf "\e[41mTest failed: %s, expected %s in output, got:\n%s\e[0m\n\n" "$1" "$2" "$3"
else
printf "\e[42mTest successful: %s, expected %s\e[0m\n\n" "$1" "$2"
printf "\e[42mTest successful: %s, expected %s to be in string\e[0m\n\n" "$1" "$2"
fi
}

# assert-success(test case name)
# Assert that the last command had an exit code of 0
function assert-success() {
if [[ $? -ne 0 ]]; then
printf "\e[41mTest failed: %s, expected exit code 0, got %s\e[0m\n\n" "$1" "$?"
Expand All @@ -25,10 +30,9 @@ function assert-success() {
fi
}

source .test.env
source .venv/bin/activate

git clone "$TEST_REPOSITORY_URL" test
git init test
cd test || exit 1

# 1. No changes; expected: 0.0.0, no changes
Expand All @@ -37,58 +41,68 @@ git add .
git commit -m 'chore: test'
output=$(python3 ../src/app.py)
assert-success "No changes"
assert-contains "No changes" "0.0.0" "$output"
assert-contains "No changes" "No changes" "$output"
assert-contains "No changes" "next_version=0.0.0" "$output"
assert-contains "No changes" "has_changes=False" "$output"

# 2. Fix, expected: 0.0.1, changes
touch test.2
git add .
git commit -m 'fix: test'
output=$(python3 ../src/app.py --create-tag true)
assert-success "Fix"
assert-contains "Fix" "0.0.1" "$output"
assert-contains "Fix" "Changes detected" "$output"
assert-equal "Fix: Check git tag" "0.0.1" "$(git describe --tags)"
assert-contains "Fix" "next_version=0.0.1" "$output"
assert-contains "Fix" "has_changes=True" "$output"
assert-equal "Fix: Check git tag" "v0.0.1" "$(git describe --tags)"

# 3. Feature, expected: 0.1.0, changes
touch test.3
git add .
git commit -m 'feat: test'
output=$(python3 ../src/app.py --create-tag true)
assert-success "Feature"
assert-contains "Feature" "0.1.0" "$output"
assert-contains "Feature" "Changes detected" "$output"
assert-equal "Feature: Check git tag" "0.1.0" "$(git describe --tags)"
assert-contains "Feature" "next_version=0.1.0" "$output"
assert-contains "Feature" "has_changes=True" "$output"
assert-equal "Feature: Check git tag" "v0.1.0" "$(git describe --tags)"

# 4. Breaking Feature, expected: 1.0.0, changes
touch test.4
git add .
git commit -m 'feat!: test'
output=$(python3 ../src/app.py --create-tag true)
assert-success "Breaking Feature"
assert-contains "Breaking Feature" "1.0.0" "$output"
assert-contains "Breaking Feature" "Changes detected" "$output"
assert-equal "Breaking Feature: Check git tag" "1.0.0" "$(git describe --tags)"
assert-contains "Breaking Feature" "next_version=1.0.0" "$output"
assert-contains "Breaking Feature" "has_changes=True" "$output"
assert-equal "Breaking Feature: Check git tag" "v1.0.0" "$(git describe --tags)"

# 5. Hotfix, expected: 1.0.0-h.1, changes
touch test.5
git add .
git commit -m 'fix: test'
output=$(python3 ../src/app.py --create-tag true --suffix h --only-increase-suffix true)
assert-success "Hotfix 1"
assert-contains "Hotfix 1" "1.0.0-h.1" "$output"
assert-contains "Hotfix 1" "Changes detected" "$output"
assert-equal "Hotfix 1: Check git tag" "1.0.0-h.1" "$(git describe --tags)"
assert-contains "Hotfix 1" "Version is 1.0.0-h.1" "$output"
assert-contains "Hotfix 1" "has_changes=True" "$output"
assert-equal "Hotfix 1: Check git tag" "v1.0.0-h.1" "$(git describe --tags)"

# 6. Hotfix, expected: 1.0.0-h.2, changes
touch test.6
git add .
git commit -m 'fix: test'
output=$(python3 ../src/app.py --create-tag true --suffix h --only-increase-suffix true)
assert-success "Hotfix 2"
assert-contains "Hotfix 2" "1.0.0-h.2" "$output"
assert-contains "Hotfix 2" "Changes detected" "$output"
assert-equal "Hotfix 2: Check git tag" "1.0.0-h.2" "$(git describe --tags)"
assert-contains "Hotfix 2" "Version is 1.0.0-h.2" "$output"
assert-contains "Hotfix 2" "has_changes=True" "$output"
assert-equal "Hotfix 2: Check git tag" "v1.0.0-h.2" "$(git describe --tags)"

# 7. Feature, expected: 1.1.0, changes
touch test.7
git add .
git commit -m 'feat: test'
output=$(python3 ../src/app.py --create-tag true)
assert-success "Feature 2"
assert-contains "Feature 2" "next_version=1.1.0" "$output"
assert-contains "Feature 2" "has_changes=True" "$output"
assert-equal "Feature 2: Check git tag" "v1.1.0" "$(git describe --tags)"

printf "\e[43mPress any key to exit...\e[0m\n"
read -n 1 -r
Expand Down

0 comments on commit 92bcd09

Please sign in to comment.