Skip to content

Commit

Permalink
custom test cases for FVT options #36
Browse files Browse the repository at this point in the history
  • Loading branch information
BradleyA committed Jan 17, 2020
1 parent 42620ab commit 9d82b64
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 0 deletions.
93 changes: 93 additions & 0 deletions hooks/bin/TEST/git-TEST-cases.sh/FVT-option-filename-005
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash
# hooks/bin/TEST/git-TEST-cases.sh/FVT-option-filename-005 2.223.989 2020-01-16T23:04:52.442714-06:00 (CST) https://github.com/BradleyA/git-TEST-commit-automation.git dev uadmin five-rpi3b.cptx86.com 2.222-2-g42620ab
# custom test cases for FVT options #36
#86# hooks/bin/TEST/git-TEST-cases.sh/FVT-option-filename-005 - custom test case
### Production standard 3.0 shellcheck
### Production standard 5.3.550 Copyright # 3.550
# Copyright (c) 2020 Bradley Allen # 3.550
# MIT License is online https://github.com/BradleyA/user-files/blob/master/LICENSE # 3.550
### Production standard 1.3.550 DEBUG variable # 3.550
# Order of precedence: environment variable, default code
if [[ "${DEBUG}" == "" ]] ; then DEBUG="0" ; fi # 0 = debug off, 1 = debug on, 'export DEBUG=1', 'unset DEBUG' to unset environment variable (bash)
if [[ "${DEBUG}" == "2" ]] ; then set -x ; fi # Print trace of simple commands before they are executed
if [[ "${DEBUG}" == "3" ]] ; then set -v ; fi # Print shell input lines as they are read
if [[ "${DEBUG}" == "4" ]] ; then set -e ; fi # Exit immediately if non-zero exit status
if [[ "${DEBUG}" == "5" ]] ; then set -e -o pipefail ; fi # Exit immediately if non-zero exit status and exit if any command in a pipeline errors
#
BOLD=$(tput -Txterm bold)
NORMAL=$(tput -Txterm sgr0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)

# Date and time function ISO 8601
get_date_stamp() {
DATE_STAMP=$(date +%Y-%m-%dT%H:%M:%S.%6N%:z)
TEMP=$(date +%Z)
DATE_STAMP="${DATE_STAMP} (${TEMP})"
}

# Fully qualified domain name FQDN hostname
LOCALHOST=$(hostname -f)

# Version
# Assumptions for the next two lines of code: The second line in this script includes the script path & name as the second item and
# the script version as the third item separated with space(s). The tool I use is called 'markit'. See example line below:
# template/template.sh 3.517.783 2019-09-13T18:20:42.144356-05:00 (CDT) https://github.com/BradleyA/user-files.git uadmin one-rpi3b.cptx86.com 3.516
SCRIPT_NAME=$(head -2 "${0}" | awk '{printf $2}') # Different from ${COMMAND_NAME}=$(echo "${0}" | sed 's/^.*\///'), SCRIPT_NAME = includes Git repository directory and can be used any where in script (for dev, test teams)
SCRIPT_VERSION=$(head -2 "${0}" | awk '{printf $3}')
if [[ "${SCRIPT_NAME}" == "" ]] ; then SCRIPT_NAME="${0}" ; fi
if [[ "${SCRIPT_VERSION}" == "" ]] ; then SCRIPT_VERSION="v?.?" ; fi

# GID
GROUP_ID=$(id -g)

### Production standard 2.3.529 log format (WHEN WHERE WHAT Version Line WHO UID:GID [TYPE] Message)
new_message() { # $1="${LINENO}" $2="DEBUG INFO ERROR WARN" $3="message"
get_date_stamp
echo -e "${NORMAL}${DATE_STAMP} ${LOCALHOST} ${SCRIPT_NAME}[$$] ${SCRIPT_VERSION} ${1} ${USER} ${UID}:${GROUP_ID} ${BOLD}[${2}]${NORMAL} ${3}"
}

# INFO
if [[ "${DEBUG}" == "1" ]] ; then new_message "${LINENO}" "${YELLOW}INFO${WHITE}" " Started..." 1>&2 ; fi

if [[ ! -x ${1} ]] ; then # Command invoked does not exist or cannot execute
RETURN_CODE=126
new_message "${LINENO}" "${RED}ERROR${WHITE}" " ${RETURN_CODE} - File, ${1}, does not exist or have execute permission" 1>&2
echo "${BOLD}${YELLOW}Test case --->${NORMAL} ${0} ${1} 126 - File, ${1}, does not exist or have execute permission - ${BOLD}${CYAN}ERROR - ERROR${NORMAL}"
exit "${RETURN_CODE}"
fi

### Place test case here

# output is blank: no files w/o hooks
touch "${0}.expected"

COMMAND_NAME=$(echo "${0}" | sed 's/^.*\///')
"${1}" -f test-"${COMMAND_NAME}" 2>&1 | cut -d ']' -f 3 >"${0}".test-case-output # pipe [ERROR] message into "${0}".test-case-output

if [[ -s ${1} ]] ; then # file size is greater than zero
diff "${0}".expected "${0}".test-case-output >/dev/null 2>&1
RETURN_CODE=${?}
else
new_message "${LINENO}" "${RED}ERROR${WHITE}" " File, ${1}, does not exist or is empty" 1>&2
RETURN_CODE=2
fi

if [[ "${RETURN_CODE}" -eq 0 ]] ; then
echo "${BOLD}${YELLOW}Test case --->${NORMAL} ${0} ${1} ${RETURN_CODE} - No difference with expected output - ${BOLD}${GREEN}PASS - PASS${NORMAL}"
cat "${0}".test-case-output
exit 0
elif [[ ${RETURN_CODE} -eq 1 ]] ; then
echo "${BOLD}${YELLOW}Test case --->${NORMAL} ${0} ${1} ${RETURN_CODE} - Differences with expected output - ${BOLD}${RED}FAIL - FAIL${NORMAL}"
diff -y "${0}".expected "${0}".test-case-output
exit 1
else
echo "${BOLD}${YELLOW}Test case --->${NORMAL} ${0} ${1} ${RETURN_CODE} - Test script ERROR - ${BOLD}${CYAN}ERROR - ERROR${NORMAL}"
exit 2
fi
echo "${BOLD}${YELLOW}Test case --->${NORMAL} ${0} ${1} 124 - Test script logic ERROR - ${BOLD}${CYAN}ERROR - ERROR${NORMAL}"
exit 124
###
93 changes: 93 additions & 0 deletions hooks/bin/TEST/git-TEST-cases.sh/FVT-option-filename-006
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash
# hooks/bin/TEST/git-TEST-cases.sh/FVT-option-filename-006 2.223.989 2020-01-16T23:04:54.726163-06:00 (CST) https://github.com/BradleyA/git-TEST-commit-automation.git dev uadmin five-rpi3b.cptx86.com 2.222-2-g42620ab
# custom test cases for FVT options #36
#86# hooks/bin/TEST/git-TEST-cases.sh/FVT-option-filename-006 - custom test case
### Production standard 3.0 shellcheck
### Production standard 5.3.550 Copyright # 3.550
# Copyright (c) 2020 Bradley Allen # 3.550
# MIT License is online https://github.com/BradleyA/user-files/blob/master/LICENSE # 3.550
### Production standard 1.3.550 DEBUG variable # 3.550
# Order of precedence: environment variable, default code
if [[ "${DEBUG}" == "" ]] ; then DEBUG="0" ; fi # 0 = debug off, 1 = debug on, 'export DEBUG=1', 'unset DEBUG' to unset environment variable (bash)
if [[ "${DEBUG}" == "2" ]] ; then set -x ; fi # Print trace of simple commands before they are executed
if [[ "${DEBUG}" == "3" ]] ; then set -v ; fi # Print shell input lines as they are read
if [[ "${DEBUG}" == "4" ]] ; then set -e ; fi # Exit immediately if non-zero exit status
if [[ "${DEBUG}" == "5" ]] ; then set -e -o pipefail ; fi # Exit immediately if non-zero exit status and exit if any command in a pipeline errors
#
BOLD=$(tput -Txterm bold)
NORMAL=$(tput -Txterm sgr0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)

# Date and time function ISO 8601
get_date_stamp() {
DATE_STAMP=$(date +%Y-%m-%dT%H:%M:%S.%6N%:z)
TEMP=$(date +%Z)
DATE_STAMP="${DATE_STAMP} (${TEMP})"
}

# Fully qualified domain name FQDN hostname
LOCALHOST=$(hostname -f)

# Version
# Assumptions for the next two lines of code: The second line in this script includes the script path & name as the second item and
# the script version as the third item separated with space(s). The tool I use is called 'markit'. See example line below:
# template/template.sh 3.517.783 2019-09-13T18:20:42.144356-05:00 (CDT) https://github.com/BradleyA/user-files.git uadmin one-rpi3b.cptx86.com 3.516
SCRIPT_NAME=$(head -2 "${0}" | awk '{printf $2}') # Different from ${COMMAND_NAME}=$(echo "${0}" | sed 's/^.*\///'), SCRIPT_NAME = includes Git repository directory and can be used any where in script (for dev, test teams)
SCRIPT_VERSION=$(head -2 "${0}" | awk '{printf $3}')
if [[ "${SCRIPT_NAME}" == "" ]] ; then SCRIPT_NAME="${0}" ; fi
if [[ "${SCRIPT_VERSION}" == "" ]] ; then SCRIPT_VERSION="v?.?" ; fi

# GID
GROUP_ID=$(id -g)

### Production standard 2.3.529 log format (WHEN WHERE WHAT Version Line WHO UID:GID [TYPE] Message)
new_message() { # $1="${LINENO}" $2="DEBUG INFO ERROR WARN" $3="message"
get_date_stamp
echo -e "${NORMAL}${DATE_STAMP} ${LOCALHOST} ${SCRIPT_NAME}[$$] ${SCRIPT_VERSION} ${1} ${USER} ${UID}:${GROUP_ID} ${BOLD}[${2}]${NORMAL} ${3}"
}

# INFO
if [[ "${DEBUG}" == "1" ]] ; then new_message "${LINENO}" "${YELLOW}INFO${WHITE}" " Started..." 1>&2 ; fi

if [[ ! -x ${1} ]] ; then # Command invoked does not exist or cannot execute
RETURN_CODE=126
new_message "${LINENO}" "${RED}ERROR${WHITE}" " ${RETURN_CODE} - File, ${1}, does not exist or have execute permission" 1>&2
echo "${BOLD}${YELLOW}Test case --->${NORMAL} ${0} ${1} 126 - File, ${1}, does not exist or have execute permission - ${BOLD}${CYAN}ERROR - ERROR${NORMAL}"
exit "${RETURN_CODE}"
fi

### Place test case here

# output is blank: no files w/o hooks
touch "${0}.expected"

COMMAND_NAME=$(echo "${0}" | sed 's/^.*\///')
"${1}" --filename test-"${COMMAND_NAME}" 2>&1 | cut -d ']' -f 3 >"${0}".test-case-output # pipe [ERROR] message into "${0}".test-case-output

if [[ -s ${1} ]] ; then # file size is greater than zero
diff "${0}".expected "${0}".test-case-output >/dev/null 2>&1
RETURN_CODE=${?}
else
new_message "${LINENO}" "${RED}ERROR${WHITE}" " File, ${1}, does not exist or is empty" 1>&2
RETURN_CODE=2
fi

if [[ "${RETURN_CODE}" -eq 0 ]] ; then
echo "${BOLD}${YELLOW}Test case --->${NORMAL} ${0} ${1} ${RETURN_CODE} - No difference with expected output - ${BOLD}${GREEN}PASS - PASS${NORMAL}"
cat "${0}".test-case-output
exit 0
elif [[ ${RETURN_CODE} -eq 1 ]] ; then
echo "${BOLD}${YELLOW}Test case --->${NORMAL} ${0} ${1} ${RETURN_CODE} - Differences with expected output - ${BOLD}${RED}FAIL - FAIL${NORMAL}"
diff -y "${0}".expected "${0}".test-case-output
exit 1
else
echo "${BOLD}${YELLOW}Test case --->${NORMAL} ${0} ${1} ${RETURN_CODE} - Test script ERROR - ${BOLD}${CYAN}ERROR - ERROR${NORMAL}"
exit 2
fi
echo "${BOLD}${YELLOW}Test case --->${NORMAL} ${0} ${1} 124 - Test script logic ERROR - ${BOLD}${CYAN}ERROR - ERROR${NORMAL}"
exit 124
###

0 comments on commit 9d82b64

Please sign in to comment.