Source file src/cmd/go/internal/modconv/modconv_test.go

     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  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  )
    14  
    15  var extMap = map[string]string{
    16  	".dep":       "Gopkg.lock",
    17  	".glide":     "glide.lock",
    18  	".glock":     "GLOCKFILE",
    19  	".godeps":    "Godeps/Godeps.json",
    20  	".tsv":       "dependencies.tsv",
    21  	".vconf":     "vendor.conf",
    22  	".vjson":     "vendor/vendor.json",
    23  	".vyml":      "vendor.yml",
    24  	".vmanifest": "vendor/manifest",
    25  }
    26  
    27  func Test(t *testing.T) {
    28  	tests, _ := filepath.Glob("testdata/*")
    29  	if len(tests) == 0 {
    30  		t.Fatalf("no tests found")
    31  	}
    32  	for _, test := range tests {
    33  		file := filepath.Base(test)
    34  		ext := filepath.Ext(file)
    35  		if ext == ".out" {
    36  			continue
    37  		}
    38  		t.Run(file, func(t *testing.T) {
    39  			if extMap[ext] == "" {
    40  				t.Fatal("unknown extension")
    41  			}
    42  			if Converters[extMap[ext]] == nil {
    43  				t.Fatalf("Converters[%q] == nil", extMap[ext])
    44  			}
    45  			data, err := os.ReadFile(test)
    46  			if err != nil {
    47  				t.Fatal(err)
    48  			}
    49  			out, err := Converters[extMap[ext]](test, data)
    50  			if err != nil {
    51  				t.Fatal(err)
    52  			}
    53  			want, err := os.ReadFile(test[:len(test)-len(ext)] + ".out")
    54  			if err != nil {
    55  				t.Error(err)
    56  			}
    57  			var buf bytes.Buffer
    58  			for _, r := range out.Require {
    59  				fmt.Fprintf(&buf, "%s %s\n", r.Mod.Path, r.Mod.Version)
    60  			}
    61  			for _, r := range out.Replace {
    62  				fmt.Fprintf(&buf, "replace: %s %s %s %s\n", r.Old.Path, r.Old.Version, r.New.Path, r.New.Version)
    63  			}
    64  			if !bytes.Equal(buf.Bytes(), want) {
    65  				t.Errorf("have:\n%s\nwant:\n%s", buf.Bytes(), want)
    66  			}
    67  		})
    68  	}
    69  }
    70  

View as plain text