1+ #! /usr/bin/env bash
2+
3+ # Tests "installJavaScriptDependencies.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 logFileContent
46+ logFileContent=$( cat " ${temporaryTestDirectory} /${SCRIPT_NAME} -${test_case_number} .log" )
47+ # Remove color codes from the output for better readability in test logs
48+ logFileContent=$( echo -e " ${logFileContent} " | sed -r " s/\x1B\[[0-9;]*[mK]//g" )
49+ echo -e " ${COLOR_DE_EMPHASIZED}${logFileContent}${COLOR_DEFAULT} "
50+ }
51+
52+ installJavaScriptDependenciesExpectingSuccessUnderTest () {
53+ local COLOR_DE_EMPHASIZED=' \033[0;90m' # dark gray
54+ (
55+ cd " ${temporaryTestDirectory} " ;
56+ source " ${SCRIPTS_DIR} /installJavaScriptDependencies.sh" " $@ " > " ${temporaryTestDirectory} /${SCRIPT_NAME} -${test_case_number} .log" 2>&1
57+ )
58+ exitCode=$?
59+ if [ ${exitCode} -ne 0 ]; then
60+ fail " ❌ Test failed: Script exited with non-zero exit code ${exitCode} ."
61+ fi
62+ printTestLogFileContent
63+ }
64+
65+ installJavaScriptDependenciesExpectingFailureUnderTest () {
66+ set +o errexit
67+ (
68+ cd " ${temporaryTestDirectory} " ;
69+ source " ${SCRIPTS_DIR} /installJavaScriptDependencies.sh" " $@ " > " ${temporaryTestDirectory} /${SCRIPT_NAME} -${test_case_number} .log" 2>&1
70+ exitCode=$?
71+ if [ ${exitCode} -eq 0 ]; then
72+ fail " ❌ Test failed: Script exited with zero exit code but was expected to fail."
73+ fi
74+ )
75+ set -o errexit
76+ printTestLogFileContent
77+ }
78+
79+ info " Starting tests...."
80+
81+ # Create testing resources
82+ temporaryTestDirectory=$( mktemp -d 2> /dev/null || mktemp -d -t " temporaryTestDirectory_${SCRIPT_NAME} " )
83+ mkdir -p " ${temporaryTestDirectory} /source"
84+
85+ # ------- Unit Test Case
86+ test_case_number=1
87+ echo " "
88+ info " ${test_case_number} .) Should do nothing if the source folder is empty (dry-run)."
89+ result=$( installJavaScriptDependenciesExpectingSuccessUnderTest " --dry-run" )
90+ echo " ${result} "
91+ if [[ " ${result} " == * " Installing" * ]]; then
92+ fail " ❌ Test failed: Expected no installation attempts, but some were found:\n${result} "
93+ fi
94+
95+ # ------- Unit Test Case
96+ test_case_number=2
97+ echo " "
98+ info " ${test_case_number} .) Should do nothing when the source folder contains no JavaScript projects (dry-run)."
99+ mkdir -p " ${temporaryTestDirectory} /source/non-javascript-project"
100+ echo " This is a text file." > " ${temporaryTestDirectory} /source/non-javascript-project/README.md"
101+ result=$( installJavaScriptDependenciesExpectingSuccessUnderTest " --dry-run" )
102+ if [[ " ${result} " == * " Installing" * ]]; then
103+ fail " ❌ Test failed: Expected no installation attempts, but some were found:\n${result} "
104+ fi
105+
106+ # ------- Unit Test Case
107+ test_case_number=3
108+ echo " "
109+ info " ${test_case_number} .) Should do nothing when the source folder contains a private npm project (dry-run)."
110+ mkdir -p " ${temporaryTestDirectory} /source/private-npm-project"
111+ echo " { \" name\" : \" private-npm-project\" }" > " ${temporaryTestDirectory} /source/private-npm-project/package-lock.json"
112+ echo " { \" name\" : \" private-npm-project\" , \" private\" : true }" > " ${temporaryTestDirectory} /source/private-npm-project/package.json"
113+ result=$( installJavaScriptDependenciesExpectingSuccessUnderTest " --dry-run" )
114+ if [[ " ${result} " == * " Installing" * ]]; then
115+ fail " ❌ Test failed: Expected no installation attempts, but some were found:\n${result} "
116+ fi
117+
118+ # ------- Unit Test Case
119+ test_case_number=4
120+ echo " "
121+ info " ${test_case_number} .) Should do nothing when the source folder contains a private yarn project (dry-run)."
122+ mkdir -p " ${temporaryTestDirectory} /source/private-yarn-project"
123+ echo " { \" name\" : \" private-yarn-project\" }" > " ${temporaryTestDirectory} /source/private-yarn-project/package-lock.json"
124+ echo " { \" name\" : \" private-yarn-project\" , \" private\" : true }" > " ${temporaryTestDirectory} /source/private-yarn-project/package.json"
125+ result=$( installJavaScriptDependenciesExpectingSuccessUnderTest " --dry-run" )
126+ if [[ " ${result} " == * " Installing" * ]]; then
127+ fail " ❌ Test failed: Expected no installation attempts, but some were found:\n${result} "
128+ fi
129+
130+ # ------- Unit Test Case
131+ test_case_number=5
132+ echo " "
133+ info " ${test_case_number} .) Should install npm dependencies for a npm project (dry-run)."
134+ mkdir -p " ${temporaryTestDirectory} /source/npm-project"
135+ echo " { \" name\" : \" npm-project\" }" > " ${temporaryTestDirectory} /source/npm-project/package-lock.json"
136+ result=$( installJavaScriptDependenciesExpectingSuccessUnderTest " --dry-run" )
137+ if [[ ! " ${result} " == * " Installing JavaScript dependencies with npm in" * ]]; then
138+ fail " ❌ Test failed: Expected npm installation attempt, but none was found:\n${result} "
139+ fi
140+
141+ # ------- Unit Test Case
142+ test_case_number=6
143+ echo " "
144+ info " ${test_case_number} .) Should install pnpm dependencies for a pnpm project (dry-run)."
145+ mkdir -p " ${temporaryTestDirectory} /source/pnpm-project"
146+ echo " name: pnpm-project" > " ${temporaryTestDirectory} /source/pnpm-project/pnpm-lock.yaml"
147+ result=$( installJavaScriptDependenciesExpectingSuccessUnderTest " --dry-run" )
148+ if [[ ! " ${result} " == * " Installing JavaScript dependencies with pnpm in" * ]]; then
149+ fail " ❌ Test failed: Expected pnpm installation attempt, but none was found:\n${result} "
150+ fi
151+
152+ # ------- Unit Test Case
153+ test_case_number=7
154+ echo " "
155+ info " ${test_case_number} .) Should install yarn dependencies for a yarn project (dry-run)."
156+ mkdir -p " ${temporaryTestDirectory} /source/yarn-project"
157+ echo " name: yarn-project" > " ${temporaryTestDirectory} /source/yarn-project/yarn.lock"
158+ result=$( installJavaScriptDependenciesExpectingSuccessUnderTest " --dry-run" )
159+ if [[ ! " ${result} " == * " Installing JavaScript dependencies with yarn in" * ]]; then
160+ fail " ❌ Test failed: Expected yarn installation attempt, but none was found:\n${result} "
161+ fi
162+
163+ successful
164+ return 0
0 commit comments