-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathactions_helpers_test.sh
More file actions
160 lines (131 loc) · 4.72 KB
/
actions_helpers_test.sh
File metadata and controls
160 lines (131 loc) · 4.72 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
#!/usr/bin/env bash
# Tests for actions_helpers.sh helpers.
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"
source ./test_helpers.sh
source ./actions_helpers.sh
TMPDIR_TEST=$(mktemp -d)
trap 'rm -rf "$TMPDIR_TEST"' EXIT
# --- log_* tests ---
test_log_error() {
local output
output=$(log_error "something broke")
[ "$output" = "::error::something broke" ]
}
expect_success "log_error: formats correctly" test_log_error
test_log_warning() {
local output
output=$(log_warning "watch out")
[ "$output" = "::warning::watch out" ]
}
expect_success "log_warning: formats correctly" test_log_warning
test_log_notice() {
local output
output=$(log_notice "fyi")
[ "$output" = "::notice::fyi" ]
}
expect_success "log_notice: formats correctly" test_log_notice
# --- set_output tests ---
test_set_output() {
GITHUB_OUTPUT="$TMPDIR_TEST/gh_out_single"
touch "$GITHUB_OUTPUT"
set_output "mykey" "myvalue"
check_contains 'mykey=myvalue' "$GITHUB_OUTPUT"
}
expect_success "set_output: writes key=value" test_set_output
test_set_output_empty_value() {
GITHUB_OUTPUT="$TMPDIR_TEST/gh_out_empty"
touch "$GITHUB_OUTPUT"
set_output "mykey" ""
check_contains 'mykey=' "$GITHUB_OUTPUT"
}
expect_success "set_output: handles empty value" test_set_output_empty_value
# --- set_output_multiline tests ---
test_set_output_multiline() {
GITHUB_OUTPUT="$TMPDIR_TEST/gh_out_multi"
touch "$GITHUB_OUTPUT"
set_output_multiline "desc" "line one
line two"
check_contains 'line one' "$GITHUB_OUTPUT" && check_contains 'line two' "$GITHUB_OUTPUT"
}
expect_success "set_output_multiline: writes multiline content" test_set_output_multiline
test_set_output_multiline_delimiters() {
GITHUB_OUTPUT="$TMPDIR_TEST/gh_out_delim"
touch "$GITHUB_OUTPUT"
set_output_multiline "desc" "content"
# Should have opening delimiter (desc<<GHEOF_...) and closing delimiter (GHEOF_...)
check_contains_pattern '^desc<<GHEOF_' "$GITHUB_OUTPUT" && check_contains_pattern '^GHEOF_' "$GITHUB_OUTPUT"
}
expect_success "set_output_multiline: uses GHEOF delimiters" test_set_output_multiline_delimiters
test_set_output_multiline_empty() {
GITHUB_OUTPUT="$TMPDIR_TEST/gh_out_multi_empty"
touch "$GITHUB_OUTPUT"
set_output_multiline "desc" ""
check_contains_pattern '^desc<<GHEOF_' "$GITHUB_OUTPUT"
}
expect_success "set_output_multiline: handles empty value" test_set_output_multiline_empty
# --- require_command tests ---
test_require_command_found() { require_command bash; }
expect_success "require_command: finds bash" test_require_command_found
test_require_command_missing() { require_command nonexistent_cmd_xyz; }
expect_failure "require_command: fails for missing command" test_require_command_missing
# --- get_base_branch tests ---
test_get_base_branch_provided() {
local result
result=$(get_base_branch "my-branch" "owner/repo" 2>&1)
echo "$result" | check_contains "my-branch"
}
expect_success "get_base_branch: returns provided branch" test_get_base_branch_provided
test_get_base_branch_parses_github_json_develop() {
# Mock gh to return actual GitHub API JSON format (verified format)
# This tests the jq parsing: .defaultBranchRef.name
gh() {
if [[ "$*" == *"repo view"* ]]; then
echo '{"defaultBranchRef":{"name":"develop"}}'
fi
}
export -f gh
local result
result=$(get_base_branch "" "owner/repo" 2>&1)
echo "$result" | check_contains "develop"
unset -f gh
}
expect_success "get_base_branch: parses GitHub JSON for develop branch" test_get_base_branch_parses_github_json_develop
test_get_base_branch_parses_github_json_null() {
# Test null defaultBranchRef (empty repo case)
gh() {
if [[ "$*" == *"repo view"* ]]; then
echo '{"defaultBranchRef":null}'
fi
}
export -f gh
local result
result=$(get_base_branch "" "owner/repo" 2>&1)
# jq outputs "null" as literal string, which should be treated as empty
echo "$result" | check_contains "main"
unset -f gh
}
expect_success "get_base_branch: handles null defaultBranchRef" test_get_base_branch_parses_github_json_null
test_get_base_branch_parses_branch_with_slash() {
# Test branch names with slashes (e.g., release/v2.0)
gh() {
if [[ "$*" == *"repo view"* ]]; then
echo '{"defaultBranchRef":{"name":"release/v2.0"}}'
fi
}
export -f gh
local result
result=$(get_base_branch "" "owner/repo" 2>&1)
echo "$result" | check_contains "release/v2.0"
unset -f gh
}
expect_success "get_base_branch: handles branch names with slashes" test_get_base_branch_parses_branch_with_slash
test_get_base_branch_gh_failure() {
# Test in a directory that's not a git repo - gh should fail
(
cd "$TMPDIR_TEST"
get_base_branch "" "owner/repo"
)
}
expect_failure "get_base_branch: fails when gh fails" test_get_base_branch_gh_failure
print_results