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