From bbbc2ff97594350ac5c9350c6e5bac2586856685 Mon Sep 17 00:00:00 2001 From: Dan Kirkwood Date: Thu, 19 Sep 2024 12:59:34 -0700 Subject: [PATCH] git clone output to Stderr --- pkg/fanal/artifact/repo/git.go | 2 +- pkg/fanal/artifact/repo/git_test.go | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/fanal/artifact/repo/git.go b/pkg/fanal/artifact/repo/git.go index 8eb3d9af7f9d..4532b631c6b7 100644 --- a/pkg/fanal/artifact/repo/git.go +++ b/pkg/fanal/artifact/repo/git.go @@ -128,7 +128,7 @@ func cloneRepo(u *url.URL, artifactOpt artifact.Option) (string, error) { cloneOptions := git.CloneOptions{ URL: u.String(), Auth: gitAuth(), - Progress: os.Stdout, + Progress: os.Stderr, InsecureSkipTLS: artifactOpt.Insecure, } diff --git a/pkg/fanal/artifact/repo/git_test.go b/pkg/fanal/artifact/repo/git_test.go index fbfbe39ff85f..1209414aeab3 100644 --- a/pkg/fanal/artifact/repo/git_test.go +++ b/pkg/fanal/artifact/repo/git_test.go @@ -4,7 +4,9 @@ package repo import ( "context" + "io" "net/http/httptest" + "os" "testing" "github.com/go-git/go-git/v5" @@ -36,6 +38,18 @@ func setupGitRepository(t *testing.T, repo, dir string) (*httptest.Server, *git. return gs, r } +// wrapNewArtifact captures stdout to test that `git clone` output is not mixed with formatted output +func wrapNewArtifact(target string, c cache.ArtifactCache, fs *walker.FS, opts artifact.Option) (func(), []byte, error) { + orig := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w + _, cleanup, err := NewArtifact(target, c, fs, opts) + os.Stdout = orig + w.Close() + out, _ := io.ReadAll(r) + return cleanup, out, err +} + func TestNewArtifact(t *testing.T) { ts, repo := setupGitRepository(t, "test-repo", "testdata/test-repo") defer ts.Close() @@ -169,13 +183,14 @@ func TestNewArtifact(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, cleanup, err := NewArtifact(tt.args.target, tt.args.c, walker.NewFS(), artifact.Option{ + cleanup, out, err := wrapNewArtifact(tt.args.target, tt.args.c, walker.NewFS(), artifact.Option{ NoProgress: tt.args.noProgress, RepoBranch: tt.args.repoBranch, RepoTag: tt.args.repoTag, RepoCommit: tt.args.repoCommit, }) tt.assertion(t, err) + assert.Empty(t, string(out)) defer cleanup() }) }