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