Skip to content

Commit

Permalink
add bats-assert functionality (#539)
Browse files Browse the repository at this point in the history
* WIP: add bats-assert functionality

* use bats "load" function instead of "source"

* add bats-extra.bash to all exercises

* transform test scripts to use bats-assert functions

* with an associative array, "${map[@]}" order is indeterinate, broke in bash5.1

* in arithmetic, bare map entry with quote in key broke in bash 5.1

* update workflow, using github cli with auto-pagination, to pull in ALL changes in PR

* add env var to workflow for auth

* update docs about bats-assert functions

* include .bash files in pr
  • Loading branch information
glennj authored Sep 4, 2021
1 parent b471b3d commit 21266ba
Show file tree
Hide file tree
Showing 183 changed files with 59,672 additions and 2,867 deletions.
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

0 comments on commit 21266ba

Please sign in to comment.