From 6b6fe8b43ee71e52141ad762a38339688278f66f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 10 Jun 2023 16:51:12 +0200 Subject: [PATCH 1/2] t1800: loosen matching of error message for bad shebang MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit t1800.16 checks whether an attempt to run a hook script with a missing executable in its #! line fails and reports that error. The expected error message differs between platforms. The test handles two common variants, but on NonStop OS we get a third one: "fatal: cannot exec 'bad-hooks/test-hook': ...", which causes the test to fail there. We don't really care about the specific message text all that much here. Use grep and a single regex with alternations to ascertain that we get an error message (fatal or otherwise) about the failed invocation of the hook, but don't bother checking if we get the right variant for the platform the test is running on or whether quoting is done. This looser check let's the test pass on NonStop OS. Reported-by: Randall S. Becker Signed-off-by: René Scharfe Signed-off-by: Junio C Hamano --- t/t1800-hook.sh | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/t/t1800-hook.sh b/t/t1800-hook.sh index 3506f627b6..c156d6decc 100755 --- a/t/t1800-hook.sh +++ b/t/t1800-hook.sh @@ -156,25 +156,15 @@ test_expect_success 'git hook run a hook with a bad shebang' ' mkdir bad-hooks && write_script bad-hooks/test-hook "/bad/path/no/spaces" expect <<-\EOF - fatal: cannot run bad-hooks/test-hook: ... - EOF - else - cat >expect <<-\EOF - error: cannot spawn bad-hooks/test-hook: ... - EOF - fi && test_expect_code 1 git \ -c core.hooksPath=bad-hooks \ hook run test-hook >out 2>err && test_must_be_empty out && - sed -e "s/test-hook: .*/test-hook: .../" actual && - test_cmp expect actual + + # TODO: We should emit the same (or at least a more similar) + # error on MINGW (essentially Git for Windows) and all other + # platforms.. See the OS-specific code in start_command() + grep -E "^(error|fatal): cannot (exec|run|spawn) .*bad-hooks/test-hook" err ' test_expect_success 'stdin to hooks' ' From 6d224ac286d62bb3a10c3697f220b0c10d4b5f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 10 Jun 2023 16:51:21 +0200 Subject: [PATCH 2/2] run-command: report exec error even on ENOENT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If execve(2) fails with ENOENT and we report the error, we use the format "cannot run %s", followed by the actual error message. For other errors we use "cannot exec '%s'". Stop making this subtle distinction and use the second format for all execve(2) errors. This simplifies the code and makes the prefix more precise by indicating the failed operation. It also allows us to slightly simplify t1800.16. On Windows -- which lacks execve(2) -- we already use a single format in all cases: "cannot spawn %s". Signed-off-by: René Scharfe Signed-off-by: Junio C Hamano --- run-command.c | 14 +++----------- t/t1800-hook.sh | 2 +- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/run-command.c b/run-command.c index 6bd16acb06..ea192882fb 100644 --- a/run-command.c +++ b/run-command.c @@ -301,7 +301,6 @@ enum child_errcode { CHILD_ERR_DUP2, CHILD_ERR_CLOSE, CHILD_ERR_SIGPROCMASK, - CHILD_ERR_ENOENT, CHILD_ERR_SILENT, CHILD_ERR_ERRNO }; @@ -384,9 +383,6 @@ static void child_err_spew(struct child_process *cmd, struct child_err *cerr) case CHILD_ERR_SIGPROCMASK: error_errno("sigprocmask failed restoring signals"); break; - case CHILD_ERR_ENOENT: - error_errno("cannot run %s", cmd->args.v[0]); - break; case CHILD_ERR_SILENT: break; case CHILD_ERR_ERRNO: @@ -840,13 +836,9 @@ fail_pipe: execve(argv.v[0], (char *const *) argv.v, (char *const *) childenv); - if (errno == ENOENT) { - if (cmd->silent_exec_failure) - child_die(CHILD_ERR_SILENT); - child_die(CHILD_ERR_ENOENT); - } else { - child_die(CHILD_ERR_ERRNO); - } + if (cmd->silent_exec_failure && errno == ENOENT) + child_die(CHILD_ERR_SILENT); + child_die(CHILD_ERR_ERRNO); } atfork_parent(&as); if (cmd->pid < 0) diff --git a/t/t1800-hook.sh b/t/t1800-hook.sh index c156d6decc..8b0234cf2d 100755 --- a/t/t1800-hook.sh +++ b/t/t1800-hook.sh @@ -164,7 +164,7 @@ test_expect_success 'git hook run a hook with a bad shebang' ' # TODO: We should emit the same (or at least a more similar) # error on MINGW (essentially Git for Windows) and all other # platforms.. See the OS-specific code in start_command() - grep -E "^(error|fatal): cannot (exec|run|spawn) .*bad-hooks/test-hook" err + grep -E "^(error|fatal): cannot (exec|spawn) .*bad-hooks/test-hook" err ' test_expect_success 'stdin to hooks' '