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+ output=$( 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+ # TODO implement further test cases
100+ # # ------- Integration Test Case
101+ # test_case_number=3
102+ # echo ""
103+ # info "${test_case_number}.) Should fail when downloading non-existing Maven artifact (real-run/integration)."
104+ # downloadMavenArtifactsExpectingFailureUnderTest "org.nonexistent:nonexistent-artifact:0.0.1"
105+
106+ # # ------- Unit Test Case
107+ # test_case_number=4
108+ # echo ""
109+ # info "${test_case_number}.) Should fail when no input is specified (dry-run)."
110+ # downloadMavenArtifactsExpectingFailureUnderTest "--dry-run"
111+
112+ # # ------- Unit Test Case
113+ # test_case_number=5
114+ # echo ""
115+ # info "${test_case_number}.) Should fail when input is empty (dry-run)."
116+ # downloadMavenArtifactsExpectingFailureUnderTest "--dry-run"
117+
118+ # # ------- Unit Test Case
119+ # test_case_number=6
120+ # echo ""
121+ # info "${test_case_number}.) Should fail on unknown arguments."
122+ # downloadMavenArtifactsExpectingFailureUnderTest "--dry-run --unknown-argument"
123+
124+ # # ------- Unit Test Case
125+ # test_case_number=7
126+ # echo ""
127+ # info "${test_case_number}.) Should fail when artifacts directory is missing (dry-run)."
128+ # # Rename artifacts directory to simulate missing directory
129+ # mv "${temporaryTestDirectory}/artifacts" "${temporaryTestDirectory}/artifacts_backup"
130+
131+ # downloadMavenArtifactsExpectingFailureUnderTest "--dry-run"
132+
133+ # # Restore artifacts directory
134+ # mv "${temporaryTestDirectory}/artifacts_backup" "${temporaryTestDirectory}/artifacts"
135+
136+ # # ------- Unit Test Case
137+ # test_case_number=8
138+ # echo ""
139+ # info "${test_case_number}.) Should fail when the artifact coordinate has a wrong format (dry-run)."
140+ # downloadMavenArtifactsExpectingFailureUnderTest "--dry-run" "org.apache.commons:commons-lang3-3.12.0"
141+
142+ successful
143+ return 0
0 commit comments