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