|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +load "test_helper/bats-support/load" |
| 4 | +load "test_helper/bats-assert/load" |
| 5 | + |
| 6 | + |
| 7 | +git() { |
| 8 | + command git \ |
| 9 | + -c init.defaultBranch=main \ |
| 10 | + |
| 11 | + -c user.name=Test \ |
| 12 | + "$@" |
| 13 | +} |
| 14 | + |
| 15 | +git-context-graph() { |
| 16 | + "${BATS_TEST_DIRNAME}"/../git-context-graph "$@" |
| 17 | +} |
| 18 | + |
| 19 | +setup_file() { |
| 20 | + cd "${BATS_TEST_DIRNAME}" || exit |
| 21 | + |
| 22 | + mkdir data && cd data || exit |
| 23 | + |
| 24 | + git init --bare -b main remote1 |
| 25 | + git init --bare -b main remote2 |
| 26 | + git init --bare -b custom remote3 |
| 27 | + |
| 28 | + git init repo && cd repo || exit |
| 29 | + |
| 30 | + git remote add r1 ../remote1 |
| 31 | + git remote add r2 ../remote2 |
| 32 | + git remote add r3 ../remote3 |
| 33 | + |
| 34 | + git switch -c main |
| 35 | + git commit --allow-empty -m "Main 1" |
| 36 | + git push r1 main |
| 37 | + git push r2 main |
| 38 | + git push r3 main:custom |
| 39 | + |
| 40 | + git switch -c feature-A |
| 41 | + git commit --allow-empty -m "Feature A - 1" |
| 42 | + git push r1 feature-A |
| 43 | + git push r2 feature-A |
| 44 | + |
| 45 | + git switch -c feature-B main |
| 46 | + git commit --allow-empty -m "Feature B - 1" |
| 47 | + git push r1 feature-B |
| 48 | + git push r2 feature-B |
| 49 | + |
| 50 | + git switch -c feature-C main |
| 51 | + git commit --allow-empty -m "Feature C - 1" |
| 52 | + git push r1 feature-C |
| 53 | + git push r3 feature-C |
| 54 | + |
| 55 | + git switch -c epic/big-feature main |
| 56 | + git commit --allow-empty -m "Epic B - 1" |
| 57 | + git push r1 epic/big-feature |
| 58 | + git push r2 epic/big-feature |
| 59 | + |
| 60 | + cd .. |
| 61 | + rm -rf repo |
| 62 | +} |
| 63 | + |
| 64 | +teardown_file() { |
| 65 | + cd "${BATS_TEST_DIRNAME}" || exit |
| 66 | + rm -rf data |
| 67 | +} |
| 68 | + |
| 69 | +setup() { |
| 70 | + cd "${BATS_TEST_DIRNAME}/data" || exit |
| 71 | +} |
| 72 | + |
| 73 | +teardown() { |
| 74 | + cd "${BATS_TEST_DIRNAME}/data" || exit |
| 75 | + rm -rf repo* |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +@test "Command fails outside a Git repository" { |
| 80 | + cd /tmp |
| 81 | + run git-context-graph --list |
| 82 | + assert_failure |
| 83 | +} |
| 84 | + |
| 85 | +@test "Local and default branches are shown with remotes" { |
| 86 | + git clone ./remote1 repo && cd repo |
| 87 | + |
| 88 | + git switch -c feature-A origin/feature-A |
| 89 | + |
| 90 | + run git-context-graph --list |
| 91 | + assert_output "$(cat <<- EOF |
| 92 | + refs/heads/feature-A |
| 93 | + refs/heads/main |
| 94 | + refs/remotes/origin/feature-A |
| 95 | + refs/remotes/origin/main |
| 96 | + EOF |
| 97 | + )" |
| 98 | + |
| 99 | + run git-context-graph --list --short |
| 100 | + assert_output "$(cat <<- EOF |
| 101 | + feature-A |
| 102 | + main |
| 103 | + origin/feature-A |
| 104 | + origin/main |
| 105 | + EOF |
| 106 | + )" |
| 107 | + |
| 108 | + run git-context-graph --list --local |
| 109 | + assert_output "$(cat <<- EOF |
| 110 | + refs/heads/feature-A |
| 111 | + refs/heads/main |
| 112 | + EOF |
| 113 | + )" |
| 114 | + |
| 115 | + run git-context-graph --list --no-default |
| 116 | + assert_output "$(cat <<- EOF |
| 117 | + refs/heads/feature-A |
| 118 | + refs/remotes/origin/feature-A |
| 119 | + EOF |
| 120 | + )" |
| 121 | +} |
| 122 | + |
| 123 | +@test "Branches to list can be passed as arguments" { |
| 124 | + git clone ./remote1 repo && cd repo |
| 125 | + |
| 126 | + git switch -c feature-A origin/feature-A |
| 127 | + git switch -c feature-B origin/feature-B |
| 128 | + |
| 129 | + run git-context-graph --list --no-default >&3 |
| 130 | + assert_output "$(cat <<- EOF |
| 131 | + refs/heads/feature-B |
| 132 | + refs/remotes/origin/feature-B |
| 133 | + EOF |
| 134 | + )" |
| 135 | + |
| 136 | + run git-context-graph feature-A --list --no-default >&3 |
| 137 | + assert_output "$(cat <<- EOF |
| 138 | + refs/heads/feature-A |
| 139 | + refs/remotes/origin/feature-A |
| 140 | + EOF |
| 141 | + )" |
| 142 | + |
| 143 | + run git-context-graph --add feature-A --list --no-default >&3 |
| 144 | + assert_output "$(cat <<- EOF |
| 145 | + refs/heads/feature-A |
| 146 | + refs/heads/feature-B |
| 147 | + refs/remotes/origin/feature-A |
| 148 | + refs/remotes/origin/feature-B |
| 149 | + EOF |
| 150 | + )" |
| 151 | +} |
| 152 | + |
| 153 | +@test "Default branch is determined from cloned remote HEAD" { |
| 154 | + git clone ./remote3 repo && cd repo |
| 155 | + git switch -c feature-C origin/feature-C |
| 156 | + |
| 157 | + run git-context-graph --list |
| 158 | + assert_line "refs/heads/custom" |
| 159 | + assert_line "refs/remotes/origin/custom" |
| 160 | +} |
| 161 | + |
| 162 | +@test "Default branch determination falls back to standard names" { |
| 163 | + # 'main' is a standard default branch |
| 164 | + ( |
| 165 | + git init -b main repo1 && cd repo1 |
| 166 | + git commit --allow-empty -m "Main 1" |
| 167 | + git switch -c feature |
| 168 | + git commit --allow-empty -m "feature 1" |
| 169 | + |
| 170 | + run git-context-graph --list |
| 171 | + assert_line "refs/heads/main" |
| 172 | + ) |
| 173 | + |
| 174 | + # 'master' is a standard default branch |
| 175 | + ( |
| 176 | + git init -b master repo2 && cd repo2 |
| 177 | + git commit --allow-empty -m "Master 1" |
| 178 | + git switch -c feature |
| 179 | + git commit --allow-empty -m "feature 1" |
| 180 | + |
| 181 | + run git-context-graph --list |
| 182 | + assert_line "refs/heads/master" |
| 183 | + ) |
| 184 | + |
| 185 | + # No standard default branch identified |
| 186 | + ( |
| 187 | + git init -b custom repo3 && cd repo3 |
| 188 | + git commit --allow-empty -m "Custom 1" |
| 189 | + git switch -c feature |
| 190 | + git commit --allow-empty -m "feature 1" |
| 191 | + |
| 192 | + run git-context-graph --list |
| 193 | + assert_output "$(cat <<- EOF |
| 194 | + refs/heads/feature |
| 195 | + EOF |
| 196 | + )" |
| 197 | + ) |
| 198 | +} |
| 199 | + |
| 200 | +@test "All remotes are considered" { |
| 201 | + git clone ./remote1 repo && cd repo |
| 202 | + git remote add other ../remote2 |
| 203 | + git fetch other |
| 204 | + |
| 205 | + git switch -c feature-A origin/feature-A |
| 206 | + |
| 207 | + run git-context-graph --list |
| 208 | + assert_output "$(cat <<- EOF |
| 209 | + refs/heads/feature-A |
| 210 | + refs/heads/main |
| 211 | + refs/remotes/origin/feature-A |
| 212 | + refs/remotes/origin/main |
| 213 | + refs/remotes/other/feature-A |
| 214 | + refs/remotes/other/main |
| 215 | + EOF |
| 216 | + )" |
| 217 | +} |
| 218 | + |
| 219 | +@test "Tracking branches with different names are considered" { |
| 220 | + git clone ./remote1 repo && cd repo |
| 221 | + git remote add other ../remote2 |
| 222 | + git fetch other |
| 223 | + |
| 224 | + git switch -c feature-A-local origin/feature-A |
| 225 | + |
| 226 | + run git-context-graph --list --no-default |
| 227 | + assert_output "$(cat <<- EOF |
| 228 | + refs/heads/feature-A-local |
| 229 | + refs/remotes/origin/feature-A |
| 230 | + EOF |
| 231 | + )" |
| 232 | +} |
| 233 | + |
| 234 | +@test "Git-log options are passed to git-log" { |
| 235 | + git clone ./remote1 repo && cd repo |
| 236 | + |
| 237 | + git switch -c feature-A origin/feature-A |
| 238 | + |
| 239 | + run git-context-graph --all --pretty=oneline --no-color |
| 240 | + assert_output --partial "(origin/epic/big-feature) Epic B - 1" |
| 241 | + assert_output --partial "(HEAD -> feature-A, origin/feature-A) Feature A - 1" |
| 242 | + assert_output --partial "(origin/feature-B) Feature B - 1" |
| 243 | + assert_output --partial "(origin/main, main) Main 1" |
| 244 | +} |
| 245 | + |
| 246 | +@test "Persistent additional context branches can be configured" { |
| 247 | + git clone ./remote1 repo && cd repo |
| 248 | + |
| 249 | + git switch -c feature-A origin/feature-A |
| 250 | + git switch -c feature-B origin/feature-B |
| 251 | + git switch -c feature-C origin/feature-C |
| 252 | + git switch -c epic/big-feature origin/epic/big-feature |
| 253 | + |
| 254 | + git switch feature-A |
| 255 | + |
| 256 | + run git-context-graph --config-add unknown-branch |
| 257 | + assert_output "$(cat <<- EOF |
| 258 | + No additional context branches for feature-A. |
| 259 | + EOF |
| 260 | + )" |
| 261 | + |
| 262 | + run git-context-graph --config-add epic/big-feature |
| 263 | + assert_output "$(cat <<- EOF |
| 264 | + Additional context branches for feature-A: |
| 265 | + epic/big-feature |
| 266 | + EOF |
| 267 | + )" |
| 268 | + |
| 269 | + run git-context-graph feature-A --list --short --local --no-default |
| 270 | + assert_output "$(cat <<- EOF |
| 271 | + epic/big-feature |
| 272 | + feature-A |
| 273 | + EOF |
| 274 | + )" |
| 275 | + |
| 276 | + git switch main |
| 277 | + |
| 278 | + run git-context-graph feature-A --config-add feature-B feature-C --config-clear epic/big-feature |
| 279 | + assert_output "$(cat <<- EOF |
| 280 | + Additional context branches for feature-A: |
| 281 | + feature-B |
| 282 | + feature-C |
| 283 | + EOF |
| 284 | + )" |
| 285 | + |
| 286 | + run git-context-graph feature-A --list --short --local --no-default |
| 287 | + assert_output "$(cat <<- EOF |
| 288 | + feature-A |
| 289 | + feature-B |
| 290 | + feature-C |
| 291 | + EOF |
| 292 | + )" |
| 293 | + |
| 294 | + git switch feature-A |
| 295 | + |
| 296 | + run git-context-graph --config-clear |
| 297 | + assert_output "$(cat <<- EOF |
| 298 | + Cleared additional context branches for feature-A. |
| 299 | + EOF |
| 300 | + )" |
| 301 | + |
| 302 | + run git-context-graph --list --short --local --no-default |
| 303 | + assert_output "$(cat <<- EOF |
| 304 | + feature-A |
| 305 | + EOF |
| 306 | + )" |
| 307 | +} |
0 commit comments