Skip to content

Commit f67b980

Browse files
committed
t0061: adjust to test-tool transition
2 parents 268fbcd + 321fd82 commit f67b980

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

run-command.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ static void child_err_spew(struct child_process *cmd, struct child_err *cerr)
380380
set_error_routine(old_errfn);
381381
}
382382

383-
static void prepare_cmd(struct argv_array *out, const struct child_process *cmd)
383+
static int prepare_cmd(struct argv_array *out, const struct child_process *cmd)
384384
{
385385
if (!cmd->argv[0])
386386
BUG("command is empty");
@@ -403,16 +403,22 @@ static void prepare_cmd(struct argv_array *out, const struct child_process *cmd)
403403
/*
404404
* If there are no '/' characters in the command then perform a path
405405
* lookup and use the resolved path as the command to exec. If there
406-
* are no '/' characters or if the command wasn't found in the path,
407-
* have exec attempt to invoke the command directly.
406+
* are '/' characters, we have exec attempt to invoke the command
407+
* directly.
408408
*/
409409
if (!strchr(out->argv[1], '/')) {
410410
char *program = locate_in_PATH(out->argv[1]);
411411
if (program) {
412412
free((char *)out->argv[1]);
413413
out->argv[1] = program;
414+
} else {
415+
argv_array_clear(out);
416+
errno = ENOENT;
417+
return -1;
414418
}
415419
}
420+
421+
return 0;
416422
}
417423

418424
static char **prep_childenv(const char *const *deltaenv)
@@ -719,6 +725,12 @@ int start_command(struct child_process *cmd)
719725
struct child_err cerr;
720726
struct atfork_state as;
721727

728+
if (prepare_cmd(&argv, cmd) < 0) {
729+
failed_errno = errno;
730+
cmd->pid = -1;
731+
goto end_of_spawn;
732+
}
733+
722734
if (pipe(notify_pipe))
723735
notify_pipe[0] = notify_pipe[1] = -1;
724736

@@ -729,7 +741,6 @@ int start_command(struct child_process *cmd)
729741
set_cloexec(null_fd);
730742
}
731743

732-
prepare_cmd(&argv, cmd);
733744
childenv = prep_childenv(cmd->env);
734745
atfork_prepare(&as);
735746

@@ -857,6 +868,8 @@ int start_command(struct child_process *cmd)
857868
argv_array_clear(&argv);
858869
free(childenv);
859870
}
871+
end_of_spawn:
872+
860873
#else
861874
{
862875
int fhin = 0, fhout = 1, fherr = 2;

t/t0061-run-command.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ cat >hello-script <<-EOF
1313
EOF
1414
>empty
1515

16-
test_expect_success 'start_command reports ENOENT' '
16+
test_expect_success 'start_command reports ENOENT (slash)' '
1717
test-tool run-command start-command-ENOENT ./does-not-exist
1818
'
1919

20+
test_expect_success 'start_command reports ENOENT (no slash)' '
21+
test-tool run-command start-command-ENOENT does-not-exist
22+
'
23+
2024
test_expect_success 'run_command can run a command' '
2125
cat hello-script >hello.sh &&
2226
chmod +x hello.sh &&
@@ -26,6 +30,13 @@ test_expect_success 'run_command can run a command' '
2630
test_cmp empty err
2731
'
2832

33+
test_expect_success 'run_command is restricted to PATH' '
34+
write_script should-not-run <<-\EOF &&
35+
echo yikes
36+
EOF
37+
test_must_fail test-tool run-command run-command should-not-run
38+
'
39+
2940
test_expect_success !MINGW 'run_command can run a script without a #! line' '
3041
cat >hello <<-\EOF &&
3142
cat hello-script

0 commit comments

Comments
 (0)