Skip to content

Commit 3b28ab5

Browse files
committed
fix: have api-diff.test.sh test api-diff.sh and add dry-run mode
1 parent 8ef4ad4 commit 3b28ab5

File tree

2 files changed

+73
-36
lines changed

2 files changed

+73
-36
lines changed

scripts/api-diff/api-diff.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@ BASE_BRANCH="${BASE_BRANCH:-origin/master}"
1616

1717
FAIL_ON_BREAKING=false
1818
TARGET_FILE=""
19+
DRY_RUN=false
1920

2021
# Parse arguments
2122
for arg in "$@"; do
2223
if [ "$arg" = "--fail-on-breaking" ]; then
2324
FAIL_ON_BREAKING=true
25+
elif [ "$arg" = "--dry-run" ]; then
26+
DRY_RUN=true
2427
elif [[ "$arg" == *.yaml ]]; then
2528
TARGET_FILE="$arg"
2629
fi
2730
done
2831

2932
# If --fail-on-breaking not explicitly set, determine based on branch name
3033
if [ "$FAIL_ON_BREAKING" = false ]; then
31-
CURRENT_BRANCH=$(git branch --show-current 2>/dev/null || echo "")
34+
CURRENT_BRANCH=${CURRENT_BRANCH:-$(git branch --show-current 2>/dev/null || echo "")}
3235
if [[ "$CURRENT_BRANCH" == *breaking* ]]; then
3336
echo "Branch '$CURRENT_BRANCH' contains 'breaking', allowing breaking changes"
3437
FAIL_ON_BREAKING=false
@@ -38,6 +41,16 @@ if [ "$FAIL_ON_BREAKING" = false ]; then
3841
fi
3942
fi
4043

44+
if [ "$DRY_RUN" = true ]; then
45+
if [ "$FAIL_ON_BREAKING" = true ]; then
46+
echo "Mode: Failing on breaking changes"
47+
else
48+
echo "Mode: Allowing breaking changes"
49+
fi
50+
echo "Dry run mode, exiting after branch check"
51+
exit 0
52+
fi
53+
4154
echo "Starting API diff check..."
4255

4356
# Ensure we're in the repo root

scripts/api-diff/api-diff.test.sh

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,85 @@
11
#!/bin/bash
22

3-
# Unit test for GitHub Actions branch logic
4-
# This tests the conditional logic used in .github/workflows/api-diff.yml
3+
# Unit test for api-diff.sh branch logic
4+
# Tests the script's branch detection and FAIL_ON_BREAKING setting
55

66
set -e
77

8-
echo "=== Unit Test: Branch Logic Pattern Matching ==="
8+
echo "=== Unit Test: api-diff.sh Branch Logic ==="
99
echo
1010

1111
TESTS_PASSED=0
1212
TESTS_FAILED=0
1313

14-
# Helper function to test pattern
14+
SCRIPT_PATH="scripts/api-diff/api-diff.sh"
15+
16+
# Helper function to test script with branch
1517
test_branch() {
1618
local branch_name="$1"
17-
local should_allow_breaking="$2"
18-
local test_ref="refs/heads/$branch_name"
19+
local expected_mode="$2" # "allow" or "fail"
20+
local test_name="$3"
1921

20-
echo "Testing: $branch_name"
22+
echo "Testing: $test_name (branch: $branch_name)"
2123

22-
if [[ "$test_ref" == *breaking* ]]; then
23-
local result="allow"
24-
else
25-
local result="fail"
26-
fi
24+
# Run the script in dry-run mode with CURRENT_BRANCH set
25+
local output
26+
output=$(CURRENT_BRANCH="$branch_name" "$SCRIPT_PATH" --dry-run 2>&1)
2727

28-
if [[ "$result" == "$should_allow_breaking" ]]; then
29-
echo " ✓ PASS: Expected '$should_allow_breaking', got '$result'"
30-
TESTS_PASSED=$((TESTS_PASSED + 1))
31-
else
32-
echo " ✗ FAIL: Expected '$should_allow_breaking', got '$result'"
33-
TESTS_FAILED=$((TESTS_FAILED + 1))
28+
# Check the output for the expected message
29+
if [[ "$expected_mode" == "allow" ]]; then
30+
if echo "$output" | grep -q "Mode: Allowing breaking changes"; then
31+
echo " ✓ PASS: Correctly allows breaking changes"
32+
TESTS_PASSED=$((TESTS_PASSED + 1))
33+
else
34+
echo " ✗ FAIL: Expected to allow breaking changes, but output was:"
35+
echo "$output"
36+
TESTS_FAILED=$((TESTS_FAILED + 1))
37+
fi
38+
elif [[ "$expected_mode" == "fail" ]]; then
39+
if echo "$output" | grep -q "Mode: Failing on breaking changes"; then
40+
echo " ✓ PASS: Correctly fails on breaking changes"
41+
TESTS_PASSED=$((TESTS_PASSED + 1))
42+
else
43+
echo " ✗ FAIL: Expected to fail on breaking changes, but output was:"
44+
echo "$output"
45+
TESTS_FAILED=$((TESTS_FAILED + 1))
46+
fi
3447
fi
3548
echo
3649
}
3750

38-
# Test cases: test_branch "branch-name" "expected-result"
39-
# expected-result: "allow" = allow breaking changes, "fail" = fail on breaking
51+
# Test cases: test_branch "branch-name" "expected-mode" "description"
52+
# expected-mode: "allow" = allows breaking changes, "fail" = fails on breaking
4053

4154
echo "--- Branches that SHOULD allow breaking changes ---"
42-
test_branch "breaking-api-changes" "allow"
43-
test_branch "breaking-remove-deprecated" "allow"
44-
test_branch "breaking-v2" "allow"
45-
test_branch "breaking-123" "allow"
46-
test_branch "feature-breaking-change" "allow" # 'breaking' anywhere in name
47-
test_branch "fix-breaking-bug" "allow" # 'breaking' anywhere in name
48-
test_branch "api-breaking-changes" "allow" # 'breaking' in middle
49-
test_branch "update-breaking-endpoint" "allow" # 'breaking' in middle
55+
test_branch "breaking-api-changes" "allow" "Branch with 'breaking' at start"
56+
test_branch "feature-breaking-change" "allow" "Branch with 'breaking' in middle"
57+
test_branch "fix-breaking-bug" "allow" "Branch with 'breaking' in middle"
58+
test_branch "api-breaking-changes" "allow" "Branch with 'breaking' in middle"
59+
test_branch "update-breaking-endpoint" "allow" "Branch with 'breaking' in middle"
5060

5161
echo "--- Branches that SHOULD fail on breaking changes ---"
52-
test_branch "feature-new-endpoint" "fail"
53-
test_branch "main" "fail"
54-
test_branch "master" "fail"
55-
test_branch "develop" "fail"
56-
test_branch "add-openapi-diff-tool" "fail"
57-
test_branch "fix-api-bug" "fail"
58-
test_branch "feature-v2" "fail"
62+
test_branch "feature-new-endpoint" "fail" "Normal feature branch"
63+
test_branch "main" "fail" "Main branch"
64+
test_branch "master" "fail" "Master branch"
65+
test_branch "develop" "fail" "Develop branch"
66+
test_branch "add-openapi-diff-tool" "fail" "Current branch name"
67+
test_branch "fix-api-bug" "fail" "Bug fix branch"
68+
test_branch "feature-v2" "fail" "Version feature branch"
69+
70+
echo "--- Test override with --fail-on-breaking ---"
71+
# Test that --fail-on-breaking overrides branch logic
72+
echo "Testing override: breaking branch with --fail-on-breaking"
73+
output=$(CURRENT_BRANCH="breaking-test" "$SCRIPT_PATH" --dry-run --fail-on-breaking 2>&1)
74+
if echo "$output" | grep -q "Mode: Failing on breaking changes"; then
75+
echo " ✓ PASS: --fail-on-breaking overrides branch logic"
76+
TESTS_PASSED=$((TESTS_PASSED + 1))
77+
else
78+
echo " ✗ FAIL: --fail-on-breaking did not override, output:"
79+
echo "$output"
80+
TESTS_FAILED=$((TESTS_FAILED + 1))
81+
fi
82+
echo
5983

6084
echo "========================================"
6185
echo "Test Results:"

0 commit comments

Comments
 (0)