t / t7006-pager.shon commit Merge branch 'po/etc-gitattributes' (2851e8e)
   1#!/bin/sh
   2
   3test_description='Test automatic use of a pager.'
   4
   5. ./test-lib.sh
   6. "$TEST_DIRECTORY"/lib-pager.sh
   7
   8cleanup_fail() {
   9        echo >&2 cleanup failed
  10        (exit 1)
  11}
  12
  13test_expect_success 'set up terminal for tests' '
  14        rm -f stdout_is_tty ||
  15        cleanup_fail &&
  16
  17        if test -t 1
  18        then
  19                >stdout_is_tty
  20        elif
  21                test_have_prereq PERL &&
  22                "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl \
  23                        sh -c "test -t 1"
  24        then
  25                >test_terminal_works
  26        fi
  27'
  28
  29if test -e stdout_is_tty
  30then
  31        test_terminal() { "$@"; }
  32        test_set_prereq TTY
  33elif test -e test_terminal_works
  34then
  35        test_terminal() {
  36                "$PERL_PATH" "$TEST_DIRECTORY"/t7006/test-terminal.perl "$@"
  37        }
  38        test_set_prereq TTY
  39else
  40        say "# no usable terminal, so skipping some tests"
  41fi
  42
  43test_expect_success 'setup' '
  44        unset GIT_PAGER GIT_PAGER_IN_USE;
  45        test_might_fail git config --unset core.pager &&
  46
  47        PAGER="cat >paginated.out" &&
  48        export PAGER &&
  49
  50        test_commit initial
  51'
  52
  53test_expect_success TTY 'some commands use a pager' '
  54        rm -f paginated.out ||
  55        cleanup_fail &&
  56
  57        test_terminal git log &&
  58        test -e paginated.out
  59'
  60
  61test_expect_failure TTY 'pager runs from subdir' '
  62        echo subdir/paginated.out >expected &&
  63        mkdir -p subdir &&
  64        rm -f paginated.out subdir/paginated.out &&
  65        (
  66                cd subdir &&
  67                test_terminal git log
  68        ) &&
  69        {
  70                ls paginated.out subdir/paginated.out ||
  71                :
  72        } >actual &&
  73        test_cmp expected actual
  74'
  75
  76test_expect_success TTY 'some commands do not use a pager' '
  77        rm -f paginated.out ||
  78        cleanup_fail &&
  79
  80        test_terminal git rev-list HEAD &&
  81        ! test -e paginated.out
  82'
  83
  84test_expect_success 'no pager when stdout is a pipe' '
  85        rm -f paginated.out ||
  86        cleanup_fail &&
  87
  88        git log | cat &&
  89        ! test -e paginated.out
  90'
  91
  92test_expect_success 'no pager when stdout is a regular file' '
  93        rm -f paginated.out ||
  94        cleanup_fail &&
  95
  96        git log >file &&
  97        ! test -e paginated.out
  98'
  99
 100test_expect_success TTY 'git --paginate rev-list uses a pager' '
 101        rm -f paginated.out ||
 102        cleanup_fail &&
 103
 104        test_terminal git --paginate rev-list HEAD &&
 105        test -e paginated.out
 106'
 107
 108test_expect_success 'no pager even with --paginate when stdout is a pipe' '
 109        rm -f file paginated.out ||
 110        cleanup_fail &&
 111
 112        git --paginate log | cat &&
 113        ! test -e paginated.out
 114'
 115
 116test_expect_success TTY 'no pager with --no-pager' '
 117        rm -f paginated.out ||
 118        cleanup_fail &&
 119
 120        test_terminal git --no-pager log &&
 121        ! test -e paginated.out
 122'
 123
 124test_expect_success TTY 'configuration can disable pager' '
 125        rm -f paginated.out &&
 126        test_might_fail git config --unset pager.grep &&
 127        test_terminal git grep initial &&
 128        test -e paginated.out &&
 129
 130        rm -f paginated.out &&
 131        git config pager.grep false &&
 132        test_when_finished "git config --unset pager.grep" &&
 133        test_terminal git grep initial &&
 134        ! test -e paginated.out
 135'
 136
 137test_expect_success TTY 'git config uses a pager if configured to' '
 138        rm -f paginated.out &&
 139        git config pager.config true &&
 140        test_when_finished "git config --unset pager.config" &&
 141        test_terminal git config --list &&
 142        test -e paginated.out
 143'
 144
 145test_expect_success TTY 'configuration can enable pager (from subdir)' '
 146        rm -f paginated.out &&
 147        mkdir -p subdir &&
 148        git config pager.bundle true &&
 149        test_when_finished "git config --unset pager.bundle" &&
 150
 151        git bundle create test.bundle --all &&
 152        rm -f paginated.out subdir/paginated.out &&
 153        (
 154                cd subdir &&
 155                test_terminal git bundle unbundle ../test.bundle
 156        ) &&
 157        {
 158                test -e paginated.out ||
 159                test -e subdir/paginated.out
 160        }
 161'
 162
 163# A colored commit log will begin with an appropriate ANSI escape
 164# for the first color; the text "commit" comes later.
 165colorful() {
 166        read firstline <$1
 167        ! expr "$firstline" : "[a-zA-Z]" >/dev/null
 168}
 169
 170test_expect_success 'tests can detect color' '
 171        rm -f colorful.log colorless.log ||
 172        cleanup_fail &&
 173
 174        git log --no-color >colorless.log &&
 175        git log --color >colorful.log &&
 176        ! colorful colorless.log &&
 177        colorful colorful.log
 178'
 179
 180test_expect_success 'no color when stdout is a regular file' '
 181        rm -f colorless.log &&
 182        git config color.ui auto ||
 183        cleanup_fail &&
 184
 185        git log >colorless.log &&
 186        ! colorful colorless.log
 187'
 188
 189test_expect_success TTY 'color when writing to a pager' '
 190        rm -f paginated.out &&
 191        git config color.ui auto ||
 192        cleanup_fail &&
 193
 194        (
 195                TERM=vt100 &&
 196                export TERM &&
 197                test_terminal git log
 198        ) &&
 199        colorful paginated.out
 200'
 201
 202test_expect_success 'color when writing to a file intended for a pager' '
 203        rm -f colorful.log &&
 204        git config color.ui auto ||
 205        cleanup_fail &&
 206
 207        (
 208                TERM=vt100 &&
 209                GIT_PAGER_IN_USE=true &&
 210                export TERM GIT_PAGER_IN_USE &&
 211                git log >colorful.log
 212        ) &&
 213        colorful colorful.log
 214'
 215
 216if test_have_prereq SIMPLEPAGER && test_have_prereq TTY
 217then
 218        test_set_prereq SIMPLEPAGERTTY
 219fi
 220
 221# Use this helper to make it easy for the caller of your
 222# terminal-using function to specify whether it should fail.
 223# If you write
 224#
 225#       your_test() {
 226#               parse_args "$@"
 227#
 228#               $test_expectation "$cmd - behaves well" "
 229#                       ...
 230#                       $full_command &&
 231#                       ...
 232#               "
 233#       }
 234#
 235# then your test can be used like this:
 236#
 237#       your_test expect_(success|failure) [test_must_fail] 'git foo'
 238#
 239parse_args() {
 240        test_expectation="test_$1"
 241        shift
 242        if test "$1" = test_must_fail
 243        then
 244                full_command="test_must_fail test_terminal "
 245                shift
 246        else
 247                full_command="test_terminal "
 248        fi
 249        cmd=$1
 250        full_command="$full_command $1"
 251}
 252
 253test_default_pager() {
 254        parse_args "$@"
 255
 256        $test_expectation SIMPLEPAGERTTY "$cmd - default pager is used by default" "
 257                unset PAGER GIT_PAGER;
 258                test_might_fail git config --unset core.pager &&
 259                rm -f default_pager_used ||
 260                cleanup_fail &&
 261
 262                cat >\$less <<-\EOF &&
 263                #!/bin/sh
 264                wc >default_pager_used
 265                EOF
 266                chmod +x \$less &&
 267                (
 268                        PATH=.:\$PATH &&
 269                        export PATH &&
 270                        $full_command
 271                ) &&
 272                test -e default_pager_used
 273        "
 274}
 275
 276test_PAGER_overrides() {
 277        parse_args "$@"
 278
 279        $test_expectation TTY "$cmd - PAGER overrides default pager" "
 280                unset GIT_PAGER;
 281                test_might_fail git config --unset core.pager &&
 282                rm -f PAGER_used ||
 283                cleanup_fail &&
 284
 285                PAGER='wc >PAGER_used' &&
 286                export PAGER &&
 287                $full_command &&
 288                test -e PAGER_used
 289        "
 290}
 291
 292test_core_pager_overrides() {
 293        if_local_config=
 294        used_if_wanted='overrides PAGER'
 295        test_core_pager "$@"
 296}
 297
 298test_local_config_ignored() {
 299        if_local_config='! '
 300        used_if_wanted='is not used'
 301        test_core_pager "$@"
 302}
 303
 304test_core_pager() {
 305        parse_args "$@"
 306
 307        $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
 308                unset GIT_PAGER;
 309                rm -f core.pager_used ||
 310                cleanup_fail &&
 311
 312                PAGER=wc &&
 313                export PAGER &&
 314                git config core.pager 'wc >core.pager_used' &&
 315                $full_command &&
 316                ${if_local_config}test -e core.pager_used
 317        "
 318}
 319
 320test_core_pager_subdir() {
 321        if_local_config=
 322        used_if_wanted='overrides PAGER'
 323        test_pager_subdir_helper "$@"
 324}
 325
 326test_no_local_config_subdir() {
 327        if_local_config='! '
 328        used_if_wanted='is not used'
 329        test_pager_subdir_helper "$@"
 330}
 331
 332test_pager_subdir_helper() {
 333        parse_args "$@"
 334
 335        $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
 336                unset GIT_PAGER;
 337                rm -f core.pager_used &&
 338                rm -fr sub ||
 339                cleanup_fail &&
 340
 341                PAGER=wc &&
 342                stampname=\$(pwd)/core.pager_used &&
 343                export PAGER stampname &&
 344                git config core.pager 'wc >\"\$stampname\"' &&
 345                mkdir sub &&
 346                (
 347                        cd sub &&
 348                        $full_command
 349                ) &&
 350                ${if_local_config}test -e core.pager_used
 351        "
 352}
 353
 354test_GIT_PAGER_overrides() {
 355        parse_args "$@"
 356
 357        $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
 358                rm -f GIT_PAGER_used ||
 359                cleanup_fail &&
 360
 361                git config core.pager wc &&
 362                GIT_PAGER='wc >GIT_PAGER_used' &&
 363                export GIT_PAGER &&
 364                $full_command &&
 365                test -e GIT_PAGER_used
 366        "
 367}
 368
 369test_doesnt_paginate() {
 370        parse_args "$@"
 371
 372        $test_expectation TTY "no pager for '$cmd'" "
 373                rm -f GIT_PAGER_used ||
 374                cleanup_fail &&
 375
 376                GIT_PAGER='wc >GIT_PAGER_used' &&
 377                export GIT_PAGER &&
 378                $full_command &&
 379                ! test -e GIT_PAGER_used
 380        "
 381}
 382
 383test_pager_choices() {
 384        test_default_pager        expect_success "$@"
 385        test_PAGER_overrides      expect_success "$@"
 386        test_core_pager_overrides expect_success "$@"
 387        test_core_pager_subdir    expect_success "$@"
 388        test_GIT_PAGER_overrides  expect_success "$@"
 389}
 390
 391test_expect_success 'setup: some aliases' '
 392        git config alias.aliasedlog log &&
 393        git config alias.true "!true"
 394'
 395
 396test_pager_choices                       'git log'
 397test_pager_choices                       'git -p log'
 398test_pager_choices                       'git aliasedlog'
 399
 400test_default_pager        expect_success 'git -p aliasedlog'
 401test_PAGER_overrides      expect_success 'git -p aliasedlog'
 402test_core_pager_overrides expect_success 'git -p aliasedlog'
 403test_core_pager_subdir    expect_failure 'git -p aliasedlog'
 404test_GIT_PAGER_overrides  expect_success 'git -p aliasedlog'
 405
 406test_default_pager        expect_success 'git -p true'
 407test_PAGER_overrides      expect_success 'git -p true'
 408test_core_pager_overrides expect_success 'git -p true'
 409test_core_pager_subdir    expect_failure 'git -p true'
 410test_GIT_PAGER_overrides  expect_success 'git -p true'
 411
 412test_default_pager        expect_success test_must_fail 'git -p request-pull'
 413test_PAGER_overrides      expect_success test_must_fail 'git -p request-pull'
 414test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
 415test_core_pager_subdir    expect_failure test_must_fail 'git -p request-pull'
 416test_GIT_PAGER_overrides  expect_success test_must_fail 'git -p request-pull'
 417
 418test_default_pager        expect_success test_must_fail 'git -p'
 419test_PAGER_overrides      expect_success test_must_fail 'git -p'
 420test_local_config_ignored expect_failure test_must_fail 'git -p'
 421test_no_local_config_subdir expect_success test_must_fail 'git -p'
 422test_GIT_PAGER_overrides  expect_success test_must_fail 'git -p'
 423
 424test_doesnt_paginate      expect_failure test_must_fail 'git -p nonsense'
 425
 426test_pager_choices                       'git shortlog'
 427test_expect_success 'setup: configure shortlog not to paginate' '
 428        git config pager.shortlog false
 429'
 430test_doesnt_paginate      expect_success 'git shortlog'
 431test_no_local_config_subdir expect_success 'git shortlog'
 432test_default_pager        expect_success 'git -p shortlog'
 433test_core_pager_subdir    expect_success 'git -p shortlog'
 434
 435test_core_pager_subdir    expect_success test_must_fail \
 436                                         'git -p apply </dev/null'
 437
 438test_done