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