Skip to content

Commit

Permalink
Use default branch instead of master #14
Browse files Browse the repository at this point in the history
Remove hardcoded 'master' branch from `git push` calls. It allows
flexibility of working with any default branch naming such as `main`.

Remove branch name when pushing tag. A tag and a branch don't have a
direct relationship. Pushing a tag does not have anything to do with
any branch. The tag points to a commit. It does not care which branch
the commit is reachable from. The commit may be on master and/or on
another branch but it's none of the tag's business.

Remove hardcored master when pushing changes. `git push` without a
specified branch will push changes on the local branch to matching
remote branch anyway.

Add related tests and extend test logic to support the new tests.
  • Loading branch information
undergroundwires committed Mar 3, 2022
1 parent 6c2963d commit 2579bbf
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 151 deletions.
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Expand Up @@ -14,6 +14,15 @@ You can contribute by:

If you're using visual studio code, [recommended extensions](./.vscode/extensions.json) would install useful linters and extensions.

## Tests

**Automated tests**:

- Test all: `bash ./tests/run.sh`.
- Defined as `.test.sh` files in [`./tests`](./tests/).

- **Manual tests**: See documentation in [docker-entrypoint](./tests/docker-entrypoint)

## Style guide

- Do not introduce any TODO comments, fix them or do not introduce the change.
Expand Down
9 changes: 0 additions & 9 deletions README.md
Expand Up @@ -152,15 +152,6 @@ You can also use following scripts individually (check script files for usage, p

[](#bump-everywhere)

## Tests

- **Automated tests**
- Test all: `bash ./tests/run.sh`
- Defined as `.test.sh` files in [`./tests`](./tests/)
- **Manual tests**: See documentation in [docker-entrypoint](./tests/docker-entrypoint)

[](#bump-everywhere)

## GitOps

CI/CD is fully automated for this repo using different GIT events and GitHub actions.
Expand Down
14 changes: 7 additions & 7 deletions scripts/bump-and-tag-version.sh
Expand Up @@ -21,13 +21,13 @@ readonly DEFAULT_VERSION="0.1.0"
source "$SCRIPTS_DIRECTORY/shared/utilities.sh"

tag_and_push() {
local -r tag="$1"
echo "Creating tag: \"$tag\""
git tag "$tag" \
|| { echo "Could not tag: \"$tag\"" ; exit 1; }
git push -u origin master "$tag" \
|| { echo "Could not push the tag: \"$tag\""; exit 1; }
echo "Tag created and pushed: \"$tag\""
local -r tag="$1"
echo "Creating tag: \"$tag\""
git tag "$tag" \
|| { echo "Could not tag: \"$tag\"" ; exit 1; }
git push -u origin "$tag" \
|| { echo "Could not push the tag: \"$tag\""; exit 1; }
echo "Tag created and pushed: \"$tag\""
}

increase_patch_version() {
Expand Down
2 changes: 1 addition & 1 deletion scripts/bump-everywhere.sh
Expand Up @@ -133,7 +133,7 @@ commit_and_push() {
local -r printable_commit_message=${commit_message/$VERSION_PLACEHOLDER/$latest_version_tag}
git commit -m "$printable_commit_message" \
|| { echo "Could not commit wit the message: $printable_commit_message"; exit 1; }
git push -u origin master \
git push -u origin \
|| { echo "Could not git push changes"; exit 1; }
fi
}
Expand Down
158 changes: 111 additions & 47 deletions tests/bump-and-tag-version.test.sh
Expand Up @@ -6,56 +6,120 @@ readonly SELF_DIRECTORY=$(dirname "$0")
source "$SELF_DIRECTORY/test-utilities.sh"

main() {
run_test test_increase_patch_version
test_utilities::run_tests \
test_increase_patch_version \
test_sets_initial_version \
test_tags_within_any_branch
}

test_tags_within_any_branch() {
# Arrange
local -r branch_name='uncommon/branch-name'
initialize_empty_repository "$branch_name"
git commit --message "Tagged commit" --allow-empty || { echo '😢 Cannot do first commit'; return 1; }
local -r expected_commit_sha_to_tag="$(git rev-parse HEAD)"

# Act
if ! run_sut; then
return 1
fi

# Assert
local -r tag="$(git describe --tags --abbrev=0)"
local -r actual_commit_sha=$(git rev-parse "$tag")
if ! test_utilities::assert_equals "$expected_commit_sha_to_tag" "$actual_commit_sha" 'Unexpected commit.'; then
((total_errors++))
fi
}

test_sets_initial_version() {
# Arrange
local -r expected_tag='0.1.0'

initialize_empty_repository 'master'
git commit --message "Tagged commit" --allow-empty || { echo '😢 Cannot do first commit'; return 1; }
local -r expected_commit_sha_to_tag="$(git rev-parse HEAD)"

# Act
if ! run_sut; then
return 1
fi

# Assert
assert_expected_latest_tag "$expected_tag" "$expected_commit_sha_to_tag"
return "$?"
}

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 "test@privacy.sexy" || { 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
local -i result=0

test_version_increase '1.5.3' '1.5.4'
((result+=$?))

test_version_increase '0.0.0' '0.0.1'
((result+=$?))

test_version_increase '1.22.333' '1.22.334'
((result+=$?))

test_version_increase '31.31.31' '31.31.32'
((result+=$?))

return "$result"
}

test_version_increase() {
# Arrange
local -r given="$1"
local -r expected_tag="$2"

initialize_empty_repository 'master'
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; }
local -r expected_commit_sha_to_tag="$(git rev-parse HEAD)"

# Act
if ! run_sut; then
return 1
fi

# Assert
assert_expected_latest_tag "$expected_tag" "$expected_commit_sha_to_tag"
return "$?"
}

initialize_empty_repository() {
local -r branch_name="$1"
rm -rf .git
git init -b "$branch_name" --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 "test@privacy.sexy" || { echo '😢 Could not set user e-mail'; return 1; }
git config --local user.name "Test User" || { echo '😢 Could not set user name'; return 1; }
}

run_sut() {
test_utilities::run_script 'bump-and-tag-version.sh'
return "$?"
}

assert_expected_latest_tag() {
local -r expected_tag="$1"
local -r expected_commit_sha_to_tag="$2"

local -i total_errors=0

local -r actual_tag="$(git describe --tags --abbrev=0)"
if ! test_utilities::assert_equals "$expected_tag" "$actual_tag" 'Unexpected tag.'; then
((total_errors++))
fi

local -r actual_commit_sha=$(git rev-parse "$actual_tag")
if ! test_utilities::assert_equals "$expected_commit_sha_to_tag" "$actual_commit_sha" 'Unexpected commit.'; then
((total_errors++))
fi

return "$total_errors"
}

main
106 changes: 49 additions & 57 deletions tests/bump-npm-version.test.sh
Expand Up @@ -5,76 +5,68 @@ readonly SELF_DIRECTORY=$(dirname "$0")
# shellcheck source=test-utilities.sh
source "$SELF_DIRECTORY/test-utilities.sh"

test() {
# arrange
local sut
local -r initial_version='0.1.0'
local -r new_version='0.2.10'
local -r expected_package_content=$(print_package_json "$new_version")
local -r expected_package_lock_content=$(print_package_lock_json "$new_version")
sut=$(get_absolute_sut_path "bump-npm-version.sh") || { echo '😢 Could not locate sut'; return 1; }
echo "Sut: $sut"
setup_git_env "$initial_version" "$new_version" || { echo "Could not setup the test environment"; return 1; }
main() {
test_utilities::run_tests test_json_version_bump
}

# act
echo "Setup repository"
if ! bash "$sut"; then
echo "Unexpected exit code: $?";
return 1;
fi
test_json_version_bump() {
# Arrange
local -r initial_version='0.1.0'
local -r new_version='0.2.10'
local -r expected_package_content=$(print_package_json "$new_version")
local -r expected_package_lock_content=$(print_package_lock_json "$new_version")
setup_git_env "$initial_version" "$new_version" || { echo "Could not setup the test environment"; return 1; }

# assert
local -r current_dir="$(pwd)"
echo "$current_dir"
local -r package_content="$(cat 'package.json')"
local -r package_lock_content="$(cat 'package-lock.json')"
local -i status=0
are_equal_json 'package.json' "$package_content" "$expected_package_content" || status=1
are_equal_json 'package-lock.json' "$package_lock_content" "$expected_package_lock_content" || status=1
return $status
}
# Act
echo "Setup repository"
if ! test_utilities::run_script 'bump-npm-version.sh'; then
echo 'Running sut failed.'
return 1;
fi

setup_git_env() {
local -r initial_version="$1"
local -r new_version="$2"
echo "Setting up git environment, initial version: \"$initial_version\", new version: \"$new_version\""
git init -b master --quiet || { echo '😢 Could not initialize git repository'; return 1; }
git config --local user.email "test@privacy.sexy" || { echo '😢 Could not set user e-mail'; return 1; }
git config --local user.name "Test User" || { echo '😢 Could not set user name'; return 1; }
print_package_json "$initial_version" > "package.json"
print_package_lock_json "$initial_version" > "package-lock.json"
git add . || { echo '😢 Could not add git changes'; return 1; }
git commit -m "inital commit" --quiet || { echo '😢 Could not do initial commit'; return 1; }
git tag "$initial_version" || { echo '😢 Could tag first version'; return 1; }
git commit -m "next commit" --allow-empty --quiet || { echo '😢 Could not do next commit'; return 1; }
git tag "$new_version" || { echo '😢 Could tag next version'; return 1; }
return 0
# Assert
local -r current_dir="$(pwd)"
echo "$current_dir"
local -r package_content="$(cat 'package.json')"
local -r package_lock_content="$(cat 'package-lock.json')"
local -i status=0
assert_equals_json 'package.json' "$package_content" "$expected_package_content" || status=1
assert_equals_json 'package-lock.json' "$package_lock_content" "$expected_package_lock_content" || status=1
return $status
}

main() {
run_test test
setup_git_env() {
local -r initial_version="$1"
local -r new_version="$2"
echo "Setting up git environment, initial version: \"$initial_version\", new version: \"$new_version\""
git init -b master --quiet || { echo '😢 Could not initialize git repository'; return 1; }
git config --local user.email "test@privacy.sexy" || { echo '😢 Could not set user e-mail'; return 1; }
git config --local user.name "Test User" || { echo '😢 Could not set user name'; return 1; }
print_package_json "$initial_version" > "package.json"
print_package_lock_json "$initial_version" > "package-lock.json"
git add . || { echo '😢 Could not add git changes'; return 1; }
git commit -m "inital commit" --quiet || { echo '😢 Could not do initial commit'; return 1; }
git tag "$initial_version" || { echo '😢 Could tag first version'; return 1; }
git commit -m "next commit" --allow-empty --quiet || { echo '😢 Could not do next commit'; return 1; }
git tag "$new_version" || { echo '😢 Could tag next version'; return 1; }
return 0
}

print_package_json() {
local -r version="$1"
echo "{ \"version\": \"$version\" }"
local -r version="$1"
echo "{ \"version\": \"$version\" }"
}

print_package_lock_json() {
local -r version="$1"
echo "{ \"version\": \"$version\", \"dependencies\": { \"ez-consent\": { \"version\": \"1.2.1\" } } }"
local -r version="$1"
echo "{ \"version\": \"$version\", \"dependencies\": { \"ez-consent\": { \"version\": \"1.2.1\" } } }"
}

are_equal_json() {
local -r name="$1"
local -r actual="$(echo "$2" | jq -cS)"
local -r expected="$(echo "$3" | jq -cS)"
if [ "$actual" == "$expected" ]; then
return 0
else
echo "Unexpected $name. Actual: \"$actual\". Expected: \"$expected\""
return 1
fi
assert_equals_json() {
local -r name="$1"
local -r actual="$(echo "$2" | jq -cS)"
local -r expected="$(echo "$3" | jq -cS)"
test_utilities::assert_equals "$expected" "$actual" "Unexpected $name."
}

main
9 changes: 5 additions & 4 deletions tests/run.sh 100644 → 100755
@@ -1,7 +1,9 @@
#!/usr/bin/env bash

# Import dependencies
readonly SELF_DIRECTORY=$(dirname "$0")

# shellcheck source=test-utilities.sh
source "$SELF_DIRECTORY/test-utilities.sh"

main() {
local succeeded_tests=()
Expand All @@ -21,8 +23,7 @@ main() {
failed_tests+=("$file")
echo $'\t'"🔴 Failed."
fi
# shellcheck disable=SC2001
echo "$output" | sed 's/^/\t/' # Tab indent output
echo "$output" | test_utilities::tab_indent_to_right
done

# Report
Expand All @@ -47,4 +48,4 @@ main() {
}


main
main

0 comments on commit 2579bbf

Please sign in to comment.