|
| 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 modified files |
| 13 | +setup_test_repo() { |
| 14 | + local repo="$1" |
| 15 | + mkdir -p "$repo" |
| 16 | + cd "$repo" |
| 17 | + |
| 18 | + git init |
| 19 | + git config user.name "Test" |
| 20 | + git config user.email "test@test.com" |
| 21 | + |
| 22 | + # Create initial commit |
| 23 | + echo "# Changelog" > CHANGELOG.md |
| 24 | + git add CHANGELOG.md |
| 25 | + git commit -m "Initial commit" |
| 26 | + |
| 27 | + # Modify multiple files |
| 28 | + echo "## [Unreleased]" >> CHANGELOG.md |
| 29 | + echo "content" > file1.txt |
| 30 | + echo "content" > file2.txt |
| 31 | + echo "secret" > credentials.json |
| 32 | +} |
| 33 | + |
| 34 | +# Helper: check if a file is in the last commit |
| 35 | +file_in_commit() { |
| 36 | + local file="$1" |
| 37 | + git diff --name-only HEAD~1 HEAD | grep --quiet --fixed-strings --line-regexp "$file" |
| 38 | +} |
| 39 | + |
| 40 | +test_commits_only_specified_files() { |
| 41 | + local repo="$TMPDIR_TEST/test1" |
| 42 | + setup_test_repo "$repo" |
| 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 | + # Should commit CHANGELOG.md and file1.txt |
| 50 | + file_in_commit "CHANGELOG.md" && file_in_commit "file1.txt" |
| 51 | + |
| 52 | + # Should NOT commit file2.txt or credentials.json |
| 53 | + ! file_in_commit "file2.txt" && ! file_in_commit "credentials.json" |
| 54 | +} |
| 55 | +expect_success "commits only specified files plus CHANGELOG.md" test_commits_only_specified_files |
| 56 | + |
| 57 | +test_multiple_files_comma_separated() { |
| 58 | + local repo="$TMPDIR_TEST/test2" |
| 59 | + setup_test_repo "$repo" |
| 60 | + |
| 61 | + export FILES_TO_COMMIT="file1.txt,file2.txt" |
| 62 | + export UNRELEASED_CHANGES="- Added feature" |
| 63 | + export VERSION="1.0.0" |
| 64 | + bash "$SCRIPT_DIR/commit_release_changes.sh" |
| 65 | + |
| 66 | + # Should commit all three files |
| 67 | + file_in_commit "CHANGELOG.md" && file_in_commit "file1.txt" && file_in_commit "file2.txt" |
| 68 | + |
| 69 | + # Should NOT commit credentials.json |
| 70 | + ! file_in_commit "credentials.json" |
| 71 | +} |
| 72 | +expect_success "commits multiple comma-separated files" test_multiple_files_comma_separated |
| 73 | + |
| 74 | +test_handles_whitespace_in_list() { |
| 75 | + local repo="$TMPDIR_TEST/test3" |
| 76 | + setup_test_repo "$repo" |
| 77 | + |
| 78 | + export FILES_TO_COMMIT="file1.txt , file2.txt" |
| 79 | + export UNRELEASED_CHANGES="- Fixed bug" |
| 80 | + export VERSION="1.0.0" |
| 81 | + bash "$SCRIPT_DIR/commit_release_changes.sh" |
| 82 | + |
| 83 | + # Should handle spaces and commit both files |
| 84 | + file_in_commit "file1.txt" && file_in_commit "file2.txt" |
| 85 | +} |
| 86 | +expect_success "handles whitespace in comma-separated list" test_handles_whitespace_in_list |
| 87 | + |
| 88 | +test_empty_files_list_commits_only_changelog() { |
| 89 | + local repo="$TMPDIR_TEST/test4" |
| 90 | + setup_test_repo "$repo" |
| 91 | + |
| 92 | + export FILES_TO_COMMIT="" |
| 93 | + export UNRELEASED_CHANGES="- Updated docs" |
| 94 | + export VERSION="1.0.0" |
| 95 | + bash "$SCRIPT_DIR/commit_release_changes.sh" |
| 96 | + |
| 97 | + # Should commit only CHANGELOG.md |
| 98 | + file_in_commit "CHANGELOG.md" |
| 99 | + |
| 100 | + # Should NOT commit any other files |
| 101 | + ! file_in_commit "file1.txt" && ! file_in_commit "file2.txt" && ! file_in_commit "credentials.json" |
| 102 | +} |
| 103 | +expect_success "empty files list commits only CHANGELOG.md" test_empty_files_list_commits_only_changelog |
| 104 | + |
| 105 | +test_commit_message_includes_version() { |
| 106 | + local repo="$TMPDIR_TEST/test5" |
| 107 | + setup_test_repo "$repo" |
| 108 | + |
| 109 | + export FILES_TO_COMMIT="file1.txt" |
| 110 | + export UNRELEASED_CHANGES="- New feature" |
| 111 | + export VERSION="2.5.3" |
| 112 | + bash "$SCRIPT_DIR/commit_release_changes.sh" |
| 113 | + |
| 114 | + git log -1 --pretty=%B | check_contains "Prepare release v2.5.3" |
| 115 | +} |
| 116 | +expect_success "commit message includes version" test_commit_message_includes_version |
| 117 | + |
| 118 | +test_commit_body_includes_unreleased_changes() { |
| 119 | + local repo="$TMPDIR_TEST/test6" |
| 120 | + setup_test_repo "$repo" |
| 121 | + |
| 122 | + export FILES_TO_COMMIT="" |
| 123 | + export UNRELEASED_CHANGES="- Added feature A |
| 124 | +- Fixed bug B" |
| 125 | + export VERSION="1.0.0" |
| 126 | + bash "$SCRIPT_DIR/commit_release_changes.sh" |
| 127 | + |
| 128 | + git log -1 --pretty=%B | check_contains "Added feature A" |
| 129 | + git log -1 --pretty=%B | check_contains "Fixed bug B" |
| 130 | +} |
| 131 | +expect_success "commit body includes unreleased changes" test_commit_body_includes_unreleased_changes |
| 132 | + |
| 133 | +test_does_not_commit_untracked_files_not_in_list() { |
| 134 | + local repo="$TMPDIR_TEST/test7" |
| 135 | + setup_test_repo "$repo" |
| 136 | + |
| 137 | + # Create untracked file that's not in the list |
| 138 | + echo "secret-key-123" > .env |
| 139 | + |
| 140 | + export FILES_TO_COMMIT="file1.txt" |
| 141 | + export UNRELEASED_CHANGES="- Security update" |
| 142 | + export VERSION="1.0.0" |
| 143 | + bash "$SCRIPT_DIR/commit_release_changes.sh" |
| 144 | + |
| 145 | + # Should NOT commit .env file |
| 146 | + ! file_in_commit ".env" |
| 147 | +} |
| 148 | +expect_success "does not commit untracked files not in list (security test)" test_does_not_commit_untracked_files_not_in_list |
| 149 | + |
| 150 | +test_warns_and_skips_missing_files() { |
| 151 | + local repo="$TMPDIR_TEST/test8" |
| 152 | + setup_test_repo "$repo" |
| 153 | + |
| 154 | + # Modify file1.txt but don't create missing_file.txt |
| 155 | + echo "content" > file1.txt |
| 156 | + |
| 157 | + export FILES_TO_COMMIT="file1.txt,missing_file.txt" |
| 158 | + export UNRELEASED_CHANGES="- New feature" |
| 159 | + |
| 160 | + # Capture output to check for warning |
| 161 | + local output |
| 162 | + export VERSION="1.0.0" |
| 163 | + output=$(bash "$SCRIPT_DIR/commit_release_changes.sh" 2>&1) |
| 164 | + |
| 165 | + # Should commit file1.txt but not missing_file.txt |
| 166 | + file_in_commit "file1.txt" |
| 167 | + ! file_in_commit "missing_file.txt" |
| 168 | + |
| 169 | + # Should log warning about missing file |
| 170 | + echo "$output" | check_contains "File not found" |
| 171 | + echo "$output" | check_contains "missing_file.txt" |
| 172 | +} |
| 173 | +expect_success "warns and skips missing files" test_warns_and_skips_missing_files |
| 174 | + |
| 175 | +print_results |
0 commit comments