416e6219ce13a16ce4aa608ac29025cefcacc231
   1#!/bin/sh
   2#
   3# Copyright (c) 2012 SZEDER Gábor
   4#
   5
   6test_description='test git-specific bash prompt functions'
   7
   8. ./lib-bash.sh
   9
  10. "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
  11
  12actual="$TRASH_DIRECTORY/actual"
  13
  14test_expect_success 'setup for prompt tests' '
  15        git init otherrepo &&
  16        echo 1 >file &&
  17        git add file &&
  18        test_tick &&
  19        git commit -m initial &&
  20        git tag -a -m msg1 t1 &&
  21        git checkout -b b1 &&
  22        echo 2 >file &&
  23        git commit -m "second b1" file &&
  24        echo 3 >file &&
  25        git commit -m "third b1" file &&
  26        git tag -a -m msg2 t2 &&
  27        git checkout -b b2 master &&
  28        echo 0 >file &&
  29        git commit -m "second b2" file &&
  30        echo 00 >file &&
  31        git commit -m "another b2" file &&
  32        echo 000 >file &&
  33        git commit -m "yet another b2" file &&
  34        git checkout master
  35'
  36
  37test_expect_success 'prompt - branch name' '
  38        printf " (master)" >expected &&
  39        __git_ps1 >"$actual" &&
  40        test_cmp expected "$actual"
  41'
  42
  43test_expect_success SYMLINKS 'prompt - branch name - symlink symref' '
  44        printf " (master)" >expected &&
  45        test_when_finished "git checkout master" &&
  46        test_config core.preferSymlinkRefs true &&
  47        git checkout master &&
  48        __git_ps1 >"$actual" &&
  49        test_cmp expected "$actual"
  50'
  51
  52test_expect_success 'prompt - detached head' '
  53        printf " ((%s...))" $(git log -1 --format="%h" b1^) >expected &&
  54        git checkout b1^ &&
  55        test_when_finished "git checkout master" &&
  56        __git_ps1 >"$actual" &&
  57        test_cmp expected "$actual"
  58'
  59
  60test_expect_success 'prompt - describe detached head - contains' '
  61        printf " ((t2~1))" >expected &&
  62        git checkout b1^ &&
  63        test_when_finished "git checkout master" &&
  64        (
  65                GIT_PS1_DESCRIBE_STYLE=contains &&
  66                __git_ps1 >"$actual"
  67        ) &&
  68        test_cmp expected "$actual"
  69'
  70
  71test_expect_success 'prompt - describe detached head - branch' '
  72        printf " ((b1~1))" >expected &&
  73        git checkout b1^ &&
  74        test_when_finished "git checkout master" &&
  75        (
  76                GIT_PS1_DESCRIBE_STYLE=branch &&
  77                __git_ps1 >"$actual"
  78        ) &&
  79        test_cmp expected "$actual"
  80'
  81
  82test_expect_success 'prompt - describe detached head - describe' '
  83        printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
  84        git checkout b1^ &&
  85        test_when_finished "git checkout master" &&
  86        (
  87                GIT_PS1_DESCRIBE_STYLE=describe &&
  88                __git_ps1 >"$actual"
  89        ) &&
  90        test_cmp expected "$actual"
  91'
  92
  93test_expect_success 'prompt - describe detached head - default' '
  94        printf " ((t2))" >expected &&
  95        git checkout --detach b1 &&
  96        test_when_finished "git checkout master" &&
  97        __git_ps1 >"$actual" &&
  98        test_cmp expected "$actual"
  99'
 100
 101test_expect_success 'prompt - inside .git directory' '
 102        printf " (GIT_DIR!)" >expected &&
 103        (
 104                cd .git &&
 105                __git_ps1 >"$actual"
 106        ) &&
 107        test_cmp expected "$actual"
 108'
 109
 110test_expect_success 'prompt - deep inside .git directory' '
 111        printf " (GIT_DIR!)" >expected &&
 112        (
 113                cd .git/refs/heads &&
 114                __git_ps1 >"$actual"
 115        ) &&
 116        test_cmp expected "$actual"
 117'
 118
 119test_expect_success 'prompt - inside bare repository' '
 120        printf " (BARE:master)" >expected &&
 121        git init --bare bare.git &&
 122        test_when_finished "rm -rf bare.git" &&
 123        (
 124                cd bare.git &&
 125                __git_ps1 >"$actual"
 126        ) &&
 127        test_cmp expected "$actual"
 128'
 129
 130test_expect_success 'prompt - interactive rebase' '
 131        printf " (b1|REBASE-i 2/3)" >expected
 132        write_script fake_editor.sh <<-\EOF &&
 133                echo "exec echo" >"$1"
 134                echo "edit $(git log -1 --format="%h")" >>"$1"
 135                echo "exec echo" >>"$1"
 136        EOF
 137        test_when_finished "rm -f fake_editor.sh" &&
 138        test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
 139        git checkout b1 &&
 140        test_when_finished "git checkout master" &&
 141        git rebase -i HEAD^ &&
 142        test_when_finished "git rebase --abort"
 143        __git_ps1 >"$actual" &&
 144        test_cmp expected "$actual"
 145'
 146
 147test_expect_success 'prompt - rebase merge' '
 148        printf " (b2|REBASE-m 1/3)" >expected &&
 149        git checkout b2 &&
 150        test_when_finished "git checkout master" &&
 151        test_must_fail git rebase --merge b1 b2 &&
 152        test_when_finished "git rebase --abort" &&
 153        __git_ps1 >"$actual" &&
 154        test_cmp expected "$actual"
 155'
 156
 157test_expect_success 'prompt - rebase' '
 158        printf " (b2|REBASE 1/3)" >expected &&
 159        git checkout b2 &&
 160        test_when_finished "git checkout master" &&
 161        test_must_fail git rebase b1 b2 &&
 162        test_when_finished "git rebase --abort" &&
 163        __git_ps1 >"$actual" &&
 164        test_cmp expected "$actual"
 165'
 166
 167test_expect_success 'prompt - merge' '
 168        printf " (b1|MERGING)" >expected &&
 169        git checkout b1 &&
 170        test_when_finished "git checkout master" &&
 171        test_must_fail git merge b2 &&
 172        test_when_finished "git reset --hard" &&
 173        __git_ps1 >"$actual" &&
 174        test_cmp expected "$actual"
 175'
 176
 177test_expect_success 'prompt - cherry-pick' '
 178        printf " (master|CHERRY-PICKING)" >expected &&
 179        test_must_fail git cherry-pick b1 &&
 180        test_when_finished "git reset --hard" &&
 181        __git_ps1 >"$actual" &&
 182        test_cmp expected "$actual"
 183'
 184
 185test_expect_success 'prompt - bisect' '
 186        printf " (master|BISECTING)" >expected &&
 187        git bisect start &&
 188        test_when_finished "git bisect reset" &&
 189        __git_ps1 >"$actual" &&
 190        test_cmp expected "$actual"
 191'
 192
 193test_expect_success 'prompt - dirty status indicator - clean' '
 194        printf " (master)" >expected &&
 195        (
 196                GIT_PS1_SHOWDIRTYSTATE=y &&
 197                __git_ps1 >"$actual"
 198        ) &&
 199        test_cmp expected "$actual"
 200'
 201
 202test_expect_success 'prompt - dirty status indicator - dirty worktree' '
 203        printf " (master *)" >expected &&
 204        echo "dirty" >file &&
 205        test_when_finished "git reset --hard" &&
 206        (
 207                GIT_PS1_SHOWDIRTYSTATE=y &&
 208                __git_ps1 >"$actual"
 209        ) &&
 210        test_cmp expected "$actual"
 211'
 212
 213test_expect_success 'prompt - dirty status indicator - dirty index' '
 214        printf " (master +)" >expected &&
 215        echo "dirty" >file &&
 216        test_when_finished "git reset --hard" &&
 217        git add -u &&
 218        (
 219                GIT_PS1_SHOWDIRTYSTATE=y &&
 220                __git_ps1 >"$actual"
 221        ) &&
 222        test_cmp expected "$actual"
 223'
 224
 225test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
 226        printf " (master *+)" >expected &&
 227        echo "dirty index" >file &&
 228        test_when_finished "git reset --hard" &&
 229        git add -u &&
 230        echo "dirty worktree" >file &&
 231        (
 232                GIT_PS1_SHOWDIRTYSTATE=y &&
 233                __git_ps1 >"$actual"
 234        ) &&
 235        test_cmp expected "$actual"
 236'
 237
 238test_expect_success 'prompt - dirty status indicator - before root commit' '
 239        printf " (master #)" >expected &&
 240        (
 241                GIT_PS1_SHOWDIRTYSTATE=y &&
 242                cd otherrepo &&
 243                __git_ps1 >"$actual"
 244        ) &&
 245        test_cmp expected "$actual"
 246'
 247
 248test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
 249        printf " (master)" >expected &&
 250        echo "dirty" >file &&
 251        test_when_finished "git reset --hard" &&
 252        test_config bash.showDirtyState false &&
 253        (
 254                sane_unset GIT_PS1_SHOWDIRTYSTATE &&
 255                __git_ps1 >"$actual"
 256        ) &&
 257        test_cmp expected "$actual"
 258'
 259
 260test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
 261        printf " (master)" >expected &&
 262        echo "dirty" >file &&
 263        test_when_finished "git reset --hard" &&
 264        test_config bash.showDirtyState true &&
 265        (
 266                sane_unset GIT_PS1_SHOWDIRTYSTATE &&
 267                __git_ps1 >"$actual"
 268        ) &&
 269        test_cmp expected "$actual"
 270'
 271
 272test_expect_success 'prompt - dirty status indicator - shell variable set with config disabled' '
 273        printf " (master)" >expected &&
 274        echo "dirty" >file &&
 275        test_when_finished "git reset --hard" &&
 276        test_config bash.showDirtyState false &&
 277        (
 278                GIT_PS1_SHOWDIRTYSTATE=y &&
 279                __git_ps1 >"$actual"
 280        ) &&
 281        test_cmp expected "$actual"
 282'
 283
 284test_expect_success 'prompt - dirty status indicator - shell variable set with config enabled' '
 285        printf " (master *)" >expected &&
 286        echo "dirty" >file &&
 287        test_when_finished "git reset --hard" &&
 288        test_config bash.showDirtyState true &&
 289        (
 290                GIT_PS1_SHOWDIRTYSTATE=y &&
 291                __git_ps1 >"$actual"
 292        ) &&
 293        test_cmp expected "$actual"
 294'
 295
 296test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
 297        printf " (GIT_DIR!)" >expected &&
 298        echo "dirty" >file &&
 299        test_when_finished "git reset --hard" &&
 300        (
 301                GIT_PS1_SHOWDIRTYSTATE=y &&
 302                cd .git &&
 303                __git_ps1 >"$actual"
 304        ) &&
 305        test_cmp expected "$actual"
 306'
 307
 308test_expect_success 'prompt - stash status indicator - no stash' '
 309        printf " (master)" >expected &&
 310        (
 311                GIT_PS1_SHOWSTASHSTATE=y &&
 312                __git_ps1 >"$actual"
 313        ) &&
 314        test_cmp expected "$actual"
 315'
 316
 317test_expect_success 'prompt - stash status indicator - stash' '
 318        printf " (master $)" >expected &&
 319        echo 2 >file &&
 320        git stash &&
 321        test_when_finished "git stash drop" &&
 322        (
 323                GIT_PS1_SHOWSTASHSTATE=y &&
 324                __git_ps1 >"$actual"
 325        ) &&
 326        test_cmp expected "$actual"
 327'
 328
 329test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
 330        printf " (GIT_DIR!)" >expected &&
 331        echo 2 >file &&
 332        git stash &&
 333        test_when_finished "git stash drop" &&
 334        (
 335                GIT_PS1_SHOWSTASHSTATE=y &&
 336                cd .git &&
 337                __git_ps1 >"$actual"
 338        ) &&
 339        test_cmp expected "$actual"
 340'
 341
 342test_expect_success 'prompt - untracked files status indicator - no untracked files' '
 343        printf " (master)" >expected &&
 344        (
 345                GIT_PS1_SHOWUNTRACKEDFILES=y &&
 346                cd otherrepo &&
 347                __git_ps1 >"$actual"
 348        ) &&
 349        test_cmp expected "$actual"
 350'
 351
 352test_expect_success 'prompt - untracked files status indicator - untracked files' '
 353        printf " (master %%)" >expected &&
 354        (
 355                GIT_PS1_SHOWUNTRACKEDFILES=y &&
 356                __git_ps1 >"$actual"
 357        ) &&
 358        test_cmp expected "$actual"
 359'
 360
 361test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
 362        printf " (master)" >expected &&
 363        test_config bash.showUntrackedFiles false &&
 364        (
 365                sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
 366                __git_ps1 >"$actual"
 367        ) &&
 368        test_cmp expected "$actual"
 369'
 370
 371test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
 372        printf " (master)" >expected &&
 373        test_config bash.showUntrackedFiles true &&
 374        (
 375                sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
 376                __git_ps1 >"$actual"
 377        ) &&
 378        test_cmp expected "$actual"
 379'
 380
 381test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
 382        printf " (master)" >expected &&
 383        test_config bash.showUntrackedFiles false &&
 384        (
 385                GIT_PS1_SHOWUNTRACKEDFILES=y &&
 386                __git_ps1 >"$actual"
 387        ) &&
 388        test_cmp expected "$actual"
 389'
 390
 391test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
 392        printf " (master %%)" >expected &&
 393        test_config bash.showUntrackedFiles true &&
 394        (
 395                GIT_PS1_SHOWUNTRACKEDFILES=y &&
 396                __git_ps1 >"$actual"
 397        ) &&
 398        test_cmp expected "$actual"
 399'
 400
 401test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
 402        printf " (GIT_DIR!)" >expected &&
 403        (
 404                GIT_PS1_SHOWUNTRACKEDFILES=y &&
 405                cd .git &&
 406                __git_ps1 >"$actual"
 407        ) &&
 408        test_cmp expected "$actual"
 409'
 410
 411test_expect_success 'prompt - format string starting with dash' '
 412        printf -- "-master" >expected &&
 413        __git_ps1 "-%s" >"$actual" &&
 414        test_cmp expected "$actual"
 415'
 416
 417test_done