-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanyform.bats
executable file
·325 lines (277 loc) · 8.6 KB
/
anyform.bats
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env bats
setup() {
# Get the absolute path of the script directory
SCRIPT_DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )"
SCRIPT_PATH="${SCRIPT_DIR}/anyform"
# Ensure the script is executable
chmod +x "$SCRIPT_PATH"
# Create mock bin directory
MOCK_BIN_DIR="$(mktemp -d)"
mkdir -p "${MOCK_BIN_DIR}"
# Create mock git script
cat > "${MOCK_BIN_DIR}/git" << 'EOF'
#!/bin/sh
case "$1" in
"clone")
mkdir -p "/tmp/terraform-provider-corner"
;;
"init"|"add"|"commit"|"tag")
return 0
;;
"fetch")
echo "Fetching..."
;;
"rev-parse")
if [ "$2" = "--quiet" ] && [ "$3" = "--verify" ] && [ "$4" = "abc123^{commit}" ]; then
# Explicitly fail for test commit
exit 1
fi
if [ "$2" = "--quiet" ] && [ "$3" = "--verify" ]; then
# For other commits, check if they exist
echo "$4" | grep -q "abc123" && exit 1 || exit 0
fi
echo "master"
;;
"checkout")
echo "Switching to $2"
;;
"describe")
echo "v1.0.0"
;;
"symbolic-ref")
echo "refs/remotes/origin/master"
;;
*)
echo "Git command $1"
;;
esac
exit 0
EOF
chmod +x "${MOCK_BIN_DIR}/git"
# Create mock go script
cat > "${MOCK_BIN_DIR}/go" << 'EOF'
#!/bin/sh
case "$1" in
"build")
touch terraform-provider-corner_v1.0.0
;;
"env")
case "$2" in
"GOOS")
echo "darwin"
;;
"GOARCH")
echo "amd64"
;;
esac
;;
esac
exit 0
EOF
chmod +x "${MOCK_BIN_DIR}/go"
# Create mock curl script
cat > "${MOCK_BIN_DIR}/curl" << 'EOF'
#!/bin/sh
echo "Mocked curl"
exit 0
EOF
chmod +x "${MOCK_BIN_DIR}/curl"
# Add mock bin to PATH
PATH="${MOCK_BIN_DIR}:/usr/bin:$PATH"
# Create temporary test directory
TEST_TEMP_DIR="$(mktemp -d)"
# Mock git repository setup
mkdir -p "${TEST_TEMP_DIR}/terraform-provider-corner"
MOCK_REPO="https://github.com/hashicorp/terraform-provider-corner"
# Set necessary environment variables for testing
export REPO_ADDRESS=""
export COMMIT_VERSION=""
export TEMP_DIR=""
# Backup original PATH
ORIGINAL_PATH="$PATH"
# Prepend MOCK_BIN_DIR to PATH
PATH="${MOCK_BIN_DIR}:$ORIGINAL_PATH"
}
teardown() {
# Restore the original PATH
PATH="$ORIGINAL_PATH"
# Clean up temporary directory
rm -rf "${TEST_TEMP_DIR}"
rm -rf "${MOCK_BIN_DIR}"
rm -rf "/tmp/terraform-provider-corner"
}
# Function to extract version from the script
get_version() {
grep '^VERSION=' "$SCRIPT_PATH" | cut -d '"' -f 2
}
# Mock functions to override external commands
mock_git() {
case "$1" in
"clone")
mkdir -p "/tmp/terraform-provider-corner"
;;
"fetch")
return 0
;;
"rev-parse")
echo "abcd1234"
;;
"checkout")
return 0
;;
"describe")
echo "v1.0.0"
;;
*)
return 0
;;
esac
}
mock_go_build() {
echo "Mocked go build"
touch terraform-provider-corner_v1.0.0
return 0
}
@test "prints usage when no arguments provided" {
run "$SCRIPT_PATH"
[ "$status" -eq 1 ]
[[ "${lines[0]}" =~ "Usage:" ]]
}
@test "prints help message with --help flag" {
run "$SCRIPT_PATH" --help
[ "$status" -eq 0 ]
[[ "${lines[0]}" =~ "Usage:" ]]
}
@test "prints version with --version flag" {
VERSION=$(get_version)
run "$SCRIPT_PATH" --version
echo "output: $output, version: $VERSION" # Debug output
[ "$status" -eq 0 ]
echo "$output" | grep -q "AnyForm version ${VERSION}"
}
@test "validates repository address format" {
run "$SCRIPT_PATH" "invalid-repo-address"
[ "$status" -eq 1 ]
[[ "${output}" =~ "Error: Unable to extract organization from repository address" ]]
}
@test "accepts valid repository address" {
run "$SCRIPT_PATH" "${MOCK_REPO}"
echo "output: $output"
echo "status: $status"
[ "$status" -eq 0 ]
[[ "${output}" =~ "Organization: hashicorp" ]]
[[ "${output}" =~ "Provider Type: corner" ]]
}
@test "handles print configuration flag" {
run "$SCRIPT_PATH" -p "${MOCK_REPO}"
[ "$status" -eq 0 ]
[[ "${output}" =~ "required_providers" ]]
}
@test "validates commit version when provided" {
# No need to export mock functions as we're using PATH
run "$SCRIPT_PATH" "${MOCK_REPO}" "abc123"
echo "output: $output"
echo "status: $status"
[ "$status" -eq 1 ]
[[ "${output}" =~ "Failed to verify commit" ]]
}
@test "processes tagged version correctly" {
export -f mock_go_build
cd "${TEST_TEMP_DIR}/terraform-provider-corner"
TAGGED_VERSION="v1.0.0"
run "$SCRIPT_PATH" "${MOCK_REPO}" "${TAGGED_VERSION}"
[ "$status" -eq 0 ]
[[ "${output}" =~ "Checked out version: ${TAGGED_VERSION}" ]]
}
@test "handles pull request URL" {
run "$SCRIPT_PATH" "https://github.com/hashicorp/terraform-provider-corner/pull/123"
[ "$status" -eq 0 ]
[[ "${output}" =~ "Fetching Pull Request #123" ]]
[[ "${output}" =~ "Organization: hashicorp" ]]
[[ "${output}" =~ "Provider Type: corner" ]]
}
@test "fails gracefully with invalid PR URL" {
run "$SCRIPT_PATH" "https://github.com/hashicorp/terraform-provider-corner/pull/abc"
[ "$status" -eq 1 ]
[[ "${output}" =~ "Error: Invalid pull request number" ]]
}
@test "checks self-update with current version" {
VERSION=$(get_version)
# Mock curl to return current version
function curl() {
echo "{\"tag_name\": \"$VERSION\"}"
}
export -f curl
run "$SCRIPT_PATH" --self-update
echo "output: $output" # Debug output
[ "$status" -eq 0 ]
echo "$output" | grep -q "Already running the latest version"
}
@test "check for updates when on latest version" {
VERSION=$(get_version)
function curl() {
echo "{\"tag_name\": \"$VERSION\"}"
}
export -f curl
run "$SCRIPT_PATH" --check-update
echo "output: $output" # Debug output
[ "$status" -eq 0 ]
echo "$output" | grep -q "You are running the latest version"
}
@test "check for updates when update available" {
VERSION=$(get_version)
if ! [[ "$VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
skip "Current version is not in semantic version format"
fi
# Create a mock curl function that returns proper JSON response
function curl() {
if [[ "$*" == *"api.github.com"* ]]; then
# Return a mock JSON response with a higher version
local current_version=${VERSION#v} # Remove 'v' prefix
local major minor patch
IFS='.' read -r major minor patch <<< "$current_version"
local new_patch=$((patch + 1))
echo "{\"tag_name\": \"v$major.$minor.$new_patch\"}"
else
command curl "$@"
fi
}
export -f curl
run "$SCRIPT_PATH" --check-update
echo "output: $output" # Debug output
[ "$status" -eq 0 ]
[[ "$output" =~ "Update available:" ]]
}
@test "fails when git is not installed" {
# Backup the original mock 'git' script
mv "${MOCK_BIN_DIR}/git" "${MOCK_BIN_DIR}/git_backup"
# Replace mock 'git' with a script that exits with code 127
echo -e '#!/bin/sh\nexit 127' > "${MOCK_BIN_DIR}/git"
chmod +x "${MOCK_BIN_DIR}/git"
run "$SCRIPT_PATH" "${MOCK_REPO}"
[ "$status" -eq 1 ]
[[ "${output}" =~ "git is not installed. Please install git and try again." ]]
# Restore the original mock 'git' script
rm "${MOCK_BIN_DIR}/git"
mv "${MOCK_BIN_DIR}/git_backup" "${MOCK_BIN_DIR}/git"
}
@test "fails when curl is not installed" {
# Backup the original mock 'curl' script
mv "${MOCK_BIN_DIR}/curl" "${MOCK_BIN_DIR}/curl_backup"
# Replace mock 'curl' with a script that exits with code 127
echo -e '#!/bin/sh\nexit 127' > "${MOCK_BIN_DIR}/curl"
chmod +x "${MOCK_BIN_DIR}/curl"
run "$SCRIPT_PATH" --self-update
# Debug output to help identify the issue
if [ "$status" -ne 1 ] || ! [[ "${output}" =~ "curl is not installed." ]]; then
echo "Debug Information:"
echo "Exit Status: $status"
echo "Output:"
echo "$output"
fi
[ "$status" -eq 1 ]
[[ "${output}" =~ "curl is not installed. Please install curl to use self-update feature." ]]
# Restore the original mock 'curl' script
rm "${MOCK_BIN_DIR}/curl"
mv "${MOCK_BIN_DIR}/curl_backup" "${MOCK_BIN_DIR}/curl"
}