github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/go/vcs/vcs_test.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package vcs 6 7 import ( 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "reflect" 12 "runtime" 13 "strings" 14 "testing" 15 ) 16 17 // Test that RepoRootForImportPath creates the correct RepoRoot for a given importPath. 18 // TODO(cmang): Add tests for SVN and BZR. 19 func TestRepoRootForImportPath(t *testing.T) { 20 if runtime.GOOS == "android" { 21 t.Skipf("incomplete source tree on %s", runtime.GOOS) 22 } 23 24 tests := []struct { 25 path string 26 want *RepoRoot 27 }{ 28 { 29 "code.google.com/p/go", 30 &RepoRoot{ 31 VCS: vcsHg, 32 Repo: "https://code.google.com/p/go", 33 }, 34 }, 35 { 36 "code.google.com/r/go", 37 &RepoRoot{ 38 VCS: vcsHg, 39 Repo: "https://code.google.com/r/go", 40 }, 41 }, 42 { 43 "github.com/golang/groupcache", 44 &RepoRoot{ 45 VCS: vcsGit, 46 Repo: "https://github.com/golang/groupcache", 47 }, 48 }, 49 } 50 51 for _, test := range tests { 52 got, err := RepoRootForImportPath(test.path, false) 53 if err != nil { 54 t.Errorf("RepoRootForImport(%q): %v", test.path, err) 55 continue 56 } 57 want := test.want 58 if got.VCS.Name != want.VCS.Name || got.Repo != want.Repo { 59 t.Errorf("RepoRootForImport(%q) = VCS(%s) Repo(%s), want VCS(%s) Repo(%s)", test.path, got.VCS, got.Repo, want.VCS, want.Repo) 60 } 61 } 62 } 63 64 // Test that FromDir correctly inspects a given directory and returns the right VCS. 65 func TestFromDir(t *testing.T) { 66 type testStruct struct { 67 path string 68 want *Cmd 69 } 70 71 tests := make([]testStruct, len(vcsList)) 72 tempDir, err := ioutil.TempDir("", "vcstest") 73 if err != nil { 74 t.Fatal(err) 75 } 76 defer os.RemoveAll(tempDir) 77 78 for i, vcs := range vcsList { 79 tests[i] = testStruct{ 80 filepath.Join(tempDir, vcs.Name, "."+vcs.Cmd), 81 vcs, 82 } 83 } 84 85 for _, test := range tests { 86 os.MkdirAll(test.path, 0755) 87 got, _, _ := FromDir(test.path, tempDir) 88 if got.Name != test.want.Name { 89 t.Errorf("FromDir(%q, %q) = %s, want %s", test.path, tempDir, got, test.want) 90 } 91 os.RemoveAll(test.path) 92 } 93 } 94 95 var parseMetaGoImportsTests = []struct { 96 in string 97 out []metaImport 98 }{ 99 { 100 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`, 101 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 102 }, 103 { 104 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 105 <meta name="go-import" content="baz/quux git http://github.com/rsc/baz/quux">`, 106 []metaImport{ 107 {"foo/bar", "git", "https://github.com/rsc/foo/bar"}, 108 {"baz/quux", "git", "http://github.com/rsc/baz/quux"}, 109 }, 110 }, 111 { 112 `<head> 113 <meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 114 </head>`, 115 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 116 }, 117 { 118 `<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar"> 119 <body>`, 120 []metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}}, 121 }, 122 } 123 124 func TestParseMetaGoImports(t *testing.T) { 125 for i, tt := range parseMetaGoImportsTests { 126 out, err := parseMetaGoImports(strings.NewReader(tt.in)) 127 if err != nil { 128 t.Errorf("test#%d: %v", i, err) 129 continue 130 } 131 if !reflect.DeepEqual(out, tt.out) { 132 t.Errorf("test#%d:\n\thave %q\n\twant %q", i, out, tt.out) 133 } 134 } 135 }