github.com/aloncn/graphics-go@v0.0.1/src/cmd/go/pkg_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  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	"reflect"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  var foldDupTests = []struct {
    17  	list   []string
    18  	f1, f2 string
    19  }{
    20  	{stringList("math/rand", "math/big"), "", ""},
    21  	{stringList("math", "strings"), "", ""},
    22  	{stringList("strings"), "", ""},
    23  	{stringList("strings", "strings"), "strings", "strings"},
    24  	{stringList("Rand", "rand", "math", "math/rand", "math/Rand"), "Rand", "rand"},
    25  }
    26  
    27  func TestFoldDup(t *testing.T) {
    28  	for _, tt := range foldDupTests {
    29  		f1, f2 := foldDup(tt.list)
    30  		if f1 != tt.f1 || f2 != tt.f2 {
    31  			t.Errorf("foldDup(%q) = %q, %q, want %q, %q", tt.list, f1, f2, tt.f1, tt.f2)
    32  		}
    33  	}
    34  }
    35  
    36  var parseMetaGoImportsTests = []struct {
    37  	in  string
    38  	out []metaImport
    39  }{
    40  	{
    41  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
    42  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    43  	},
    44  	{
    45  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
    46  		<meta name="go-import" content="baz/quux git http://github.com/rsc/baz/quux">`,
    47  		[]metaImport{
    48  			{"foo/bar", "git", "https://github.com/rsc/foo/bar"},
    49  			{"baz/quux", "git", "http://github.com/rsc/baz/quux"},
    50  		},
    51  	},
    52  	{
    53  		`<head>
    54  		<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
    55  		</head>`,
    56  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    57  	},
    58  	{
    59  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
    60  		<body>`,
    61  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    62  	},
    63  	{
    64  		`<!doctype html><meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
    65  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    66  	},
    67  	{
    68  		// XML doesn't like <div style=position:relative>.
    69  		`<!doctype html><title>Page Not Found</title><meta name=go-import content="chitin.io/chitin git https://github.com/chitin-io/chitin"><div style=position:relative>DRAFT</div>`,
    70  		[]metaImport{{"chitin.io/chitin", "git", "https://github.com/chitin-io/chitin"}},
    71  	},
    72  }
    73  
    74  func TestParseMetaGoImports(t *testing.T) {
    75  	for i, tt := range parseMetaGoImportsTests {
    76  		out, err := parseMetaGoImports(strings.NewReader(tt.in))
    77  		if err != nil {
    78  			t.Errorf("test#%d: %v", i, err)
    79  			continue
    80  		}
    81  		if !reflect.DeepEqual(out, tt.out) {
    82  			t.Errorf("test#%d:\n\thave %q\n\twant %q", i, out, tt.out)
    83  		}
    84  	}
    85  }
    86  
    87  func TestSharedLibName(t *testing.T) {
    88  	// TODO(avdva) - make these values platform-specific
    89  	prefix := "lib"
    90  	suffix := ".so"
    91  	testData := []struct {
    92  		args      []string
    93  		pkgs      []*Package
    94  		expected  string
    95  		expectErr bool
    96  		rootedAt  string
    97  	}{
    98  		{
    99  			args:     []string{"std"},
   100  			pkgs:     []*Package{},
   101  			expected: "std",
   102  		},
   103  		{
   104  			args:     []string{"std", "cmd"},
   105  			pkgs:     []*Package{},
   106  			expected: "std,cmd",
   107  		},
   108  		{
   109  			args:     []string{},
   110  			pkgs:     []*Package{&Package{ImportPath: "gopkg.in/somelib"}},
   111  			expected: "gopkg.in-somelib",
   112  		},
   113  		{
   114  			args:     []string{"./..."},
   115  			pkgs:     []*Package{&Package{ImportPath: "somelib"}},
   116  			expected: "somelib",
   117  			rootedAt: "somelib",
   118  		},
   119  		{
   120  			args:     []string{"../somelib", "../somelib"},
   121  			pkgs:     []*Package{&Package{ImportPath: "somelib"}},
   122  			expected: "somelib",
   123  		},
   124  		{
   125  			args:     []string{"../lib1", "../lib2"},
   126  			pkgs:     []*Package{&Package{ImportPath: "gopkg.in/lib1"}, &Package{ImportPath: "gopkg.in/lib2"}},
   127  			expected: "gopkg.in-lib1,gopkg.in-lib2",
   128  		},
   129  		{
   130  			args: []string{"./..."},
   131  			pkgs: []*Package{
   132  				&Package{ImportPath: "gopkg.in/dir/lib1"},
   133  				&Package{ImportPath: "gopkg.in/lib2"},
   134  				&Package{ImportPath: "gopkg.in/lib3"},
   135  			},
   136  			expected: "gopkg.in",
   137  			rootedAt: "gopkg.in",
   138  		},
   139  		{
   140  			args:      []string{"std", "../lib2"},
   141  			pkgs:      []*Package{},
   142  			expectErr: true,
   143  		},
   144  		{
   145  			args:      []string{"all", "./"},
   146  			pkgs:      []*Package{},
   147  			expectErr: true,
   148  		},
   149  		{
   150  			args:      []string{"cmd", "fmt"},
   151  			pkgs:      []*Package{},
   152  			expectErr: true,
   153  		},
   154  	}
   155  	for _, data := range testData {
   156  		func() {
   157  			if data.rootedAt != "" {
   158  				tmpGopath, err := ioutil.TempDir("", "gopath")
   159  				if err != nil {
   160  					t.Fatal(err)
   161  				}
   162  				oldGopath := buildContext.GOPATH
   163  				defer func() {
   164  					os.RemoveAll(tmpGopath)
   165  					buildContext.GOPATH = oldGopath
   166  					os.Chdir(cwd)
   167  				}()
   168  				root := filepath.Join(tmpGopath, "src", data.rootedAt)
   169  				err = os.MkdirAll(root, 0755)
   170  				if err != nil {
   171  					t.Fatal(err)
   172  				}
   173  				buildContext.GOPATH = tmpGopath
   174  				os.Chdir(root)
   175  			}
   176  			computed, err := libname(data.args, data.pkgs)
   177  			if err != nil {
   178  				if !data.expectErr {
   179  					t.Errorf("libname returned an error %q, expected a name", err.Error())
   180  				}
   181  			} else if data.expectErr {
   182  				t.Errorf("libname returned %q, expected an error", computed)
   183  			} else {
   184  				expected := prefix + data.expected + suffix
   185  				if expected != computed {
   186  					t.Errorf("libname returned %q, expected %q", computed, expected)
   187  				}
   188  			}
   189  		}()
   190  	}
   191  }