1
2
3
4
5 package vcs
6
7 import (
8 "reflect"
9 "strings"
10 "testing"
11 )
12
13 var parseMetaGoImportsTests = []struct {
14 in string
15 mod ModuleMode
16 out []metaImport
17 }{
18 {
19 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
20 IgnoreMod,
21 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar", ""}},
22 },
23 {
24 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
25 <meta name="go-import" content="baz/quux git http://github.com/rsc/baz/quux">`,
26 IgnoreMod,
27 []metaImport{
28 {"foo/bar", "git", "https://github.com/rsc/foo/bar", ""},
29 {"baz/quux", "git", "http://github.com/rsc/baz/quux", ""},
30 },
31 },
32 {
33 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
34 <meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">`,
35 IgnoreMod,
36 []metaImport{
37 {"foo/bar", "git", "https://github.com/rsc/foo/bar", ""},
38 },
39 },
40 {
41 `<meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">
42 <meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
43 IgnoreMod,
44 []metaImport{
45 {"foo/bar", "git", "https://github.com/rsc/foo/bar", ""},
46 },
47 },
48 {
49 `<meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">
50 <meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
51 PreferMod,
52 []metaImport{
53 {"foo/bar", "mod", "http://github.com/rsc/baz/quux", ""},
54 },
55 },
56 {
57 `<head>
58 <meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
59 </head>`,
60 IgnoreMod,
61 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar", ""}},
62 },
63 {
64 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
65 <body>`,
66 IgnoreMod,
67 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar", ""}},
68 },
69 {
70 `<!doctype html><meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
71 IgnoreMod,
72 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar", ""}},
73 },
74 {
75
76 `<!doctype html><title>Page Not Found</title><meta name=go-import content="chitin.io/chitin git https://github.com/chitin-io/chitin"><div style=position:relative>DRAFT</div>`,
77 IgnoreMod,
78 []metaImport{{"chitin.io/chitin", "git", "https://github.com/chitin-io/chitin", ""}},
79 },
80 {
81 `<meta name="go-import" content="myitcv.io git https://github.com/myitcv/x">
82 <meta name="go-import" content="myitcv.io/blah2 mod https://raw.githubusercontent.com/myitcv/pubx/master">
83 `,
84 IgnoreMod,
85 []metaImport{{"myitcv.io", "git", "https://github.com/myitcv/x", ""}},
86 },
87 {
88 `<meta name="go-import" content="myitcv.io git https://github.com/myitcv/x">
89 <meta name="go-import" content="myitcv.io/blah2 mod https://raw.githubusercontent.com/myitcv/pubx/master">
90 `,
91 PreferMod,
92 []metaImport{
93 {"myitcv.io/blah2", "mod", "https://raw.githubusercontent.com/myitcv/pubx/master", ""},
94 {"myitcv.io", "git", "https://github.com/myitcv/x", ""},
95 },
96 },
97 {
98 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar subdir">`,
99 IgnoreMod,
100 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar", "subdir"}},
101 },
102 {
103 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar subdir/path">`,
104 IgnoreMod,
105 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar", "subdir/path"}},
106 },
107 }
108
109 func TestParseMetaGoImports(t *testing.T) {
110 for i, tt := range parseMetaGoImportsTests {
111 out, err := parseMetaGoImports(strings.NewReader(tt.in), tt.mod)
112 if err != nil {
113 t.Errorf("test#%d: %v", i, err)
114 continue
115 }
116 if !reflect.DeepEqual(out, tt.out) {
117 t.Errorf("test#%d:\n\thave %q\n\twant %q", i, out, tt.out)
118 }
119 }
120 }
121
View as plain text