t / t7006-pager.shon commit t7006: add tests for how git tag paginates (b3ee740)
   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 not 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 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 not 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 tag &&
 166        test -e paginated.out
 167'
 168
 169test_expect_success TTY 'git tag --contains defaults to not 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 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_failure 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
 204# A colored commit log will begin with an appropriate ANSI escape
 205# for the first color; the text "commit" comes later.
 206colorful() {
 207        read firstline <$1
 208        ! expr "$firstline" : "[a-zA-Z]" >/dev/null
 209}
 210
 211test_expect_success 'tests can detect color' '
 212        rm -f colorful.log colorless.log &&
 213        git log --no-color >colorless.log &&
 214        git log --color >colorful.log &&
 215        ! colorful colorless.log &&
 216        colorful colorful.log
 217'
 218
 219test_expect_success 'no color when stdout is a regular file' '
 220        rm -f colorless.log &&
 221        test_config color.ui auto &&
 222        git log >colorless.log &&
 223        ! colorful colorless.log
 224'
 225
 226test_expect_success TTY 'color when writing to a pager' '
 227        rm -f paginated.out &&
 228        test_config color.ui auto &&
 229        test_terminal env TERM=vt100 git log &&
 230        colorful paginated.out
 231'
 232
 233test_expect_success TTY 'colors are suppressed by color.pager' '
 234        rm -f paginated.out &&
 235        test_config color.ui auto &&
 236        test_config color.pager false &&
 237        test_terminal env TERM=vt100 git log &&
 238        ! colorful paginated.out
 239'
 240
 241test_expect_success 'color when writing to a file intended for a pager' '
 242        rm -f colorful.log &&
 243        test_config color.ui auto &&
 244        (
 245                TERM=vt100 &&
 246                GIT_PAGER_IN_USE=true &&
 247                export TERM GIT_PAGER_IN_USE &&
 248                git log >colorful.log
 249        ) &&
 250        colorful colorful.log
 251'
 252
 253test_expect_success TTY 'colors are sent to pager for external commands' '
 254        test_config alias.externallog "!git log" &&
 255        test_config color.ui auto &&
 256        test_terminal env TERM=vt100 git -p externallog &&
 257        colorful paginated.out
 258'
 259
 260# Use this helper to make it easy for the caller of your
 261# terminal-using function to specify whether it should fail.
 262# If you write
 263#
 264#       your_test() {
 265#               parse_args "$@"
 266#
 267#               $test_expectation "$cmd - behaves well" "
 268#                       ...
 269#                       $full_command &&
 270#                       ...
 271#               "
 272#       }
 273#
 274# then your test can be used like this:
 275#
 276#       your_test expect_(success|failure) [test_must_fail] 'git foo'
 277#
 278parse_args() {
 279        test_expectation="test_$1"
 280        shift
 281        if test "$1" = test_must_fail
 282        then
 283                full_command="test_must_fail test_terminal "
 284                shift
 285        else
 286                full_command="test_terminal "
 287        fi
 288        cmd=$1
 289        full_command="$full_command $1"
 290}
 291
 292test_default_pager() {
 293        parse_args "$@"
 294
 295        $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
 296                sane_unset PAGER GIT_PAGER &&
 297                test_unconfig core.pager &&
 298                rm -f default_pager_used &&
 299                cat >\$less <<-\EOF &&
 300                #!/bin/sh
 301                wc >default_pager_used
 302                EOF
 303                chmod +x \$less &&
 304                (
 305                        PATH=.:\$PATH &&
 306                        export PATH &&
 307                        $full_command
 308                ) &&
 309                test -e default_pager_used
 310        "
 311}
 312
 313test_PAGER_overrides() {
 314        parse_args "$@"
 315
 316        $test_expectation TTY "$cmd - PAGER overrides default pager" "
 317                sane_unset GIT_PAGER &&
 318                test_unconfig core.pager &&
 319                rm -f PAGER_used &&
 320                PAGER='wc >PAGER_used' &&
 321                export PAGER &&
 322                $full_command &&
 323                test -e PAGER_used
 324        "
 325}
 326
 327test_core_pager_overrides() {
 328        if_local_config=
 329        used_if_wanted='overrides PAGER'
 330        test_core_pager "$@"
 331}
 332
 333test_local_config_ignored() {
 334        if_local_config='! '
 335        used_if_wanted='is not used'
 336        test_core_pager "$@"
 337}
 338
 339test_core_pager() {
 340        parse_args "$@"
 341
 342        $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
 343                sane_unset GIT_PAGER &&
 344                rm -f core.pager_used &&
 345                PAGER=wc &&
 346                export PAGER &&
 347                test_config core.pager 'wc >core.pager_used' &&
 348                $full_command &&
 349                ${if_local_config}test -e core.pager_used
 350        "
 351}
 352
 353test_core_pager_subdir() {
 354        if_local_config=
 355        used_if_wanted='overrides PAGER'
 356        test_pager_subdir_helper "$@"
 357}
 358
 359test_no_local_config_subdir() {
 360        if_local_config='! '
 361        used_if_wanted='is not used'
 362        test_pager_subdir_helper "$@"
 363}
 364
 365test_pager_subdir_helper() {
 366        parse_args "$@"
 367
 368        $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
 369                sane_unset GIT_PAGER &&
 370                rm -f core.pager_used &&
 371                rm -fr sub &&
 372                PAGER=wc &&
 373                stampname=\$(pwd)/core.pager_used &&
 374                export PAGER stampname &&
 375                test_config core.pager 'wc >\"\$stampname\"' &&
 376                mkdir sub &&
 377                (
 378                        cd sub &&
 379                        $full_command
 380                ) &&
 381                ${if_local_config}test -e core.pager_used
 382        "
 383}
 384
 385test_GIT_PAGER_overrides() {
 386        parse_args "$@"
 387
 388        $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
 389                rm -f GIT_PAGER_used &&
 390                test_config core.pager wc &&
 391                GIT_PAGER='wc >GIT_PAGER_used' &&
 392                export GIT_PAGER &&
 393                $full_command &&
 394                test -e GIT_PAGER_used
 395        "
 396}
 397
 398test_doesnt_paginate() {
 399        parse_args "$@"
 400
 401        $test_expectation TTY "no pager for '$cmd'" "
 402                rm -f GIT_PAGER_used &&
 403                GIT_PAGER='wc >GIT_PAGER_used' &&
 404                export GIT_PAGER &&
 405                $full_command &&
 406                ! test -e GIT_PAGER_used
 407        "
 408}
 409
 410test_pager_choices() {
 411        test_default_pager        expect_success "$@"
 412        test_PAGER_overrides      expect_success "$@"
 413        test_core_pager_overrides expect_success "$@"
 414        test_core_pager_subdir    expect_success "$@"
 415        test_GIT_PAGER_overrides  expect_success "$@"
 416}
 417
 418test_expect_success 'setup: some aliases' '
 419        git config alias.aliasedlog log &&
 420        git config alias.true "!true"
 421'
 422
 423test_pager_choices                       'git log'
 424test_pager_choices                       'git -p log'
 425test_pager_choices                       'git aliasedlog'
 426
 427test_default_pager        expect_success 'git -p aliasedlog'
 428test_PAGER_overrides      expect_success 'git -p aliasedlog'
 429test_core_pager_overrides expect_success 'git -p aliasedlog'
 430test_core_pager_subdir    expect_success 'git -p aliasedlog'
 431test_GIT_PAGER_overrides  expect_success 'git -p aliasedlog'
 432
 433test_default_pager        expect_success 'git -p true'
 434test_PAGER_overrides      expect_success 'git -p true'
 435test_core_pager_overrides expect_success 'git -p true'
 436test_core_pager_subdir    expect_success 'git -p true'
 437test_GIT_PAGER_overrides  expect_success 'git -p true'
 438
 439test_default_pager        expect_success test_must_fail 'git -p request-pull'
 440test_PAGER_overrides      expect_success test_must_fail 'git -p request-pull'
 441test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
 442test_core_pager_subdir    expect_success test_must_fail 'git -p request-pull'
 443test_GIT_PAGER_overrides  expect_success test_must_fail 'git -p request-pull'
 444
 445test_default_pager        expect_success test_must_fail 'git -p'
 446test_PAGER_overrides      expect_success test_must_fail 'git -p'
 447test_local_config_ignored expect_failure test_must_fail 'git -p'
 448test_GIT_PAGER_overrides  expect_success test_must_fail 'git -p'
 449
 450test_expect_success TTY 'core.pager in repo config works and retains cwd' '
 451        sane_unset GIT_PAGER &&
 452        test_config core.pager "cat >cwd-retained" &&
 453        (
 454                cd sub &&
 455                rm -f cwd-retained &&
 456                test_terminal git -p rev-parse HEAD &&
 457                test_path_is_file cwd-retained
 458        )
 459'
 460
 461test_expect_success TTY 'core.pager is found via alias in subdirectory' '
 462        sane_unset GIT_PAGER &&
 463        test_config core.pager "cat >via-alias" &&
 464        (
 465                cd sub &&
 466                rm -f via-alias &&
 467                test_terminal git -c alias.r="-p rev-parse" r HEAD &&
 468                test_path_is_file via-alias
 469        )
 470'
 471
 472test_doesnt_paginate      expect_failure test_must_fail 'git -p nonsense'
 473
 474test_pager_choices                       'git shortlog'
 475test_expect_success 'setup: configure shortlog not to paginate' '
 476        git config pager.shortlog false
 477'
 478test_doesnt_paginate      expect_success 'git shortlog'
 479test_no_local_config_subdir expect_success 'git shortlog'
 480test_default_pager        expect_success 'git -p shortlog'
 481test_core_pager_subdir    expect_success 'git -p shortlog'
 482
 483test_core_pager_subdir    expect_success test_must_fail \
 484                                         'git -p apply </dev/null'
 485
 486test_expect_success TTY 'command-specific pager' '
 487        sane_unset PAGER GIT_PAGER &&
 488        echo "foo:initial" >expect &&
 489        >actual &&
 490        test_unconfig core.pager &&
 491        test_config pager.log "sed s/^/foo:/ >actual" &&
 492        test_terminal git log --format=%s -1 &&
 493        test_cmp expect actual
 494'
 495
 496test_expect_success TTY 'command-specific pager overrides core.pager' '
 497        sane_unset PAGER GIT_PAGER &&
 498        echo "foo:initial" >expect &&
 499        >actual &&
 500        test_config core.pager "exit 1" &&
 501        test_config pager.log "sed s/^/foo:/ >actual" &&
 502        test_terminal git log --format=%s -1 &&
 503        test_cmp expect actual
 504'
 505
 506test_expect_success TTY 'command-specific pager overridden by environment' '
 507        GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
 508        >actual &&
 509        echo "foo:initial" >expect &&
 510        test_config pager.log "exit 1" &&
 511        test_terminal git log --format=%s -1 &&
 512        test_cmp expect actual
 513'
 514
 515test_expect_success 'setup external command' '
 516        cat >git-external <<-\EOF &&
 517        #!/bin/sh
 518        git "$@"
 519        EOF
 520        chmod +x git-external
 521'
 522
 523test_expect_success TTY 'command-specific pager works for external commands' '
 524        sane_unset PAGER GIT_PAGER &&
 525        echo "foo:initial" >expect &&
 526        >actual &&
 527        test_config pager.external "sed s/^/foo:/ >actual" &&
 528        test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
 529        test_cmp expect actual
 530'
 531
 532test_expect_success TTY 'sub-commands of externals use their own pager' '
 533        sane_unset PAGER GIT_PAGER &&
 534        echo "foo:initial" >expect &&
 535        >actual &&
 536        test_config pager.log "sed s/^/foo:/ >actual" &&
 537        test_terminal git --exec-path=. external log --format=%s -1 &&
 538        test_cmp expect actual
 539'
 540
 541test_expect_success TTY 'external command pagers override sub-commands' '
 542        sane_unset PAGER GIT_PAGER &&
 543        >expect &&
 544        >actual &&
 545        test_config pager.external false &&
 546        test_config pager.log "sed s/^/log:/ >actual" &&
 547        test_terminal git --exec-path=. external log --format=%s -1 &&
 548        test_cmp expect actual
 549'
 550
 551test_expect_success 'command with underscores does not complain' '
 552        write_script git-under_score <<-\EOF &&
 553        echo ok
 554        EOF
 555        git --exec-path=. under_score >actual 2>&1 &&
 556        echo ok >expect &&
 557        test_cmp expect actual
 558'
 559
 560test_done