Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(remote): add tests for git remote taskfiles #1894

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Comment on lines +1094 to +1096
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put this as a guard for now, but I would expect pretty much every CI and dev environment will have git installed. If you all agree, I will remove this guard to reduce noise in the code.


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",
Expand All @@ -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{
Expand All @@ -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)

Expand Down Expand Up @@ -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"

Expand Down
Loading