diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..bba7903 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,30 @@ +name: tests + +on: push + +jobs: + tests: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Install Shell Mock + run: | + git clone https://github.com/capitalone/bash_shell_mock + cd bash_shell_mock + sudo ./install.sh /usr/local + + - name: Where is shellmock? + run: which shellmock + + - name: GitHub Detection + run: | + pwd + + - name: Install BATS + run: yarn install + + - name: Run Tests + run: yarn test + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e3fe73b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +tests/test_repo +tests/tmpstubs +tests/shellmock.* diff --git a/entrypoint.sh b/entrypoint.sh index f51521a..e018310 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -68,7 +68,7 @@ _tag_commit() { echo "::debug::Create tag $INPUT_TAGGING_MESSAGE"; git -c user.name="$INPUT_COMMIT_USER_NAME" -c user.email="$INPUT_COMMIT_USER_EMAIL" tag -a "$INPUT_TAGGING_MESSAGE" -m "$INPUT_TAGGING_MESSAGE"; else - echo " No tagging message supplied. No tag will be added."; + echo "No tagging message supplied. No tag will be added."; fi } diff --git a/package.json b/package.json new file mode 100644 index 0000000..68fc437 --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "devDependencies": { + "bats": "^1.1.0" + }, + "scripts": { + "test": "bats tests" + } +} diff --git a/tests/commit.bats b/tests/commit.bats new file mode 100644 index 0000000..9133547 --- /dev/null +++ b/tests/commit.bats @@ -0,0 +1,121 @@ +#!/usr/bin/env bats + +setup() { + . shellmock + + # Build World + export test_repository="${BATS_TEST_DIRNAME}/test_repo" + + rm -rf "${test_repository}" + mkdir "${test_repository}" + touch "${test_repository}"/{a,b,c}.txt + cd "${test_repository}" + + git init --quiet + git add . > /dev/null 2>&1 + git -c user.name="Test Suite" -c user.email="test@github.com" commit --quiet -m "Init Repo" + + # Set default INPUT variables + export INPUT_REPOSITORY="${BATS_TEST_DIRNAME}/test_repo" + export INPUT_COMMIT_MESSAGE="Commit Message" + export INPUT_BRANCH="master" + export INPUT_COMMIT_OPTIONS="" + export INPUT_FILE_PATTERN="." + export INPUT_COMMIT_USER_NAME="Test Suite" + export INPUT_COMMIT_USER_EMAIL="test@github.com" + export INPUT_COMMIT_AUTHOR="Test Suite " + export INPUT_TAGGING_MESSAGE="" + export INPUT_PUSH_OPTIONS="" + export INPUT_SKIP_DIRTY_CHECK=false +} + +teardown() { + if [ -z "$TEST_FUNCTION" ]; then + shellmock_clean + fi + + rm -rf "${test_repository}" +} + +main() { + bash "${BATS_TEST_DIRNAME}"/../entrypoint.sh +} + + +@test "clean-repo-prints-nothing-to-commit-message" { + + run main + + [ "$status" -eq 0 ] + [ "${lines[0]}" = "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}" ] + [ "${lines[1]}" = "::set-output name=changes_detected::false" ] + [ "${lines[2]}" = "Working tree clean. Nothing to commit." ] +} + +# TODO: Fix Issue where changes in git repo are not detected +# @test "commit-changed-files-and-push-to-remote" { + +# touch "${test_repository}"/new-file-{1,2,3}.txt + +# shellmock_expect git --type partial --match "status" +# shellmock_expect git --type partial --match "checkout" +# shellmock_expect git --type partial --match "add" +# shellmock_expect git --type partial --match '-c' +# shellmock_expect git --type partial --match 'push origin' + +# run main + +# echo "$output" + +# # Success Exit Code +# [ "$status" = 0 ] + +# [ "${lines[0]}" = "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}" ] +# [ "${lines[1]}" = "::set-output name=changes_detected::true" ] +# [ "${lines[2]}" = "INPUT_BRANCH value: master" ] +# [ "${lines[3]}" = "INPUT_FILE_PATTERN: ." ] +# [ "${lines[4]}" = "INPUT_COMMIT_OPTIONS: " ] +# [ "${lines[5]}" = "::debug::Apply commit options " ] + + +# shellmock_verify +# [ "${capture[0]}" = "git-stub status -s -- ." ] +# [ "${capture[1]}" = "git-stub checkout master" ] +# [ "${capture[2]}" = "git-stub add ." ] +# [ "${capture[3]}" = "git-stub -c user.name=Test Suite -c user.email=test@github.com commit -m Commit Message --author=Test Suite " ] +# [ "${capture[4]}" = "git-stub push --set-upstream origin HEAD:master --tags" ] +# } + + +@test "skip-dirty-on-clean-repo-failure" { + + INPUT_SKIP_DIRTY_CHECK=true + + shellmock_expect git --type exact --match "status -s ." + shellmock_expect git --type exact --match "checkout master" + shellmock_expect git --type exact --match "add ." + shellmock_expect git --type partial --match '-c' + shellmock_expect git --type partial --match 'push origin' + + run main + + echo "$output" + + shellmock_verify + [ "${capture[0]}" = "git-stub status -s -- ." ] + [ "${capture[1]}" = "git-stub checkout master" ] + [ "${capture[2]}" = "git-stub add ." ] + [ "${capture[3]}" = "git-stub -c user.name=Test Suite -c user.email=test@github.com commit -m Commit Message --author=Test Suite " ] + [ "${capture[4]}" = "git-stub push --set-upstream origin HEAD:master --tags" ] + + # Failed Exit Code + [ "$status" -ne 0 ] + + [ "${lines[0]}" = "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}" ] + [ "${lines[1]}" = "::set-output name=changes_detected::true" ] + [ "${lines[2]}" = "INPUT_BRANCH value: master" ] + [ "${lines[3]}" = "INPUT_FILE_PATTERN: ." ] + [ "${lines[4]}" = "INPUT_COMMIT_OPTIONS: " ] + [ "${lines[5]}" = "::debug::Apply commit options " ] +} + diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..74b9b82 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +bats@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/bats/-/bats-1.1.0.tgz#6fc44f283ed4e7af2b6ffac93ec5026a1acbdc66" + integrity sha512-1pA29OhDByrUtAXX+nmqZxgRgx2y8PvuZzbLJVjd2dpEDVDvz0MjcBMdmIPNq5lC+tG53G+RbeRsbIlv3vw7tg==