github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-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  	"github.com/gagliardetto/golang-go/not-internal/testenv"
    10  	"io/ioutil"
    11  	"os"
    12  	"path"
    13  	"path/filepath"
    14  	"testing"
    15  
    16  	"github.com/gagliardetto/golang-go/cmd/go/not-internal/web"
    17  )
    18  
    19  // Test that RepoRootForImportPath determines 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  			"hubajazz.net",
    64  			nil,
    65  		},
    66  		{
    67  			"hub2.jazz.net",
    68  			nil,
    69  		},
    70  		{
    71  			"hub.jazz.net/someotherprefix",
    72  			nil,
    73  		},
    74  		{
    75  			"hub.jazz.net/someotherprefix/user1/pkgname",
    76  			nil,
    77  		},
    78  		// Spaces are not valid in user names or package names
    79  		{
    80  			"hub.jazz.net/git/User 1/pkgname",
    81  			nil,
    82  		},
    83  		{
    84  			"hub.jazz.net/git/user1/pkg name",
    85  			nil,
    86  		},
    87  		// Dots are not valid in user names
    88  		{
    89  			"hub.jazz.net/git/user.1/pkgname",
    90  			nil,
    91  		},
    92  		{
    93  			"hub.jazz.net/git/user/pkg.name",
    94  			&RepoRoot{
    95  				vcs:  vcsGit,
    96  				Repo: "https://hub.jazz.net/git/user/pkg.name",
    97  			},
    98  		},
    99  		// User names cannot have uppercase letters
   100  		{
   101  			"hub.jazz.net/git/USER/pkgname",
   102  			nil,
   103  		},
   104  		// OpenStack tests
   105  		{
   106  			"git.openstack.org/openstack/swift",
   107  			&RepoRoot{
   108  				vcs:  vcsGit,
   109  				Repo: "https://git.openstack.org/openstack/swift",
   110  			},
   111  		},
   112  		// Trailing .git is less preferred but included for
   113  		// compatibility purposes while the same source needs to
   114  		// be compilable on both old and new go
   115  		{
   116  			"git.openstack.org/openstack/swift.git",
   117  			&RepoRoot{
   118  				vcs:  vcsGit,
   119  				Repo: "https://git.openstack.org/openstack/swift.git",
   120  			},
   121  		},
   122  		{
   123  			"git.openstack.org/openstack/swift/go/hummingbird",
   124  			&RepoRoot{
   125  				vcs:  vcsGit,
   126  				Repo: "https://git.openstack.org/openstack/swift",
   127  			},
   128  		},
   129  		{
   130  			"git.openstack.org",
   131  			nil,
   132  		},
   133  		{
   134  			"git.openstack.org/openstack",
   135  			nil,
   136  		},
   137  		// Spaces are not valid in package name
   138  		{
   139  			"git.apache.org/package name/path/to/lib",
   140  			nil,
   141  		},
   142  		// Should have ".git" suffix
   143  		{
   144  			"git.apache.org/package-name/path/to/lib",
   145  			nil,
   146  		},
   147  		{
   148  			"gitbapache.org",
   149  			nil,
   150  		},
   151  		{
   152  			"git.apache.org/package-name.git",
   153  			&RepoRoot{
   154  				vcs:  vcsGit,
   155  				Repo: "https://git.apache.org/package-name.git",
   156  			},
   157  		},
   158  		{
   159  			"git.apache.org/package-name_2.x.git/path/to/lib",
   160  			&RepoRoot{
   161  				vcs:  vcsGit,
   162  				Repo: "https://git.apache.org/package-name_2.x.git",
   163  			},
   164  		},
   165  		{
   166  			"chiselapp.com/user/kyle/repository/fossilgg",
   167  			&RepoRoot{
   168  				vcs:  vcsFossil,
   169  				Repo: "https://chiselapp.com/user/kyle/repository/fossilgg",
   170  			},
   171  		},
   172  		{
   173  			// must have a user/$name/repository/$repo path
   174  			"chiselapp.com/kyle/repository/fossilgg",
   175  			nil,
   176  		},
   177  		{
   178  			"chiselapp.com/user/kyle/fossilgg",
   179  			nil,
   180  		},
   181  	}
   182  
   183  	for _, test := range tests {
   184  		got, err := RepoRootForImportPath(test.path, IgnoreMod, web.SecureOnly)
   185  		want := test.want
   186  
   187  		if want == nil {
   188  			if err == nil {
   189  				t.Errorf("RepoRootForImportPath(%q): Error expected but not received", test.path)
   190  			}
   191  			continue
   192  		}
   193  		if err != nil {
   194  			t.Errorf("RepoRootForImportPath(%q): %v", test.path, err)
   195  			continue
   196  		}
   197  		if got.vcs.name != want.vcs.name || got.Repo != want.Repo {
   198  			t.Errorf("RepoRootForImportPath(%q) = VCS(%s) Repo(%s), want VCS(%s) Repo(%s)", test.path, got.vcs, got.Repo, want.vcs, want.Repo)
   199  		}
   200  	}
   201  }
   202  
   203  // Test that vcsFromDir correctly inspects a given directory and returns the right VCS and root.
   204  func TestFromDir(t *testing.T) {
   205  	tempDir, err := ioutil.TempDir("", "vcstest")
   206  	if err != nil {
   207  		t.Fatal(err)
   208  	}
   209  	defer os.RemoveAll(tempDir)
   210  
   211  	for j, vcs := range vcsList {
   212  		dir := filepath.Join(tempDir, "example.com", vcs.name, "."+vcs.cmd)
   213  		if j&1 == 0 {
   214  			err := os.MkdirAll(dir, 0755)
   215  			if err != nil {
   216  				t.Fatal(err)
   217  			}
   218  		} else {
   219  			err := os.MkdirAll(filepath.Dir(dir), 0755)
   220  			if err != nil {
   221  				t.Fatal(err)
   222  			}
   223  			f, err := os.Create(dir)
   224  			if err != nil {
   225  				t.Fatal(err)
   226  			}
   227  			f.Close()
   228  		}
   229  
   230  		want := RepoRoot{
   231  			vcs:  vcs,
   232  			Root: path.Join("example.com", vcs.name),
   233  		}
   234  		var got RepoRoot
   235  		got.vcs, got.Root, err = vcsFromDir(dir, tempDir)
   236  		if err != nil {
   237  			t.Errorf("FromDir(%q, %q): %v", dir, tempDir, err)
   238  			continue
   239  		}
   240  		if got.vcs.name != want.vcs.name || got.Root != want.Root {
   241  			t.Errorf("FromDir(%q, %q) = VCS(%s) Root(%s), want VCS(%s) Root(%s)", dir, tempDir, got.vcs, got.Root, want.vcs, want.Root)
   242  		}
   243  	}
   244  }
   245  
   246  func TestIsSecure(t *testing.T) {
   247  	tests := []struct {
   248  		vcs    *vcsCmd
   249  		url    string
   250  		secure bool
   251  	}{
   252  		{vcsGit, "http://example.com/foo.git", false},
   253  		{vcsGit, "https://example.com/foo.git", true},
   254  		{vcsBzr, "http://example.com/foo.bzr", false},
   255  		{vcsBzr, "https://example.com/foo.bzr", true},
   256  		{vcsSvn, "http://example.com/svn", false},
   257  		{vcsSvn, "https://example.com/svn", true},
   258  		{vcsHg, "http://example.com/foo.hg", false},
   259  		{vcsHg, "https://example.com/foo.hg", true},
   260  		{vcsGit, "ssh://user@example.com/foo.git", true},
   261  		{vcsGit, "user@server:path/to/repo.git", false},
   262  		{vcsGit, "user@server:", false},
   263  		{vcsGit, "server:repo.git", false},
   264  		{vcsGit, "server:path/to/repo.git", false},
   265  		{vcsGit, "example.com:path/to/repo.git", false},
   266  		{vcsGit, "path/that/contains/a:colon/repo.git", false},
   267  		{vcsHg, "ssh://user@example.com/path/to/repo.hg", true},
   268  		{vcsFossil, "http://example.com/foo", false},
   269  		{vcsFossil, "https://example.com/foo", true},
   270  	}
   271  
   272  	for _, test := range tests {
   273  		secure := test.vcs.isSecure(test.url)
   274  		if secure != test.secure {
   275  			t.Errorf("%s isSecure(%q) = %t; want %t", test.vcs, test.url, secure, test.secure)
   276  		}
   277  	}
   278  }
   279  
   280  func TestIsSecureGitAllowProtocol(t *testing.T) {
   281  	tests := []struct {
   282  		vcs    *vcsCmd
   283  		url    string
   284  		secure bool
   285  	}{
   286  		// Same as TestIsSecure to verify same behavior.
   287  		{vcsGit, "http://example.com/foo.git", false},
   288  		{vcsGit, "https://example.com/foo.git", true},
   289  		{vcsBzr, "http://example.com/foo.bzr", false},
   290  		{vcsBzr, "https://example.com/foo.bzr", true},
   291  		{vcsSvn, "http://example.com/svn", false},
   292  		{vcsSvn, "https://example.com/svn", true},
   293  		{vcsHg, "http://example.com/foo.hg", false},
   294  		{vcsHg, "https://example.com/foo.hg", true},
   295  		{vcsGit, "user@server:path/to/repo.git", false},
   296  		{vcsGit, "user@server:", false},
   297  		{vcsGit, "server:repo.git", false},
   298  		{vcsGit, "server:path/to/repo.git", false},
   299  		{vcsGit, "example.com:path/to/repo.git", false},
   300  		{vcsGit, "path/that/contains/a:colon/repo.git", false},
   301  		{vcsHg, "ssh://user@example.com/path/to/repo.hg", true},
   302  		// New behavior.
   303  		{vcsGit, "ssh://user@example.com/foo.git", false},
   304  		{vcsGit, "foo://example.com/bar.git", true},
   305  		{vcsHg, "foo://example.com/bar.hg", false},
   306  		{vcsSvn, "foo://example.com/svn", false},
   307  		{vcsBzr, "foo://example.com/bar.bzr", false},
   308  	}
   309  
   310  	defer os.Unsetenv("GIT_ALLOW_PROTOCOL")
   311  	os.Setenv("GIT_ALLOW_PROTOCOL", "https:foo")
   312  	for _, test := range tests {
   313  		secure := test.vcs.isSecure(test.url)
   314  		if secure != test.secure {
   315  			t.Errorf("%s isSecure(%q) = %t; want %t", test.vcs, test.url, secure, test.secure)
   316  		}
   317  	}
   318  }
   319  
   320  func TestMatchGoImport(t *testing.T) {
   321  	tests := []struct {
   322  		imports []metaImport
   323  		path    string
   324  		mi      metaImport
   325  		err     error
   326  	}{
   327  		{
   328  			imports: []metaImport{
   329  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   330  			},
   331  			path: "example.com/user/foo",
   332  			mi:   metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   333  		},
   334  		{
   335  			imports: []metaImport{
   336  				{Prefix: "example.com/user/foo", 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/foo",
   347  			mi:   metaImport{Prefix: "example.com/user/foo", 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/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   353  			},
   354  			path: "example.com/user/fooa",
   355  			mi:   metaImport{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   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",
   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",
   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/qux",
   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/user/foo/bar/baz/",
   387  			err:  errors.New("should not be allowed to create nested repo"),
   388  		},
   389  		{
   390  			imports: []metaImport{
   391  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   392  				{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   393  			},
   394  			path: "example.com",
   395  			err:  errors.New("pathologically short path"),
   396  		},
   397  		{
   398  			imports: []metaImport{
   399  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   400  			},
   401  			path: "different.example.com/user/foo",
   402  			err:  errors.New("meta tags do not match import path"),
   403  		},
   404  		{
   405  			imports: []metaImport{
   406  				{Prefix: "myitcv.io/blah2", VCS: "mod", RepoRoot: "https://raw.githubusercontent.com/myitcv/pubx/master"},
   407  				{Prefix: "myitcv.io", VCS: "git", RepoRoot: "https://github.com/myitcv/x"},
   408  			},
   409  			path: "myitcv.io/blah2/foo",
   410  			mi:   metaImport{Prefix: "myitcv.io/blah2", VCS: "mod", RepoRoot: "https://raw.githubusercontent.com/myitcv/pubx/master"},
   411  		},
   412  		{
   413  			imports: []metaImport{
   414  				{Prefix: "myitcv.io/blah2", VCS: "mod", RepoRoot: "https://raw.githubusercontent.com/myitcv/pubx/master"},
   415  				{Prefix: "myitcv.io", VCS: "git", RepoRoot: "https://github.com/myitcv/x"},
   416  			},
   417  			path: "myitcv.io/other",
   418  			mi:   metaImport{Prefix: "myitcv.io", VCS: "git", RepoRoot: "https://github.com/myitcv/x"},
   419  		},
   420  	}
   421  
   422  	for _, test := range tests {
   423  		mi, err := matchGoImport(test.imports, test.path)
   424  		if mi != test.mi {
   425  			t.Errorf("unexpected metaImport; got %v, want %v", mi, test.mi)
   426  		}
   427  
   428  		got := err
   429  		want := test.err
   430  		if (got == nil) != (want == nil) {
   431  			t.Errorf("unexpected error; got %v, want %v", got, want)
   432  		}
   433  	}
   434  }
   435  
   436  func TestValidateRepoRoot(t *testing.T) {
   437  	tests := []struct {
   438  		root string
   439  		ok   bool
   440  	}{
   441  		{
   442  			root: "",
   443  			ok:   false,
   444  		},
   445  		{
   446  			root: "http://",
   447  			ok:   true,
   448  		},
   449  		{
   450  			root: "git+ssh://",
   451  			ok:   true,
   452  		},
   453  		{
   454  			root: "http#://",
   455  			ok:   false,
   456  		},
   457  		{
   458  			root: "-config",
   459  			ok:   false,
   460  		},
   461  		{
   462  			root: "-config://",
   463  			ok:   false,
   464  		},
   465  	}
   466  
   467  	for _, test := range tests {
   468  		err := validateRepoRoot(test.root)
   469  		ok := err == nil
   470  		if ok != test.ok {
   471  			want := "error"
   472  			if test.ok {
   473  				want = "nil"
   474  			}
   475  			t.Errorf("validateRepoRoot(%q) = %q, want %s", test.root, err, want)
   476  		}
   477  	}
   478  }