Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(repo): git clone output to Stderr #7561

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/fanal/artifact/repo/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
17 changes: 16 additions & 1 deletion pkg/fanal/artifact/repo/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package repo

import (
"context"
"io"
"net/http/httptest"
"os"
"testing"

"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
})
}
Expand Down
Loading