Skip to content

Commit

Permalink
git clone output to Stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
dangogh committed Sep 19, 2024
1 parent 5dd94eb commit bbbc2ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
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

0 comments on commit bbbc2ff

Please sign in to comment.