Skip to content

Commit

Permalink
Pulled in changes from bash-by-aws and fixed a few issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Holmes committed Dec 22, 2015
1 parent 0111f36 commit 90324c9
Showing 1 changed file with 76 additions and 100 deletions.
176 changes: 76 additions & 100 deletions bash-spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,85 +14,77 @@
# Modified by REA Group 2014
#==================================================================================

result_file=$RANDOM
# XXX: should use mktemp for proper random file name -- (GM)
result_file="$RANDOM"
_passed_=0
_failed_=0

exec 6<&1
exec > "$result_file"

function output_results {
exec 1>&6 6>&-
local results=$( cat "$result_file" )
rm "$result_file"
local passes=$( echo "$results" | grep "PASS" | wc -l )
local fails=$( echo "$results" | grep "\*\*\*\* FAIL" | wc -l )
echo "$results"
echo "--SUMMARY--"
echo "$passes PASSED"
echo "$fails FAILED"
if [[ $fails -gt 0 ]]; then
exit 1
else
exit 0
fi
local results="$(<$result_file)"
rm -f -- "$result_file"
local passes=$(printf '%s' "$results" | grep -F PASS | wc -l)
local fails=$(printf '%s' "$results" | grep -F '**** FAIL' | wc -l )
printf '%s\n--SUMMARY\n%d PASSED\n%d FAILED\n' "$results" "$passes" "$fails"
[[ ${fails:-1} -eq 0 ]]
exit $?
}

function _array_contains_ {
for elem in "${_actual_[@]}"; do
if [[ "$elem" == "$_expected_" ]]; then
return 0
fi
[[ "$elem" == "$_expected_" ]] && return 0
done
return 1
}

function _negation_check_ {
if [[ "$_negation_" == true ]]; then
if [[ "$_pass_" == true ]]; then
_pass_=false
else
_pass_=true
fi
fi
if [[ "$_negation_" == true ]]; then
if [[ "$_pass_" == true ]]; then
(( _passed_+=1 ))
pass
_pass_=false
else
(( _failed_+=1 ))
fail
_pass_=true
fi
fi
if [[ "$_pass_" == true ]]; then
(( _passed_+=1 ))
pass
else
(( _failed_+=1 ))
fail
fi
}

function it {
echo " $1"
echo " $2"
printf ' %s\n %s\n' "$1" "$2"
}

function describe {
echo "$1"
echo "$2"
printf '%s\n%s\n' "$1" "$2"
}

function context {
echo "$1"
echo "$2"
printf '%s\n%s\n' "$1" "$2"
}

function pass {
echo " PASS"
}

function fail {
echo "**** FAIL - expected:$( if [[ "$_negation_" == true ]]; then echo " NOT"; fi; ) '$_expected_' | actual: '${_actual_[@]}'"
echo "**** FAIL - expected:$( if [[ "$_negation_" == true ]]; then echo ' NOT'; fi; ) '$_expected_' | actual: '${_actual_[@]}'"
}

function expect {
_expected_=
_negation_=false
declare -a _actual_
until [[ "$1" == to_* || "$1" == not || -z "$1" ]]; do
_actual_+=("$1")
shift
done
_negation_=false
declare -a _actual_
until [[ "${1:0:3}" == to_ || "$1" == not || -z "$1" ]]; do
_actual_+=("$1")
shift
done
"$@"
}

Expand All @@ -102,90 +94,74 @@ function not {
}

function to_be {
_expected_="$1"
if [[ "${_actual_[0]}" == "$_expected_" ]]; then
_pass_=true
else
_pass_=false
fi
_negation_check_
_expected_="$1"
_pass_=false
[[ "${_actual_[0]}" == "$_expected_" ]] && _pass_=true
_negation_check_
}

function to_be_true {
_expected_="$@ IS TRUE"
if $@; then
_pass=false
_actual_="$@ IS FALSE"
if "$@"; then
_pass_=true
_actual_="$@ IS TRUE"
else
_pass=false
_actual_="$@ IS FALSE"
fi
_negation_check_
}

function to_match {
_expected_="$1"
if [[ "${_actual_[0]}" =~ $_expected_ ]]; then
_pass_=true
else
_pass_=false
fi
_negation_check_
_expected_="$1"
_pass_=false
[[ "${_actual_[0]}" =~ $_expected_ ]] && _pass_=true
_negation_check_
}

function to_contain {
_expected_="$1"

if _array_contains_ "$_expected_" "$_actual_"; then
_pass_=true
else
_pass_=false
fi
_negation_check_
_expected_="$1"
_pass_=false
_array_contains_ "$_expected_" "$_actual_" && _pass_=true
_negation_check_
}

function to_exist {
_pass_=false
_expected_="$_actual_ EXISTS"
if [[ -e "${_actual_[0]}" ]]; then
_pass_=true
if [[ "$_negation_" == true ]]; then
_expected_="$_actual_ EXISTS"
fi
else
_pass_=false
_expected_="$_actual_ EXISTS"
_actual_="File not found"
fi
_negation_check_
_pass_=true
[[ "$_negation_" == true ]] && _expected_="$_actual_ EXISTS"
else
_actual_="File not found"
fi
_negation_check_
}

function to_have_mode {
_filename_="${_actual_[0]}"
_expected_="$1"
if [[ -e "$_filename_" ]]; then
_fullname_="$_filename_"
else
_fullname_="$(which $_filename_)"
fi
if [[ -e "$_fullname_" ]]; then
_os_=$(uname -a | cut -f 1 -d ' ')
if [[ $_os_ == Linux ]]; then
_actual_="$(stat -c %A $_fullname_)"
else
_actual_="$(stat $_fullname_ | cut -f 3 -d ' ')"
fi
if [[ "$_actual_" =~ $_expected_ ]]; then
_pass_=true
else
_pass_=false
fi
_negation_check_
_filename_="${_actual_[0]}"
_expected_="$1"
_pass_=false
if [[ -e "$_filename_" ]]; then
_fullname_="$_filename_"
else
_fullname_="$(which $_filename_)"
fi
if [[ -e "$_fullname_" ]]; then
_os_="$(uname -s)"
if [[ "$_os_" == Linux ]]; then
_actual_="$(stat -c %A $_fullname_)"
else
echo "File not found: $_fullname_"
_actual_="$(stat $_fullname_ | cut -f 3 -d ' ')"
fi
[[ "$_actual_" =~ "$_expected_" ]] && _pass_=true
else
echo "File not found: $_fullname_"
fi
_negation_check_
}

TEMP=`getopt -o h --long help \
-n 'javawrap' -- "$@"`
TEMP="$(getopt -o h --long help \
-n 'javawrap' -- $@)"

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

Expand Down

0 comments on commit 90324c9

Please sign in to comment.