github.com/sc0rp1us/gb@v0.4.1-0.20160319180011-4ba8cf1baa5a/vendor/imports_test.go (about) 1 package vendor 2 3 import ( 4 "bytes" 5 "io" 6 "os" 7 "path/filepath" 8 "reflect" 9 "testing" 10 ) 11 12 func TestParseImports(t *testing.T) { 13 root := filepath.Join(getwd(t), "_testdata", "src") 14 15 got, err := ParseImports(root) 16 if err != nil { 17 t.Fatalf("ParseImports(%q): %v", root, err) 18 } 19 20 want := set("fmt", "github.com/quux/flobble", "github.com/lypo/moopo", "github.com/hoo/wuu") 21 if !reflect.DeepEqual(got, want) { 22 t.Fatalf("ParseImports(%q): want: %v, got %v", root, want, got) 23 } 24 } 25 26 func TestFetchMetadata(t *testing.T) { 27 if testing.Short() { 28 t.Skipf("skipping network tests in -short mode") 29 } 30 type testParams struct { 31 path string 32 want string 33 insecure bool 34 } 35 tests := []testParams{{ 36 path: "golang.org/x/tools/cmd/godoc", 37 want: `<!DOCTYPE html> 38 <html> 39 <head> 40 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 41 <meta name="go-import" content="golang.org/x/tools git https://go.googlesource.com/tools"> 42 <meta name="go-source" content="golang.org/x/tools https://github.com/golang/tools/ https://github.com/golang/tools/tree/master{/dir} https://github.com/golang/tools/blob/master{/dir}/{file}#L{line}"> 43 <meta http-equiv="refresh" content="0; url=https://godoc.org/golang.org/x/tools/cmd/godoc"> 44 </head> 45 <body> 46 Nothing to see here; <a href="https://godoc.org/golang.org/x/tools/cmd/godoc">move along</a>. 47 </body> 48 </html> 49 `, 50 }, { 51 path: "gopkg.in/check.v1", 52 want: ` 53 <html> 54 <head> 55 <meta name="go-import" content="gopkg.in/check.v1 git https://gopkg.in/check.v1"> 56 <meta name="go-source" content="gopkg.in/check.v1 _ https://github.com/go-check/check/tree/v1{/dir} https://github.com/go-check/check/blob/v1{/dir}/{file}#L{line}"> 57 </head> 58 <body> 59 go get gopkg.in/check.v1 60 </body> 61 </html> 62 `, 63 }} 64 65 for _, tt := range tests { 66 r, err := FetchMetadata(tt.path, tt.insecure) 67 if err != nil { 68 t.Error(err) 69 continue 70 } 71 var buf bytes.Buffer 72 if _, err := io.Copy(&buf, r); err != nil { 73 t.Error(err) 74 r.Close() 75 continue 76 } 77 r.Close() 78 got := buf.String() 79 if got != tt.want { 80 t.Errorf("FetchMetadata(%q): want %q, got %q", tt.path, tt.want, got) 81 } 82 } 83 84 // Test for error catch. 85 errTests := []testParams{{ 86 path: "any.inaccessible.server/the.project", 87 want: `unable to determine remote metadata protocol: failed to access url "http://any.inaccessible.server/the.project?go-get=1"`, 88 insecure: true, 89 }, { 90 path: "any.inaccessible.server/the.project", 91 want: `unable to determine remote metadata protocol: failed to access url "https://any.inaccessible.server/the.project?go-get=1"`, 92 insecure: false, 93 }} 94 95 for _, ett := range errTests { 96 r, err := FetchMetadata(ett.path, ett.insecure) 97 if err == nil { 98 t.Errorf("Access to url %q without any error, but the error should be happen.", ett.path) 99 if r != nil { 100 r.Close() 101 } 102 continue 103 } 104 got := err.Error() 105 if got != ett.want { 106 t.Errorf("FetchMetadata(%q): want %q, got %q", ett.path, ett.want, got) 107 } 108 } 109 } 110 111 func TestParseMetadata(t *testing.T) { 112 if testing.Short() { 113 t.Skipf("skipping network tests in -short mode") 114 } 115 tests := []struct { 116 path string 117 importpath string 118 vcs string 119 reporoot string 120 insecure bool 121 err error 122 }{{ 123 path: "golang.org/x/tools/cmd/godoc", 124 importpath: "golang.org/x/tools", 125 vcs: "git", 126 reporoot: "https://go.googlesource.com/tools", 127 }, { 128 path: "gopkg.in/check.v1", 129 importpath: "gopkg.in/check.v1", 130 vcs: "git", 131 reporoot: "https://gopkg.in/check.v1", 132 }, { 133 path: "gopkg.in/mgo.v2/bson", 134 importpath: "gopkg.in/mgo.v2", 135 vcs: "git", 136 reporoot: "https://gopkg.in/mgo.v2", 137 // }, { 138 // path: "speter.net/go/exp", 139 // err: fmt.Errorf("go-import metadata not found"), 140 }} 141 142 for _, tt := range tests { 143 importpath, vcs, reporoot, err := ParseMetadata(tt.path, tt.insecure) 144 if !reflect.DeepEqual(err, tt.err) { 145 t.Error(err) 146 continue 147 } 148 if importpath != tt.importpath || vcs != tt.vcs || reporoot != tt.reporoot { 149 t.Errorf("ParseMetadata(%q): want %s %s %s, got %s %s %s ", tt.path, tt.importpath, tt.vcs, tt.reporoot, importpath, vcs, reporoot) 150 } 151 } 152 } 153 154 func getwd(t *testing.T) string { 155 cwd, err := os.Getwd() 156 if err != nil { 157 t.Fatal(err) 158 } 159 return cwd 160 }