1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
package modload
import (
"archive/zip"
"bytes"
"context"
"fmt"
"io"
"io/fs"
"path/filepath"
"strings"
"testing"
"cuelabs.dev/go/oci/ociregistry/ociclient"
"github.com/go-quicktest/qt"
"github.com/google/go-cmp/cmp"
"golang.org/x/tools/txtar"
"cuelang.org/go/internal/registrytest"
"cuelang.org/go/mod/modfile"
"cuelang.org/go/mod/modregistry"
"cuelang.org/go/mod/module"
)
func TestTidy(t *testing.T) {
files, err := filepath.Glob("testdata/tidy/*.txtar")
qt.Assert(t, qt.IsNil(err))
for _, f := range files {
t.Run(f, func(t *testing.T) {
ar, err := txtar.ParseFile(f)
qt.Assert(t, qt.IsNil(err))
tfs, err := txtar.FS(ar)
qt.Assert(t, qt.IsNil(err))
reg := newRegistry(t, tfs)
want, err := fs.ReadFile(tfs, "want")
qt.Assert(t, qt.IsNil(err))
err = CheckTidy(context.Background(), tfs, ".", reg)
wantCheckTidyError := stringFromFile(tfs, "tidy-check-error")
if wantCheckTidyError == "" {
qt.Check(t, qt.IsNil(err))
} else {
qt.Check(t, qt.ErrorMatches(err, wantCheckTidyError))
}
var out strings.Builder
var tidyFile []byte
mf, err := Tidy(context.Background(), tfs, ".", reg)
if err != nil {
fmt.Fprintf(&out, "error: %v\n", err)
} else {
tidyFile, err = mf.Format()
qt.Assert(t, qt.IsNil(err))
out.Write(tidyFile)
}
if diff := cmp.Diff(string(want), out.String()); diff != "" {
t.Log("actual result:\n", out.String())
t.Fatalf("unexpected results (-want +got):\n%s", diff)
}
// Ensure that CheckTidy does not error after a successful Tidy.
// We make a new txtar FS given that an FS is read-only.
if len(tidyFile) > 0 {
for i := range ar.Files {
file := &ar.Files[i]
if file.Name == "cue.mod/module.cue" {
file.Data = []byte(out.String())
}
}
tfs, err := txtar.FS(ar)
qt.Assert(t, qt.IsNil(err))
err = CheckTidy(context.Background(), tfs, ".", reg)
qt.Check(t, qt.IsNil(err), qt.Commentf("CheckTidy after a successful Tidy should not fail"))
}
})
}
}
func stringFromFile(fsys fs.FS, file string) string {
data, _ := fs.ReadFile(fsys, file)
return strings.TrimSpace(string(data))
}
func newRegistry(t *testing.T, fsys fs.FS) Registry {
fsys, err := fs.Sub(fsys, "_registry")
qt.Assert(t, qt.IsNil(err))
regSrv, err := registrytest.New(fsys, "")
qt.Assert(t, qt.IsNil(err))
t.Cleanup(regSrv.Close)
regOCI, err := ociclient.New(regSrv.Host(), &ociclient.Options{
Insecure: true,
})
qt.Assert(t, qt.IsNil(err))
return ®istryImpl{modregistry.NewClient(regOCI)}
}
type registryImpl struct {
reg *modregistry.Client
}
func (r *registryImpl) Requirements(ctx context.Context, mv module.Version) ([]module.Version, error) {
m, err := r.reg.GetModule(ctx, mv)
if err != nil {
return nil, err
}
data, err := m.ModuleFile(ctx)
if err != nil {
return nil, fmt.Errorf("cannot get module file from %v: %v", m, err)
}
mf, err := modfile.Parse(data, mv.String())
if err != nil {
return nil, fmt.Errorf("cannot parse module file from %v: %v", m, err)
}
return mf.DepVersions(), nil
}
// getModContents downloads the module with the given version
// and returns the directory where it's stored.
func (c *registryImpl) Fetch(ctx context.Context, mv module.Version) (module.SourceLoc, error) {
m, err := c.reg.GetModule(ctx, mv)
if err != nil {
return module.SourceLoc{}, err
}
r, err := m.GetZip(ctx)
if err != nil {
return module.SourceLoc{}, err
}
defer r.Close()
zipData, err := io.ReadAll(r)
if err != nil {
return module.SourceLoc{}, err
}
zipr, err := zip.NewReader(bytes.NewReader(zipData), int64(len(zipData)))
if err != nil {
return module.SourceLoc{}, err
}
return module.SourceLoc{
FS: zipr,
Dir: ".",
}, nil
}
func (r *registryImpl) ModuleVersions(ctx context.Context, mpath string) ([]string, error) {
versions, err := r.reg.ModuleVersions(ctx, mpath)
if err != nil {
return nil, fmt.Errorf("cannot obtain versions for module %q: %v", mpath, err)
}
return versions, nil
}
|