56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package platform
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
type gitFakeRunner struct {
|
|
calls [][]string
|
|
err error
|
|
}
|
|
|
|
func (f *gitFakeRunner) Run(_ context.Context, name string, args ...string) error {
|
|
f.calls = append(f.calls, append([]string{name}, args...))
|
|
return f.err
|
|
}
|
|
func (f *gitFakeRunner) Output(_ context.Context, name string, args ...string) ([]byte, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func TestGitEnsureRepoClonesWhenMissing(t *testing.T) {
|
|
r := &gitFakeRunner{}
|
|
g := GitClient{Runner: r}
|
|
dest := filepath.Join(t.TempDir(), "repo")
|
|
if err := g.EnsureRepo(context.Background(), "https://x/y.git", dest); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(r.calls) != 1 {
|
|
t.Fatalf("want 1 call, got %d: %v", len(r.calls), r.calls)
|
|
}
|
|
got := r.calls[0]
|
|
want := []string{"git", "clone", "--depth=1", "https://x/y.git", dest}
|
|
for i := range want {
|
|
if i >= len(got) || got[i] != want[i] {
|
|
t.Fatalf("call = %v, want %v", got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGitEnsureRepoSkipsWhenPresent(t *testing.T) {
|
|
dest := t.TempDir()
|
|
if err := os.MkdirAll(filepath.Join(dest, ".git"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := &gitFakeRunner{}
|
|
g := GitClient{Runner: r}
|
|
if err := g.EnsureRepo(context.Background(), "https://x/y.git", dest); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(r.calls) != 0 {
|
|
t.Fatalf("expected no calls, got %v", r.calls)
|
|
}
|
|
}
|