|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Tests for commit-release-changes.sh |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +cd "$(dirname "${BASH_SOURCE[0]}")" |
| 6 | +source ../../test_helpers.sh |
| 7 | + |
| 8 | +SCRIPT_DIR="$(pwd)" |
| 9 | +TMPDIR_TEST=$(mktemp -d) |
| 10 | +trap 'rm -rf "$TMPDIR_TEST"' EXIT |
| 11 | + |
| 12 | +# Helper: set up a git repo with an initial commit and modified CHANGELOG.md |
| 13 | +# Creates optional additional untracked files for testing |
| 14 | +# Usage: setup_repo "$repo_path" [file1 file2 ...] |
| 15 | +setup_repo() { |
| 16 | + local repo="$1" |
| 17 | + shift # Remove repo from arguments, rest are files to create |
| 18 | + |
| 19 | + mkdir -p "$repo" |
| 20 | + cd "$repo" |
| 21 | + |
| 22 | + git init |
| 23 | + git config user.name "Test" |
| 24 | + git config user.email "test@test.com" |
| 25 | + |
| 26 | + # Create initial commit |
| 27 | + echo "# Changelog" > CHANGELOG.md |
| 28 | + git add CHANGELOG.md |
| 29 | + git commit -m "Initial commit" |
| 30 | + |
| 31 | + # Modify CHANGELOG |
| 32 | + echo "## [Unreleased]" >> CHANGELOG.md |
| 33 | + |
| 34 | + # Create any additional files passed as arguments |
| 35 | + for file in "$@"; do |
| 36 | + echo "content" > "$file" |
| 37 | + done |
| 38 | +} |
| 39 | + |
| 40 | +test_commits_specified_file() { |
| 41 | + local repo="$TMPDIR_TEST/test1" |
| 42 | + setup_repo "$repo" file1.txt |
| 43 | + |
| 44 | + export FILES_TO_COMMIT="file1.txt" |
| 45 | + export UNRELEASED_CHANGES="- Added new feature" |
| 46 | + export VERSION="1.0.0" |
| 47 | + bash "$SCRIPT_DIR/commit_release_changes.sh" |
| 48 | + |
| 49 | + expect_files_in_commit "CHANGELOG.md" "file1.txt" |
| 50 | +} |
| 51 | +expect_success "commits specified file plus CHANGELOG.md" test_commits_specified_file |
| 52 | + |
| 53 | +test_multiple_files_newline_separated() { |
| 54 | + local repo="$TMPDIR_TEST/test2" |
| 55 | + setup_repo "$repo" file1.txt file2.txt |
| 56 | + |
| 57 | + export FILES_TO_COMMIT="file1.txt |
| 58 | +file2.txt" |
| 59 | + export UNRELEASED_CHANGES="- Added feature" |
| 60 | + export VERSION="1.0.0" |
| 61 | + bash "$SCRIPT_DIR/commit_release_changes.sh" |
| 62 | + |
| 63 | + expect_files_in_commit "CHANGELOG.md" "file1.txt" "file2.txt" |
| 64 | +} |
| 65 | +expect_success "commits multiple newline-separated files" test_multiple_files_newline_separated |
| 66 | + |
| 67 | +test_handles_whitespace_in_list() { |
| 68 | + local repo="$TMPDIR_TEST/test3" |
| 69 | + setup_repo "$repo" file1.txt file2.txt credentials.json |
| 70 | + |
| 71 | + export FILES_TO_COMMIT=" file1.txt |
| 72 | + file2.txt |
| 73 | + credentials.json " |
| 74 | + export UNRELEASED_CHANGES="- Fixed bug" |
| 75 | + export VERSION="1.0.0" |
| 76 | + bash "$SCRIPT_DIR/commit_release_changes.sh" |
| 77 | + |
| 78 | + expect_files_in_commit "CHANGELOG.md" "file1.txt" "file2.txt" "credentials.json" |
| 79 | +} |
| 80 | +expect_success "handles whitespace in newline-separated list" test_handles_whitespace_in_list |
| 81 | + |
| 82 | +test_empty_files_list_commits_only_changelog() { |
| 83 | + local repo="$TMPDIR_TEST/test4" |
| 84 | + setup_repo "$repo" |
| 85 | + |
| 86 | + export FILES_TO_COMMIT="" |
| 87 | + export UNRELEASED_CHANGES="- Updated docs" |
| 88 | + export VERSION="1.0.0" |
| 89 | + bash "$SCRIPT_DIR/commit_release_changes.sh" |
| 90 | + |
| 91 | + expect_files_in_commit "CHANGELOG.md" |
| 92 | +} |
| 93 | +expect_success "empty files list commits only CHANGELOG.md" test_empty_files_list_commits_only_changelog |
| 94 | + |
| 95 | +test_commit_message_includes_version() { |
| 96 | + local repo="$TMPDIR_TEST/test5" |
| 97 | + setup_repo "$repo" file1.txt file2.txt credentials.json |
| 98 | + |
| 99 | + export FILES_TO_COMMIT="file1.txt |
| 100 | +file2.txt |
| 101 | +credentials.json" |
| 102 | + export UNRELEASED_CHANGES="- New feature" |
| 103 | + export VERSION="2.5.3" |
| 104 | + bash "$SCRIPT_DIR/commit_release_changes.sh" |
| 105 | + |
| 106 | + git log --max-count=1 --pretty=%B | check_contains "Prepare release v2.5.3" |
| 107 | +} |
| 108 | +expect_success "commit message includes version" test_commit_message_includes_version |
| 109 | + |
| 110 | +test_commit_body_includes_unreleased_changes() { |
| 111 | + local repo="$TMPDIR_TEST/test6" |
| 112 | + setup_repo "$repo" |
| 113 | + |
| 114 | + export FILES_TO_COMMIT="" |
| 115 | + export UNRELEASED_CHANGES="- Added feature A |
| 116 | +- Fixed bug B" |
| 117 | + export VERSION="1.0.0" |
| 118 | + bash "$SCRIPT_DIR/commit_release_changes.sh" |
| 119 | + |
| 120 | + git log --max-count=1 --pretty=%B | check_contains "Added feature A" |
| 121 | + git log --max-count=1 --pretty=%B | check_contains "Fixed bug B" |
| 122 | +} |
| 123 | +expect_success "commit body includes unreleased changes" test_commit_body_includes_unreleased_changes |
| 124 | + |
| 125 | +test_fails_when_leftover_untracked_files_exist() { |
| 126 | + local repo="$TMPDIR_TEST/test7" |
| 127 | + setup_repo "$repo" file1.txt .env |
| 128 | + |
| 129 | + export FILES_TO_COMMIT="file1.txt" |
| 130 | + export UNRELEASED_CHANGES="- Security update" |
| 131 | + export VERSION="1.0.0" |
| 132 | + bash "$SCRIPT_DIR/commit_release_changes.sh" 2>&1 |
| 133 | +} |
| 134 | +expect_failure_output "fails when leftover untracked files exist" "Leftover files detected" test_fails_when_leftover_untracked_files_exist |
| 135 | + |
| 136 | +test_warns_and_skips_missing_files() { |
| 137 | + local repo="$TMPDIR_TEST/test8" |
| 138 | + setup_repo "$repo" file1.txt file2.txt credentials.json |
| 139 | + |
| 140 | + export FILES_TO_COMMIT="file1.txt |
| 141 | +file2.txt |
| 142 | +credentials.json |
| 143 | +missing_file.txt" |
| 144 | + export UNRELEASED_CHANGES="- New feature" |
| 145 | + |
| 146 | + # Capture output to check for warning |
| 147 | + local output |
| 148 | + export VERSION="1.0.0" |
| 149 | + output=$(bash "$SCRIPT_DIR/commit_release_changes.sh" 2>&1) |
| 150 | + |
| 151 | + expect_files_in_commit "file1.txt" "file2.txt" "credentials.json" |
| 152 | + expect_files_not_in_commit "missing_file.txt" |
| 153 | + |
| 154 | + # Should log warning about missing file |
| 155 | + echo "$output" | check_contains "File not found" |
| 156 | + echo "$output" | check_contains "missing_file.txt" |
| 157 | +} |
| 158 | +expect_success "warns and skips missing files" test_warns_and_skips_missing_files |
| 159 | + |
| 160 | +test_fails_when_leftover_unstaged_changes_exist() { |
| 161 | + local repo="$TMPDIR_TEST/test9" |
| 162 | + setup_repo "$repo" |
| 163 | + |
| 164 | + # Create and commit a file, then modify it without staging |
| 165 | + echo "original" > file1.txt |
| 166 | + git add file1.txt |
| 167 | + git commit -m "Add file1.txt" |
| 168 | + |
| 169 | + # Modify CHANGELOG again for the release |
| 170 | + echo "## [Unreleased]" >> CHANGELOG.md |
| 171 | + |
| 172 | + # Now modify file1.txt but don't include it in FILES_TO_COMMIT |
| 173 | + echo "modified" > file1.txt |
| 174 | + |
| 175 | + export FILES_TO_COMMIT="" |
| 176 | + export UNRELEASED_CHANGES="- New release" |
| 177 | + export VERSION="1.0.0" |
| 178 | + bash "$SCRIPT_DIR/commit_release_changes.sh" 2>&1 |
| 179 | +} |
| 180 | +expect_failure_output "fails when leftover unstaged changes exist" "Leftover files detected" test_fails_when_leftover_unstaged_changes_exist |
| 181 | + |
| 182 | +# Return to original directory before cleanup |
| 183 | +cd "$SCRIPT_DIR" |
| 184 | + |
| 185 | +print_results |
0 commit comments