1+ #! /usr/bin/env bash
2+
3+ # Tests "cloneGitRepository.sh".
4+
5+ # Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
6+ set -o errexit -o pipefail
7+
8+ # Local constants
9+ SCRIPT_NAME=$( basename " ${0} " )
10+ COLOR_ERROR=' \033[0;31m' # red
11+ COLOR_DE_EMPHASIZED=' \033[0;90m' # dark gray
12+ COLOR_SUCCESSFUL=" \033[0;32m" # green
13+ COLOR_DEFAULT=' \033[0m'
14+
15+ # # Get this "scripts" directory if not already set
16+ # Even if $BASH_SOURCE is made for Bourne-like shells it is also supported by others and therefore here the preferred solution.
17+ # CDPATH reduces the scope of the cd command to potentially prevent unintended directory changes.
18+ # This way non-standard tools like readlink aren't needed.
19+ SCRIPTS_DIR=${SCRIPTS_DIR:- $( CDPATH=. cd -- " $( dirname -- " ${BASH_SOURCE[0]} " ) " && pwd -P )} # Repository directory containing the shell scripts
20+
21+ tearDown () {
22+ # echo "${SCRIPT_NAME}: Tear down tests...."
23+ rm -rf " ${temporaryTestDirectory} "
24+ }
25+
26+ successful () {
27+ echo " "
28+ echo -e " ${COLOR_DE_EMPHASIZED}${SCRIPT_NAME} :${COLOR_DEFAULT} ${COLOR_SUCCESSFUL} ✅ Tests finished successfully.${COLOR_DEFAULT} "
29+ tearDown
30+ }
31+
32+ info () {
33+ local infoMessage=" ${1} "
34+ echo -e " ${COLOR_DE_EMPHASIZED}${SCRIPT_NAME} :${COLOR_DEFAULT} ${infoMessage} "
35+ }
36+
37+ fail () {
38+ local errorMessage=" ${1} "
39+ echo -e " ${COLOR_DE_EMPHASIZED}${SCRIPT_NAME} : ${COLOR_ERROR}${errorMessage}${COLOR_DEFAULT} "
40+ tearDown
41+ return 1
42+ }
43+
44+ printTestLogFileContent () {
45+ local logFileName=" ${temporaryTestDirectory} /${SCRIPT_NAME} -${test_case_number} .log"
46+ if [ -f " ${logFileName} " ]; then
47+ local logFileContent=$( cat " ${logFileName} " )
48+ # Remove color codes from the output for better readability in test logs
49+ logFileContent=$( echo -e " ${logFileContent} " | sed -r " s/\x1B\[[0-9;]*[mK]//g" )
50+ echo -e " ${COLOR_DE_EMPHASIZED}${logFileContent}${COLOR_DEFAULT} "
51+ else
52+ echo -e " ${COLOR_ERROR} No log file found at expected location: ${logFileName}${COLOR_DEFAULT} "
53+ fi
54+ }
55+
56+ cloneGitRepositoryExpectingSuccessUnderTest () {
57+ local COLOR_DE_EMPHASIZED=' \033[0;90m' # dark gray
58+ (
59+ cd " ${temporaryTestDirectory} " ;
60+ source " ${SCRIPTS_DIR} /cloneGitRepository.sh" " $@ " > " ${temporaryTestDirectory} /${SCRIPT_NAME} -${test_case_number} .log"
61+ )
62+ exitCode=$?
63+ if [ ${exitCode} -ne 0 ]; then
64+ fail " ❌ Test failed: Script exited with non-zero exit code ${exitCode} ."
65+ fi
66+ printTestLogFileContent
67+ }
68+
69+ cloneGitRepositoryExpectingFailureUnderTest () {
70+ set +o errexit
71+ (
72+ cd " ${temporaryTestDirectory} " ;
73+ source " ${SCRIPTS_DIR} /cloneGitRepository.sh" " $@ " > " ${temporaryTestDirectory} /${SCRIPT_NAME} -${test_case_number} .log" 2>&1
74+ exitCode=$?
75+ if [ ${exitCode} -eq 0 ]; then
76+ fail " ❌ Test failed: Script exited with zero exit code but was expected to fail."
77+ fi
78+ )
79+ set -o errexit
80+ printTestLogFileContent
81+ }
82+
83+ info " Starting tests...."
84+
85+ # Create testing resources
86+ temporaryTestDirectory=$( mktemp -d 2> /dev/null || mktemp -d -t ' temporaryTestDirectory_${SCRIPT_NAME}' )
87+ mkdir -p " ${temporaryTestDirectory} "
88+
89+ # ------- Integration Test Case
90+ test_case_number=1
91+ echo " "
92+ info " ${test_case_number} .) Should clone a valid GitHub Repository successfully (real-run/integration)."
93+
94+ cloneGitRepositoryExpectingSuccessUnderTest --url " https://github.com/JohT/livecoding.git" --branch " main" --target " ${temporaryTestDirectory} /livecoding"
95+ if [ ! -f " ${temporaryTestDirectory} /livecoding/README.md" ]; then
96+ fail " ${test_case_number} .) Test failed: Expected 'README.md' in cloned repository 'livecoding'."
97+ fi
98+
99+ # ------- Unit Test Case
100+ test_case_number=2
101+ echo " "
102+ info " ${test_case_number} .) Should fail when an unknown option is used (dry-run)."
103+ cloneGitRepositoryExpectingFailureUnderTest --non-existing-parameter --dry-run
104+
105+ # ------- Unit Test Case
106+ test_case_number=3
107+ echo " "
108+ info " ${test_case_number} .) Should fail when --url is from a different domain than GitHub (dry-run)."
109+ cloneGitRepositoryExpectingFailureUnderTest --url " https://example.com/JohT/livecoding.git" --dry-run
110+
111+ # ------- Unit Test Case
112+ test_case_number=4
113+ echo " "
114+ info " ${test_case_number} .) Should fail when --branch is empty (dry-run)."
115+ cloneGitRepositoryExpectingFailureUnderTest --url " https://github.com/JohT/livecoding.git" --branch " " --dry-run
116+
117+ # ------- Unit Test Case
118+ test_case_number=5
119+ echo " "
120+ info " ${test_case_number} .) Should fail when --branch contains invalid characters (dry-run)."
121+ cloneGitRepositoryExpectingFailureUnderTest --url " https://github.com/JohT/livecoding.git" --branch " main;" --dry-run
122+
123+ # ------- Unit Test Case
124+ test_case_number=6
125+ echo " "
126+ info " ${test_case_number} .) Should fail when --history-only is neither true nor false (dry-run)."
127+ cloneGitRepositoryExpectingFailureUnderTest --url " https://github.com/JohT/livecoding.git" --history-only " invalid" --dry-run
128+
129+ # ------- Unit Test Case
130+ test_case_number=7
131+ echo " "
132+ info " ${test_case_number} .) Should fail when --target is empty (dry-run)."
133+ cloneGitRepositoryExpectingFailureUnderTest --url " https://github.com/JohT/livecoding.git" --target " " --dry-run
134+
135+ # ------- Unit Test Case
136+ test_case_number=8
137+ echo " "
138+ info " ${test_case_number} .) Should include the bare option in git clone when --history-option is true (dry-run)."
139+ output=$( cloneGitRepositoryExpectingSuccessUnderTest --url " https://github.com/JohT/livecoding.git" --history-only " true" --target " ${temporaryTestDirectory} /livecoding" --dry-run)
140+
141+ if ! echo " ${output} " | grep -q " git clone --bare" ; then
142+ fail " ${test_case_number} .) Test failed: Expected '--bare' option in git clone command."
143+ fi
144+
145+ successful
146+ return 0
0 commit comments