...

Source file src/golang.org/x/tools/internal/proxydir/proxydir_test.go

Documentation: golang.org/x/tools/internal/proxydir

     1  // Copyright 2020 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 proxydir
     6  
     7  import (
     8  	"archive/zip"
     9  	"fmt"
    10  	"io"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  func TestWriteModuleVersion(t *testing.T) {
    18  	tests := []struct {
    19  		modulePath, version string
    20  		files               map[string][]byte
    21  	}{
    22  		{
    23  			modulePath: "mod.test/module",
    24  			version:    "v1.2.3",
    25  			files: map[string][]byte{
    26  				"go.mod":   []byte("module mod.com\n\ngo 1.12"),
    27  				"const.go": []byte("package module\n\nconst Answer = 42"),
    28  			},
    29  		},
    30  		{
    31  			modulePath: "mod.test/module",
    32  			version:    "v1.2.4",
    33  			files: map[string][]byte{
    34  				"go.mod":   []byte("module mod.com\n\ngo 1.12"),
    35  				"const.go": []byte("package module\n\nconst Answer = 43"),
    36  			},
    37  		},
    38  		{
    39  			modulePath: "mod.test/nogomod",
    40  			version:    "v0.9.0",
    41  			files: map[string][]byte{
    42  				"const.go": []byte("package module\n\nconst Other = \"Other\""),
    43  			},
    44  		},
    45  	}
    46  	dir, err := os.MkdirTemp("", "proxydirtest-")
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	defer os.RemoveAll(dir)
    51  	for _, test := range tests {
    52  		// Since we later assert on the contents of /list, don't use subtests.
    53  		if err := WriteModuleVersion(dir, test.modulePath, test.version, test.files); err != nil {
    54  			t.Fatal(err)
    55  		}
    56  		rootDir := filepath.Join(dir, filepath.FromSlash(test.modulePath), "@v")
    57  		gomod, err := os.ReadFile(filepath.Join(rootDir, test.version+".mod"))
    58  		if err != nil {
    59  			t.Fatal(err)
    60  		}
    61  		wantMod, ok := test.files["go.mod"]
    62  		if !ok {
    63  			wantMod = []byte("module " + test.modulePath)
    64  		}
    65  		if got, want := string(gomod), string(wantMod); got != want {
    66  			t.Errorf("reading %s/@v/%s.mod: got %q, want %q", test.modulePath, test.version, got, want)
    67  		}
    68  		zr, err := zip.OpenReader(filepath.Join(rootDir, test.version+".zip"))
    69  		if err != nil {
    70  			t.Fatal(err)
    71  		}
    72  		defer zr.Close()
    73  
    74  		for _, zf := range zr.File {
    75  			r, err := zf.Open()
    76  			if err != nil {
    77  				t.Fatal(err)
    78  			}
    79  			defer r.Close()
    80  			content, err := io.ReadAll(r)
    81  			if err != nil {
    82  				t.Fatal(err)
    83  			}
    84  			name := strings.TrimPrefix(zf.Name, fmt.Sprintf("%s@%s/", test.modulePath, test.version))
    85  			if got, want := string(content), string(test.files[name]); got != want {
    86  				t.Errorf("unzipping %q: got %q, want %q", zf.Name, got, want)
    87  			}
    88  			delete(test.files, name)
    89  		}
    90  		for name := range test.files {
    91  			t.Errorf("file %q not present in the module zip", name)
    92  		}
    93  	}
    94  
    95  	lists := []struct {
    96  		modulePath, want string
    97  	}{
    98  		{"mod.test/module", "v1.2.3\nv1.2.4\n"},
    99  		{"mod.test/nogomod", "v0.9.0\n"},
   100  	}
   101  
   102  	for _, test := range lists {
   103  		fp := filepath.Join(dir, filepath.FromSlash(test.modulePath), "@v", "list")
   104  		list, err := os.ReadFile(fp)
   105  		if err != nil {
   106  			t.Fatal(err)
   107  		}
   108  		if got := string(list); got != test.want {
   109  			t.Errorf("%q/@v/list: got %q, want %q", test.modulePath, got, test.want)
   110  		}
   111  	}
   112  }
   113  

View as plain text