Skip to content

Commit 870946f

Browse files
committed
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.
1 parent fe09c01 commit 870946f

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

task_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import (
66
"fmt"
77
"io"
88
"io/fs"
9+
"log"
910
rand "math/rand/v2"
1011
"net/http"
12+
"net/http/cgi"
1113
"net/http/httptest"
1214
"os"
15+
"os/exec"
1316
"path/filepath"
1417
"regexp"
1518
"runtime"
@@ -1086,12 +1089,23 @@ func TestIncludesRemote(t *testing.T) {
10861089

10871090
dir := "testdata/includes_remote"
10881091

1089-
srv := httptest.NewServer(http.FileServer(http.Dir(dir)))
1092+
cwd, _ := os.Getwd()
1093+
gitHandler, gitSupported := createGitHTTPHandler(t, cwd)
1094+
if !gitSupported {
1095+
t.Log("git tests not supported and will be skipped")
1096+
}
1097+
1098+
mux := http.NewServeMux()
1099+
mux.Handle("/{path...}", http.FileServer(http.Dir(dir)))
1100+
mux.Handle("/repo.git/", http.StripPrefix("/repo.git", gitHandler))
1101+
1102+
srv := httptest.NewServer(mux)
10901103
defer srv.Close()
10911104

10921105
tcs := []struct {
10931106
firstRemote string
10941107
secondRemote string
1108+
skip bool
10951109
}{
10961110
{
10971111
firstRemote: srv.URL + "/first/Taskfile.yml",
@@ -1101,6 +1115,11 @@ func TestIncludesRemote(t *testing.T) {
11011115
firstRemote: srv.URL + "/first/Taskfile.yml",
11021116
secondRemote: "./second/Taskfile.yml",
11031117
},
1118+
{
1119+
firstRemote: srv.URL + "/repo.git//" + dir + "/first/Taskfile.yml?ref=main",
1120+
secondRemote: "./second/Taskfile.yml",
1121+
skip: !gitSupported,
1122+
},
11041123
}
11051124

11061125
tasks := []string{
@@ -1110,6 +1129,10 @@ func TestIncludesRemote(t *testing.T) {
11101129

11111130
for i, tc := range tcs {
11121131
t.Run(fmt.Sprint(i), func(t *testing.T) {
1132+
if tc.skip {
1133+
t.Skip()
1134+
}
1135+
11131136
t.Setenv("FIRST_REMOTE_URL", tc.firstRemote)
11141137
t.Setenv("SECOND_REMOTE_URL", tc.secondRemote)
11151138

@@ -1182,6 +1205,24 @@ func TestIncludesRemote(t *testing.T) {
11821205
}
11831206
}
11841207

1208+
func createGitHTTPHandler(t *testing.T, root string) (handler http.Handler, supported bool) {
1209+
executable, err := exec.LookPath("git")
1210+
if err != nil {
1211+
t.Log("git executable not found in PATH")
1212+
return nil, false
1213+
}
1214+
return &cgi.Handler{
1215+
Path: executable,
1216+
Args: []string{"http-backend"},
1217+
Logger: log.Default(),
1218+
Stderr: os.Stderr,
1219+
Env: []string{
1220+
"GIT_PROJECT_ROOT=" + root,
1221+
"GIT_HTTP_EXPORT_ALL=1",
1222+
},
1223+
}, true
1224+
}
1225+
11851226
func TestIncludeCycle(t *testing.T) {
11861227
const dir = "testdata/includes_cycle"
11871228

0 commit comments

Comments
 (0)