|
| 1 | +#!/usr/bin/env bats |
| 2 | +# shellcheck shell=bash |
| 3 | + |
| 4 | +source "$BATS_TEST_DIRNAME/test_util.sh" |
| 5 | + |
| 6 | +setup_file() { |
| 7 | + test_util.setup_file |
| 8 | +} |
| 9 | + |
| 10 | +setup() { |
| 11 | + test_util.cd_test |
| 12 | + test_util.git_init |
| 13 | + |
| 14 | + # Source the utility file |
| 15 | + source "$BATS_TEST_DIRNAME/../helper/git-extra-utility" |
| 16 | +} |
| 17 | + |
| 18 | +# Tests for git_extra_mktemp |
| 19 | +@test "git_extra_mktemp: creates a temporary file" { |
| 20 | + local temp_file |
| 21 | + temp_file=$(git_extra_mktemp) |
| 22 | + |
| 23 | + # Check that file was created |
| 24 | + [[ -e "$temp_file" ]] |
| 25 | + |
| 26 | + # Cleanup |
| 27 | + rm -f "$temp_file" |
| 28 | +} |
| 29 | + |
| 30 | +@test "git_extra_mktemp: creates files with unique names" { |
| 31 | + local temp_file1 temp_file2 |
| 32 | + temp_file1=$(git_extra_mktemp) |
| 33 | + temp_file2=$(git_extra_mktemp) |
| 34 | + |
| 35 | + # Check that files are different |
| 36 | + [[ "$temp_file1" != "$temp_file2" ]] |
| 37 | + |
| 38 | + # Cleanup |
| 39 | + rm -f "$temp_file1" "$temp_file2" |
| 40 | +} |
| 41 | + |
| 42 | +@test "git_extra_mktemp: temp files are writable" { |
| 43 | + local temp_file |
| 44 | + temp_file=$(git_extra_mktemp) |
| 45 | + |
| 46 | + # Write to the file |
| 47 | + echo "test content" > "$temp_file" |
| 48 | + |
| 49 | + # Verify content |
| 50 | + [[ "$(cat "$temp_file")" == "test content" ]] |
| 51 | + |
| 52 | + # Cleanup |
| 53 | + rm -f "$temp_file" |
| 54 | +} |
| 55 | + |
| 56 | +# Tests for git_extra_default_branch |
| 57 | +@test "git_extra_default_branch: respects git-extras.default-branch config" { |
| 58 | + git config git-extras.default-branch "custom-branch" |
| 59 | + |
| 60 | + result=$(git_extra_default_branch) |
| 61 | + |
| 62 | + [[ "$result" == "custom-branch" ]] |
| 63 | +} |
| 64 | + |
| 65 | +@test "git_extra_default_branch: detects from remote HEAD when origin exists" { |
| 66 | + # Setup a remote with main branch (we're already on main from setup) |
| 67 | + echo "content" > file.txt |
| 68 | + git add file.txt |
| 69 | + git commit -m "initial commit" |
| 70 | + |
| 71 | + # Create a bare repo to act as remote |
| 72 | + local remote_dir="$BATS_TEST_TMPDIR/remote.git" |
| 73 | + git init --bare "$remote_dir" |
| 74 | + git remote add origin "$remote_dir" |
| 75 | + git push -u origin main |
| 76 | + git remote set-head origin main |
| 77 | + |
| 78 | + result=$(git_extra_default_branch) |
| 79 | + |
| 80 | + [[ "$result" == "main" ]] |
| 81 | +} |
| 82 | + |
| 83 | +@test "git_extra_default_branch: respects init.defaultBranch config when no remote" { |
| 84 | + git config init.defaultBranch "develop" |
| 85 | + |
| 86 | + result=$(git_extra_default_branch) |
| 87 | + |
| 88 | + [[ "$result" == "develop" ]] |
| 89 | +} |
| 90 | + |
| 91 | +@test "git_extra_default_branch: detects 'main' branch when it exists locally" { |
| 92 | + # We're already on main from setup |
| 93 | + result=$(git_extra_default_branch) |
| 94 | + |
| 95 | + [[ "$result" == "main" ]] |
| 96 | +} |
| 97 | + |
| 98 | +@test "git_extra_default_branch: detects 'master' branch when it exists locally" { |
| 99 | + # Create a new repo with master branch |
| 100 | + cd "$BATS_TEST_TMPDIR" |
| 101 | + rm -rf test_master |
| 102 | + mkdir test_master |
| 103 | + cd test_master |
| 104 | + git init --initial-branch master |
| 105 | + git config user.name 'Name' |
| 106 | + git config user.email 'name@example.com' |
| 107 | + # Need to create a commit so the branch actually exists |
| 108 | + echo "content" > file.txt |
| 109 | + git add file.txt |
| 110 | + git commit -m "initial commit" |
| 111 | + |
| 112 | + source "$BATS_TEST_DIRNAME/../helper/git-extra-utility" |
| 113 | + result=$(git_extra_default_branch) |
| 114 | + |
| 115 | + [[ "$result" == "master" ]] |
| 116 | +} |
| 117 | + |
| 118 | +@test "git_extra_default_branch: prefers 'main' over 'master' when both exist" { |
| 119 | + # First commit to establish main branch |
| 120 | + echo "content" > file.txt |
| 121 | + git add file.txt |
| 122 | + git commit -m "initial commit" |
| 123 | + |
| 124 | + # Create master branch |
| 125 | + git checkout -b master |
| 126 | + echo "content2" > file2.txt |
| 127 | + git add file2.txt |
| 128 | + git commit -m "master commit" |
| 129 | + |
| 130 | + # Switch back to main |
| 131 | + git checkout main |
| 132 | + |
| 133 | + result=$(git_extra_default_branch) |
| 134 | + |
| 135 | + [[ "$result" == "main" ]] |
| 136 | +} |
| 137 | + |
| 138 | +@test "git_extra_default_branch: detects 'trunk' branch" { |
| 139 | + # Create a new repo with trunk branch |
| 140 | + cd "$BATS_TEST_TMPDIR" |
| 141 | + rm -rf test_trunk |
| 142 | + mkdir test_trunk |
| 143 | + cd test_trunk |
| 144 | + git init --initial-branch trunk |
| 145 | + git config user.name 'Name' |
| 146 | + git config user.email 'name@example.com' |
| 147 | + # Need to create a commit so the branch actually exists |
| 148 | + echo "content" > file.txt |
| 149 | + git add file.txt |
| 150 | + git commit -m "initial commit" |
| 151 | + |
| 152 | + source "$BATS_TEST_DIRNAME/../helper/git-extra-utility" |
| 153 | + result=$(git_extra_default_branch) |
| 154 | + |
| 155 | + [[ "$result" == "trunk" ]] |
| 156 | +} |
| 157 | + |
| 158 | +@test "git_extra_default_branch: detects 'development' branch" { |
| 159 | + # Create a new repo with development branch |
| 160 | + cd "$BATS_TEST_TMPDIR" |
| 161 | + rm -rf test_dev |
| 162 | + mkdir test_dev |
| 163 | + cd test_dev |
| 164 | + git init --initial-branch development |
| 165 | + git config user.name 'Name' |
| 166 | + git config user.email 'name@example.com' |
| 167 | + # Need to create a commit so the branch actually exists |
| 168 | + echo "content" > file.txt |
| 169 | + git add file.txt |
| 170 | + git commit -m "initial commit" |
| 171 | + |
| 172 | + source "$BATS_TEST_DIRNAME/../helper/git-extra-utility" |
| 173 | + result=$(git_extra_default_branch) |
| 174 | + |
| 175 | + [[ "$result" == "development" ]] |
| 176 | +} |
| 177 | + |
| 178 | +@test "git_extra_default_branch: falls back to 'main' when no branches exist" { |
| 179 | + # Create a completely empty repo |
| 180 | + cd "$BATS_TEST_TMPDIR" |
| 181 | + rm -rf test_empty |
| 182 | + mkdir test_empty |
| 183 | + cd test_empty |
| 184 | + git init |
| 185 | + git config user.name 'Name' |
| 186 | + git config user.email 'name@example.com' |
| 187 | + |
| 188 | + source "$BATS_TEST_DIRNAME/../helper/git-extra-utility" |
| 189 | + result=$(git_extra_default_branch) |
| 190 | + |
| 191 | + [[ "$result" == "main" ]] |
| 192 | +} |
| 193 | + |
| 194 | +@test "git_extra_default_branch: git-extras.default-branch takes precedence over remote HEAD" { |
| 195 | + # Setup a remote with main branch (we're already on main from setup) |
| 196 | + echo "content" > file.txt |
| 197 | + git add file.txt |
| 198 | + git commit -m "initial commit" |
| 199 | + |
| 200 | + local remote_dir="$BATS_TEST_TMPDIR/remote.git" |
| 201 | + git init --bare "$remote_dir" |
| 202 | + git remote add origin "$remote_dir" |
| 203 | + git push -u origin main |
| 204 | + git remote set-head origin main |
| 205 | + |
| 206 | + # Set a different default in config |
| 207 | + git config git-extras.default-branch "custom" |
| 208 | + |
| 209 | + result=$(git_extra_default_branch) |
| 210 | + |
| 211 | + # Should use config value, not remote HEAD |
| 212 | + [[ "$result" == "custom" ]] |
| 213 | +} |
| 214 | + |
| 215 | +@test "git_extra_default_branch: git-extras.default-branch takes precedence over init.defaultBranch" { |
| 216 | + git config init.defaultBranch "develop" |
| 217 | + git config git-extras.default-branch "custom" |
| 218 | + |
| 219 | + result=$(git_extra_default_branch) |
| 220 | + |
| 221 | + [[ "$result" == "custom" ]] |
| 222 | +} |
| 223 | + |
| 224 | +@test "git_extra_default_branch: remote HEAD takes precedence over init.defaultBranch" { |
| 225 | + # Setup a remote with main branch |
| 226 | + echo "content" > file.txt |
| 227 | + git add file.txt |
| 228 | + git commit -m "initial commit" |
| 229 | + |
| 230 | + local remote_dir="$BATS_TEST_TMPDIR/remote.git" |
| 231 | + git init --bare "$remote_dir" |
| 232 | + git remote add origin "$remote_dir" |
| 233 | + git push -u origin main |
| 234 | + git remote set-head origin main |
| 235 | + |
| 236 | + # Set a different default in init config |
| 237 | + git config init.defaultBranch "develop" |
| 238 | + |
| 239 | + result=$(git_extra_default_branch) |
| 240 | + |
| 241 | + # Should use remote HEAD, not init.defaultBranch |
| 242 | + [[ "$result" == "main" ]] |
| 243 | +} |
0 commit comments