Skip to content

Commit

Permalink
x/tools: address review of CL 564515 (CombinedOutput)
Browse files Browse the repository at this point in the history
I fumbled with git and forked the CL, stranding some review
comments from bcmills on the ill-fated fork.

Updates golang/go#65729

Change-Id: I6d0bf431f841dacb94e9e13a90bf39f8e2ed2fbf
Reviewed-on: https://go-review.googlesource.com/c/tools/+/564339
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
adonovan authored and gopherbot committed Feb 16, 2024
1 parent 4bc74c3 commit 451218f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
3 changes: 3 additions & 0 deletions go/internal/cgo/cgo_pkgconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func pkgConfig(mode string, pkgs []string) (flags []string, err error) {
if len(out) > 0 {
s = fmt.Sprintf("%s: %s", s, out)
}
if err, ok := err.(*exec.ExitError); ok && len(err.Stderr) > 0 {
s = fmt.Sprintf("%s\nstderr:\n%s", s, err.Stderr)
}
return nil, errors.New(s)
}
if len(out) > 0 {
Expand Down
3 changes: 3 additions & 0 deletions go/internal/gccgoimporter/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func TestObjImporter(t *testing.T) {
verout, err := exec.Command(gpath, "--version").Output()
if err != nil {
t.Logf("%s", verout)
if exit, ok := err.(*exec.ExitError); ok && len(exit.Stderr) > 0 {
t.Logf("stderr:\n%s", exit.Stderr)
}
t.Fatal(err)
}
vers := regexp.MustCompile(`([0-9]+)\.([0-9]+)`).FindSubmatch(verout)
Expand Down
10 changes: 8 additions & 2 deletions internal/diff/difftest/difftest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,15 @@ func getDiffOutput(a, b string) (string, error) {
cmd.Env = append(cmd.Env, "LANG=en_US.UTF-8")
out, err := cmd.Output()
if err != nil {
if _, ok := err.(*exec.ExitError); !ok {
return "", fmt.Errorf("failed to run diff -u %v %v: %v\n%v", fileA.Name(), fileB.Name(), err, string(out))
exit, ok := err.(*exec.ExitError)
if !ok {
return "", fmt.Errorf("can't exec %s: %v", cmd, err)
}
if len(out) == 0 {
// Nonzero exit with no output: terminated by signal?
return "", fmt.Errorf("%s failed: %v; stderr:\n%s", cmd, err, exit.Stderr)
}
// nonzero exit + output => files differ
}
diff := string(out)
if len(diff) <= 0 {
Expand Down
15 changes: 12 additions & 3 deletions internal/testenv/testenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func hasTool(tool string) error {
// certainly not what the user intended.
out, err := exec.Command(tool, "env", "GOROOT").Output()
if err != nil {
if exit, ok := err.(*exec.ExitError); ok && len(exit.Stderr) > 0 {
err = fmt.Errorf("%w\nstderr:\n%s)", err, exit.Stderr)
}
checkGoBuild.err = err
return
}
Expand Down Expand Up @@ -143,6 +146,9 @@ func cgoEnabled(bypassEnvironment bool) (bool, error) {
}
out, err := cmd.Output()
if err != nil {
if exit, ok := err.(*exec.ExitError); ok && len(exit.Stderr) > 0 {
err = fmt.Errorf("%w\nstderr:\n%s", err, exit.Stderr)
}
return false, err
}
enabled := strings.TrimSpace(string(out))
Expand Down Expand Up @@ -199,9 +205,12 @@ func NeedsTool(t testing.TB, tool string) {

t.Helper()
if allowMissingTool(tool) {
// TODO(adonovan): might silently skipping because of
// (e.g.) mismatched go env GOROOT and runtime.GOROOT
// risk some users not getting the coverage they expect?
// TODO(adonovan): if we skip because of (e.g.)
// mismatched go env GOROOT and runtime.GOROOT, don't
// we risk some users not getting the coverage they expect?
// bcmills notes: this shouldn't be a concern as of CL 404134 (Go 1.19).
// We could probably safely get rid of that GOPATH consistency
// check entirely at this point.
t.Skipf("skipping because %s tool not available: %v", tool, err)
} else {
t.Fatalf("%s tool not available: %v", tool, err)
Expand Down
3 changes: 3 additions & 0 deletions present/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package present

import (
"bytes"
"fmt"
"html/template"
"os"
"os/exec"
Expand Down Expand Up @@ -84,6 +85,8 @@ func diff(prefix string, name1 string, b1 []byte, name2 string, b2 []byte) ([]by
// diff exits with a non-zero status when the files don't match.
// Ignore that failure as long as we get output.
err = nil
} else if exit, ok := err.(*exec.ExitError); ok && len(exit.Stderr) > 0 {
err = fmt.Errorf("%w\nstderr:\n%s)", err, exit.Stderr)
}

data = bytes.Replace(data, []byte(f1), []byte(name1), -1)
Expand Down
3 changes: 3 additions & 0 deletions refactor/rename/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,9 @@ func diff(filename string, content []byte) error {
return nil
}
if err != nil {
if exit, ok := err.(*exec.ExitError); ok && len(exit.Stderr) > 0 {
err = fmt.Errorf("%w\nstderr:\n%s", err, exit.Stderr)
}
return fmt.Errorf("computing diff: %v", err)
}
return nil
Expand Down

0 comments on commit 451218f

Please sign in to comment.