github.com/sanprasirt/go@v0.0.0-20170607001320-a027466e4b6d/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  
   159  	for _, test := range tests {
   160  		got, err := repoRootForImportPath(test.path, web.Secure)
   161  		want := test.want
   162  
   163  		if want == nil {
   164  			if err == nil {
   165  				t.Errorf("repoRootForImportPath(%q): Error expected but not received", test.path)
   166  			}
   167  			continue
   168  		}
   169  		if err != nil {
   170  			t.Errorf("repoRootForImportPath(%q): %v", test.path, err)
   171  			continue
   172  		}
   173  		if got.vcs.name != want.vcs.name || got.repo != want.repo {
   174  			t.Errorf("repoRootForImportPath(%q) = VCS(%s) Repo(%s), want VCS(%s) Repo(%s)", test.path, got.vcs, got.repo, want.vcs, want.repo)
   175  		}
   176  	}
   177  }
   178  
   179  // Test that vcsFromDir correctly inspects a given directory and returns the right VCS and root.
   180  func TestFromDir(t *testing.T) {
   181  	tempDir, err := ioutil.TempDir("", "vcstest")
   182  	if err != nil {
   183  		t.Fatal(err)
   184  	}
   185  	defer os.RemoveAll(tempDir)
   186  
   187  	for j, vcs := range vcsList {
   188  		dir := filepath.Join(tempDir, "example.com", vcs.name, "."+vcs.cmd)
   189  		if j&1 == 0 {
   190  			err := os.MkdirAll(dir, 0755)
   191  			if err != nil {
   192  				t.Fatal(err)
   193  			}
   194  		} else {
   195  			err := os.MkdirAll(filepath.Dir(dir), 0755)
   196  			if err != nil {
   197  				t.Fatal(err)
   198  			}
   199  			f, err := os.Create(dir)
   200  			if err != nil {
   201  				t.Fatal(err)
   202  			}
   203  			f.Close()
   204  		}
   205  
   206  		want := repoRoot{
   207  			vcs:  vcs,
   208  			root: path.Join("example.com", vcs.name),
   209  		}
   210  		var got repoRoot
   211  		got.vcs, got.root, err = vcsFromDir(dir, tempDir)
   212  		if err != nil {
   213  			t.Errorf("FromDir(%q, %q): %v", dir, tempDir, err)
   214  			continue
   215  		}
   216  		if got.vcs.name != want.vcs.name || got.root != want.root {
   217  			t.Errorf("FromDir(%q, %q) = VCS(%s) Root(%s), want VCS(%s) Root(%s)", dir, tempDir, got.vcs, got.root, want.vcs, want.root)
   218  		}
   219  	}
   220  }
   221  
   222  func TestIsSecure(t *testing.T) {
   223  	tests := []struct {
   224  		vcs    *vcsCmd
   225  		url    string
   226  		secure bool
   227  	}{
   228  		{vcsGit, "http://example.com/foo.git", false},
   229  		{vcsGit, "https://example.com/foo.git", true},
   230  		{vcsBzr, "http://example.com/foo.bzr", false},
   231  		{vcsBzr, "https://example.com/foo.bzr", true},
   232  		{vcsSvn, "http://example.com/svn", false},
   233  		{vcsSvn, "https://example.com/svn", true},
   234  		{vcsHg, "http://example.com/foo.hg", false},
   235  		{vcsHg, "https://example.com/foo.hg", true},
   236  		{vcsGit, "ssh://user@example.com/foo.git", true},
   237  		{vcsGit, "user@server:path/to/repo.git", false},
   238  		{vcsGit, "user@server:", false},
   239  		{vcsGit, "server:repo.git", false},
   240  		{vcsGit, "server:path/to/repo.git", false},
   241  		{vcsGit, "example.com:path/to/repo.git", false},
   242  		{vcsGit, "path/that/contains/a:colon/repo.git", false},
   243  		{vcsHg, "ssh://user@example.com/path/to/repo.hg", true},
   244  	}
   245  
   246  	for _, test := range tests {
   247  		secure := test.vcs.isSecure(test.url)
   248  		if secure != test.secure {
   249  			t.Errorf("%s isSecure(%q) = %t; want %t", test.vcs, test.url, secure, test.secure)
   250  		}
   251  	}
   252  }
   253  
   254  func TestIsSecureGitAllowProtocol(t *testing.T) {
   255  	tests := []struct {
   256  		vcs    *vcsCmd
   257  		url    string
   258  		secure bool
   259  	}{
   260  		// Same as TestIsSecure to verify same behavior.
   261  		{vcsGit, "http://example.com/foo.git", false},
   262  		{vcsGit, "https://example.com/foo.git", true},
   263  		{vcsBzr, "http://example.com/foo.bzr", false},
   264  		{vcsBzr, "https://example.com/foo.bzr", true},
   265  		{vcsSvn, "http://example.com/svn", false},
   266  		{vcsSvn, "https://example.com/svn", true},
   267  		{vcsHg, "http://example.com/foo.hg", false},
   268  		{vcsHg, "https://example.com/foo.hg", true},
   269  		{vcsGit, "user@server:path/to/repo.git", false},
   270  		{vcsGit, "user@server:", false},
   271  		{vcsGit, "server:repo.git", false},
   272  		{vcsGit, "server:path/to/repo.git", false},
   273  		{vcsGit, "example.com:path/to/repo.git", false},
   274  		{vcsGit, "path/that/contains/a:colon/repo.git", false},
   275  		{vcsHg, "ssh://user@example.com/path/to/repo.hg", true},
   276  		// New behavior.
   277  		{vcsGit, "ssh://user@example.com/foo.git", false},
   278  		{vcsGit, "foo://example.com/bar.git", true},
   279  		{vcsHg, "foo://example.com/bar.hg", false},
   280  		{vcsSvn, "foo://example.com/svn", false},
   281  		{vcsBzr, "foo://example.com/bar.bzr", false},
   282  	}
   283  
   284  	defer os.Unsetenv("GIT_ALLOW_PROTOCOL")
   285  	os.Setenv("GIT_ALLOW_PROTOCOL", "https:foo")
   286  	for _, test := range tests {
   287  		secure := test.vcs.isSecure(test.url)
   288  		if secure != test.secure {
   289  			t.Errorf("%s isSecure(%q) = %t; want %t", test.vcs, test.url, secure, test.secure)
   290  		}
   291  	}
   292  }
   293  
   294  func TestMatchGoImport(t *testing.T) {
   295  	tests := []struct {
   296  		imports []metaImport
   297  		path    string
   298  		mi      metaImport
   299  		err     error
   300  	}{
   301  		{
   302  			imports: []metaImport{
   303  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   304  			},
   305  			path: "example.com/user/foo",
   306  			mi:   metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   307  		},
   308  		{
   309  			imports: []metaImport{
   310  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   311  			},
   312  			path: "example.com/user/foo/",
   313  			mi:   metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   314  		},
   315  		{
   316  			imports: []metaImport{
   317  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   318  				{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   319  			},
   320  			path: "example.com/user/foo",
   321  			mi:   metaImport{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   322  		},
   323  		{
   324  			imports: []metaImport{
   325  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   326  				{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   327  			},
   328  			path: "example.com/user/fooa",
   329  			mi:   metaImport{Prefix: "example.com/user/fooa", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   330  		},
   331  		{
   332  			imports: []metaImport{
   333  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   334  				{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   335  			},
   336  			path: "example.com/user/foo/bar",
   337  			err:  errors.New("should not be allowed to create nested repo"),
   338  		},
   339  		{
   340  			imports: []metaImport{
   341  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   342  				{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   343  			},
   344  			path: "example.com/user/foo/bar/baz",
   345  			err:  errors.New("should not be allowed to create nested repo"),
   346  		},
   347  		{
   348  			imports: []metaImport{
   349  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   350  				{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   351  			},
   352  			path: "example.com/user/foo/bar/baz/qux",
   353  			err:  errors.New("should not be allowed to create nested repo"),
   354  		},
   355  		{
   356  			imports: []metaImport{
   357  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   358  				{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   359  			},
   360  			path: "example.com/user/foo/bar/baz/",
   361  			err:  errors.New("should not be allowed to create nested repo"),
   362  		},
   363  		{
   364  			imports: []metaImport{
   365  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   366  				{Prefix: "example.com/user/foo/bar", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   367  			},
   368  			path: "example.com",
   369  			err:  errors.New("pathologically short path"),
   370  		},
   371  		{
   372  			imports: []metaImport{
   373  				{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
   374  			},
   375  			path: "different.example.com/user/foo",
   376  			err:  errors.New("meta tags do not match import path"),
   377  		},
   378  	}
   379  
   380  	for _, test := range tests {
   381  		mi, err := matchGoImport(test.imports, test.path)
   382  		if mi != test.mi {
   383  			t.Errorf("unexpected metaImport; got %v, want %v", mi, test.mi)
   384  		}
   385  
   386  		got := err
   387  		want := test.err
   388  		if (got == nil) != (want == nil) {
   389  			t.Errorf("unexpected error; got %v, want %v", got, want)
   390  		}
   391  	}
   392  }