Source file src/cmd/go/internal/vcs/discovery_test.go

     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 vcs
     6  
     7  import (
     8  	"reflect"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  var parseMetaGoImportsTests = []struct {
    14  	in  string
    15  	mod ModuleMode
    16  	out []metaImport
    17  }{
    18  	{
    19  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
    20  		IgnoreMod,
    21  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    22  	},
    23  	{
    24  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
    25  		<meta name="go-import" content="baz/quux git http://github.com/rsc/baz/quux">`,
    26  		IgnoreMod,
    27  		[]metaImport{
    28  			{"foo/bar", "git", "https://github.com/rsc/foo/bar"},
    29  			{"baz/quux", "git", "http://github.com/rsc/baz/quux"},
    30  		},
    31  	},
    32  	{
    33  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
    34  		<meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">`,
    35  		IgnoreMod,
    36  		[]metaImport{
    37  			{"foo/bar", "git", "https://github.com/rsc/foo/bar"},
    38  		},
    39  	},
    40  	{
    41  		`<meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">
    42  		<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
    43  		IgnoreMod,
    44  		[]metaImport{
    45  			{"foo/bar", "git", "https://github.com/rsc/foo/bar"},
    46  		},
    47  	},
    48  	{
    49  		`<meta name="go-import" content="foo/bar mod http://github.com/rsc/baz/quux">
    50  		<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
    51  		PreferMod,
    52  		[]metaImport{
    53  			{"foo/bar", "mod", "http://github.com/rsc/baz/quux"},
    54  		},
    55  	},
    56  	{
    57  		`<head>
    58  		<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
    59  		</head>`,
    60  		IgnoreMod,
    61  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    62  	},
    63  	{
    64  		`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
    65  		<body>`,
    66  		IgnoreMod,
    67  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    68  	},
    69  	{
    70  		`<!doctype html><meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
    71  		IgnoreMod,
    72  		[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
    73  	},
    74  	{
    75  		// XML doesn't like <div style=position:relative>.
    76  		`<!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>`,
    77  		IgnoreMod,
    78  		[]metaImport{{"chitin.io/chitin", "git", "https://github.com/chitin-io/chitin"}},
    79  	},
    80  	{
    81  		`<meta name="go-import" content="myitcv.io git https://github.com/myitcv/x">
    82  	        <meta name="go-import" content="myitcv.io/blah2 mod https://raw.githubusercontent.com/myitcv/pubx/master">
    83  	        `,
    84  		IgnoreMod,
    85  		[]metaImport{{"myitcv.io", "git", "https://github.com/myitcv/x"}},
    86  	},
    87  	{
    88  		`<meta name="go-import" content="myitcv.io git https://github.com/myitcv/x">
    89  	        <meta name="go-import" content="myitcv.io/blah2 mod https://raw.githubusercontent.com/myitcv/pubx/master">
    90  	        `,
    91  		PreferMod,
    92  		[]metaImport{
    93  			{"myitcv.io/blah2", "mod", "https://raw.githubusercontent.com/myitcv/pubx/master"},
    94  			{"myitcv.io", "git", "https://github.com/myitcv/x"},
    95  		},
    96  	},
    97  }
    98  
    99  func TestParseMetaGoImports(t *testing.T) {
   100  	for i, tt := range parseMetaGoImportsTests {
   101  		out, err := parseMetaGoImports(strings.NewReader(tt.in), tt.mod)
   102  		if err != nil {
   103  			t.Errorf("test#%d: %v", i, err)
   104  			continue
   105  		}
   106  		if !reflect.DeepEqual(out, tt.out) {
   107  			t.Errorf("test#%d:\n\thave %q\n\twant %q", i, out, tt.out)
   108  		}
   109  	}
   110  }
   111  

View as plain text