Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Jul 4, 2024
1 parent 8f3a8e4 commit bc33eba
Showing 1 changed file with 67 additions and 41 deletions.
108 changes: 67 additions & 41 deletions attest/vcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,20 @@ func TestVCS(t *testing.T) {
},
}

repoCache := repoCache{}

tempDir := t.TempDir()
repos := &repos{}
repos.init()
defer repos.cleanup()

for _, tc := range testCases {
t.Run(tc.Name(), makeVCSTest(repoCache, tc, tempDir))
t.Run(tc.Name(), makeVCSTest(repos, tc))
}
}

func makeVCSTest(repoCache repoCache, tc vcsTestCase, tempDir string) func(t *testing.T) {
func makeVCSTest(repos *repos, tc vcsTestCase) func(t *testing.T) {
return func(t *testing.T) {
g := NewWithT(t)

tempDir := func() string {
tempDir, err := os.MkdirTemp(tempDir, "vcs-test-*")
g.Expect(err).NotTo(HaveOccurred())
return tempDir
}

checkoutPath, err := repoCache.clone(context.Background(), tc, tempDir)
checkoutPath, err := repos.clone(context.Background(), tc)
g.Expect(err).NotTo(HaveOccurred())

loadPath := filepath.Join(checkoutPath, tc.LoadPath)
Expand All @@ -94,26 +88,62 @@ func makeVCSTest(repoCache repoCache, tc vcsTestCase, tempDir string) func(t *te
}
}

type repoCache map[string]string
type repos struct {
workDir string
tempDir string
cache map[string]string
}

func (r *repos) init() error {
workDir, err := os.Getwd()
if err != nil {
return err
}
r.workDir = workDir
tempDir, err := os.MkdirTemp(workDir, ".vcs-test-*")
if err != nil {
return err
}
r.tempDir = tempDir
r.cache = map[string]string{}
return nil
}

func (r *repos) cleanup() error {
if r.tempDir == "" {
return nil
}
return os.RemoveAll(r.tempDir)
}

func (r *repos) mktemp() (string, error) {
return os.MkdirTemp(r.tempDir, "repo-*")
}

func (r repoCache) mirror(ctx context.Context, tc vcsTestCase, mktemp func() string) (string, error) {
if _, ok := r[tc.URL]; !ok {
mirrorDir := mktemp()
_, err := gogit.PlainCloneContext(ctx, mirrorDir, true, &gogit.CloneOptions{Mirror: true, URL: tc.URL})
func (r *repos) mirror(ctx context.Context, tc vcsTestCase) (string, error) {
if _, ok := r.cache[tc.URL]; !ok {
mirrorDir, err := r.mktemp()
if err != nil {
return "", err
}
r[tc.URL] = mirrorDir
_, err = gogit.PlainCloneContext(ctx, mirrorDir, true, &gogit.CloneOptions{Mirror: true, URL: tc.URL})
if err != nil {
return "", err
}
r.cache[tc.URL] = mirrorDir
}
return r[tc.URL], nil
return r.cache[tc.URL], nil
}

func (r repoCache) clone(ctx context.Context, tc vcsTestCase, mktemp func() string) (string, error) {
mirrorDir, err := r.mirror(ctx, tc, mktemp)
func (r *repos) clone(ctx context.Context, tc vcsTestCase) (string, error) {
mirrorDir, err := r.mirror(ctx, tc)
if err != nil {
return "", err
}
checkoutDir, err := r.mktemp()
if err != nil {
return "", err
}
checkoutDir := mktemp()

opts := &gogit.CloneOptions{URL: mirrorDir}
if tc.CheckoutTag != "" {
Expand All @@ -122,28 +152,24 @@ func (r repoCache) clone(ctx context.Context, tc vcsTestCase, mktemp func() stri
opts.ReferenceName = plumbing.NewBranchReferenceName(tc.Branch)
}

_, err = gogit.PlainCloneContext(ctx, checkoutDir, false, opts)
repo, err := gogit.PlainCloneContext(ctx, checkoutDir, false, opts)
if err != nil {
return "", fmt.Errorf("failed to clone: %w", err)
}

// if tc.CheckoutHash != "" {
// workTree, err := repo.Worktree()
// if err != nil {
// return "", err
// }
// opts := &gogit.CheckoutOptions{
// Hash: plumbing.NewHash(tc.CheckoutHash),
// Branch: plumbing.ReferenceName(tc.Branch),
// }

// if err := workTree.Checkout(opts); err != nil {
// return "", err
// }
// }
workDir, err := os.Getwd()
if err != nil {
return "", err
if tc.CheckoutHash != "" {
workTree, err := repo.Worktree()
if err != nil {
return "", err
}
opts := &gogit.CheckoutOptions{
Hash: plumbing.NewHash(tc.CheckoutHash),
//Branch: plumbing.ReferenceName(tc.Branch),
}

if err := workTree.Checkout(opts); err != nil {
return "", err
}
}
return filepath.Rel(workDir, checkoutDir)
return filepath.Rel(r.workDir, checkoutDir)
}

0 comments on commit bc33eba

Please sign in to comment.