diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index a32837d..32cb299 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -3,6 +3,32 @@ name: Testing on: [push, pull_request] jobs: + test: + name: Run tests + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v1 + + - name: Set up Python + uses: actions/setup-python@v2 + + - name: Install codespell + run: pip3 --quiet --quiet install codespell + + - name: Install Bats + run: | + git clone --quiet https://github.com/bats-core/bats-core.git + cd bats-core + git fetch --tags + # Checkout the latest tag + git checkout --quiet $(git describe --tags `git rev-list --tags --max-count=1`) + sudo ./install.sh "/usr/local" > /dev/null + + - name: Run Bats tests + run: bats "./test" + run_action: name: Test run action runs-on: ubuntu-latest @@ -10,5 +36,5 @@ jobs: - uses: actions/checkout@v1 - uses: ./ with: - path: example + path: test/testdata only_warn: 1 diff --git a/entrypoint.sh b/entrypoint.sh index 7c486e0..203cace 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -33,8 +33,9 @@ else echo "Codespell found one or more problems" fi -# Remove the matcher, so no other jobs hit it. -echo "::remove-matcher owner=codespell::" +# Remove the matchers, so no other jobs hit them. +echo "::remove-matcher owner=codespell-matcher-default::" +echo "::remove-matcher owner=codespell-matcher-specified::" # If we are in warn-only mode, return always as if we pass if [ -n "${INPUT_ONLY_WARN}" ]; then diff --git a/test/exclude-file.txt b/test/exclude-file.txt new file mode 100644 index 0000000..795e9e7 --- /dev/null +++ b/test/exclude-file.txt @@ -0,0 +1,2 @@ +1 abandonned 1 +2 abandonned 2 diff --git a/test/test.bats b/test/test.bats new file mode 100644 index 0000000..ea6b853 --- /dev/null +++ b/test/test.bats @@ -0,0 +1,84 @@ +#!/usr/bin/env bats + +# Tests using the Bats testing framework +# https://github.com/bats-core/bats-core + +ROOT_MISSPELLING_COUNT=5 +FILENAME_MISSPELLING_COUNT=1 +HIDDEN_MISSPELLING_COUNT=1 +EXCLUDED_MISSPELLING_COUNT=1 +SUBFOLDER_MISSPELLING_COUNT=1 + +export RUNNER_TEMP="/foo/runner_temp" + +# This function runs before every test +function setup() { + # Set default input values + export INPUT_CHECK_FILENAMES="" + export INPUT_CHECK_HIDDEN="" + export INPUT_EXCLUDE_FILE="" + export INPUT_PATH="./test/testdata" + export INPUT_ONLY_WARN="" +} + +@test "Run with defaults" { + # codespell's exit status is the number of misspelled words found + expectedExitStatus=$((ROOT_MISSPELLING_COUNT + HIDDEN_MISSPELLING_COUNT + SUBFOLDER_MISSPELLING_COUNT)) + run "./entrypoint.sh" + [ $status -eq $expectedExitStatus ] + + # Check output + [ "${lines[1]}" == "::add-matcher::${RUNNER_TEMP}/_github_workflow/codespell-matcher.json" ] + outputRegex="^Running codespell on '${INPUT_PATH}'" + [[ "${lines[2]}" =~ $outputRegex ]] + [ "${lines[-3]}" == "Codespell found one or more problems" ] + [ "${lines[-2]}" == "::remove-matcher owner=codespell-matcher-default::" ] + [ "${lines[-1]}" == "::remove-matcher owner=codespell-matcher-specified::" ] +} + +@test "Check file names" { + expectedExitStatus=$((ROOT_MISSPELLING_COUNT + HIDDEN_MISSPELLING_COUNT + SUBFOLDER_MISSPELLING_COUNT + FILENAME_MISSPELLING_COUNT)) + INPUT_CHECK_FILENAMES=true + run "./entrypoint.sh" + [ $status -eq $expectedExitStatus ] +} + +@test "Check a hidden file" { + # codespell --check-hidden has a somewhat unintuitive behavior. When run on + # a directory, codespell works on hidden files even if this option is not + # enabled. It's only when INPUT_PATH points directly at a hidden file that + # codespell ignores it by default. + expectedExitStatus=$HIDDEN_MISSPELLING_COUNT + INPUT_CHECK_HIDDEN=true + INPUT_PATH="./test/testdata/.hidden" + run "./entrypoint.sh" + [ $status -eq $expectedExitStatus ] +} + +@test "Check a hidden file without INPUT_CHECK_HIDDEN set" { + expectedExitStatus=0 + INPUT_PATH="./test/testdata/.hidden" + run "./entrypoint.sh" + [ $status -eq $expectedExitStatus ] +} + +@test "Use an exclude file" { + expectedExitStatus=$((ROOT_MISSPELLING_COUNT + HIDDEN_MISSPELLING_COUNT + SUBFOLDER_MISSPELLING_COUNT - EXCLUDED_MISSPELLING_COUNT)) + INPUT_EXCLUDE_FILE="./test/exclude-file.txt" + run "./entrypoint.sh" + [ $status -eq $expectedExitStatus ] +} + +@test "Custom path" { + expectedExitStatus=$((SUBFOLDER_MISSPELLING_COUNT)) + INPUT_PATH="./test/testdata/subfolder" + run "./entrypoint.sh" + [ $status -eq $expectedExitStatus ] +} + +@test "Only warn" { + expectedExitStatus=0 + INPUT_ONLY_WARN=true + run "./entrypoint.sh" + [ $status -eq $expectedExitStatus ] +} diff --git a/test/testdata/.hidden b/test/testdata/.hidden new file mode 100644 index 0000000..3eda6e1 --- /dev/null +++ b/test/testdata/.hidden @@ -0,0 +1 @@ +abandonned diff --git a/test/testdata/abandonned.txt b/test/testdata/abandonned.txt new file mode 100644 index 0000000..79726ff --- /dev/null +++ b/test/testdata/abandonned.txt @@ -0,0 +1 @@ +abandoned diff --git a/example/example.bin b/test/testdata/example.bin similarity index 100% rename from example/example.bin rename to test/testdata/example.bin diff --git a/example/example.txt b/test/testdata/example.txt similarity index 68% rename from example/example.txt rename to test/testdata/example.txt index 024f418..e1118f6 100644 --- a/example/example.txt +++ b/test/testdata/example.txt @@ -1,4 +1,4 @@ -abandonned +1 abandonned 1 Abandonned ABANDONNED AbAnDoNnEd diff --git a/example/example:2.txt b/test/testdata/example:2.txt similarity index 100% rename from example/example:2.txt rename to test/testdata/example:2.txt diff --git a/test/testdata/subfolder/example.txt b/test/testdata/subfolder/example.txt new file mode 100644 index 0000000..3eda6e1 --- /dev/null +++ b/test/testdata/subfolder/example.txt @@ -0,0 +1 @@ +abandonned