Skip to content

Commit

Permalink
Merge pull request #7 from per1234/bats-tests
Browse files Browse the repository at this point in the history
Add Bats tests for script
  • Loading branch information
peternewman authored May 30, 2020
2 parents 2cdc340 + 80a186d commit d852f7a
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 4 deletions.
28 changes: 27 additions & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,38 @@ 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
steps:
- uses: actions/checkout@v1
- uses: ./
with:
path: example
path: test/testdata
only_warn: 1
5 changes: 3 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/exclude-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1 abandonned 1
2 abandonned 2
84 changes: 84 additions & 0 deletions test/test.bats
Original file line number Diff line number Diff line change
@@ -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 ]
}
1 change: 1 addition & 0 deletions test/testdata/.hidden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abandonned
1 change: 1 addition & 0 deletions test/testdata/abandonned.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abandoned
File renamed without changes.
2 changes: 1 addition & 1 deletion example/example.txt → test/testdata/example.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
abandonned
1 abandonned 1
Abandonned
ABANDONNED
AbAnDoNnEd
File renamed without changes.
1 change: 1 addition & 0 deletions test/testdata/subfolder/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abandonned

0 comments on commit d852f7a

Please sign in to comment.