-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.bats
More file actions
197 lines (163 loc) · 5.48 KB
/
test.bats
File metadata and controls
197 lines (163 loc) · 5.48 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
#!/usr/bin/env bats
load './phaset.sh'
# Mock environment variables
setup() {
export ORG_ID="org123"
export RECORD_ID="rec123"
export TOKEN="token123"
export ACTION="deployment"
send_request() {
echo "Mocked send_request called with URL: $1, method: $2, data file: $3"
return 0
}
}
teardown() {
unset ORG_ID
unset RECORD_ID
unset TOKEN
unset ACTION
}
# Test parse_arguments
@test "parse_arguments correctly sets variables" {
run parse_arguments --org-id "test_org" --record-id "test_record" --token "test_token" --action "deployment"
[ "$status" -eq 0 ]
#[ "$ORG_ID" = "test_org" ]
#[ "$RECORD_ID" = "test_record" ]
#[ "$TOKEN" = "test_token" ]
#[ "$ACTION" = "deployment" ]
}
@test "parse_arguments fails on unknown argument" {
run parse_arguments --unknown "value"
[ "$status" -ne 0 ]
[[ "$output" =~ "❌ Unknown argument: --unknown" ]]
}
# Test validate_arguments
@test "validate_arguments succeeds when all required arguments are set" {
run validate_arguments x y z a
[ "$status" -eq 0 ]
}
@test "validate_arguments fails when ORG_ID is missing" {
unset ORG_ID
run validate_arguments
[ "$status" -ne 0 ]
[[ "$output" =~ "❌ ERROR: ORG_ID is not set!" ]]
}
@test "validate_arguments fails when RECORD_ID is missing and cannot be inferred" {
unset RECORD_ID
# Create a temporary manifest file without an "id" key
local manifest_file="test_manifest.json"
echo '{}' >"$manifest_file"
MANIFEST_FILE="$manifest_file"
run validate_arguments
# Ensure the function fails and outputs the appropriate error
[ "$status" -ne 0 ]
[[ "$output" =~ "❌ ERROR: RECORD_ID is not set and could not be inferred from '$manifest_file'!" ]]
# Cleanup
rm -f "$manifest_file"
}
@test "validate_arguments succeeds when RECORD_ID is inferred from the manifest file" {
unset RECORD_ID
# Create a temporary manifest file with an "id" key
local manifest_file="test_manifest.json"
echo '{"id": "test-id"}' >"$manifest_file"
MANIFEST_FILE="$manifest_file"
run validate_arguments
# Ensure the function succeeds and RECORD_ID is set correctly
[ "$status" -eq 0 ]
[[ -n "$RECORD_ID" ]]
[[ "$RECORD_ID" == "test-id" ]]
# Cleanup
rm -f "$manifest_file"
}
@test "validate_arguments fails when TOKEN is missing" {
unset TOKEN
run validate_arguments
[ "$status" -ne 0 ]
[[ "$output" =~ "❌ ERROR: TOKEN is not set!" ]]
}
@test "validate_arguments fails when ACTION is missing" {
unset ACTION
run validate_arguments
[ "$status" -ne 0 ]
[[ "$output" =~ "❌ ERROR: ACTION is not set!" ]]
}
@test "handle_standards sends results when file exists and baseline is fetched" {
touch standardlint.results.json
touch phaset.manifest.json
echo '{"baseline": {"id": "test_baseline_id"}}' >phaset.manifest.json
echo '{}' >standardlint.results.json
run handle_standards
[ "$status" -eq 0 ]
[[ "$output" =~ "Baseline ID: 'test_baseline_id'" ]]
[[ "$output" =~ "✅ Downloaded standards baseline to standardlint.json." ]]
[[ "$output" =~ "✅ Request succeeded with HTTP Status" ]]
rm -f standardlint.results.json
rm -f phaset.manifest.json
}
@test "handle_standards fetches baseline with empty ID when baseline.id is missing" {
touch phaset.manifest.json
echo '{"baseline": {}}' >phaset.manifest.json
run handle_standards
[ "$status" -eq 0 ]
[[ "$output" =~ "Baseline ID: ''" ]]
[[ "$output" =~ "✅ Downloaded standards baseline to standardlint.json." ]]
rm -f phaset.manifest.json
}
@test "handle_standards fails when Node.js is not installed" {
PATH="" # Temporarily unset PATH to simulate Node.js not being installed
run handle_standards
[ "$status" -ne 0 ]
[[ "$output" =~ "❌ Node.js is required to generate Standards output" ]]
}
@test "handle_standards skips when no standardlint.results.json file exists" {
touch phaset.manifest.json
echo '{"baseline": {"id": "test_baseline_id"}}' >phaset.manifest.json
run handle_standards
[ "$status" -eq 0 ]
[[ "$output" =~ "Baseline ID: 'test_baseline_id'" ]]
[[ "$output" =~ "✅ Downloaded standards baseline to standardlint.json." ]]
[[ "$output" =~ "⚠️ No standards results file found; skipping." ]]
rm -f phaset.manifest.json
}
@test "handle_record uploads manifest file" {
touch phaset.manifest.json
echo '{}' >phaset.manifest.json
run handle_record
[ "$status" -eq 0 ]
[[ "$output" =~ "✅ Request succeeded with HTTP Status" ]]
rm -f phaset.manifest.json
}
@test "handle_deployment fails if Git is not installed" {
PATH="" # Temporarily unset PATH to simulate Git not being installed
run handle_deployment
[ "$status" -ne 0 ]
[[ "$output" =~ "❌ ERROR: Git is not installed" ]]
}
@test "handle_deployment uses fallback SHA when Git repository has no commits" {
git init test_repo
cd test_repo || exit
run handle_deployment
[ "$status" -eq 0 ]
[[ "$output" =~ "⚠️ WARNING: Git repository has no commits or is invalid" ]]
cd ..
rm -f -rf test_repo
}
@test "handle_deployment succeeds with a valid Git repository" {
git init test_repo
cd test_repo || exit
touch file.txt && git add file.txt && git commit -m "Initial commit"
run handle_deployment
[ "$status" -eq 0 ]
[[ "$output" =~ "✅ Deployment data successfully sent" ]]
cd ..
rm -f -rf test_repo
}
@test "send_request succeeds with valid inputs" {
touch test.json
echo '{}' >test.json
MOCK_CURL_FAILURE=""
run send_request "https://example.com" "POST" "test.json"
[ "$status" -eq 0 ]
[[ "$output" =~ "✅ Request succeeded with HTTP Status" ]]
rm -f test.json
}