From 62959ffee41549e3bf7c44aeb21ce5e4888aafa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Va=C5=A1ek?= Date: Sat, 14 Sep 2024 12:55:31 +0200 Subject: [PATCH] [WIP] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej VaĊĦek --- cmd/repository_test.go | 2 +- pkg/functions/repositories_test.go | 2 +- pkg/testing/testing.go | 62 ++++++++++++++++++++++++------ 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/cmd/repository_test.go b/cmd/repository_test.go index 267924bc3..f2041b307 100644 --- a/cmd/repository_test.go +++ b/cmd/repository_test.go @@ -33,7 +33,7 @@ func TestRepository_List(t *testing.T) { // arguments, respects the repositories' path flag, and the expected name is echoed // upon subsequent 'list'. func TestRepository_Add(t *testing.T) { - url := ServeRepo("repository.git#main", t) + url := ServeRepo("repository.git", t) _ = FromTempDirectory(t) t.Log(url) diff --git a/pkg/functions/repositories_test.go b/pkg/functions/repositories_test.go index cbf5e40c1..64cd7d3c6 100644 --- a/pkg/functions/repositories_test.go +++ b/pkg/functions/repositories_test.go @@ -179,7 +179,7 @@ func TestRepositories_AddWithManifest(t *testing.T) { // defines a custom language pack and makes full use of the manifest.yaml. // The manifest.yaml is included which specifies things like custom templates // location and (appropos to this test) a default name/ - uri := ServeRepo("repository-a", t) // ./testdata/repository-a.git + uri := ServeRepo("repository-a.git", t) // ./testdata/repository-a.git root, rm := Mktemp(t) defer rm() diff --git a/pkg/testing/testing.go b/pkg/testing/testing.go index f953b7d26..5a420ccc2 100644 --- a/pkg/testing/testing.go +++ b/pkg/testing/testing.go @@ -17,6 +17,8 @@ package testing import ( "fmt" + "io" + "io/fs" "net" "net/http" "net/http/cgi" @@ -136,21 +138,57 @@ func cd(t *testing.T, dir string) { // such as fromTempDirectory(t) func ServeRepo(name string, t *testing.T) string { t.Helper() - var ( - path = filepath.Join("./testdata", name) - dir = filepath.Dir(path) - abs, _ = filepath.Abs(dir) - repo = filepath.Base(path) - url = RunGitServer(abs, t) - ) - // This is to prevent "fatal: detected dubious ownership in repository at " while executing - // unit tests on other environments (such as Prow CI) - cmd := exec.Command("git", "config", "--global", "--add", "safe.directory", abs) - _, err := cmd.CombinedOutput() + + srcDir := filepath.Join("./testdata", name) + gitRoot := t.TempDir() + + err := filepath.Walk(srcDir, func(path string, fi fs.FileInfo, err error) error { + if err != nil { + return err + } + + relp, err := filepath.Rel(srcDir, path) + if err != nil { + return fmt.Errorf("cannot get relpath: %v", err) + } + + dest := filepath.Join(gitRoot, name, relp) + + switch { + case fi.Mode().IsRegular(): + var in, out *os.File + in, err = os.Open(path) + if err != nil { + return fmt.Errorf("cannot open source file: %v", err) + } + defer in.Close() + out, err = os.OpenFile(dest, os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return fmt.Errorf("cannot open desitnation file: %v", err) + } + defer out.Close() + _, err = io.Copy(out, in) + if err != nil { + return fmt.Errorf("cannot copy data: %v", err) + } + case fi.IsDir(): + err = os.MkdirAll(dest, 0755) + if err != nil { + return fmt.Errorf("cannot mkdir: %v", err) + } + default: + return fmt.Errorf("unsupported file type") + } + + return nil + }) if err != nil { t.Fatal(err) } - return fmt.Sprintf("%v/%v", url, repo) + + url := RunGitServer(gitRoot, t) + + return fmt.Sprintf("%v/%v", url, name) } // WithExecutable creates an executable of the given name and source in a temp