cuelang.org/go@v0.13.0/cue/load/module_test.go (about) 1 package load_test 2 3 import ( 4 "fmt" 5 "io/fs" 6 "os" 7 "path/filepath" 8 "strings" 9 "testing" 10 11 "github.com/go-quicktest/qt" 12 "golang.org/x/tools/txtar" 13 14 "cuelang.org/go/cue/cuecontext" 15 "cuelang.org/go/cue/errors" 16 "cuelang.org/go/cue/load" 17 "cuelang.org/go/internal/cuetxtar" 18 "cuelang.org/go/mod/modcache" 19 "cuelang.org/go/mod/modregistrytest" 20 ) 21 22 func TestModuleLoadWithInvalidRegistryConfig(t *testing.T) { 23 // When there's an invalid registry configuration, 24 // we shouldn't get an error unless the module actually tries to use a registry. 25 t.Setenv("CUE_REGISTRY", "invalid}host:") 26 cacheDir := t.TempDir() 27 t.Setenv("CUE_CACHE_DIR", cacheDir) 28 29 insts := load.Instances([]string{"./imports"}, &load.Config{ 30 Dir: filepath.Join("testdata", "testmod"), 31 }) 32 qt.Assert(t, qt.IsNil(insts[0].Err)) 33 34 // Check that nothing has been created in the cache directory (no 35 // side-effects). 36 entries, err := os.ReadDir(cacheDir) 37 qt.Assert(t, qt.IsNil(err)) 38 qt.Assert(t, qt.HasLen(entries, 0)) 39 40 // Now check that we do get an error when we try to use a module 41 // that requires module resolution. 42 testData, err := os.ReadFile(filepath.Join("testdata", "testfetch", "simple.txtar")) 43 qt.Assert(t, qt.IsNil(err)) 44 45 cfg := &load.Config{ 46 Dir: t.TempDir(), 47 Overlay: map[string]load.Source{}, 48 } 49 a := txtar.Parse(testData) 50 for _, f := range a.Files { 51 if !strings.HasPrefix(f.Name, "_registry/") { 52 cfg.Overlay[filepath.Join(cfg.Dir, f.Name)] = load.FromBytes(f.Data) 53 } 54 } 55 insts = load.Instances([]string{"."}, cfg) 56 qt.Assert(t, qt.ErrorMatches(insts[0].Err, `import failed: .*main.cue:2:8: cannot find package "example.com@v0": cannot fetch example.com@v0.0.1: bad value for registry: invalid registry "invalid}host:": invalid host name "invalid}host:" in registry`)) 57 58 // Try again with environment variables passed in Env. 59 // This is really just a smoke test to make sure that Env is 60 // passed through to the underlying modconfig call. 61 cfg.Env = []string{ 62 "CUE_REGISTRY=invalid}host2:", 63 "CUE_CACHE_DIR=" + cacheDir, 64 } 65 insts = load.Instances([]string{"."}, cfg) 66 qt.Assert(t, qt.ErrorMatches(insts[0].Err, `import failed: .*main.cue:2:8: cannot find package "example.com@v0": cannot fetch example.com@v0.0.1: bad value for registry: invalid registry "invalid}host2:": invalid host name "invalid}host2:" in registry`)) 67 } 68 69 func TestModuleFetch(t *testing.T) { 70 test := cuetxtar.TxTarTest{ 71 Root: "./testdata/testfetch", 72 Name: "modfetch", 73 } 74 test.Run(t, func(t *cuetxtar.Test) { 75 tfs, err := txtar.FS(t.Archive) 76 qt.Assert(t, qt.IsNil(err)) 77 rfs, err := fs.Sub(tfs, "_registry") 78 qt.Assert(t, qt.IsNil(err)) 79 r, err := modregistrytest.New(rfs, "") 80 qt.Assert(t, qt.IsNil(err)) 81 defer r.Close() 82 83 tmpDir := t.TempDir() 84 t.LoadConfig.Env = []string{ 85 "CUE_CACHE_DIR=" + filepath.Join(tmpDir, "cache"), 86 "CUE_REGISTRY=" + r.Host() + "+insecure", 87 "CUE_CONFIG_DIR=" + filepath.Join(tmpDir, "config"), 88 } 89 // The fetched files are read-only, so testing fails when trying 90 // to remove them. 91 defer modcache.RemoveAll(tmpDir) 92 ctx := cuecontext.New() 93 insts := t.RawInstances() 94 if len(insts) != 1 { 95 t.Fatalf("wrong instance count; got %d want 1", len(insts)) 96 } 97 inst := insts[0] 98 if inst.Err != nil { 99 errors.Print(t.Writer("error"), inst.Err, &errors.Config{ 100 ToSlash: true, 101 Cwd: t.Dir, 102 }) 103 return 104 } 105 v := ctx.BuildInstance(inst) 106 if err := v.Validate(); err != nil { 107 t.Fatal(err) 108 } 109 fmt.Fprintf(t, "%v\n", v) 110 }) 111 }