github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/src/cmd/go/internal/get/vcs_test.go (about)

     1  // Copyright 2014 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 get
     6  
     7  import (
     8  	"errors"
     9  	"internal/testenv"
    10  	"io/ioutil"
    11  	"os"
    12  	"path"
    13  	"path/filepath"
    14  	"testing"
    15  
    16  	"cmd/go/internal/web"
    17  )
    18  
    19  // Test that RepoRootForImportPath creates the correct RepoRoot for a given importPath.
    20  // TODO(cmang): Add tests for SVN and BZR.
    21  func TestRepoRootForImportPath(t *testing.T) {
    22  	testenv.MustHaveExternalNetwork(t)
    23  
    24  	tests := []struct {
    25  		path string
    26  		want *repoRoot
    27  	}{
    28  		{
    29  			"github.com/golang/groupcache",
    30  			&repoRoot{
    31  				vcs:  vcsGit,
    32  				repo: "https://github.com/golang/groupcache",
    33  			},
    34  		},
    35  		// Unicode letters in directories (issue 18660).
    36  		{
    37  			"github.com/user/unicode/испытание",
    38  			&repoRoot{
    39  				vcs:  vcsGit,
    40  				repo: "https://github.com/user/unicode",
    41  			},
    42  		},
    43  		// IBM DevOps Services tests
    44  		{
    45  			"hub.jazz.net/git/user1/pkgname",
    46  			&repoRoot{
    47  				vcs:  vcsGit,
    48  				repo: "https://hub.jazz.net/git/user1/pkgname",
    49  			},
    50  		},
    51  		{
    52  			"hub.jazz.net/git/user1/pkgname/submodule/submodule/submodule",
    53  			&repoRoot{
    54  				vcs:  vcsGit,
    55  				repo: "https://hub.jazz.net/git/user1/pkgname",
    56  			},
    57  		},
    58  		{
    59  			"hub.jazz.net",
    60  			nil,
    61  		},
    62  		{
    63  			"hub2.jazz.net",
    64  			nil,
    65  		},
    66  		{
    67  			"hub.jazz.net/someotherprefix",
    68  			nil,
    69  		},
    70  		{
    71  			"hub.jazz.net/someotherprefix/user1/pkgname",
    72  			nil,
    73  		},
    74  		// Spaces are not valid in user names or package names
    75  		{
    76  			"hub.jazz.net/git/User 1/pkgname",
    77  			nil,
    78  		},
    79  		{
    80  			"hub.jazz.net/git/user1/pkg name",
    81  			nil,
    82  		},
    83  		// Dots are not valid in user names
    84  		{
    85  			"hub.jazz.net/git/user.1/pkgname",
    86  			nil,
    87  		},
    88  		{
    89  			"hub.jazz.net/git/user/pkg.name",
    90  			&repoRoot{
    91  				vcs:  vcsGit,
    92  				repo: "https://hub.jazz.net/git/user/pkg.name",
    93  			},
    94  		},
    95  		// User names cannot have uppercase letters
    96  		{
    97  			"hub.jazz.net/git/USER/pkgname",
    98  			nil,
    99  		},
   100  		// OpenStack tests
   101  		{
   102  			"git.openstack.org/openstack/swift",
   103  			&repoRoot{
   104  				vcs:  vcsGit,
   105  				repo: "https://git.openstack.org/openstack/swift",
   106  			},
   107  		},
   108  		// Trailing .git is less preferred but included for
   109  		// compatibility purposes while the same source needs to
   110  		// be compilable on both old and new go
   111  		{
   112  			"git.openstack.org/openstack/swift.git",
   113  			&repoRoot{
   114  				vcs:  vcsGit,
   115  				repo: "https://git.openstack.org/openstack/swift.git",
   116  			},
   117  		},
   118  		{
   119  			"git.openstack.org/openstack/swift/go/hummingbird",
   120  			&repoRoot{
   121  				vcs:  vcsGit,
   122  				repo: "https://git.openstack.org/openstack/swift",
   123  			},
   124  		},
   125  		{
   126  			"git.openstack.org",
   127  			nil,
   128  		},
   129  		{
   130  			"git.openstack.org/openstack",
   131  			nil,
   132  		},
   133  		// Spaces are not valid in package name
   134  		{
   135  			"git.apache.org/package name/path/to/lib",
   136  			nil,
   137  		},
   138  		// Should have ".git" suffix
   139  		{
   140  			"git.apache.org/package-name/path/to/lib",
   141  			nil,
   142  		},
   143  		{
   144  			"git.apache.org/package-name.git",
   145  			&repoRoot{
   146  				vcs:  vcsGit,
   147  				repo: "https://git.apache.org/package-name.git",
   148  			},
   149  		},
   150  		{
   151  			"git.apache.org/package-name_2.x.git/path/to/lib",
   152  			&repoRoot{
   153  				vcs:  vcsGit,
   154  				repo: "https://git.apache.org/package-name_2.x.git",
   155  			},
   156  		},
   157  		{
   158  			"chiselapp.com/user/kyle/repository/fossilgg",
   159  			&repoRoot{
   160  				vcs:  vcsFossil,
   161  				repo: "https://chiselapp.com/user/kyle/repository/fossilgg",
   162  			},
   163  		},
   164  		{
   165  			// must have a user/$name/repository/$repo path
   166  			"chiselapp.com/kyle/repository/fossilgg",
   167  			nil,
   168  		},
   169  		{
   170  			"chiselapp.com/user/kyle/fossilgg",
   171  			nil,
   172  		},
   173  	}
   174  
   175  	for _, test := range tests {
   176  		got, err := repoRootForImportPath(test.path, web.Secure)
   177  		want := test.want
   178  
   179  		if want == nil {
   180  			if err == nil {
   181  				t.Errorf("repoRootForImportPath(%q): Error expected but not received", test.path)
   182  			}
   183  			continue
   184  		}
   185  		if err != nil {
   186  			t.Errorf("repoRootForImportPath(%q): %v", test.path, err)
   187  			continue
   188  		}
   189  		if got.vcs.name != want.vcs.name || got.repo != want.repo {
   190  			t.Errorf("repoRootForImportPath(%q) = VCS(%s) Repo(%s), want VCS(%s) Repo(%s)", test.path, got.vcs, got.repo, want.vcs, want.repo)
   191  		}
   192  	}
   193  }
   194  
   195  // Test that vcsFromDir correctly inspects a given directory and returns the right VCS and root.
   196  func TestFromDir(t *testing.T) {
   197  	tempDir, err := ioutil.TempDir("", "vcstest")
   198  	if err != nil {
   199  		t.Fatal(err)
   200  	}
   201  	defer os.RemoveAll(tempDir)
   202  
   203  	for j, vcs := range vcsList {
   204  		dir := filepath.Join(tempDir, "example.com", vcs.name, "."+vcs.cmd)
   205  		if j&1 == 0 {
   206  			err := os.MkdirAll(dir, 0755)
   207  			if err != nil {
   208  				t.Fatal(err)
   209  			}
   210  		} else {
   211  			err := os.MkdirAll(filepath.Dir(dir), 0755)
   212  			if err != nil {
   213  				t.Fatal(err)
   214  			}
   215  			f, err := os.Create(dir)
   216  			if err != nil {
   217  				t.Fatal(err)
   218  			}
   219  			f.Close()
   220  		}
   221  
   222  		want := repoRoot{
   223  			vcs:  vcs,
   224  			root: path.Join("example.com", vcs.name),
   225  		}
   226  		var got repoRoot
   227  		got.vcs, got.root, err = vcsFromDir(dir, tempDir)
   228  		if err != nil {
   229  			t.Errorf("FromDir(%q, %q): %v", dir, tempDir, err)
   230  			continue
   231  		}
   232  		if got.vcs.name != want.vcs.name || got.root != want.root {
   233  			t.Errorf("FromDir(%q, %q) = VCS(%s) Root(%s), want VCS(%s) Root(%s)", dir, tempDir, got.vcs, got.root, want.vcs, want.root)
   234  		}
   235  	}
   236  }
   237  
   238  func TestIsSecure(t *testing.T) {
   239  	tests := []struct {
   240  		vcs    *vcsCmd
   241  		url    string
   242  		secure bool
   243  	}{
   244  		{vcsGit, "http://example.com/foo.git", false},
   245  		{vcsGit, "https://example.com/foo.git", true},
   246  		{vcsBzr, "http://example.com/foo.bzr", false},
   247  		{vcsBzr, "https://example.com/foo.bzr", true},
   248  		{vcsSvn, "http://example.com/svn", false},
   249  		{vcsSvn, "https://example.com/svn", true},
   250  		{vcsHg, "http://example.com/foo.hg", false},
   251  		{vcsHg, "https://example.com/foo.hg", true},
   252  		{vcsGit, "ssh://user@example.com/foo.git", true},
   253  		{vcsGit, "user@server:path/to/repo.git", false},
   254  		{vcsGit, "user@server:", false},
   255  		{vcsGit, "server:repo.git", false},
   256  		{vcsGit, "server:path/to/repo.git", false},
   257  		{vcsGit, "example.com:path/to/repo.git", false},
   258  		{vcsGit, "path/that/contains/a:colon/repo.git", false},
   259  		{vcsHg, "ssh://user@example.com/path/to/repo.hg", true},
   260  		{vcsFossil, "http://example.com/foo", false},
   261  		{vcsFossil, "https://example.com/foo", true},
   262  	}
   263  
   264  	for _, test := range tests {
   265  		secure := test.vcs.isSecure(test.url)
   266  		if secure != test.secure {
   267  			t.Errorf("%s isSecure(%q) = %t; want %t", test.vcs, test.url, secure, test.secure)
   268  		}
   269  	}
   270  }
   271  
   272  func TestIsSecureGitAllowProtocol(t *testing.T) {
   273  	tests := []struct {
   274  		vcs    *vcsCmd
   275  		url    string
   276  		secure bool
   277  	}{
   278  		// Same as TestIsSecure to verify same behavior.
   279  		{vcsGit, "http://example.com/foo.git", false},
   280  		{vcsGit, "https://example.com/foo.git", true},
   281  		{vcsBzr, "http://example.com/foo.bzr", false},
   282  		{vcsBzr, "https://example.com/foo.bzr", true},
   283  		{vcsSvn, "http://example.com/svn", false},
   284  		{vcsSvn, "https://example.com/svn", true},
   285  		{vcsHg, "http://example.com/foo.hg", false},
   286  		{vcsHg, "https://example.com/foo.hg", true},
   287  		{vcsGit, "user@server:path/to/repo.git", false},
   288  		{vcsGit, "user@server:", false},
   289  		{vcsGit, "server:repo.git", false},
   290  		{vcsGit, "server:path/to/repo.git", false},
   291  		{vcsGit, "example.com:path/to/repo.git", false},
   292  		{vcsGit, "path/that/contains/a:colon/repo.git", false},
   293  		{vcsHg, "ssh://user@example.com/path/to/repo.hg", true},
   294  		// New behavior.
   295  		{vcsGit, "ssh://user@example.com/foo.git", false},
   296  		{vcsGit, "foo://example.com/bar.git", true},
   297  		{vcsHg, "foo://example.com/bar.hg", false},
   298  		{vcsSvn, "foo://example.com/svn", false},
   299  		{vcsBzr, "foo://example.com/bar.bzr", false},
   300  	}
   301  
   302  	defer os.Unsetenv("GIT_ALLOW_PROTOCOL")
   303  	os.Setenv("GIT_ALLOW_PROTOCOL", "https:foo")
   304  	for _, test := range tests {
   305  		secure := test.vcs.isSecure(test.url)
   306  		if secure != test.secure {
   307  			t.Errorf("%s isSecure(%q) = %t; want %t", test.vcs, test.url, secure, test.secure)
   308  		}
   309  	}
   310  }
   311  
   312  func TestMatchGoImport(t *testing.T) {
   313  	tests := []struct {
   314  		imports []metaImport
   315  		path    string
   316  		mi      metaImport
   317  		err     error
   318  	}{
   319  		{
   320  			imports: []metaImport{
   321  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   322  			},
   323  			path: "example.com/user/foo",
   324  			mi:   metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   325  		},
   326  		{
   327  			imports: []metaImport{
   328  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   329  			},
   330  			path: "example.com/user/foo/",
   331  			mi:   metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   332  		},
   333  		{
   334  			imports: []metaImport{
   335  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   336  				{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   337  			},
   338  			path: "example.com/user/foo",
   339  			mi:   metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   340  		},
   341  		{
   342  			imports: []metaImport{
   343  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   344  				{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   345  			},
   346  			path: "example.com/user/fooa",
   347  			mi:   metaImport{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   348  		},
   349  		{
   350  			imports: []metaImport{
   351  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   352  				{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   353  			},
   354  			path: "example.com/user/foo/bar",
   355  			err:  errors.New("should not be allowed to create nested repo"),
   356  		},
   357  		{
   358  			imports: []metaImport{
   359  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   360  				{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   361  			},
   362  			path: "example.com/user/foo/bar/baz",
   363  			err:  errors.New("should not be allowed to create nested repo"),
   364  		},
   365  		{
   366  			imports: []metaImport{
   367  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   368  				{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   369  			},
   370  			path: "example.com/user/foo/bar/baz/qux",
   371  			err:  errors.New("should not be allowed to create nested repo"),
   372  		},
   373  		{
   374  			imports: []metaImport{
   375  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   376  				{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   377  			},
   378  			path: "example.com/user/foo/bar/baz/",
   379  			err:  errors.New("should not be allowed to create nested repo"),
   380  		},
   381  		{
   382  			imports: []metaImport{
   383  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   384  				{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   385  			},
   386  			path: "example.com",
   387  			err:  errors.New("pathologically short path"),
   388  		},
   389  		{
   390  			imports: []metaImport{
   391  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   392  			},
   393  			path: "different.example.com/user/foo",
   394  			err:  errors.New("meta tags do not match import path"),
   395  		},
   396  	}
   397  
   398  	for _, test := range tests {
   399  		mi, err := matchGoImport(test.imports, test.path)
   400  		if mi != test.mi {
   401  			t.Errorf("unexpected metaImport; got %v, want %v", mi, test.mi)
   402  		}
   403  
   404  		got := err
   405  		want := test.err
   406  		if (got == nil) != (want == nil) {
   407  			t.Errorf("unexpected error; got %v, want %v", got, want)
   408  		}
   409  	}
   410  }