|
| 1 | +#!/bin/bash |
| 2 | +# Test script for update-changelog.sh |
| 3 | + |
| 4 | +set -e |
| 5 | + |
| 6 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 7 | +UPDATE_SCRIPT="${SCRIPT_DIR}/update-changelog.sh" |
| 8 | +TEST_DIR="/tmp/changelog-test-$$" |
| 9 | + |
| 10 | +# Colors for output |
| 11 | +GREEN='\033[0;32m' |
| 12 | +RED='\033[0;31m' |
| 13 | +NC='\033[0m' # No Color |
| 14 | + |
| 15 | +passed=0 |
| 16 | +failed=0 |
| 17 | + |
| 18 | +# Setup test directory |
| 19 | +mkdir -p "$TEST_DIR" |
| 20 | + |
| 21 | +# Cleanup on exit |
| 22 | +cleanup() { |
| 23 | + rm -rf "$TEST_DIR" |
| 24 | +} |
| 25 | +trap cleanup EXIT |
| 26 | + |
| 27 | +# Helper function to run a test |
| 28 | +run_test() { |
| 29 | + local test_name="$1" |
| 30 | + local test_func="$2" |
| 31 | + |
| 32 | + echo -n "Testing: $test_name ... " |
| 33 | + |
| 34 | + if $test_func; then |
| 35 | + echo -e "${GREEN}PASSED${NC}" |
| 36 | + ((passed++)) |
| 37 | + else |
| 38 | + echo -e "${RED}FAILED${NC}" |
| 39 | + ((failed++)) |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +# Test 1: Basic functionality - Replace Unreleased with version |
| 44 | +test_basic_replace() { |
| 45 | + local test_file="${TEST_DIR}/test1.md" |
| 46 | + cat > "$test_file" << 'EOF' |
| 47 | +# Changelog |
| 48 | +
|
| 49 | +## [Unreleased] |
| 50 | +
|
| 51 | +### Added |
| 52 | +- New feature |
| 53 | +
|
| 54 | +## [1.0.0] - 2026-01-01 |
| 55 | +
|
| 56 | +### Added |
| 57 | +- Initial release |
| 58 | +
|
| 59 | +[1.0.0]: https://github.com/test/repo/releases/tag/1.0.0 |
| 60 | +EOF |
| 61 | + |
| 62 | + # Run the script |
| 63 | + CHANGELOG_FILE="$test_file" bash "$UPDATE_SCRIPT" 1.0.1 > /dev/null 2>&1 |
| 64 | + |
| 65 | + # Verify the changes |
| 66 | + if grep -q "## \[Unreleased\]" "$test_file" && \ |
| 67 | + grep -q "## \[1.0.1\] - $(date +%Y-%m-%d)" "$test_file" && \ |
| 68 | + grep -q "\[Unreleased\]: https://github.com/test/repo/compare/v1.0.1...HEAD" "$test_file" && \ |
| 69 | + grep -q "\[1.0.1\]: https://github.com/test/repo/compare/v1.0.0...v1.0.1" "$test_file"; then |
| 70 | + return 0 |
| 71 | + else |
| 72 | + return 1 |
| 73 | + fi |
| 74 | +} |
| 75 | + |
| 76 | +# Test 2: Handle CHANGELOG without Unreleased link |
| 77 | +test_no_unreleased_link() { |
| 78 | + local test_file="${TEST_DIR}/test2.md" |
| 79 | + cat > "$test_file" << 'EOF' |
| 80 | +# Changelog |
| 81 | +
|
| 82 | +## [Unreleased] |
| 83 | +
|
| 84 | +### Added |
| 85 | +- New feature |
| 86 | +
|
| 87 | +## [1.0.0] - 2026-01-01 |
| 88 | +
|
| 89 | +[1.0.0]: https://github.com/test/repo/releases/tag/1.0.0 |
| 90 | +EOF |
| 91 | + |
| 92 | + CHANGELOG_FILE="$test_file" bash "$UPDATE_SCRIPT" 1.0.1 > /dev/null 2>&1 |
| 93 | + |
| 94 | + # Should add both Unreleased and version links |
| 95 | + if grep -q "\[Unreleased\]: https://github.com/test/repo/compare/v1.0.1...HEAD" "$test_file" && \ |
| 96 | + grep -q "\[1.0.1\]: https://github.com/test/repo/compare/v1.0.0...v1.0.1" "$test_file"; then |
| 97 | + return 0 |
| 98 | + else |
| 99 | + return 1 |
| 100 | + fi |
| 101 | +} |
| 102 | + |
| 103 | +# Test 3: Preserve content structure |
| 104 | +test_preserve_content() { |
| 105 | + local test_file="${TEST_DIR}/test3.md" |
| 106 | + cat > "$test_file" << 'EOF' |
| 107 | +# Changelog |
| 108 | +
|
| 109 | +## [Unreleased] |
| 110 | +
|
| 111 | +### Added |
| 112 | +- Feature A |
| 113 | +- Feature B |
| 114 | +
|
| 115 | +### Fixed |
| 116 | +- Bug fix |
| 117 | +
|
| 118 | +## [1.0.0] - 2026-01-01 |
| 119 | +
|
| 120 | +[1.0.0]: https://github.com/test/repo/releases/tag/1.0.0 |
| 121 | +EOF |
| 122 | + |
| 123 | + CHANGELOG_FILE="$test_file" bash "$UPDATE_SCRIPT" 1.0.1 > /dev/null 2>&1 |
| 124 | + |
| 125 | + # Verify content is preserved under the new version |
| 126 | + if grep -A 6 "## \[1.0.1\]" "$test_file" | grep -q "Feature A" && \ |
| 127 | + grep -A 6 "## \[1.0.1\]" "$test_file" | grep -q "Bug fix"; then |
| 128 | + return 0 |
| 129 | + else |
| 130 | + return 1 |
| 131 | + fi |
| 132 | +} |
| 133 | + |
| 134 | +# Test 4: Error handling - no Unreleased section |
| 135 | +test_no_unreleased_section() { |
| 136 | + local test_file="${TEST_DIR}/test4.md" |
| 137 | + cat > "$test_file" << 'EOF' |
| 138 | +# Changelog |
| 139 | +
|
| 140 | +## [1.0.0] - 2026-01-01 |
| 141 | +
|
| 142 | +[1.0.0]: https://github.com/test/repo/releases/tag/1.0.0 |
| 143 | +EOF |
| 144 | + |
| 145 | + # Should fail because there's no Unreleased section |
| 146 | + if ! CHANGELOG_FILE="$test_file" bash "$UPDATE_SCRIPT" 1.0.1 > /dev/null 2>&1; then |
| 147 | + return 0 |
| 148 | + else |
| 149 | + return 1 |
| 150 | + fi |
| 151 | +} |
| 152 | + |
| 153 | +# Test 5: Multiple version handling |
| 154 | +test_multiple_versions() { |
| 155 | + local test_file="${TEST_DIR}/test5.md" |
| 156 | + cat > "$test_file" << 'EOF' |
| 157 | +# Changelog |
| 158 | +
|
| 159 | +## [Unreleased] |
| 160 | +
|
| 161 | +### Added |
| 162 | +- New feature |
| 163 | +
|
| 164 | +## [1.0.1] - 2026-02-01 |
| 165 | +
|
| 166 | +## [1.0.0] - 2026-01-01 |
| 167 | +
|
| 168 | +[1.0.1]: https://github.com/test/repo/compare/v1.0.0...v1.0.1 |
| 169 | +[1.0.0]: https://github.com/test/repo/releases/tag/1.0.0 |
| 170 | +EOF |
| 171 | + |
| 172 | + CHANGELOG_FILE="$test_file" bash "$UPDATE_SCRIPT" 1.0.2 > /dev/null 2>&1 |
| 173 | + |
| 174 | + # Verify the new version is added and links are updated |
| 175 | + if grep -q "## \[1.0.2\] - $(date +%Y-%m-%d)" "$test_file" && \ |
| 176 | + grep -q "\[1.0.2\]: https://github.com/test/repo/compare/v1.0.1...v1.0.2" "$test_file"; then |
| 177 | + return 0 |
| 178 | + else |
| 179 | + return 1 |
| 180 | + fi |
| 181 | +} |
| 182 | + |
| 183 | +# Run all tests |
| 184 | +echo "Running CHANGELOG update script tests..." |
| 185 | +echo "" |
| 186 | + |
| 187 | +run_test "Basic functionality - Replace Unreleased with version" test_basic_replace |
| 188 | +run_test "Handle CHANGELOG without Unreleased link" test_no_unreleased_link |
| 189 | +run_test "Preserve content structure" test_preserve_content |
| 190 | +run_test "Error handling - no Unreleased section" test_no_unreleased_section |
| 191 | +run_test "Multiple version handling" test_multiple_versions |
| 192 | + |
| 193 | +echo "" |
| 194 | +echo "==========================================" |
| 195 | +echo -e "Tests passed: ${GREEN}${passed}${NC}" |
| 196 | +echo -e "Tests failed: ${RED}${failed}${NC}" |
| 197 | +echo "==========================================" |
| 198 | + |
| 199 | +if [ $failed -eq 0 ]; then |
| 200 | + exit 0 |
| 201 | +else |
| 202 | + exit 1 |
| 203 | +fi |
0 commit comments