t / t7006-pager.shon commit bundle: run setup_git_directory_gently() sooner (2cb6009)
   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 'configuration can enable pager (from subdir)' '
 138        rm -f paginated.out &&
 139        mkdir -p subdir &&
 140        git config pager.bundle true &&
 141        test_when_finished "git config --unset pager.bundle" &&
 142
 143        git bundle create test.bundle --all &&
 144        rm -f paginated.out subdir/paginated.out &&
 145        (
 146                cd subdir &&
 147                test_terminal git bundle unbundle ../test.bundle
 148        ) &&
 149        {
 150                test -e paginated.out ||
 151                test -e subdir/paginated.out
 152        }
 153'
 154
 155# A colored commit log will begin with an appropriate ANSI escape
 156# for the first color; the text "commit" comes later.
 157colorful() {
 158        read firstline <$1
 159        ! expr "$firstline" : "[a-zA-Z]" >/dev/null
 160}
 161
 162test_expect_success 'tests can detect color' '
 163        rm -f colorful.log colorless.log ||
 164        cleanup_fail &&
 165
 166        git log --no-color >colorless.log &&
 167        git log --color >colorful.log &&
 168        ! colorful colorless.log &&
 169        colorful colorful.log
 170'
 171
 172test_expect_success 'no color when stdout is a regular file' '
 173        rm -f colorless.log &&
 174        git config color.ui auto ||
 175        cleanup_fail &&
 176
 177        git log >colorless.log &&
 178        ! colorful colorless.log
 179'
 180
 181test_expect_success TTY 'color when writing to a pager' '
 182        rm -f paginated.out &&
 183        git config color.ui auto ||
 184        cleanup_fail &&
 185
 186        (
 187                TERM=vt100 &&
 188                export TERM &&
 189                test_terminal git log
 190        ) &&
 191        colorful paginated.out
 192'
 193
 194test_expect_success 'color when writing to a file intended for a pager' '
 195        rm -f colorful.log &&
 196        git config color.ui auto ||
 197        cleanup_fail &&
 198
 199        (
 200                TERM=vt100 &&
 201                GIT_PAGER_IN_USE=true &&
 202                export TERM GIT_PAGER_IN_USE &&
 203                git log >colorful.log
 204        ) &&
 205        colorful colorful.log
 206'
 207
 208if test_have_prereq SIMPLEPAGER && test_have_prereq TTY
 209then
 210        test_set_prereq SIMPLEPAGERTTY
 211fi
 212
 213# Use this helper to make it easy for the caller of your
 214# terminal-using function to specify whether it should fail.
 215# If you write
 216#
 217#       your_test() {
 218#               parse_args "$@"
 219#
 220#               $test_expectation "$cmd - behaves well" "
 221#                       ...
 222#                       $full_command &&
 223#                       ...
 224#               "
 225#       }
 226#
 227# then your test can be used like this:
 228#
 229#       your_test expect_(success|failure) [test_must_fail] 'git foo'
 230#
 231parse_args() {
 232        test_expectation="test_$1"
 233        shift
 234        if test "$1" = test_must_fail
 235        then
 236                full_command="test_must_fail test_terminal "
 237                shift
 238        else
 239                full_command="test_terminal "
 240        fi
 241        cmd=$1
 242        full_command="$full_command $1"
 243}
 244
 245test_default_pager() {
 246        parse_args "$@"
 247
 248        $test_expectation SIMPLEPAGERTTY "$cmd - default pager is used by default" "
 249                unset PAGER GIT_PAGER;
 250                test_might_fail git config --unset core.pager &&
 251                rm -f default_pager_used ||
 252                cleanup_fail &&
 253
 254                cat >\$less <<-\EOF &&
 255                #!/bin/sh
 256                wc >default_pager_used
 257                EOF
 258                chmod +x \$less &&
 259                (
 260                        PATH=.:\$PATH &&
 261                        export PATH &&
 262                        $full_command
 263                ) &&
 264                test -e default_pager_used
 265        "
 266}
 267
 268test_PAGER_overrides() {
 269        parse_args "$@"
 270
 271        $test_expectation TTY "$cmd - PAGER overrides default pager" "
 272                unset GIT_PAGER;
 273                test_might_fail git config --unset core.pager &&
 274                rm -f PAGER_used ||
 275                cleanup_fail &&
 276
 277                PAGER='wc >PAGER_used' &&
 278                export PAGER &&
 279                $full_command &&
 280                test -e PAGER_used
 281        "
 282}
 283
 284test_core_pager_overrides() {
 285        if_local_config=
 286        used_if_wanted='overrides PAGER'
 287        test_core_pager "$@"
 288}
 289
 290test_local_config_ignored() {
 291        if_local_config='! '
 292        used_if_wanted='is not used'
 293        test_core_pager "$@"
 294}
 295
 296test_core_pager() {
 297        parse_args "$@"
 298
 299        $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
 300                unset GIT_PAGER;
 301                rm -f core.pager_used ||
 302                cleanup_fail &&
 303
 304                PAGER=wc &&
 305                export PAGER &&
 306                git config core.pager 'wc >core.pager_used' &&
 307                $full_command &&
 308                ${if_local_config}test -e core.pager_used
 309        "
 310}
 311
 312test_core_pager_subdir() {
 313        if_local_config=
 314        used_if_wanted='overrides PAGER'
 315        test_pager_subdir_helper "$@"
 316}
 317
 318test_no_local_config_subdir() {
 319        if_local_config='! '
 320        used_if_wanted='is not used'
 321        test_pager_subdir_helper "$@"
 322}
 323
 324test_pager_subdir_helper() {
 325        parse_args "$@"
 326
 327        $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
 328                unset GIT_PAGER;
 329                rm -f core.pager_used &&
 330                rm -fr sub ||
 331                cleanup_fail &&
 332
 333                PAGER=wc &&
 334                stampname=\$(pwd)/core.pager_used &&
 335                export PAGER stampname &&
 336                git config core.pager 'wc >\"\$stampname\"' &&
 337                mkdir sub &&
 338                (
 339                        cd sub &&
 340                        $full_command
 341                ) &&
 342                ${if_local_config}test -e core.pager_used
 343        "
 344}
 345
 346test_GIT_PAGER_overrides() {
 347        parse_args "$@"
 348
 349        $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
 350                rm -f GIT_PAGER_used ||
 351                cleanup_fail &&
 352
 353                git config core.pager wc &&
 354                GIT_PAGER='wc >GIT_PAGER_used' &&
 355                export GIT_PAGER &&
 356                $full_command &&
 357                test -e GIT_PAGER_used
 358        "
 359}
 360
 361test_doesnt_paginate() {
 362        parse_args "$@"
 363
 364        $test_expectation TTY "no pager for '$cmd'" "
 365                rm -f GIT_PAGER_used ||
 366                cleanup_fail &&
 367
 368                GIT_PAGER='wc >GIT_PAGER_used' &&
 369                export GIT_PAGER &&
 370                $full_command &&
 371                ! test -e GIT_PAGER_used
 372        "
 373}
 374
 375test_pager_choices() {
 376        test_default_pager        expect_success "$@"
 377        test_PAGER_overrides      expect_success "$@"
 378        test_core_pager_overrides expect_success "$@"
 379        test_core_pager_subdir    expect_success "$@"
 380        test_GIT_PAGER_overrides  expect_success "$@"
 381}
 382
 383test_expect_success 'setup: some aliases' '
 384        git config alias.aliasedlog log &&
 385        git config alias.true "!true"
 386'
 387
 388test_pager_choices                       'git log'
 389test_pager_choices                       'git -p log'
 390test_pager_choices                       'git aliasedlog'
 391
 392test_default_pager        expect_success 'git -p aliasedlog'
 393test_PAGER_overrides      expect_success 'git -p aliasedlog'
 394test_core_pager_overrides expect_success 'git -p aliasedlog'
 395test_core_pager_subdir    expect_failure 'git -p aliasedlog'
 396test_GIT_PAGER_overrides  expect_success 'git -p aliasedlog'
 397
 398test_default_pager        expect_success 'git -p true'
 399test_PAGER_overrides      expect_success 'git -p true'
 400test_core_pager_overrides expect_success 'git -p true'
 401test_core_pager_subdir    expect_failure 'git -p true'
 402test_GIT_PAGER_overrides  expect_success 'git -p true'
 403
 404test_default_pager        expect_success test_must_fail 'git -p request-pull'
 405test_PAGER_overrides      expect_success test_must_fail 'git -p request-pull'
 406test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
 407test_core_pager_subdir    expect_failure test_must_fail 'git -p request-pull'
 408test_GIT_PAGER_overrides  expect_success test_must_fail 'git -p request-pull'
 409
 410test_default_pager        expect_success test_must_fail 'git -p'
 411test_PAGER_overrides      expect_success test_must_fail 'git -p'
 412test_local_config_ignored expect_failure test_must_fail 'git -p'
 413test_no_local_config_subdir expect_success test_must_fail 'git -p'
 414test_GIT_PAGER_overrides  expect_success test_must_fail 'git -p'
 415
 416test_doesnt_paginate      expect_failure test_must_fail 'git -p nonsense'
 417
 418test_pager_choices                       'git shortlog'
 419test_expect_success 'setup: configure shortlog not to paginate' '
 420        git config pager.shortlog false
 421'
 422test_doesnt_paginate      expect_success 'git shortlog'
 423test_no_local_config_subdir expect_success 'git shortlog'
 424test_default_pager        expect_success 'git -p shortlog'
 425test_core_pager_subdir    expect_success 'git -p shortlog'
 426
 427test_core_pager_subdir    expect_success test_must_fail \
 428                                         'git -p apply </dev/null'
 429
 430test_done