add tests for meta import resolver
This commit is contained in:
parent
0140bf1319
commit
f67e23486c
@ -8,6 +8,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errPrefixDoesNotMatch = errors.New("prefix does not match the module")
|
||||||
|
errMetaNotFound = errors.New("go-import meta tag not found")
|
||||||
|
)
|
||||||
|
|
||||||
// MetaImports resolved module import path for certain hosts using the special <meta> tag.
|
// MetaImports resolved module import path for certain hosts using the special <meta> tag.
|
||||||
func MetaImports(ctx context.Context, module string) (string, error) {
|
func MetaImports(ctx context.Context, module string) (string, error) {
|
||||||
if strings.HasPrefix(module, "github.com/") || strings.HasPrefix(module, "bitbucket.org/") {
|
if strings.HasPrefix(module, "github.com/") || strings.HasPrefix(module, "bitbucket.org/") {
|
||||||
@ -39,7 +44,7 @@ func MetaImports(ctx context.Context, module string) (string, error) {
|
|||||||
if meta.Name == "go-import" {
|
if meta.Name == "go-import" {
|
||||||
if f := strings.Fields(meta.Content); len(f) == 3 {
|
if f := strings.Fields(meta.Content); len(f) == 3 {
|
||||||
if f[0] != module {
|
if f[0] != module {
|
||||||
return "", errors.New("prefix does not match the module")
|
return "", errPrefixDoesNotMatch
|
||||||
}
|
}
|
||||||
url := f[2]
|
url := f[2]
|
||||||
if i := strings.Index(url, "://"); i >= 0 {
|
if i := strings.Index(url, "://"); i >= 0 {
|
||||||
@ -49,5 +54,5 @@ func MetaImports(ctx context.Context, module string) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", errors.New("go-import meta tag not found")
|
return "", errMetaNotFound
|
||||||
}
|
}
|
||||||
|
70
pkg/vcs/meta_test.go
Normal file
70
pkg/vcs/meta_test.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package vcs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMetaImports(t *testing.T) {
|
||||||
|
var hostname string
|
||||||
|
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||||
|
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Query().Get("go-get") != "1" {
|
||||||
|
fmt.Fprint(w, `<!doctype html><html><body>Hello</body></html>`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, `<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
||||||
|
<meta name="go-import" content="%s git https://example.com%s">
|
||||||
|
</head>
|
||||||
|
<body></body>
|
||||||
|
</html>
|
||||||
|
`, hostname+r.URL.Path, r.URL.Path)
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
hostname = strings.TrimPrefix(ts.URL, "https://")
|
||||||
|
|
||||||
|
if url, err := MetaImports(context.Background(), hostname+"/foo/bar"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if url != "example.com/foo/bar" {
|
||||||
|
t.Fatal(url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMetaImportsExternal(t *testing.T) {
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip("testing with external VCS might be slow")
|
||||||
|
}
|
||||||
|
for _, test := range []struct {
|
||||||
|
Pkg string
|
||||||
|
URL string
|
||||||
|
}{
|
||||||
|
// Common VCS should be resolved immediately without any checks
|
||||||
|
{Pkg: "github.com/user/repo", URL: "github.com/user/repo"},
|
||||||
|
{Pkg: "bitbucket.org/user/repo", URL: "bitbucket.org/user/repo"},
|
||||||
|
// Otherwise, HTML meta tag should be checked
|
||||||
|
{Pkg: "golang.org/x/sys", URL: "go.googlesource.com/sys"},
|
||||||
|
{Pkg: "gopkg.in/warnings.v0", URL: "gopkg.in/warnings.v0"},
|
||||||
|
{Pkg: "gopkg.in/src-d/go-git.v4", URL: "gopkg.in/src-d/go-git.v4"},
|
||||||
|
// On errors URL should be empty and error should be not nil
|
||||||
|
{Pkg: "google.com/foo", URL: ""},
|
||||||
|
{Pkg: "golang.org/x/sys/unix", URL: ""},
|
||||||
|
{Pkg: "example.com/foo", URL: ""},
|
||||||
|
{Pkg: "foo/bar", URL: ""},
|
||||||
|
} {
|
||||||
|
url, err := MetaImports(context.Background(), test.Pkg)
|
||||||
|
if url != test.URL {
|
||||||
|
t.Fatal(test, url, err)
|
||||||
|
}
|
||||||
|
if url == "" && err == nil {
|
||||||
|
t.Fatal(test, "error should be set if module import can not be resolved")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user