0

I am trying to run go test command using the exec library as this.

output, err := exec.Command("bash", "-c", "go test -v -run TestX ./test/*").Output()
    if err != nil {
        log.GetLogger().Error(err.Error())
    }

It is failing with exit status 1. The command runs fine from the command line. What am I missing here?

techjourneyman
  • 1,701
  • 3
  • 33
  • 53
  • You should not be using shell globs to provide the arguments to the `go` command. If the commands fails, what is the error output? – JimB Apr 04 '22 at 23:05
  • I will be routing the stdout and stderr of the execution to a report by extending the command. – techjourneyman Apr 05 '22 at 01:00
  • Ok, so check the stderr output for why the command failed (running the command directly rather than through a shell may provide better feedback and fewer way to fail) – JimB Apr 05 '22 at 01:43
  • It just says `exit status 1`. The command does not fail when I execute it directly from the CLI. – techjourneyman Apr 05 '22 at 04:37
  • Maybe this can help https://stackoverflow.com/a/18177139/18012302 – Rahmat Fathoni Apr 05 '22 at 05:57
  • "exit status 1" is the exit error, not the stderr output. You can assign stderr like you mentioned you were going to do anyway, or use `CombinedOutput`, or use `*exec.ExitError.Stderr`, but there's probably more information. You are still likely to get better feedback executing the command directly, there's not really any need to involve a shell here. – JimB Apr 05 '22 at 15:53
  • Thanks, everyone! This worked: `exec.Command("/bin/sh", "-c", "go test -v -run TestX ./test/* > output.txt")` – techjourneyman Apr 06 '22 at 02:15

0 Answers0