github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/cmd/go/internal/modconv/convert_test.go (about)

     1  // Copyright 2018 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 modconv
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"internal/testenv"
    11  	"io/ioutil"
    12  	"log"
    13  	"os"
    14  	"os/exec"
    15  	"path/filepath"
    16  	"strings"
    17  	"testing"
    18  
    19  	"cmd/go/internal/cfg"
    20  	"cmd/go/internal/modfetch"
    21  	"cmd/go/internal/modfetch/codehost"
    22  	"cmd/go/internal/modfile"
    23  	"cmd/go/internal/module"
    24  )
    25  
    26  func TestMain(m *testing.M) {
    27  	os.Exit(testMain(m))
    28  }
    29  
    30  func testMain(m *testing.M) int {
    31  	if _, err := exec.LookPath("git"); err != nil {
    32  		fmt.Fprintln(os.Stderr, "skipping because git binary not found")
    33  		fmt.Println("PASS")
    34  		return 0
    35  	}
    36  
    37  	dir, err := ioutil.TempDir("", "modconv-test-")
    38  	if err != nil {
    39  		log.Fatal(err)
    40  	}
    41  	defer os.RemoveAll(dir)
    42  	modfetch.PkgMod = filepath.Join(dir, "pkg/mod")
    43  	codehost.WorkRoot = filepath.Join(dir, "codework")
    44  
    45  	return m.Run()
    46  }
    47  
    48  func TestConvertLegacyConfig(t *testing.T) {
    49  	testenv.MustHaveExternalNetwork(t)
    50  
    51  	if testing.Verbose() {
    52  		old := cfg.BuildX
    53  		defer func() {
    54  			cfg.BuildX = old
    55  		}()
    56  		cfg.BuildX = true
    57  	}
    58  
    59  	var tests = []struct {
    60  		path  string
    61  		vers  string
    62  		gomod string
    63  	}{
    64  		/*
    65  			Different versions of git seem to find or not find
    66  			github.com/Masterminds/semver's a93e51b5a57e,
    67  			which is an unmerged pull request.
    68  			We'd rather not provide access to unmerged pull requests,
    69  			so the line is removed from the golden file here,
    70  			but some git commands still find it somehow.
    71  
    72  			{
    73  				// Gopkg.lock parsing.
    74  				"github.com/golang/dep", "v0.4.0",
    75  				`module github.com/golang/dep
    76  
    77  				require (
    78  					github.com/Masterminds/vcs v1.11.1
    79  					github.com/armon/go-radix v0.0.0-20160115234725-4239b77079c7
    80  					github.com/boltdb/bolt v1.3.1
    81  					github.com/go-yaml/yaml v0.0.0-20170407172122-cd8b52f8269e
    82  					github.com/golang/protobuf v0.0.0-20170901042739-5afd06f9d81a
    83  					github.com/jmank88/nuts v0.3.0
    84  					github.com/nightlyone/lockfile v0.0.0-20170707060451-e83dc5e7bba0
    85  					github.com/pelletier/go-toml v0.0.0-20171218135716-b8b5e7696574
    86  					github.com/pkg/errors v0.8.0
    87  					github.com/sdboyer/constext v0.0.0-20170321163424-836a14457353
    88  					golang.org/x/net v0.0.0-20170828231752-66aacef3dd8a
    89  					golang.org/x/sync v0.0.0-20170517211232-f52d1811a629
    90  					golang.org/x/sys v0.0.0-20170830134202-bb24a47a89ea
    91  				)`,
    92  			},
    93  		*/
    94  
    95  		// TODO: https://github.com/docker/distribution uses vendor.conf
    96  
    97  		{
    98  			// Godeps.json parsing.
    99  			// TODO: Should v2.0.0 work here too?
   100  			"github.com/docker/distribution", "v0.0.0-20150410205453-85de3967aa93",
   101  			`module github.com/docker/distribution
   102  
   103  			require (
   104  				github.com/AdRoll/goamz v0.0.0-20150130162828-d3664b76d905
   105  				github.com/MSOpenTech/azure-sdk-for-go v0.0.0-20150323223030-d90753bcad2e
   106  				github.com/Sirupsen/logrus v0.7.3
   107  				github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd
   108  				github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b
   109  				github.com/bugsnag/panicwrap v0.0.0-20141110184334-e5f9854865b9
   110  				github.com/codegangsta/cli v0.0.0-20150131031259-6086d7927ec3
   111  				github.com/docker/docker v0.0.0-20150204013315-165ea5c158cf
   112  				github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1
   113  				github.com/garyburd/redigo v0.0.0-20150301180006-535138d7bcd7
   114  				github.com/gorilla/context v0.0.0-20140604161150-14f550f51af5
   115  				github.com/gorilla/handlers v0.0.0-20140825150757-0e84b7d810c1
   116  				github.com/gorilla/mux v0.0.0-20140926153814-e444e69cbd2e
   117  				github.com/jlhawn/go-crypto v0.0.0-20150401213827-cd738dde20f0
   118  				github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43
   119  				github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50
   120  				github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f
   121  				golang.org/x/net v0.0.0-20150202051010-1dfe7915deaf
   122  				gopkg.in/check.v1 v1.0.0-20141024133853-64131543e789
   123  				gopkg.in/yaml.v2 v2.0.0-20150116202057-bef53efd0c76
   124  			)`,
   125  		},
   126  
   127  		{
   128  			// golang.org/issue/24585 - confusion about v2.0.0 tag in legacy non-v2 module
   129  			"github.com/fishy/gcsbucket", "v0.0.0-20150410205453-618d60fe84e0",
   130  			`module github.com/fishy/gcsbucket
   131  
   132  			require (
   133  				cloud.google.com/go v0.18.0
   134  				github.com/fishy/fsdb v0.0.0-20180217030800-5527ded01371
   135  				github.com/golang/protobuf v1.0.0
   136  				github.com/googleapis/gax-go v2.0.0+incompatible
   137  				golang.org/x/net v0.0.0-20180216171745-136a25c244d3
   138  				golang.org/x/oauth2 v0.0.0-20180207181906-543e37812f10
   139  				golang.org/x/text v0.0.0-20180208041248-4e4a3210bb54
   140  				google.golang.org/api v0.0.0-20180217000815-c7a403bb5fe1
   141  				google.golang.org/appengine v1.0.0
   142  				google.golang.org/genproto v0.0.0-20180206005123-2b5a72b8730b
   143  				google.golang.org/grpc v1.10.0
   144  			)`,
   145  		},
   146  	}
   147  
   148  	for _, tt := range tests {
   149  		t.Run(strings.ReplaceAll(tt.path, "/", "_")+"_"+tt.vers, func(t *testing.T) {
   150  			f, err := modfile.Parse("golden", []byte(tt.gomod), nil)
   151  			if err != nil {
   152  				t.Fatal(err)
   153  			}
   154  			want, err := f.Format()
   155  			if err != nil {
   156  				t.Fatal(err)
   157  			}
   158  
   159  			dir, err := modfetch.Download(module.Version{Path: tt.path, Version: tt.vers})
   160  			if err != nil {
   161  				t.Fatal(err)
   162  			}
   163  
   164  			for name := range Converters {
   165  				file := filepath.Join(dir, name)
   166  				data, err := ioutil.ReadFile(file)
   167  				if err == nil {
   168  					f := new(modfile.File)
   169  					f.AddModuleStmt(tt.path)
   170  					if err := ConvertLegacyConfig(f, filepath.ToSlash(file), data); err != nil {
   171  						t.Fatal(err)
   172  					}
   173  					out, err := f.Format()
   174  					if err != nil {
   175  						t.Fatalf("format after conversion: %v", err)
   176  					}
   177  					if !bytes.Equal(out, want) {
   178  						t.Fatalf("final go.mod:\n%s\n\nwant:\n%s", out, want)
   179  					}
   180  					return
   181  				}
   182  			}
   183  			t.Fatalf("no converter found for %s@%s", tt.path, tt.vers)
   184  		})
   185  	}
   186  }