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