Skip to content

Commit

Permalink
Printing stderr when exec.Command fails
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Dec 11, 2023
1 parent cfd28a4 commit de65f5f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
Expand Up @@ -37,9 +37,6 @@ func TestGenerate(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
if !binaryAvailable("git") {
t.Skip("skipping test because git is not available")
}

_ = klogtest.InitKlog(t)
klog.SetLogger(ktesting.NewLogger(t, ktesting.NewConfig()))
Expand Down Expand Up @@ -70,11 +67,6 @@ func TestGenerate(t *testing.T) {
}
}

func binaryAvailable(bin string) bool {
p, err := exec.LookPath(bin)
return err == nil && len(p) > 0
}

func prepareWorkdir(tb testing.TB) (string, string) {
var sourcedir string
if s, err := current.Dir(); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions staging/src/k8s.io/code-generator/pkg/osbin/git/find.go
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package git

import (
"errors"
"io"
"k8s.io/klog/v2"
"os"
Expand Down Expand Up @@ -58,6 +59,10 @@ func Find(pathspec string, opts ...FindOpt) ([]string, error) {
klog.V(3).Infof("Running: %q", cmd)
bytes, err := cmd.Output()
if err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) {
klog.Errorf("git ls-files stderr: %s", string(ee.Stderr))
}
return nil, err
}
out := strings.Trim(string(bytes), "\n\x00")
Expand Down
5 changes: 5 additions & 0 deletions staging/src/k8s.io/code-generator/pkg/osbin/git/grep.go
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package git

import (
"errors"
"io"
"k8s.io/klog/v2"
"os"
Expand Down Expand Up @@ -66,6 +67,10 @@ func Grep(pattern, pathspec string, opts ...GrepOpt) ([]string, error) {
klog.V(3).Infof("Running: %q", cmd)
bytes, err := cmd.Output()
if err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) {
klog.Errorf("git grep stderr: %s", string(ee.Stderr))
}
return nil, err
}
out := strings.Trim(string(bytes), "\n\x00")
Expand Down
Expand Up @@ -33,9 +33,10 @@ func PackageOf(path string) (string, error) {
klog.V(3).Infof("Running: %q", c)
out, err := c.Output()
if err != nil {
var exitErr *exec.ExitError
errors.As(err, &exitErr)
klog.Errorf("Stderr ==> %s", exitErr.Stderr)
var ee *exec.ExitError
if errors.As(err, &ee) {
klog.Errorf("go list stderr: %s", ee.Stderr)
}
return "", err
}
return strings.Trim(string(out), "\n"), nil
Expand Down

0 comments on commit de65f5f

Please sign in to comment.