Skip to content

Commit

Permalink
Merge pull request #377 from glennj/acronym-add-test-designed-to-trip…
Browse files Browse the repository at this point in the history
…-up-unquoted-variables

Force students to handle unquoted variables safely

exercises affected: acronym, pig-latin, proverb, word-count
  • Loading branch information
glennj authored Aug 19, 2019
2 parents 2d25f25 + 9ac0c8f commit 65c3684
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion exercises/acronym/.meta/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.7.0
1.7.1
7 changes: 7 additions & 0 deletions exercises/acronym/acronym_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,10 @@
[[ "$status" -eq 0 ]]
[[ "$output" == "TRNT" ]]
}

@test "contains shell globbing character" {
[[ $BATS_RUN_SKIPPED == true ]] || skip
run bash acronym.sh "Two * Words"
[[ "$status" -eq 0 ]]
[[ "$output" == "TW" ]]
}
5 changes: 2 additions & 3 deletions exercises/acronym/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ if [ "$#" -ne 1 ]; then
exit 1
fi

phrase=$(echo "$1" | tr '-' ' ')
set -f -- junk $phrase
shift
phrase=$(echo "$1" | tr -s '*-' ' ')
set -f -- $phrase

shopt -s extglob
for word; do
Expand Down
2 changes: 1 addition & 1 deletion exercises/pig-latin/.meta/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.0
1.2.1
2 changes: 1 addition & 1 deletion exercises/pig-latin/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ translate() {
fi
}

main "$@"
main "$@"
8 changes: 8 additions & 0 deletions exercises/pig-latin/pig_latin_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,11 @@
[[ $status -eq 0 ]]
[[ $output == "ickquay astfay unray" ]]
}

# danger this way lies
@test "shell globbing" {
[[ $BATS_RUN_SKIPPED == true ]] || skip
run bash pig_latin.sh "pig*"
[[ $status -eq 0 ]]
[[ $output == "ig*pay" ]]
}
2 changes: 1 addition & 1 deletion exercises/proverb/.meta/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.0
1.1.1
24 changes: 24 additions & 0 deletions exercises/proverb/proverb_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,27 @@ END
[[ $status -eq 0 ]]
[[ $output == "$expected" ]]
}

@test "items with whitespace" {
[[ $BATS_RUN_SKIPPED == true ]] || skip
expected=$(cat <<END
For want of a rusty nail the horse shoe was lost.
And all for the want of a rusty nail.
END
)
run bash proverb.sh "rusty nail" "horse shoe"
[[ $status -eq 0 ]]
[[ $output == "$expected" ]]
}

@test "shell globbing character" {
[[ $BATS_RUN_SKIPPED == true ]] || skip
expected=$(cat <<END
For want of a quotes the * was lost.
And all for the want of a quotes.
END
)
run bash proverb.sh quotes "*"
[[ $status -eq 0 ]]
[[ $output == "$expected" ]]
}
2 changes: 1 addition & 1 deletion exercises/word-count/.meta/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.0
1.4.1
2 changes: 1 addition & 1 deletion exercises/word-count/example.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

init_var=$(echo -e "$1" | tr -d '!@$%^&:.')
init_var=$(echo -e "$1" | tr -d '*!@$%^&:.')
preprocessed_var=$(echo "${init_var//[$'\n,']/ }" | tr '[:upper:]' '[:lower:]')

read -r -a words <<< "$preprocessed_var"
Expand Down
9 changes: 9 additions & 0 deletions exercises/word-count/word_count_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,12 @@
[[ $(wc -l <<< "$output") -eq 3 ]]
}

@test "contains shell globbing character" {
[[ $BATS_RUN_SKIPPED == true ]] || skip
run bash word_count.sh "two * words"
[[ $status -eq 0 ]]
echo "$output" | grep -qFx "two: 1"
echo "$output" | grep -qFx "words: 1"
[[ $(wc -l <<< "$output") -eq 2 ]]
}

0 comments on commit 65c3684

Please sign in to comment.