github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/cmd/go/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 main
     6  
     7  import (
     8  	"internal/testenv"
     9  	"testing"
    10  )
    11  
    12  // Test that RepoRootForImportPath creates the correct RepoRoot for a given importPath.
    13  // TODO(cmang): Add tests for SVN and BZR.
    14  func TestRepoRootForImportPath(t *testing.T) {
    15  	testenv.MustHaveExternalNetwork(t)
    16  
    17  	tests := []struct {
    18  		path string
    19  		want *repoRoot
    20  	}{
    21  		{
    22  			"code.google.com/p/go",
    23  			&repoRoot{
    24  				vcs:  vcsHg,
    25  				repo: "https://code.google.com/p/go",
    26  			},
    27  		},
    28  		/*{
    29  		        "code.google.com/r/go",
    30  		        &repoRoot{
    31  		                vcs:  vcsHg,
    32  		                repo: "https://code.google.com/r/go",
    33  		        },
    34  		},*/
    35  		{
    36  			"github.com/golang/groupcache",
    37  			&repoRoot{
    38  				vcs:  vcsGit,
    39  				repo: "https://github.com/golang/groupcache",
    40  			},
    41  		},
    42  		// IBM DevOps Services tests
    43  		{
    44  			"hub.jazz.net/git/user1/pkgname",
    45  			&repoRoot{
    46  				vcs:  vcsGit,
    47  				repo: "https://hub.jazz.net/git/user1/pkgname",
    48  			},
    49  		},
    50  		{
    51  			"hub.jazz.net/git/user1/pkgname/submodule/submodule/submodule",
    52  			&repoRoot{
    53  				vcs:  vcsGit,
    54  				repo: "https://hub.jazz.net/git/user1/pkgname",
    55  			},
    56  		},
    57  		{
    58  			"hub.jazz.net",
    59  			nil,
    60  		},
    61  		{
    62  			"hub2.jazz.net",
    63  			nil,
    64  		},
    65  		{
    66  			"hub.jazz.net/someotherprefix",
    67  			nil,
    68  		},
    69  		{
    70  			"hub.jazz.net/someotherprefix/user1/pkgname",
    71  			nil,
    72  		},
    73  		// Spaces are not valid in user names or package names
    74  		{
    75  			"hub.jazz.net/git/User 1/pkgname",
    76  			nil,
    77  		},
    78  		{
    79  			"hub.jazz.net/git/user1/pkg name",
    80  			nil,
    81  		},
    82  		// Dots are not valid in user names
    83  		{
    84  			"hub.jazz.net/git/user.1/pkgname",
    85  			nil,
    86  		},
    87  		{
    88  			"hub.jazz.net/git/user/pkg.name",
    89  			&repoRoot{
    90  				vcs:  vcsGit,
    91  				repo: "https://hub.jazz.net/git/user/pkg.name",
    92  			},
    93  		},
    94  		// User names cannot have uppercase letters
    95  		{
    96  			"hub.jazz.net/git/USER/pkgname",
    97  			nil,
    98  		},
    99  		// Spaces are not valid in package name
   100  		{
   101  			"git.apache.org/package name/path/to/lib",
   102  			nil,
   103  		},
   104  		// Should have ".git" suffix
   105  		{
   106  			"git.apache.org/package-name/path/to/lib",
   107  			nil,
   108  		},
   109  		{
   110  			"git.apache.org/package-name.git",
   111  			&repoRoot{
   112  				vcs:  vcsGit,
   113  				repo: "https://git.apache.org/package-name.git",
   114  			},
   115  		},
   116  		{
   117  			"git.apache.org/package-name_2.x.git/path/to/lib",
   118  			&repoRoot{
   119  				vcs:  vcsGit,
   120  				repo: "https://git.apache.org/package-name_2.x.git",
   121  			},
   122  		},
   123  	}
   124  
   125  	for _, test := range tests {
   126  		got, err := repoRootForImportPath(test.path, secure)
   127  		want := test.want
   128  
   129  		if want == nil {
   130  			if err == nil {
   131  				t.Errorf("RepoRootForImport(%q): Error expected but not received", test.path)
   132  			}
   133  			continue
   134  		}
   135  		if err != nil {
   136  			t.Errorf("RepoRootForImport(%q): %v", test.path, err)
   137  			continue
   138  		}
   139  		if got.vcs.name != want.vcs.name || got.repo != want.repo {
   140  			t.Errorf("RepoRootForImport(%q) = VCS(%s) Repo(%s), want VCS(%s) Repo(%s)", test.path, got.vcs, got.repo, want.vcs, want.repo)
   141  		}
   142  	}
   143  }
   144  
   145  func TestIsSecure(t *testing.T) {
   146  	tests := []struct {
   147  		vcs    *vcsCmd
   148  		url    string
   149  		secure bool
   150  	}{
   151  		{vcsGit, "http://example.com/foo.git", false},
   152  		{vcsGit, "https://example.com/foo.git", true},
   153  		{vcsBzr, "http://example.com/foo.bzr", false},
   154  		{vcsBzr, "https://example.com/foo.bzr", true},
   155  		{vcsSvn, "http://example.com/svn", false},
   156  		{vcsSvn, "https://example.com/svn", true},
   157  		{vcsHg, "http://example.com/foo.hg", false},
   158  		{vcsHg, "https://example.com/foo.hg", true},
   159  		{vcsGit, "ssh://user@example.com/foo.git", true},
   160  		{vcsGit, "user@server:path/to/repo.git", false},
   161  		{vcsGit, "user@server:", false},
   162  		{vcsGit, "server:repo.git", false},
   163  		{vcsGit, "server:path/to/repo.git", false},
   164  		{vcsGit, "example.com:path/to/repo.git", false},
   165  		{vcsGit, "path/that/contains/a:colon/repo.git", false},
   166  		{vcsHg, "ssh://user@example.com/path/to/repo.hg", true},
   167  	}
   168  
   169  	for _, test := range tests {
   170  		secure := test.vcs.isSecure(test.url)
   171  		if secure != test.secure {
   172  			t.Errorf("%s isSecure(%q) = %t; want %t", test.vcs, test.url, secure, test.secure)
   173  		}
   174  	}
   175  }