github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gotools/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  	"strings"
    13  	"testing"
    14  )
    15  
    16  // Test that RepoRootForImportPath creates the correct RepoRoot for a given importPath.
    17  // TODO(cmang): Add tests for SVN and BZR.
    18  func TestRepoRootForImportPath(t *testing.T) {
    19  	tests := []struct {
    20  		path string
    21  		want *RepoRoot
    22  	}{
    23  		{
    24  			"code.google.com/p/go",
    25  			&RepoRoot{
    26  				VCS:  vcsHg,
    27  				Repo: "https://code.google.com/p/go",
    28  			},
    29  		},
    30  		{
    31  			"code.google.com/r/go",
    32  			&RepoRoot{
    33  				VCS:  vcsHg,
    34  				Repo: "https://code.google.com/r/go",
    35  			},
    36  		},
    37  		{
    38  			"github.com/golang/groupcache",
    39  			&RepoRoot{
    40  				VCS:  vcsGit,
    41  				Repo: "https://github.com/golang/groupcache",
    42  			},
    43  		},
    44  	}
    45  
    46  	for _, test := range tests {
    47  		got, err := RepoRootForImportPath(test.path, false)
    48  		if err != nil {
    49  			t.Errorf("RepoRootForImport(%q): %v", test.path, err)
    50  			continue
    51  		}
    52  		want := test.want
    53  		if got.VCS.Name != want.VCS.Name || got.Repo != want.Repo {
    54  			t.Errorf("RepoRootForImport(%q) = VCS(%s) Repo(%s), want VCS(%s) Repo(%s)", test.path, got.VCS, got.Repo, want.VCS, want.Repo)
    55  		}
    56  	}
    57  }
    58  
    59  // Test that FromDir correctly inspects a given directory and returns the right VCS.
    60  func TestFromDir(t *testing.T) {
    61  	type testStruct struct {
    62  		path string
    63  		want *Cmd
    64  	}
    65  
    66  	tests := make([]testStruct, len(vcsList))
    67  	tempDir, err := ioutil.TempDir("", "vcstest")
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	defer os.RemoveAll(tempDir)
    72  
    73  	for i, vcs := range vcsList {
    74  		tests[i] = testStruct{
    75  			filepath.Join(tempDir, vcs.Name, "."+vcs.Cmd),
    76  			vcs,
    77  		}
    78  	}
    79  
    80  	for _, test := range tests {
    81  		os.MkdirAll(test.path, 0755)
    82  		got, _, _ := FromDir(test.path, tempDir)
    83  		if got.Name != test.want.Name {
    84  			t.Errorf("FromDir(%q, %q) = %s, want %s", test.path, tempDir, got, test.want)
    85  		}
    86  		os.RemoveAll(test.path)
    87  	}
    88  }
    89  
    90  var parseMetaGoImportsTests = []struct {
    91  	in  string
    92  	out []metaImport
    93  }{
    94  	{
    95  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
    96  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    97  	},
    98  	{
    99  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
   100  		<meta name="go-import" content="baz/quux git http://github.com/rsc/baz/quux">`,
   101  		[]metaImport{
   102  			{"foo/bar", "git", "https://github.com/rsc/foo/bar"},
   103  			{"baz/quux", "git", "http://github.com/rsc/baz/quux"},
   104  		},
   105  	},
   106  	{
   107  		`<head>
   108  		<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
   109  		</head>`,
   110  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
   111  	},
   112  	{
   113  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
   114  		<body>`,
   115  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
   116  	},
   117  }
   118  
   119  func TestParseMetaGoImports(t *testing.T) {
   120  	for i, tt := range parseMetaGoImportsTests {
   121  		out, err := parseMetaGoImports(strings.NewReader(tt.in))
   122  		if err != nil {
   123  			t.Errorf("test#%d: %v", i, err)
   124  			continue
   125  		}
   126  		if !reflect.DeepEqual(out, tt.out) {
   127  			t.Errorf("test#%d:\n\thave %q\n\twant %q", i, out, tt.out)
   128  		}
   129  	}
   130  }