cuelang.org/go@v0.13.0/internal/mod/modload/update_test.go (about) 1 package modload 2 3 import ( 4 "context" 5 "fmt" 6 "io/fs" 7 "path" 8 "path/filepath" 9 "strings" 10 "testing" 11 12 "cuelang.org/go/cue/ast" 13 "cuelang.org/go/internal/mod/semver" 14 "cuelang.org/go/mod/modcache" 15 "github.com/go-quicktest/qt" 16 "github.com/google/go-cmp/cmp" 17 "golang.org/x/tools/txtar" 18 ) 19 20 func TestUpdateVersions(t *testing.T) { 21 files, err := filepath.Glob("testdata/updateversions/*.txtar") 22 qt.Assert(t, qt.IsNil(err)) 23 for _, f := range files { 24 t.Run(f, func(t *testing.T) { 25 ar, err := txtar.ParseFile(f) 26 qt.Assert(t, qt.IsNil(err)) 27 tfs, err := txtar.FS(ar) 28 qt.Assert(t, qt.IsNil(err)) 29 reg, _ := newRegistry(t, tfs) 30 31 want, err := fs.ReadFile(tfs, "want") 32 qt.Assert(t, qt.IsNil(err)) 33 34 versionsData, _ := fs.ReadFile(tfs, "versions") 35 versions := strings.Fields(string(versionsData)) 36 37 var out strings.Builder 38 mf, err := UpdateVersions(context.Background(), tfs, ".", reg, versions) 39 if err != nil { 40 fmt.Fprintf(&out, "error: %v\n", err) 41 } else { 42 data, err := mf.Format() 43 qt.Assert(t, qt.IsNil(err)) 44 out.Write(data) 45 } 46 if diff := cmp.Diff(string(want), out.String()); diff != "" { 47 t.Log("actual result:\n", out.String()) 48 t.Fatalf("unexpected results (-want +got):\n%s", diff) 49 } 50 }) 51 } 52 } 53 54 func TestResolveAbsolutePackage(t *testing.T) { 55 files, err := filepath.Glob("testdata/resolveabsolutepackage/*.txtar") 56 qt.Assert(t, qt.IsNil(err)) 57 for _, f := range files { 58 t.Run(f, func(t *testing.T) { 59 ar, err := txtar.ParseFile(f) 60 qt.Assert(t, qt.IsNil(err)) 61 tfs, err := txtar.FS(ar) 62 qt.Assert(t, qt.IsNil(err)) 63 reg, cacheDir := newRegistry(t, tfs) 64 65 testEntries, err := fs.ReadDir(tfs, "tests") 66 qt.Assert(t, qt.IsNil(err)) 67 for _, e := range testEntries { 68 t.Run(e.Name(), func(t *testing.T) { 69 ctx := context.Background() 70 pkgData, err := fs.ReadFile(tfs, path.Join("tests", e.Name(), "package")) 71 qt.Assert(t, qt.IsNil(err)) 72 pkg := strings.TrimSpace(string(pkgData)) 73 74 wantData, err := fs.ReadFile(tfs, path.Join("tests", e.Name(), "want")) 75 qt.Assert(t, qt.IsNil(err)) 76 testResolve := func(reg Registry, p string) { 77 mv, loc, err := ResolveAbsolutePackage(ctx, reg, pkg) 78 var got strings.Builder 79 if err != nil { 80 fmt.Fprintf(&got, "ERROR: %v\n", err) 81 } else { 82 modLoc, err := reg.Fetch(ctx, mv) 83 qt.Assert(t, qt.IsNil(err)) 84 fmt.Fprintf(&got, "module: %v\n", mv) 85 rel := strings.TrimPrefix(loc.Dir, modLoc.Dir) 86 if rel == "" { 87 rel = "." 88 } 89 fmt.Fprintf(&got, "loc: %s\n", rel) 90 } 91 qt.Assert(t, qt.Equals(got.String(), string(wantData))) 92 } 93 testResolve(reg, pkg) 94 if strings.HasPrefix(string(wantData), "ERROR:") { 95 return 96 } 97 if v := ast.ParseImportPath(pkg).Version; v == "" || semver.Canonical(v) != v { 98 return 99 } 100 t.Logf("trying again with %v", pkg) 101 // The version is canonical and the query succeeded, so we should be able to run 102 // the same query again without hitting the registry. 103 // Check that by creating another cache registry instance that points 104 // to the same cache directory but has no backing network registry. 105 reg1, err := modcache.New(nil, cacheDir) 106 qt.Assert(t, qt.IsNil(err)) 107 testResolve(reg1, pkg) 108 }) 109 } 110 }) 111 } 112 }