|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +# Tests for utils/git-utils.sh |
| 8 | +# |
| 9 | +# These tests verify get_default_branch works correctly across different |
| 10 | +# repository configurations: |
| 11 | +# - Cloned repos (with origin/HEAD) |
| 12 | +# - Local repos pushed to remote (origin/main exists but no origin/HEAD) |
| 13 | +# - Pure local repos (no remote) |
| 14 | +# - Repos with master instead of main |
| 15 | + |
| 16 | +bats_require_minimum_version 1.13.0 |
| 17 | + |
| 18 | +load "${BATS_TEST_DIRNAME}/libs/bats-support/load.bash" |
| 19 | +load "${BATS_TEST_DIRNAME}/libs/bats-assert/load.bash" |
| 20 | +load "${BATS_TEST_DIRNAME}/libs/bats-file/load.bash" |
| 21 | +load "${BATS_TEST_DIRNAME}/test_helper.bash" |
| 22 | + |
| 23 | +setup() { |
| 24 | + common_setup |
| 25 | + export DEVTOOLS_ROOT="${BATS_TEST_DIRNAME}/.." |
| 26 | + source "${DEVTOOLS_ROOT}/utils/git-utils.sh" |
| 27 | + cd "$TEST_DIR" |
| 28 | +} |
| 29 | + |
| 30 | +teardown() { |
| 31 | + common_teardown |
| 32 | +} |
| 33 | + |
| 34 | +# ============================================================================= |
| 35 | +# Helper: Create git repo with specific configuration |
| 36 | +# ============================================================================= |
| 37 | + |
| 38 | +# Create a bare remote repo to simulate origin |
| 39 | +create_bare_remote() { |
| 40 | + local remote_path="${TEST_DIR}/remote.git" |
| 41 | + git init -q --bare "$remote_path" |
| 42 | + echo "$remote_path" |
| 43 | +} |
| 44 | + |
| 45 | +# ============================================================================= |
| 46 | +# get_default_branch tests |
| 47 | +# ============================================================================= |
| 48 | + |
| 49 | +@test "get_default_branch: cloned repo with origin/HEAD returns correct branch" { |
| 50 | + # Simulate a cloned repo by setting up origin/HEAD symbolic ref |
| 51 | + init_git_repo |
| 52 | + local remote_path |
| 53 | + remote_path=$(create_bare_remote) |
| 54 | + git remote add origin "$remote_path" |
| 55 | + git push -u origin main 2>/dev/null |
| 56 | + # Manually set origin/HEAD like git clone does |
| 57 | + git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main |
| 58 | + |
| 59 | + run get_default_branch |
| 60 | + |
| 61 | + assert_success |
| 62 | + assert_output "main" |
| 63 | +} |
| 64 | + |
| 65 | +@test "get_default_branch: local repo pushed to remote (no origin/HEAD) returns main" { |
| 66 | + # This is the failing case: git init + git remote add + git push |
| 67 | + # origin/main exists but origin/HEAD does not |
| 68 | + init_git_repo |
| 69 | + local remote_path |
| 70 | + remote_path=$(create_bare_remote) |
| 71 | + git remote add origin "$remote_path" |
| 72 | + git push -u origin main 2>/dev/null |
| 73 | + # Do NOT set origin/HEAD - this simulates the real scenario |
| 74 | + |
| 75 | + run get_default_branch |
| 76 | + |
| 77 | + assert_success |
| 78 | + assert_output "main" |
| 79 | +} |
| 80 | + |
| 81 | +@test "get_default_branch: local repo with master pushed to remote returns master" { |
| 82 | + # Same as above but with master branch |
| 83 | + export GIT_CONFIG_NOSYSTEM=1 |
| 84 | + git init -q --initial-branch=master |
| 85 | + git config user.email "test@example.com" |
| 86 | + git config user.name "Test User" |
| 87 | + echo "initial" > file.txt |
| 88 | + git add file.txt |
| 89 | + git commit -q -m "Initial commit" |
| 90 | + |
| 91 | + local remote_path |
| 92 | + remote_path=$(create_bare_remote) |
| 93 | + git remote add origin "$remote_path" |
| 94 | + git push -u origin master 2>/dev/null |
| 95 | + |
| 96 | + run get_default_branch |
| 97 | + |
| 98 | + assert_success |
| 99 | + assert_output "master" |
| 100 | +} |
| 101 | + |
| 102 | +@test "get_default_branch: pure local repo with main returns main" { |
| 103 | + # No remote at all |
| 104 | + init_git_repo |
| 105 | + |
| 106 | + run get_default_branch |
| 107 | + |
| 108 | + assert_success |
| 109 | + assert_output "main" |
| 110 | +} |
| 111 | + |
| 112 | +@test "get_default_branch: pure local repo with master returns master" { |
| 113 | + # No remote, default branch is master |
| 114 | + export GIT_CONFIG_NOSYSTEM=1 |
| 115 | + git init -q --initial-branch=master |
| 116 | + git config user.email "test@example.com" |
| 117 | + git config user.name "Test User" |
| 118 | + echo "initial" > file.txt |
| 119 | + git add file.txt |
| 120 | + git commit -q -m "Initial commit" |
| 121 | + |
| 122 | + run get_default_branch |
| 123 | + |
| 124 | + assert_success |
| 125 | + assert_output "master" |
| 126 | +} |
| 127 | + |
| 128 | +@test "get_default_branch: prefers origin/main over local master" { |
| 129 | + # Edge case: local master exists, but origin/main also exists |
| 130 | + export GIT_CONFIG_NOSYSTEM=1 |
| 131 | + git init -q --initial-branch=master |
| 132 | + git config user.email "test@example.com" |
| 133 | + git config user.name "Test User" |
| 134 | + echo "initial" > file.txt |
| 135 | + git add file.txt |
| 136 | + git commit -q -m "Initial commit" |
| 137 | + |
| 138 | + local remote_path |
| 139 | + remote_path=$(create_bare_remote) |
| 140 | + git remote add origin "$remote_path" |
| 141 | + # Rename to main for push, then create local master |
| 142 | + git branch -m master main |
| 143 | + git push -u origin main 2>/dev/null |
| 144 | + git checkout -b master 2>/dev/null |
| 145 | + |
| 146 | + run get_default_branch |
| 147 | + |
| 148 | + assert_success |
| 149 | + assert_output "main" |
| 150 | +} |
| 151 | + |
| 152 | +@test "get_default_branch: empty repo falls back to main" { |
| 153 | + # Repo with no commits yet |
| 154 | + export GIT_CONFIG_NOSYSTEM=1 |
| 155 | + git init -q |
| 156 | + |
| 157 | + run get_default_branch |
| 158 | + |
| 159 | + assert_success |
| 160 | + assert_output "main" |
| 161 | +} |
| 162 | + |
| 163 | +# ============================================================================= |
| 164 | +# branch_exists tests |
| 165 | +# ============================================================================= |
| 166 | + |
| 167 | +@test "branch_exists: returns true for existing local branch" { |
| 168 | + init_git_repo |
| 169 | + |
| 170 | + run branch_exists "main" |
| 171 | + |
| 172 | + assert_success |
| 173 | +} |
| 174 | + |
| 175 | +@test "branch_exists: returns false for non-existing branch" { |
| 176 | + init_git_repo |
| 177 | + |
| 178 | + run branch_exists "nonexistent" |
| 179 | + |
| 180 | + assert_failure |
| 181 | +} |
| 182 | + |
| 183 | +@test "branch_exists: returns true for remote tracking branch" { |
| 184 | + init_git_repo |
| 185 | + local remote_path |
| 186 | + remote_path=$(create_bare_remote) |
| 187 | + git remote add origin "$remote_path" |
| 188 | + git push -u origin main 2>/dev/null |
| 189 | + |
| 190 | + run branch_exists "main" |
| 191 | + |
| 192 | + assert_success |
| 193 | +} |
| 194 | + |
| 195 | +# ============================================================================= |
| 196 | +# has_commits_since tests |
| 197 | +# ============================================================================= |
| 198 | + |
| 199 | +@test "has_commits_since: returns true when commits exist on feature branch" { |
| 200 | + init_git_repo |
| 201 | + git checkout -b feature 2>/dev/null |
| 202 | + echo "feature" > feature.txt |
| 203 | + git add feature.txt |
| 204 | + git commit -q -m "Feature commit" |
| 205 | + |
| 206 | + run has_commits_since "main" |
| 207 | + |
| 208 | + assert_success |
| 209 | +} |
| 210 | + |
| 211 | +@test "has_commits_since: returns false when no commits since branch" { |
| 212 | + init_git_repo |
| 213 | + git checkout -b feature 2>/dev/null |
| 214 | + # No new commits |
| 215 | + |
| 216 | + run has_commits_since "main" |
| 217 | + |
| 218 | + assert_failure |
| 219 | +} |
| 220 | + |
| 221 | +@test "has_commits_since: returns false when base branch does not exist" { |
| 222 | + init_git_repo |
| 223 | + |
| 224 | + run has_commits_since "nonexistent" |
| 225 | + |
| 226 | + assert_failure |
| 227 | +} |
| 228 | + |
| 229 | +@test "has_commits_since: works with remote tracking branch as base" { |
| 230 | + init_git_repo |
| 231 | + local remote_path |
| 232 | + remote_path=$(create_bare_remote) |
| 233 | + git remote add origin "$remote_path" |
| 234 | + git push -u origin main 2>/dev/null |
| 235 | + git checkout -b feature 2>/dev/null |
| 236 | + echo "feature" > feature.txt |
| 237 | + git add feature.txt |
| 238 | + git commit -q -m "Feature commit" |
| 239 | + |
| 240 | + run has_commits_since "main" |
| 241 | + |
| 242 | + assert_success |
| 243 | +} |
0 commit comments