Skip to content

add bats-assert functionality #539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 4, 2021
13 changes: 9 additions & 4 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ jobs:
run: sudo npm install -g bats

- name: Run tests for changed/added exercises
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PULL_REQUEST_URL=$(jq -r ".pull_request.url" "$GITHUB_EVENT_PATH")
curl --silent --url $"${PULL_REQUEST_URL}/files" --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' | \
jq -c '.[] | select(.status == "added" or .status == "modified") | select(.filename | match("\\.(md|sh)$")) | .filename' | \
xargs -r bash .github/scripts/pr
pr_endpoint=$(jq -r '"repos/\(.repository.full_name)/pulls/\(.pull_request.number)"' "$GITHUB_EVENT_PATH")
gh api "$pr_endpoint/files" --paginate --jq '
.[] |
select(.status == "added" or .status == "modified") |
select(.filename | match("\\.(md|sh|bash)$")) |
.filename
' | xargs -r bash .github/scripts/pr
4 changes: 4 additions & 0 deletions docs/TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ cd /path/to/your/exercise_workspace/bash/whatever
bats whatever_test.sh
```

For help on the meaning of the various `assert*` commands in the
tests, refer to the documentation for the
[bats-assert](https://github.com/bats-core/bats-assert) library.

## Installing `bats-core`

You should be able to install it from your favorite package manager:
Expand Down
41 changes: 21 additions & 20 deletions exercises/practice/acronym/acronym_test.sh
Original file line number Diff line number Diff line change
@@ -1,68 +1,69 @@
#!/usr/bin/env bash
load bats-extra.bash

# local version: 1.7.0.1

@test 'basic' {
#[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash acronym.sh 'Portable Network Graphics'
(( status == 0 ))
[[ "$output" == 'PNG' ]]
assert_success
assert_output 'PNG'
}

@test 'lowercase words' {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash acronym.sh 'Ruby on Rails'
(( status == 0 ))
[[ "$output" == 'ROR' ]]
assert_success
assert_output 'ROR'
}

@test 'punctuation' {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash acronym.sh 'First In, First Out'
(( status == 0 ))
[[ "$output" == 'FIFO' ]]
assert_success
assert_output 'FIFO'
}

@test 'all caps word' {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash acronym.sh 'GNU Image Manipulation Program'
(( status == 0 ))
[[ "$output" == 'GIMP' ]]
assert_success
assert_output 'GIMP'
}

@test 'punctuation without whitespace' {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash acronym.sh 'Complementary metal-oxide semiconductor'
(( status == 0 ))
[[ "$output" == 'CMOS' ]]
assert_success
assert_output 'CMOS'
}

@test 'very long abbreviation' {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash acronym.sh 'Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me'
(( status == 0 ))
[[ "$output" == 'ROTFLSHTMDCOALM' ]]
assert_success
assert_output 'ROTFLSHTMDCOALM'
}

@test "consecutive delimiters" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash acronym.sh "Something - I made up from thin air"
(( status == 0 ))
[[ "$output" == "SIMUFTA" ]]
assert_success
assert_output "SIMUFTA"
}

@test "apostrophes" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash acronym.sh "Halley's Comet"
(( status == 0 ))
[[ "$output" == "HC" ]]
assert_success
assert_output "HC"
}

@test "underscore emphasis" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash acronym.sh "The Road __Not__ Taken"
(( status == 0 ))
[[ "$output" == "TRNT" ]]
assert_success
assert_output "TRNT"
}

# bash-specific test: Focus the student's attention on the effects of
Expand All @@ -72,6 +73,6 @@
@test "contains shell globbing character" {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run bash acronym.sh "Two * Words"
(( status == 0 ))
[[ "$output" == "TW" ]]
assert_success
assert_output "TW"
}
Loading