From 870946f43622b896896f217b1a4275e8e11f4d5a Mon Sep 17 00:00:00 2001 From: Paulo Bittencourt Date: Tue, 29 Oct 2024 16:54:36 -0400 Subject: [PATCH 1/2] feat(remote): add tests for git remote taskfiles This uses a similar approach as the HTTP remote taskfile tests, leveraging the `git` tool's built-in support for hosting a local HTTP server via CGI. --- task_test.go | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/task_test.go b/task_test.go index 599277c8c5..24a6390ca8 100644 --- a/task_test.go +++ b/task_test.go @@ -6,10 +6,13 @@ import ( "fmt" "io" "io/fs" + "log" rand "math/rand/v2" "net/http" + "net/http/cgi" "net/http/httptest" "os" + "os/exec" "path/filepath" "regexp" "runtime" @@ -1086,12 +1089,23 @@ func TestIncludesRemote(t *testing.T) { dir := "testdata/includes_remote" - srv := httptest.NewServer(http.FileServer(http.Dir(dir))) + cwd, _ := os.Getwd() + gitHandler, gitSupported := createGitHTTPHandler(t, cwd) + if !gitSupported { + t.Log("git tests not supported and will be skipped") + } + + mux := http.NewServeMux() + mux.Handle("/{path...}", http.FileServer(http.Dir(dir))) + mux.Handle("/repo.git/", http.StripPrefix("/repo.git", gitHandler)) + + srv := httptest.NewServer(mux) defer srv.Close() tcs := []struct { firstRemote string secondRemote string + skip bool }{ { firstRemote: srv.URL + "/first/Taskfile.yml", @@ -1101,6 +1115,11 @@ func TestIncludesRemote(t *testing.T) { firstRemote: srv.URL + "/first/Taskfile.yml", secondRemote: "./second/Taskfile.yml", }, + { + firstRemote: srv.URL + "/repo.git//" + dir + "/first/Taskfile.yml?ref=main", + secondRemote: "./second/Taskfile.yml", + skip: !gitSupported, + }, } tasks := []string{ @@ -1110,6 +1129,10 @@ func TestIncludesRemote(t *testing.T) { for i, tc := range tcs { t.Run(fmt.Sprint(i), func(t *testing.T) { + if tc.skip { + t.Skip() + } + t.Setenv("FIRST_REMOTE_URL", tc.firstRemote) t.Setenv("SECOND_REMOTE_URL", tc.secondRemote) @@ -1182,6 +1205,24 @@ func TestIncludesRemote(t *testing.T) { } } +func createGitHTTPHandler(t *testing.T, root string) (handler http.Handler, supported bool) { + executable, err := exec.LookPath("git") + if err != nil { + t.Log("git executable not found in PATH") + return nil, false + } + return &cgi.Handler{ + Path: executable, + Args: []string{"http-backend"}, + Logger: log.Default(), + Stderr: os.Stderr, + Env: []string{ + "GIT_PROJECT_ROOT=" + root, + "GIT_HTTP_EXPORT_ALL=1", + }, + }, true +} + func TestIncludeCycle(t *testing.T) { const dir = "testdata/includes_cycle" From 61db810311ce8c2c19d46b0e111662d3b9a594fc Mon Sep 17 00:00:00 2001 From: Paulo Bittencourt Date: Tue, 29 Oct 2024 18:54:16 -0400 Subject: [PATCH 2/2] Try checking out HEAD --- task_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task_test.go b/task_test.go index 24a6390ca8..b74f8eadc8 100644 --- a/task_test.go +++ b/task_test.go @@ -1116,7 +1116,7 @@ func TestIncludesRemote(t *testing.T) { secondRemote: "./second/Taskfile.yml", }, { - firstRemote: srv.URL + "/repo.git//" + dir + "/first/Taskfile.yml?ref=main", + firstRemote: srv.URL + "/repo.git//" + dir + "/first/Taskfile.yml?ref=HEAD", secondRemote: "./second/Taskfile.yml", skip: !gitSupported, },