t / t7006-pager.shon commit connect: split git:// setup into a separate function (2ac67cb)
   1#!/bin/sh
   2
   3test_description='Test automatic use of a pager.'
   4
   5. ./test-lib.sh
   6. "$TEST_DIRECTORY"/lib-pager.sh
   7. "$TEST_DIRECTORY"/lib-terminal.sh
   8
   9test_expect_success 'setup' '
  10        sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
  11        test_unconfig core.pager &&
  12
  13        PAGER="cat >paginated.out" &&
  14        export PAGER &&
  15
  16        test_commit initial
  17'
  18
  19test_expect_success TTY 'some commands use a pager' '
  20        rm -f paginated.out &&
  21        test_terminal git log &&
  22        test -e paginated.out
  23'
  24
  25test_expect_failure TTY 'pager runs from subdir' '
  26        echo subdir/paginated.out >expected &&
  27        mkdir -p subdir &&
  28        rm -f paginated.out subdir/paginated.out &&
  29        (
  30                cd subdir &&
  31                test_terminal git log
  32        ) &&
  33        {
  34                ls paginated.out subdir/paginated.out ||
  35                :
  36        } >actual &&
  37        test_cmp expected actual
  38'
  39
  40test_expect_success TTY 'LESS and LV envvars are set for pagination' '
  41        (
  42                sane_unset LESS LV &&
  43                PAGER="env >pager-env.out; wc" &&
  44                export PAGER &&
  45
  46                test_terminal git log
  47        ) &&
  48        grep ^LESS= pager-env.out &&
  49        grep ^LV= pager-env.out
  50'
  51
  52test_expect_success !MINGW,TTY 'LESS and LV envvars set by git-sh-setup' '
  53        (
  54                sane_unset LESS LV &&
  55                PAGER="env >pager-env.out; wc" &&
  56                export PAGER &&
  57                PATH="$(git --exec-path):$PATH" &&
  58                export PATH &&
  59                test_terminal sh -c ". git-sh-setup && git_pager"
  60        ) &&
  61        grep ^LESS= pager-env.out &&
  62        grep ^LV= pager-env.out
  63'
  64
  65test_expect_success TTY 'some commands do not use a pager' '
  66        rm -f paginated.out &&
  67        test_terminal git rev-list HEAD &&
  68        ! test -e paginated.out
  69'
  70
  71test_expect_success 'no pager when stdout is a pipe' '
  72        rm -f paginated.out &&
  73        git log | cat &&
  74        ! test -e paginated.out
  75'
  76
  77test_expect_success 'no pager when stdout is a regular file' '
  78        rm -f paginated.out &&
  79        git log >file &&
  80        ! test -e paginated.out
  81'
  82
  83test_expect_success TTY 'git --paginate rev-list uses a pager' '
  84        rm -f paginated.out &&
  85        test_terminal git --paginate rev-list HEAD &&
  86        test -e paginated.out
  87'
  88
  89test_expect_success 'no pager even with --paginate when stdout is a pipe' '
  90        rm -f file paginated.out &&
  91        git --paginate log | cat &&
  92        ! test -e paginated.out
  93'
  94
  95test_expect_success TTY 'no pager with --no-pager' '
  96        rm -f paginated.out &&
  97        test_terminal git --no-pager log &&
  98        ! test -e paginated.out
  99'
 100
 101test_expect_success TTY 'configuration can disable pager' '
 102        rm -f paginated.out &&
 103        test_unconfig pager.grep &&
 104        test_terminal git grep initial &&
 105        test -e paginated.out &&
 106
 107        rm -f paginated.out &&
 108        test_config pager.grep false &&
 109        test_terminal git grep initial &&
 110        ! test -e paginated.out
 111'
 112
 113test_expect_success TTY 'git config uses a pager if configured to' '
 114        rm -f paginated.out &&
 115        test_config pager.config true &&
 116        test_terminal git config --list &&
 117        test -e paginated.out
 118'
 119
 120test_expect_success TTY 'configuration can enable pager (from subdir)' '
 121        rm -f paginated.out &&
 122        mkdir -p subdir &&
 123        test_config pager.bundle true &&
 124
 125        git bundle create test.bundle --all &&
 126        rm -f paginated.out subdir/paginated.out &&
 127        (
 128                cd subdir &&
 129                test_terminal git bundle unbundle ../test.bundle
 130        ) &&
 131        {
 132                test -e paginated.out ||
 133                test -e subdir/paginated.out
 134        }
 135'
 136
 137test_expect_success TTY 'git tag -l defaults to paging' '
 138        rm -f paginated.out &&
 139        test_terminal git tag -l &&
 140        test -e paginated.out
 141'
 142
 143test_expect_success TTY 'git tag -l respects pager.tag' '
 144        rm -f paginated.out &&
 145        test_terminal git -c pager.tag=false tag -l &&
 146        ! test -e paginated.out
 147'
 148
 149test_expect_success TTY 'git tag -l respects --no-pager' '
 150        rm -f paginated.out &&
 151        test_terminal git -c pager.tag --no-pager tag -l &&
 152        ! test -e paginated.out
 153'
 154
 155test_expect_success TTY 'git tag with no args defaults to paging' '
 156        # no args implies -l so this should page like -l
 157        rm -f paginated.out &&
 158        test_terminal git tag &&
 159        test -e paginated.out
 160'
 161
 162test_expect_success TTY 'git tag with no args respects pager.tag' '
 163        # no args implies -l so this should page like -l
 164        rm -f paginated.out &&
 165        test_terminal git -c pager.tag=false tag &&
 166        ! test -e paginated.out
 167'
 168
 169test_expect_success TTY 'git tag --contains defaults to paging' '
 170        # --contains implies -l so this should page like -l
 171        rm -f paginated.out &&
 172        test_terminal git tag --contains &&
 173        test -e paginated.out
 174'
 175
 176test_expect_success TTY 'git tag --contains respects pager.tag' '
 177        # --contains implies -l so this should page like -l
 178        rm -f paginated.out &&
 179        test_terminal git -c pager.tag=false tag --contains &&
 180        ! test -e paginated.out
 181'
 182
 183test_expect_success TTY 'git tag -a defaults to not paging' '
 184        test_when_finished "git tag -d newtag" &&
 185        rm -f paginated.out &&
 186        test_terminal git tag -am message newtag &&
 187        ! test -e paginated.out
 188'
 189
 190test_expect_success TTY 'git tag -a ignores pager.tag' '
 191        test_when_finished "git tag -d newtag" &&
 192        rm -f paginated.out &&
 193        test_terminal git -c pager.tag tag -am message newtag &&
 194        ! test -e paginated.out
 195'
 196
 197test_expect_success TTY 'git tag -a respects --paginate' '
 198        test_when_finished "git tag -d newtag" &&
 199        rm -f paginated.out &&
 200        test_terminal git --paginate tag -am message newtag &&
 201        test -e paginated.out
 202'
 203
 204test_expect_success TTY 'git tag as alias ignores pager.tag with -a' '
 205        test_when_finished "git tag -d newtag" &&
 206        rm -f paginated.out &&
 207        test_terminal git -c pager.tag -c alias.t=tag t -am message newtag &&
 208        ! test -e paginated.out
 209'
 210
 211test_expect_success TTY 'git tag as alias respects pager.tag with -l' '
 212        rm -f paginated.out &&
 213        test_terminal git -c pager.tag=false -c alias.t=tag t -l &&
 214        ! test -e paginated.out
 215'
 216
 217# A colored commit log will begin with an appropriate ANSI escape
 218# for the first color; the text "commit" comes later.
 219colorful() {
 220        read firstline <$1
 221        ! expr "$firstline" : "[a-zA-Z]" >/dev/null
 222}
 223
 224test_expect_success 'tests can detect color' '
 225        rm -f colorful.log colorless.log &&
 226        git log --no-color >colorless.log &&
 227        git log --color >colorful.log &&
 228        ! colorful colorless.log &&
 229        colorful colorful.log
 230'
 231
 232test_expect_success 'no color when stdout is a regular file' '
 233        rm -f colorless.log &&
 234        test_config color.ui auto &&
 235        git log >colorless.log &&
 236        ! colorful colorless.log
 237'
 238
 239test_expect_success TTY 'color when writing to a pager' '
 240        rm -f paginated.out &&
 241        test_config color.ui auto &&
 242        test_terminal env TERM=vt100 git log &&
 243        colorful paginated.out
 244'
 245
 246test_expect_success TTY 'colors are suppressed by color.pager' '
 247        rm -f paginated.out &&
 248        test_config color.ui auto &&
 249        test_config color.pager false &&
 250        test_terminal env TERM=vt100 git log &&
 251        ! colorful paginated.out
 252'
 253
 254test_expect_success 'color when writing to a file intended for a pager' '
 255        rm -f colorful.log &&
 256        test_config color.ui auto &&
 257        (
 258                TERM=vt100 &&
 259                GIT_PAGER_IN_USE=true &&
 260                export TERM GIT_PAGER_IN_USE &&
 261                git log >colorful.log
 262        ) &&
 263        colorful colorful.log
 264'
 265
 266test_expect_success TTY 'colors are sent to pager for external commands' '
 267        test_config alias.externallog "!git log" &&
 268        test_config color.ui auto &&
 269        test_terminal env TERM=vt100 git -p externallog &&
 270        colorful paginated.out
 271'
 272
 273# Use this helper to make it easy for the caller of your
 274# terminal-using function to specify whether it should fail.
 275# If you write
 276#
 277#       your_test() {
 278#               parse_args "$@"
 279#
 280#               $test_expectation "$cmd - behaves well" "
 281#                       ...
 282#                       $full_command &&
 283#                       ...
 284#               "
 285#       }
 286#
 287# then your test can be used like this:
 288#
 289#       your_test expect_(success|failure) [test_must_fail] 'git foo'
 290#
 291parse_args() {
 292        test_expectation="test_$1"
 293        shift
 294        if test "$1" = test_must_fail
 295        then
 296                full_command="test_must_fail test_terminal "
 297                shift
 298        else
 299                full_command="test_terminal "
 300        fi
 301        cmd=$1
 302        full_command="$full_command $1"
 303}
 304
 305test_default_pager() {
 306        parse_args "$@"
 307
 308        $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
 309                sane_unset PAGER GIT_PAGER &&
 310                test_unconfig core.pager &&
 311                rm -f default_pager_used &&
 312                cat >\$less <<-\EOF &&
 313                #!/bin/sh
 314                wc >default_pager_used
 315                EOF
 316                chmod +x \$less &&
 317                (
 318                        PATH=.:\$PATH &&
 319                        export PATH &&
 320                        $full_command
 321                ) &&
 322                test -e default_pager_used
 323        "
 324}
 325
 326test_PAGER_overrides() {
 327        parse_args "$@"
 328
 329        $test_expectation TTY "$cmd - PAGER overrides default pager" "
 330                sane_unset GIT_PAGER &&
 331                test_unconfig core.pager &&
 332                rm -f PAGER_used &&
 333                PAGER='wc >PAGER_used' &&
 334                export PAGER &&
 335                $full_command &&
 336                test -e PAGER_used
 337        "
 338}
 339
 340test_core_pager_overrides() {
 341        if_local_config=
 342        used_if_wanted='overrides PAGER'
 343        test_core_pager "$@"
 344}
 345
 346test_local_config_ignored() {
 347        if_local_config='! '
 348        used_if_wanted='is not used'
 349        test_core_pager "$@"
 350}
 351
 352test_core_pager() {
 353        parse_args "$@"
 354
 355        $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
 356                sane_unset GIT_PAGER &&
 357                rm -f core.pager_used &&
 358                PAGER=wc &&
 359                export PAGER &&
 360                test_config core.pager 'wc >core.pager_used' &&
 361                $full_command &&
 362                ${if_local_config}test -e core.pager_used
 363        "
 364}
 365
 366test_core_pager_subdir() {
 367        if_local_config=
 368        used_if_wanted='overrides PAGER'
 369        test_pager_subdir_helper "$@"
 370}
 371
 372test_no_local_config_subdir() {
 373        if_local_config='! '
 374        used_if_wanted='is not used'
 375        test_pager_subdir_helper "$@"
 376}
 377
 378test_pager_subdir_helper() {
 379        parse_args "$@"
 380
 381        $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
 382                sane_unset GIT_PAGER &&
 383                rm -f core.pager_used &&
 384                rm -fr sub &&
 385                PAGER=wc &&
 386                stampname=\$(pwd)/core.pager_used &&
 387                export PAGER stampname &&
 388                test_config core.pager 'wc >\"\$stampname\"' &&
 389                mkdir sub &&
 390                (
 391                        cd sub &&
 392                        $full_command
 393                ) &&
 394                ${if_local_config}test -e core.pager_used
 395        "
 396}
 397
 398test_GIT_PAGER_overrides() {
 399        parse_args "$@"
 400
 401        $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
 402                rm -f GIT_PAGER_used &&
 403                test_config core.pager wc &&
 404                GIT_PAGER='wc >GIT_PAGER_used' &&
 405                export GIT_PAGER &&
 406                $full_command &&
 407                test -e GIT_PAGER_used
 408        "
 409}
 410
 411test_doesnt_paginate() {
 412        parse_args "$@"
 413
 414        $test_expectation TTY "no pager for '$cmd'" "
 415                rm -f GIT_PAGER_used &&
 416                GIT_PAGER='wc >GIT_PAGER_used' &&
 417                export GIT_PAGER &&
 418                $full_command &&
 419                ! test -e GIT_PAGER_used
 420        "
 421}
 422
 423test_pager_choices() {
 424        test_default_pager        expect_success "$@"
 425        test_PAGER_overrides      expect_success "$@"
 426        test_core_pager_overrides expect_success "$@"
 427        test_core_pager_subdir    expect_success "$@"
 428        test_GIT_PAGER_overrides  expect_success "$@"
 429}
 430
 431test_expect_success 'setup: some aliases' '
 432        git config alias.aliasedlog log &&
 433        git config alias.true "!true"
 434'
 435
 436test_pager_choices                       'git log'
 437test_pager_choices                       'git -p log'
 438test_pager_choices                       'git aliasedlog'
 439
 440test_default_pager        expect_success 'git -p aliasedlog'
 441test_PAGER_overrides      expect_success 'git -p aliasedlog'
 442test_core_pager_overrides expect_success 'git -p aliasedlog'
 443test_core_pager_subdir    expect_success 'git -p aliasedlog'
 444test_GIT_PAGER_overrides  expect_success 'git -p aliasedlog'
 445
 446test_default_pager        expect_success 'git -p true'
 447test_PAGER_overrides      expect_success 'git -p true'
 448test_core_pager_overrides expect_success 'git -p true'
 449test_core_pager_subdir    expect_success 'git -p true'
 450test_GIT_PAGER_overrides  expect_success 'git -p true'
 451
 452test_default_pager        expect_success test_must_fail 'git -p request-pull'
 453test_PAGER_overrides      expect_success test_must_fail 'git -p request-pull'
 454test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
 455test_core_pager_subdir    expect_success test_must_fail 'git -p request-pull'
 456test_GIT_PAGER_overrides  expect_success test_must_fail 'git -p request-pull'
 457
 458test_default_pager        expect_success test_must_fail 'git -p'
 459test_PAGER_overrides      expect_success test_must_fail 'git -p'
 460test_local_config_ignored expect_failure test_must_fail 'git -p'
 461test_GIT_PAGER_overrides  expect_success test_must_fail 'git -p'
 462
 463test_expect_success TTY 'core.pager in repo config works and retains cwd' '
 464        sane_unset GIT_PAGER &&
 465        test_config core.pager "cat >cwd-retained" &&
 466        (
 467                cd sub &&
 468                rm -f cwd-retained &&
 469                test_terminal git -p rev-parse HEAD &&
 470                test_path_is_file cwd-retained
 471        )
 472'
 473
 474test_expect_success TTY 'core.pager is found via alias in subdirectory' '
 475        sane_unset GIT_PAGER &&
 476        test_config core.pager "cat >via-alias" &&
 477        (
 478                cd sub &&
 479                rm -f via-alias &&
 480                test_terminal git -c alias.r="-p rev-parse" r HEAD &&
 481                test_path_is_file via-alias
 482        )
 483'
 484
 485test_doesnt_paginate      expect_failure test_must_fail 'git -p nonsense'
 486
 487test_pager_choices                       'git shortlog'
 488test_expect_success 'setup: configure shortlog not to paginate' '
 489        git config pager.shortlog false
 490'
 491test_doesnt_paginate      expect_success 'git shortlog'
 492test_no_local_config_subdir expect_success 'git shortlog'
 493test_default_pager        expect_success 'git -p shortlog'
 494test_core_pager_subdir    expect_success 'git -p shortlog'
 495
 496test_core_pager_subdir    expect_success test_must_fail \
 497                                         'git -p apply </dev/null'
 498
 499test_expect_success TTY 'command-specific pager' '
 500        sane_unset PAGER GIT_PAGER &&
 501        echo "foo:initial" >expect &&
 502        >actual &&
 503        test_unconfig core.pager &&
 504        test_config pager.log "sed s/^/foo:/ >actual" &&
 505        test_terminal git log --format=%s -1 &&
 506        test_cmp expect actual
 507'
 508
 509test_expect_success TTY 'command-specific pager overrides core.pager' '
 510        sane_unset PAGER GIT_PAGER &&
 511        echo "foo:initial" >expect &&
 512        >actual &&
 513        test_config core.pager "exit 1" &&
 514        test_config pager.log "sed s/^/foo:/ >actual" &&
 515        test_terminal git log --format=%s -1 &&
 516        test_cmp expect actual
 517'
 518
 519test_expect_success TTY 'command-specific pager overridden by environment' '
 520        GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
 521        >actual &&
 522        echo "foo:initial" >expect &&
 523        test_config pager.log "exit 1" &&
 524        test_terminal git log --format=%s -1 &&
 525        test_cmp expect actual
 526'
 527
 528test_expect_success 'setup external command' '
 529        cat >git-external <<-\EOF &&
 530        #!/bin/sh
 531        git "$@"
 532        EOF
 533        chmod +x git-external
 534'
 535
 536test_expect_success TTY 'command-specific pager works for external commands' '
 537        sane_unset PAGER GIT_PAGER &&
 538        echo "foo:initial" >expect &&
 539        >actual &&
 540        test_config pager.external "sed s/^/foo:/ >actual" &&
 541        test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
 542        test_cmp expect actual
 543'
 544
 545test_expect_success TTY 'sub-commands of externals use their own pager' '
 546        sane_unset PAGER GIT_PAGER &&
 547        echo "foo:initial" >expect &&
 548        >actual &&
 549        test_config pager.log "sed s/^/foo:/ >actual" &&
 550        test_terminal git --exec-path=. external log --format=%s -1 &&
 551        test_cmp expect actual
 552'
 553
 554test_expect_success TTY 'external command pagers override sub-commands' '
 555        sane_unset PAGER GIT_PAGER &&
 556        >expect &&
 557        >actual &&
 558        test_config pager.external false &&
 559        test_config pager.log "sed s/^/log:/ >actual" &&
 560        test_terminal git --exec-path=. external log --format=%s -1 &&
 561        test_cmp expect actual
 562'
 563
 564test_expect_success 'command with underscores does not complain' '
 565        write_script git-under_score <<-\EOF &&
 566        echo ok
 567        EOF
 568        git --exec-path=. under_score >actual 2>&1 &&
 569        echo ok >expect &&
 570        test_cmp expect actual
 571'
 572
 573test_done