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