From bc33eba29a684c5330c9fe579738723870630751 Mon Sep 17 00:00:00 2001 From: Ilya Dmitrichenko Date: Thu, 4 Jul 2024 10:13:44 +0100 Subject: [PATCH] WIP --- attest/vcs_test.go | 108 ++++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 41 deletions(-) diff --git a/attest/vcs_test.go b/attest/vcs_test.go index 54d08a9..8ad8667 100644 --- a/attest/vcs_test.go +++ b/attest/vcs_test.go @@ -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) @@ -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 != "" { @@ -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) }