...

Source file src/cuelang.org/go/cue/load/module_test.go

Documentation: cuelang.org/go/cue/load

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

View as plain text