Skip to content

Commit

Permalink
Use bats for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
DragosDumitrache committed Jan 17, 2024
1 parent 47bef9b commit 2aed4c0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 95 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test-and-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ jobs:
fetch-depth: 0
- name: Run tests
run: |
./run_tests.sh
sudo apt-get upgrade && apt-get update
sudo apt-get install -Y bats
./test.sh
- uses: ./versioner/
id: versioner
- name: publish tag
Expand Down
99 changes: 55 additions & 44 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,57 +1,68 @@
#!/usr/bin/env bash
source /work/version.sh
#!/usr/bin/env bats
set -e
source ./version.sh

source critic.sh
@test "addition using bc" {
result="$(echo 2+2 | bc)"
[ "$result" -eq 4 ]
}

@test "Version if not git repo" {
result=$(semver "1")
[ "$result" == "1.0.0-SNAPSHOT" ]
}

_describe "Version if not git repo"
_test "Should be 1.0.0-SNAPSHOT" semver "1"
_assert _output_equals "1.0.0-SNAPSHOT"
@test "Version on first commit default branch" {
result=$(_calculate_version "master" "vskd341" "0" "0" "master")
[ "$result" == "0.0.0" ]
}

_describe "Version on first commit default branch"
_test "Should be 0.0.0" calculate_version "master" "vskd341" "0" "0" "master"
_assert _output_equals "0.0.0"
@test "Version on second commit default branch" {
result=$(_calculate_version "master" "0.0.2" "0" "0" "master")
[ "$result" == "0.0.2" ]
}

_describe "Version on second commit default branch"
_test "Should be 0.0.2" calculate_version "master" "0.0.2" "0" "0" "master"
_assert _output_equals "0.0.2"
@test "Version on fifth commit default branch" {
result=$(_calculate_version "master" "0.0.6" "0" "0" "master")
[ "$result" == "0.0.6" ]
}

_describe "Version on fifth commit default branch"
_test "Should be 0.0.6" calculate_version "master" "0.0.6" "0" "0" "master"
_assert _output_equals "0.0.6"
@test "Version on with different minor on default branch" {
result=$(_calculate_version "master" "0.0.1-5-aaghas27" "0" "1" "master")
[ "$result" == "0.1.0" ]
}

_describe "Version on with different minor on default branch"
_test "Should be 0.1.0" calculate_version "master" "0.0.1-5-aaghas27" "0" "1" "master"
_assert _output_equals "0.1.0"
@test "Version on with different major on default branch" {
result=$(_calculate_version "master" "0.0.1-5-aaghas27" "1" "0" "master")
[ "$result" == "1.0.0" ]
}

_describe "Version on with different major on default branch"
_test "Should be 1.0.0" calculate_version "master" "0.0.1-5-aaghas27" "1" "0" "master"
_assert _output_equals "1.0.0"
@test "Version on with different major and minor on default branch" {
result=$(_calculate_version "master" "0.0.1-5-aaghas27" "1" "1" "master")
[ "$result" == "1.1.0" ]
}

_describe "Version on with different major and minor on default branch"
_test "Should be 1.1.0" calculate_version "master" "0.0.1-5-aaghas27" "1" "1" "master"
_assert _output_equals "1.1.0"
@test "Version on second commit on feature branch" {
result=$(_calculate_version "feature" "0.0.1-1-aaghas27" "0" "0" "master")
[ "$result" == "0.0.1-1-aaghas27-feature" ]
}

_describe "Version on second commit on feature branch"
_test "Should be 0.0.1-1-aaghas27-feature" calculate_version "feature" "0.0.1-1-aaghas27" "0" "0" "master"
_assert _output_equals "0.0.1-1-aaghas27-feature"
@test "Version on fifth commit on feature branch" {
result=$(_calculate_version "feature" "0.0.1-5-aaghas27" "0" "0" "master")
[ "$result" == "0.0.1-5-aaghas27-feature" ]
}

_describe "Version on fifth commit on feature branch"
_test "Should be 0.0.1-5-aaghas27-feature" calculate_version "feature" "0.0.1-5-aaghas27" "0" "0" "master"
_assert _output_equals "0.0.1-5-aaghas27-feature"
@test "Version on feature branch with different minor" {
result=$(_calculate_version "feature" "0.0.1-5-aaghas27" "0" "1" "master")
[ "$result" == "0.0.1-5-aaghas27-feature" ]
}

_describe "Version on feature branch with different minor"
_test "Should be 0.0.1-5-aaghas27-feature" calculate_version "feature" "0.0.1-5-aaghas27" "0" "1" "master"
_assert _output_equals "0.0.1-5-aaghas27-feature"
@test "Version on feature branch with different major" {
result=$(_calculate_version "feature" "0.0.1-5-aaghas27" "1" "0" "master")
[ "$result" == "0.0.1-5-aaghas27-feature" ]
}

_describe "Version on feature branch with different major"
_test "Should be 0.0.1-5-aaghas27-feature" calculate_version "feature" "0.0.1-5-aaghas27" "1" "0" "master"
_assert _output_equals "0.0.1-5-aaghas27-feature"

_describe "Version on feature branch with different major and minor"
_test "Should be 0.0.1-5-aaghas27-feature" calculate_version "feature" "0.0.1-5-aaghas27" "1" "1" "master"
_assert _output_equals "0.0.1-5-aaghas27-feature"

_describe "Version on default branch even if tag starts with v"
_test "Should be 0.0.2" calculate_version "master" "0.0.2" "0" "0" "master"
_assert _output_equals "0.0.2"
@test "Version on feature branch with different major and minor" {
result=$(_calculate_version "feature" "0.0.1-5-aaghas27" "1" "1" "master")
[ "$result" == "0.0.1-5-aaghas27-feature" ]
}
74 changes: 24 additions & 50 deletions version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#source lib.sh

function initialise_version() {
function _initialise_version() {
# This will initialise your repository for versioner to be able to consume
if [[ ! -f "version.json" ]]; then
cat <<EOF >version.json
Expand All @@ -16,67 +16,41 @@ EOF
}

# Function to check if the current directory is a git repository
function is_git_repository() {
function _is_git_repository() {
[ -d ".git" ]
}

function sanitise_version {
raw_version=$1
no_v=${raw_version#v}
no_sha=${no_v%-*}
if [[ $no_sha != "$no_v" ]]; then
no_extra_patch=${no_sha%-*}
major=$(echo "$no_sha" | cut -d '.' -f1)
minor=$(echo "$no_sha" | cut -d '.' -f2)
patch_number=$(echo "$no_extra_patch" | cut -d '.' -f3)
if [[ $major -lt $future_major ]]; then
echo "${future_major}.${future_minor}.0"
else
if [[ $minor -lt $future_minor ]]; then
echo "${major}.${future_minor}.0"
else
git_generated_version="${major}.${minor}.${patch_number}"
echo "$git_generated_version"
fi
fi
else
major=$(echo "$no_sha" | cut -d '.' -f1)
minor=$(echo "$no_sha" | cut -d '.' -f2)
patch_number=$(echo "$no_sha" | cut -d '.' -f3)
if [[ $major -lt $future_major ]]; then
echo "${future_major}.${future_minor}.0"
else
if [[ $minor -lt $future_minor ]]; then
echo "${major}.${future_minor}.0"
else
git_generated_version="${major}.${minor}.${patch_number}"
echo "$git_generated_version"
fi
fi
fi
}

function calculate_version {
function _calculate_version {
git_branch=$1
git_generated_version=$2
future_major=$3
future_minor=$4
next_major=$3
next_minor=$4
default_branch=$5
if [[ ! "${git_generated_version}" =~ ^v?.+\..+\.[^-]+ ]]; then
git_generated_version="${future_major}.${future_minor}.0"
# This case should never happen
git_generated_version="${next_major}.${next_minor}.0"
fi
final_version=$git_generated_version
if [[ $git_branch == "$default_branch" ]]; then
final_version=$(sanitise_version "$git_generated_version")
major=$(echo "$final_version" | cut -d '.' -f1)
minor=$(echo "$final_version" | cut -d '.' -f2)
patch_number=$(echo "$final_version" | cut -d '.' -f3)
if [[ $major -lt $next_major ]]; then
final_version="${next_major}.${next_minor}.0"
else
if [[ $minor -lt $next_minor ]]; then
final_version="${major}.${next_minor}.0"
else
final_version="${major}.${minor}.${patch_number}"
fi
fi
else
final_version="${git_generated_version}-${git_branch}"
final_version=${final_version#v}
fi

echo "$final_version" | tr -d '[:space:]'
}

function extract_branch_name {
function _extract_branch_name {
git_branch=$(git branch --show-current)
if [[ ! $git_branch ]]; then
git_branch=$(git name-rev --name-only $(git show -s --format=%H))
Expand All @@ -95,9 +69,9 @@ function semver {
fi

# If no version.json file is present, add it, then proceed to calculate the version
initialise_version
_initialise_version

git_branch=$(extract_branch_name)
git_branch=$(_extract_branch_name)

next_major=$(cat version.json | jq ".major" --raw-output)
next_minor=$(cat version.json | jq ".minor" --raw-output)
Expand All @@ -118,11 +92,11 @@ function semver {
next_version="${next_major}.${next_minor}.${next_patch}"
fi

final_version=$(calculate_version "$git_branch" "$next_version" "$next_major" "$next_minor" "$default_branch")
final_version=$(_calculate_version "$git_branch" "$next_version" "$next_major" "$next_minor" "$default_branch")
echo "$final_version"
}


if [ "${BASH_SOURCE[0]}" = "$0" ]; then
semver "$(is_git_repository)"
semver "$(_is_git_repository)"
fi

0 comments on commit 2aed4c0

Please sign in to comment.