|
1 | 1 | #!/bin/bash |
2 | 2 |
|
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 |
5 | 5 |
|
6 | 6 | set -e |
7 | 7 |
|
8 | | -echo "=== Unit Test: Branch Logic Pattern Matching ===" |
| 8 | +echo "=== Unit Test: api-diff.sh Branch Logic ===" |
9 | 9 | echo |
10 | 10 |
|
11 | 11 | TESTS_PASSED=0 |
12 | 12 | TESTS_FAILED=0 |
13 | 13 |
|
14 | | -# Helper function to test pattern |
| 14 | +SCRIPT_PATH="scripts/api-diff/api-diff.sh" |
| 15 | + |
| 16 | +# Helper function to test script with branch |
15 | 17 | test_branch() { |
16 | 18 | 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" |
19 | 21 |
|
20 | | - echo "Testing: $branch_name" |
| 22 | + echo "Testing: $test_name (branch: $branch_name)" |
21 | 23 |
|
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) |
27 | 27 |
|
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 |
34 | 47 | fi |
35 | 48 | echo |
36 | 49 | } |
37 | 50 |
|
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 |
40 | 53 |
|
41 | 54 | 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" |
50 | 60 |
|
51 | 61 | 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 |
59 | 83 |
|
60 | 84 | echo "========================================" |
61 | 85 | echo "Test Results:" |
|
0 commit comments