Skip to content

Commit

Permalink
Merge pull request #36 from jasonheecs/feature/shellcheck
Browse files Browse the repository at this point in the history
Refactored bash scripts based on shellcheck
  • Loading branch information
Ryan Fritts committed Apr 5, 2018
2 parents 4f57d0e + 7fcf908 commit b5a666e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 47 deletions.
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: bash

# Use container-based infrastructure for quicker build start-up
sudo: false

addons:
apt:
sources:
- debian-sid # Grab shellcheck from the Debian repo
packages:
- shellcheck

script:
- bash -c 'shopt -s globstar; shellcheck **/*.shl; shellcheck ./bunit_unit_tests/*.ut'
- cd bunit_unit_tests && bash UnitTestsWithNoFailures.ut -v

matrix:
fast_finish: true
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ files or directories that you are intentionally trying to delete. This probably

-----

### Please remember this is a work in progress!
### Please remember this is a work in progress!
31 changes: 18 additions & 13 deletions bunit.shl
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ assertFalse () {
}

assertContains () {
# shellcheck disable=SC2034
local args=(
"${1}"
"${2}"
Expand All @@ -105,12 +106,14 @@ __assert () {

local test_case_calling_assert=${FUNCNAME[2]}
local assert_caller_func_name=${FUNCNAME[1]}
local line=$(echo $(caller 1) | awk '{print $1}')

if [ $(correctNumberOfArgs ${actual_num_of_args} ${correct_num_of_args}) = "true" ]; then
local result=$(${callback_function} ${callback_args})
line=$(echo $"(caller 1)" | awk '{print $1}')
local line

if [ ${result} = "true" ]; then
if [ "$(correctNumberOfArgs "${actual_num_of_args}" "${correct_num_of_args}")" = "true" ]; then
result=$(${callback_function} "${callback_args}")

if [ "${result}" = "true" ]; then
[[ "$VERBOSE_MODE" = "true" ]] && echo "Line ${line}: Passed - ${test_case_calling_assert}"
incrementPassedTests
else
Expand Down Expand Up @@ -163,7 +166,7 @@ __notNullCallback() {

__trueCallback() {
declare -a callback_args=("${!1}")
eval [ ${callback_args[0]} ]
eval [ "${callback_args[0]}" ]
testResult=$?
if [ "$testResult" = "0" ]; then
echo "true"
Expand All @@ -174,7 +177,7 @@ __trueCallback() {

__falseCallback() {
declare -a callback_args=("${!1}")
eval [ ${callback_args[0]} ]
eval [ "${callback_args[0]}" ]
testResult=$?
if [ "$testResult" = "1" ]; then
echo "true"
Expand All @@ -196,13 +199,15 @@ __containsCallback() {

runUnitTests () {
START_TIME=$(date +%s)

# Explanation of the following command:
# grep - E - Use extended regexp
# ^(function )? - Lines may start with the keyword function followed by space
# test.*?\( - Match any line with 'test[anything] (', doesn't need 'function'
# sed 's/^function //' - Remove the 'function ' prefix from any string, if it occurs
# tr - d ' (){' - Remove the trailing ' (){' characters
test_names=$(grep -E '^(function )?test.*?\(' $0 | sed 's/^function //' | tr -d ' (){')
test_names=$(grep -E '^(function )?test.*?\(' "$0" | sed 's/^function //' | tr -d ' (){')

test_names_array=($test_names)

beginUnitTests
Expand Down Expand Up @@ -233,33 +238,33 @@ endUnitTests () {
END_TIME=$(date +%s)
echo ""
echo "RESULTS: $passed_tests tests passed. $failed_tests tests failed. $total_tests tests total."
echo "Execution completed in $(($END_TIME - $START_TIME)) second(s)..."
echo "Execution completed in $((END_TIME - START_TIME)) second(s)..."
echo ""
fi
}

incrementPassedTests () {
passed_tests=$(($passed_tests+1))
passed_tests=$((passed_tests+1))
incrementTotalTests
}

incrementFailedTests () {
failed_tests=$(($failed_tests+1))
failed_tests=$((failed_tests+1))
incrementTotalTests
}

incrementTotalTests () {
total_tests=$(($total_tests+1))
total_tests=$((total_tests+1))
}

correctNumberOfArgs () {
if [ $1 = $2 ]; then
if [ "${1}" = "${2}" ]; then
echo "true"
else
echo "false"
fi
}

numericCompare () {
awk -v n1=$1 -v n2=$2 \ 'BEGIN { print (n1 == n2) ? "true" : "false" }' 2>/dev/null
awk -v n1="${1}" -v n2="${2}" \ 'BEGIN { print (n1 == n2) ? "true" : "false" }' 2>/dev/null
}
48 changes: 27 additions & 21 deletions bunit_unit_tests/UnitTestsWithIntentionalFailures.ut
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

# shellcheck disable=SC1091
source ../bunit.shl

testSetup () {
Expand Down Expand Up @@ -37,9 +38,9 @@ testAssertEqualsWithDoubles2 () {


testAssertEqualsWithArrays () {
local array1=(1,2,3)
local array2=(1,2,3)
assertEquals $array1 $array2
local array1=(1 2 3)
local array2=(1 2 3)
assertEquals "${array1[*]}" "${array2[*]}"
}


Expand All @@ -54,9 +55,9 @@ testAssertNotEqualsWithIntegers () {


testAssertNotEqualsWithArrays () {
local array1=(1,2,3)
local array2=(4,5,6)
assertNotEquals $array1 $array2
local array1=(1 2 3)
local array2=(4 5 6)
assertNotEquals "${array1[*]}" "${array2[*]}"
}


Expand Down Expand Up @@ -87,8 +88,10 @@ testAssertFalseWithFilename () {


testAssertFalseWithArray () {
local array1=(1,2,3)
local array2=(4,5,6)
local array1=(1 2 3)
local array2=(4 5 6)
# shellcheck disable=SC2128
# We are comparing the actual arrays, not just the elements
assertFalse "${array1} = ${array2}"
}

Expand All @@ -99,9 +102,9 @@ testAssertContainsWithString () {
}

testAssertContainsWithArray () {
local array1=(1,2,3)
local array1=(1 2 3)
local num2=2
assertContains $num2 $array1
assertContains $num2 "${array1[*]}"
}

testAssertEqualsWithStringsFailure () {
Expand All @@ -115,9 +118,9 @@ testAssertEqualsWithIntegersFailure () {


testAssertEqualsWithArraysFailure () {
local array1=(1,2,3)
local array2=(4,5,6)
assertEquals $array1 $array2
local array1=(1 2 3)
local array2=(4 5 6)
assertEquals "${array1[*]}" "${array2[*]}"
}


Expand All @@ -132,9 +135,9 @@ testAssertNotEqualsWithStringsFailure () {


testAssertNotEqualsWithArrayFailure () {
local array1=(1,2,3)
local array2=(1,2,3)
assertNotEquals $array1 $array2
local array1=(1 2 3)
local array2=(1 2 3)
assertNotEquals "${array1[*]}" "${array2[*]}"
}


Expand All @@ -149,8 +152,8 @@ testAssertNullWithStringFailure () {


testAssertNullWithArrayFailure () {
local array1=(1,2,3)
assertNull $array1
local array1=(1 2 3)
assertNull "${array1[*]}"
}


Expand Down Expand Up @@ -179,9 +182,9 @@ testAssertContainsWithStringsFailure () {
}

testAssertContainsWithArrayFailure () {
local array1=(4,5,6)
local array1=(4 5 6)
local num2=7
assertContains $num2 $array1
assertContains $num2 "${array1[*]}"
}

testAssertEqualsWrongArgsNum() {
Expand All @@ -208,9 +211,12 @@ testAssertContainsWrongArgsNum() {
assertContains "Foo"
}

function testAssertNotEqualsWithStringsFunction() {
assertEquals "something" "something else"
}

testTeardown () {
rm testFile
}


runUnitTests
26 changes: 16 additions & 10 deletions bunit_unit_tests/UnitTestsWithNoFailures.ut
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

# shellcheck disable=SC1091
source ../bunit.shl

testSetup () {
Expand Down Expand Up @@ -38,9 +39,9 @@ testAssertEqualsWithDoubles2 () {


testAssertEqualsWithArrays () {
local array1=(1,2,3)
local array2=(1,2,3)
assertEquals $array1 $array2
local array1=(1 2 3)
local array2=(1 2 3)
assertEquals "${array1[*]}" "${array2[*]}"
}


Expand All @@ -55,9 +56,9 @@ testAssertNotEqualsWithIntegers () {


testAssertNotEqualsWithArrays () {
local array1=(1,2,3)
local array2=(4,5,6)
assertNotEquals $array1 $array2
local array1=(1 2 3)
local array2=(4 5 6)
assertNotEquals "${array1[*]}" "${array2[*]}"
}


Expand Down Expand Up @@ -88,8 +89,10 @@ testAssertFalseWithFilename () {


testAssertFalseWithArray () {
local array1=(1,2,3)
local array2=(4,5,6)
local array1=(1 2 3)
local array2=(4 5 6)
# shellcheck disable=SC2128
# We are comparing the actual arrays, not just the elements
assertFalse "${array1} = ${array2}"
}

Expand All @@ -102,11 +105,14 @@ testAssertContainsWithString () {


testAssertContainsWithArray () {
local array1=(1,2,3)
local array1=(1 2 3)
local num2=2
assertContains $num2 $array1
assertContains $num2 "${array1[*]}"
}

function testAssertEqualsWithStringsFunction() {
assertEquals "something" "something"
}

testTeardown () {
rm testFile
Expand Down
2 changes: 1 addition & 1 deletion bunit_unit_tests/runUnitTestSuite.ut
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# This is experimental! Use something like this at your own risk


# shellcheck disable=SC1091
source ../bunit.shl

testRunUnitTestSuite () {
Expand Down

0 comments on commit b5a666e

Please sign in to comment.