t / t0061-run-command.shon commit t4208: abstract away SHA-1-specific constants (0b2c4af)
   1#!/bin/sh
   2#
   3# Copyright (c) 2009 Ilari Liusvaara
   4#
   5
   6test_description='Test run command'
   7
   8. ./test-lib.sh
   9
  10cat >hello-script <<-EOF
  11        #!$SHELL_PATH
  12        cat hello-script
  13EOF
  14>empty
  15
  16test_expect_success 'start_command reports ENOENT' '
  17        test-tool run-command start-command-ENOENT ./does-not-exist
  18'
  19
  20test_expect_success 'run_command can run a command' '
  21        cat hello-script >hello.sh &&
  22        chmod +x hello.sh &&
  23        test-tool run-command run-command ./hello.sh >actual 2>err &&
  24
  25        test_cmp hello-script actual &&
  26        test_cmp empty err
  27'
  28
  29test_expect_success !MINGW 'run_command can run a script without a #! line' '
  30        cat >hello <<-\EOF &&
  31        cat hello-script
  32        EOF
  33        chmod +x hello &&
  34        test-tool run-command run-command ./hello >actual 2>err &&
  35
  36        test_cmp hello-script actual &&
  37        test_cmp empty err
  38'
  39
  40test_expect_success 'run_command does not try to execute a directory' '
  41        test_when_finished "rm -rf bin1 bin2" &&
  42        mkdir -p bin1/greet bin2 &&
  43        write_script bin2/greet <<-\EOF &&
  44        cat bin2/greet
  45        EOF
  46
  47        PATH=$PWD/bin1:$PWD/bin2:$PATH \
  48                test-tool run-command run-command greet >actual 2>err &&
  49        test_cmp bin2/greet actual &&
  50        test_cmp empty err
  51'
  52
  53test_expect_success POSIXPERM 'run_command passes over non-executable file' '
  54        test_when_finished "rm -rf bin1 bin2" &&
  55        mkdir -p bin1 bin2 &&
  56        write_script bin1/greet <<-\EOF &&
  57        cat bin1/greet
  58        EOF
  59        chmod -x bin1/greet &&
  60        write_script bin2/greet <<-\EOF &&
  61        cat bin2/greet
  62        EOF
  63
  64        PATH=$PWD/bin1:$PWD/bin2:$PATH \
  65                test-tool run-command run-command greet >actual 2>err &&
  66        test_cmp bin2/greet actual &&
  67        test_cmp empty err
  68'
  69
  70test_expect_success POSIXPERM 'run_command reports EACCES' '
  71        cat hello-script >hello.sh &&
  72        chmod -x hello.sh &&
  73        test_must_fail test-tool run-command run-command ./hello.sh 2>err &&
  74
  75        grep "fatal: cannot exec.*hello.sh" err
  76'
  77
  78test_expect_success POSIXPERM,SANITY 'unreadable directory in PATH' '
  79        mkdir local-command &&
  80        test_when_finished "chmod u+rwx local-command && rm -fr local-command" &&
  81        git config alias.nitfol "!echo frotz" &&
  82        chmod a-rx local-command &&
  83        (
  84                PATH=./local-command:$PATH &&
  85                git nitfol >actual
  86        ) &&
  87        echo frotz >expect &&
  88        test_cmp expect actual
  89'
  90
  91cat >expect <<-EOF
  92preloaded output of a child
  93Hello
  94World
  95preloaded output of a child
  96Hello
  97World
  98preloaded output of a child
  99Hello
 100World
 101preloaded output of a child
 102Hello
 103World
 104EOF
 105
 106test_expect_success 'run_command runs in parallel with more jobs available than tasks' '
 107        test-tool run-command run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
 108        test_cmp expect actual
 109'
 110
 111test_expect_success 'run_command runs in parallel with as many jobs as tasks' '
 112        test-tool run-command run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
 113        test_cmp expect actual
 114'
 115
 116test_expect_success 'run_command runs in parallel with more tasks than jobs available' '
 117        test-tool run-command run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
 118        test_cmp expect actual
 119'
 120
 121cat >expect <<-EOF
 122preloaded output of a child
 123asking for a quick stop
 124preloaded output of a child
 125asking for a quick stop
 126preloaded output of a child
 127asking for a quick stop
 128EOF
 129
 130test_expect_success 'run_command is asked to abort gracefully' '
 131        test-tool run-command run-command-abort 3 false 2>actual &&
 132        test_cmp expect actual
 133'
 134
 135cat >expect <<-EOF
 136no further jobs available
 137EOF
 138
 139test_expect_success 'run_command outputs ' '
 140        test-tool run-command run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" 2>actual &&
 141        test_cmp expect actual
 142'
 143
 144test_trace () {
 145        expect="$1"
 146        shift
 147        GIT_TRACE=1 test-tool run-command "$@" run-command true 2>&1 >/dev/null | \
 148                sed -e 's/.* run_command: //' -e '/trace: .*/d' >actual &&
 149        echo "$expect true" >expect &&
 150        test_cmp expect actual
 151}
 152
 153test_expect_success 'GIT_TRACE with environment variables' '
 154        test_trace "abc=1 def=2" env abc=1 env def=2 &&
 155        test_trace "abc=2" env abc env abc=1 env abc=2 &&
 156        test_trace "abc=2" env abc env abc=2 &&
 157        (
 158                abc=1 && export abc &&
 159                test_trace "def=1" env abc=1 env def=1
 160        ) &&
 161        (
 162                abc=1 && export abc &&
 163                test_trace "def=1" env abc env abc=1 env def=1
 164        ) &&
 165        test_trace "def=1" env non-exist env def=1 &&
 166        test_trace "abc=2" env abc=1 env abc env abc=2 &&
 167        (
 168                abc=1 def=2 && export abc def &&
 169                test_trace "unset abc def;" env abc env def
 170        ) &&
 171        (
 172                abc=1 def=2 && export abc def &&
 173                test_trace "unset def; abc=3" env abc env def env abc=3
 174        ) &&
 175        (
 176                abc=1 && export abc &&
 177                test_trace "unset abc;" env abc=2 env abc
 178        )
 179'
 180
 181test_done