From 92bcd09b2be1f42c8549964668bd0436847220df Mon Sep 17 00:00:00 2001 From: bennetrr Date: Fri, 15 Mar 2024 16:16:56 +0100 Subject: [PATCH] chore: Improve tests --- src/app.py | 2 +- test.sh | 62 +++++++++++++++++++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/app.py b/src/app.py index 2680db6..e3606c3 100644 --- a/src/app.py +++ b/src/app.py @@ -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 ) diff --git a/test.sh b/test.sh index 156368e..7e0cc6f 100644 --- a/test.sh +++ b/test.sh @@ -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" "$?" @@ -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 @@ -37,8 +41,8 @@ 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 @@ -46,9 +50,9 @@ 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 @@ -56,9 +60,9 @@ 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 @@ -66,9 +70,9 @@ 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 @@ -76,9 +80,9 @@ 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 @@ -86,9 +90,19 @@ 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