-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest-parse-fossa-params.sh
More file actions
executable file
·292 lines (231 loc) · 8.42 KB
/
test-parse-fossa-params.sh
File metadata and controls
executable file
·292 lines (231 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/bin/bash
set -euo pipefail
###############################################################################
# Test Suite for parse-fossa-params.sh
#
# Usage:
# ./test-parse-fossa-params.sh
###############################################################################
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEST_PASSED=0
TEST_FAILED=0
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
###############################################################################
# Test Helper Functions
###############################################################################
assert_equals() {
local expected="$1"
local actual="$2"
local test_name="$3"
if [ "$expected" == "$actual" ]; then
echo -e "${GREEN}✓${NC} $test_name"
((TEST_PASSED++))
return 0
else
echo -e "${RED}✗${NC} $test_name"
echo " Expected: $expected"
echo " Actual: $actual"
((TEST_FAILED++))
return 1
fi
}
assert_contains() {
local haystack="$1"
local needle="$2"
local test_name="$3"
if [[ "$haystack" == *"$needle"* ]]; then
echo -e "${GREEN}✓${NC} $test_name"
((TEST_PASSED++))
return 0
else
echo -e "${RED}✗${NC} $test_name"
echo " Expected to contain: $needle"
echo " Actual: $haystack"
((TEST_FAILED++))
return 1
fi
}
###############################################################################
# Test Cases
###############################################################################
test_basic_flag_parameter() {
echo ""
echo "Test: Basic flag parameter"
export SCA_FOSSA_ANALYZE_DEBUG="true"
export FOSSA_PARAMS_CONFIG="$SCRIPT_DIR/fossa-params.json"
source "$SCRIPT_DIR/parse-fossa-params.sh"
build_fossa_args > /dev/null
assert_contains "$FOSSA_CLI_ARGS" "--debug" "Should include --debug flag"
unset SCA_FOSSA_ANALYZE_DEBUG
unset FOSSA_CLI_ARGS
}
test_value_parameter() {
echo ""
echo "Test: Value parameter"
export SCA_FOSSA_BRANCH="main"
export FOSSA_PARAMS_CONFIG="$SCRIPT_DIR/fossa-params.json"
source "$SCRIPT_DIR/parse-fossa-params.sh"
build_fossa_args > /dev/null
assert_contains "$FOSSA_CLI_ARGS" "--branch main" "Should include --branch with value"
unset SCA_FOSSA_BRANCH
unset FOSSA_CLI_ARGS
}
test_multiple_parameters() {
echo ""
echo "Test: Multiple parameters"
export SCA_FOSSA_ANALYZE_DEBUG="true"
export SCA_FOSSA_BRANCH="PR"
export SCA_FOSSA_REVISION="abc123"
export SCA_FOSSA_PATH="sam-mongodb"
export SCA_FOSSA_CONFIG="sam-mongodb/.fossa.yml"
export FOSSA_PARAMS_CONFIG="$SCRIPT_DIR/fossa-params.json"
source "$SCRIPT_DIR/parse-fossa-params.sh"
build_fossa_args > /dev/null
assert_contains "$FOSSA_CLI_ARGS" "--debug" "Should include --debug"
assert_contains "$FOSSA_CLI_ARGS" "--branch PR" "Should include --branch PR"
assert_contains "$FOSSA_CLI_ARGS" "--revision abc123" "Should include --revision"
assert_contains "$FOSSA_CLI_ARGS" "--config sam-mongodb/.fossa.yml" "Should include --config"
unset SCA_FOSSA_ANALYZE_DEBUG SCA_FOSSA_BRANCH SCA_FOSSA_REVISION SCA_FOSSA_PATH SCA_FOSSA_CONFIG
unset FOSSA_CLI_ARGS
}
test_empty_value_not_included() {
echo ""
echo "Test: Empty value parameter not included"
export SCA_FOSSA_BRANCH=""
export FOSSA_PARAMS_CONFIG="$SCRIPT_DIR/fossa-params.json"
source "$SCRIPT_DIR/parse-fossa-params.sh"
build_fossa_args > /dev/null
if [[ "$FOSSA_CLI_ARGS" == *"--branch"* ]]; then
echo -e "${RED}✗${NC} Should not include --branch when value is empty"
echo " Actual: $FOSSA_CLI_ARGS"
((TEST_FAILED++))
else
echo -e "${GREEN}✓${NC} Should not include --branch when value is empty"
((TEST_PASSED++))
fi
unset SCA_FOSSA_BRANCH
unset FOSSA_CLI_ARGS
}
test_false_flag_not_included() {
echo ""
echo "Test: False flag parameter not included"
export SCA_FOSSA_ANALYZE_DEBUG="false"
export FOSSA_PARAMS_CONFIG="$SCRIPT_DIR/fossa-params.json"
source "$SCRIPT_DIR/parse-fossa-params.sh"
build_fossa_args > /dev/null
if [[ "$FOSSA_CLI_ARGS" == *"--debug"* ]]; then
echo -e "${RED}✗${NC} Should not include --debug when set to false"
echo " Actual: $FOSSA_CLI_ARGS"
((TEST_FAILED++))
else
echo -e "${GREEN}✓${NC} Should not include --debug when set to false"
((TEST_PASSED++))
fi
unset SCA_FOSSA_ANALYZE_DEBUG
unset FOSSA_CLI_ARGS
}
test_project_parameter() {
echo ""
echo "Test: Project parameter"
export SCA_FOSSA_PROJECT="MyOrg_my-project"
export FOSSA_PARAMS_CONFIG="$SCRIPT_DIR/fossa-params.json"
source "$SCRIPT_DIR/parse-fossa-params.sh"
build_fossa_args > /dev/null
assert_contains "$FOSSA_CLI_ARGS" "--project MyOrg_my-project" "Should include project override"
unset SCA_FOSSA_PROJECT
unset FOSSA_CLI_ARGS
}
test_command_filtering_analyze() {
echo ""
echo "Test: Command filtering - analyze"
export SCA_FOSSA_ANALYZE_DEBUG="true"
export SCA_FOSSA_CONFIG="sam-mongodb/.fossa.yml"
export FOSSA_PARAMS_CONFIG="$SCRIPT_DIR/fossa-params.json"
source "$SCRIPT_DIR/parse-fossa-params.sh"
build_fossa_args "analyze" > /dev/null
assert_contains "$FOSSA_CLI_ARGS" "--debug" "Analyze should include --debug"
assert_contains "$FOSSA_CLI_ARGS" "--config sam-mongodb/.fossa.yml" "Analyze should include --config"
unset SCA_FOSSA_ANALYZE_DEBUG SCA_FOSSA_CONFIG
unset FOSSA_CLI_ARGS
}
test_command_filtering_test() {
echo ""
echo "Test: Command filtering - test"
export SCA_FOSSA_ANALYZE_DEBUG="true"
export SCA_FOSSA_CONFIG="sam-mongodb/.fossa.yml"
export SCA_FOSSA_BRANCH="PR"
export SCA_FOSSA_REVISION="abc123"
export SCA_FOSSA_PROJECT="MyOrg_project"
export FOSSA_PARAMS_CONFIG="$SCRIPT_DIR/fossa-params.json"
source "$SCRIPT_DIR/parse-fossa-params.sh"
build_fossa_args "test" > /dev/null
# Test command should include these
assert_contains "$FOSSA_CLI_ARGS" "--revision abc123" "Test should include --revision"
assert_contains "$FOSSA_CLI_ARGS" "--project MyOrg_project" "Test should include --project"
assert_contains "$FOSSA_CLI_ARGS" "--config sam-mongodb/.fossa.yml" "Test should include --config"
# Test command should NOT include these (analyze-only)
if [[ "$FOSSA_CLI_ARGS" == *"--debug"* ]]; then
echo -e "${RED}✗${NC} Test should NOT include --debug (analyze-only)"
((TEST_FAILED++))
else
echo -e "${GREEN}✓${NC} Test should NOT include --debug (analyze-only)"
((TEST_PASSED++))
fi
if [[ "$FOSSA_CLI_ARGS" == *"--branch"* ]]; then
echo -e "${RED}✗${NC} Test should NOT include --branch (analyze-only)"
((TEST_FAILED++))
else
echo -e "${GREEN}✓${NC} Test should NOT include --branch (analyze-only)"
((TEST_PASSED++))
fi
unset SCA_FOSSA_ANALYZE_DEBUG SCA_FOSSA_CONFIG SCA_FOSSA_BRANCH SCA_FOSSA_REVISION SCA_FOSSA_PROJECT
unset FOSSA_CLI_ARGS
}
test_monorepo_use_case() {
echo ""
echo "Test: Monorepo use case (real-world scenario)"
export SCA_FOSSA_CONFIG="sam-mongodb/.fossa.yml"
export SCA_FOSSA_PROJECT="SolaceLabs_sam-mongodb"
export SCA_FOSSA_BRANCH="PR"
export SCA_FOSSA_REVISION="feature-branch"
export FOSSA_PARAMS_CONFIG="$SCRIPT_DIR/fossa-params.json"
source "$SCRIPT_DIR/parse-fossa-params.sh"
build_fossa_args > /dev/null
assert_contains "$FOSSA_CLI_ARGS" "--project SolaceLabs_sam-mongodb" "Should include project name"
assert_contains "$FOSSA_CLI_ARGS" "--config sam-mongodb/.fossa.yml" "Should include plugin config"
unset SCA_FOSSA_CONFIG SCA_FOSSA_PROJECT SCA_FOSSA_BRANCH SCA_FOSSA_REVISION
unset FOSSA_CLI_ARGS
}
###############################################################################
# Run All Tests
###############################################################################
echo "================================================="
echo "🧪 FOSSA Parameter Parser Test Suite"
echo "================================================="
test_basic_flag_parameter
test_value_parameter
test_multiple_parameters
test_empty_value_not_included
test_false_flag_not_included
test_project_parameter
test_command_filtering_analyze
test_command_filtering_test
test_monorepo_use_case
echo ""
echo "================================================="
echo "📊 Test Results"
echo "================================================="
echo -e "${GREEN}Passed:${NC} $TEST_PASSED"
echo -e "${RED}Failed:${NC} $TEST_FAILED"
echo "================================================="
if [ $TEST_FAILED -eq 0 ]; then
echo -e "${GREEN}✅ All tests passed!${NC}"
exit 0
else
echo -e "${RED}❌ Some tests failed${NC}"
exit 1
fi