Skip to content

Commit

Permalink
cue/load: simplify registry example
Browse files Browse the repository at this point in the history
CUE_EXPERIMENT=modules has been on by default for many months now,
and we will not revert that change at this point,
so it doesn't make any sense for the examples and tests to still
reach into the ./internal/cueexperiment package to set it to true.

Particularly since that confuses users reading the example,
as well as showing them internal APIs that they cannot use themselves.

While here, tweak the docs for Config.Registry a bit
to link the reader directly to the documentation for $CUE_REGISTRY.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I114cf047153cde3f00f0d18ddb96c6e92bbb9282
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202597
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
  • Loading branch information
mvdan committed Oct 18, 2024
1 parent 4642d77 commit 457a112
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
6 changes: 4 additions & 2 deletions cue/load/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,12 @@ type Config struct {
// Registry is used to fetch CUE module dependencies.
//
// When nil, [modconfig.NewRegistry] will be used to create a
// registry instance using the usual cmd/cue conventions for
// environment variables (but see the Env field below).
// registry instance using the variables set in [Config.Env]
// as documented in `[cue help registryconfig]`.
//
// THIS IS EXPERIMENTAL. API MIGHT CHANGE.
//
// [cue help registryconfig]: https://cuelang.org/docs/reference/command/cue-help-registryconfig/
Registry modconfig.Registry

// Env provides environment variables for use in the configuration.
Expand Down
19 changes: 5 additions & 14 deletions cue/load/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/load"
"cuelang.org/go/internal/cueexperiment"
"cuelang.org/go/internal/registrytest"
)

Expand Down Expand Up @@ -90,8 +89,9 @@ func Example() {

func Example_externalModules() {
// setUpModulesExample starts a temporary in-memory registry,
// populates it with an example module, and sets CUE_REGISTRY
// to refer to it
// populates it with an example module, and sets CUE_REGISTRY to refer to it.
// Users can leave [load.Config.Env] empty to use the default registry,
// or set one globally with os.Setenv("CUE_REGISTRY", "registry.myorg.com").
env, cleanup := setUpModulesExample()
defer cleanup()

Expand Down Expand Up @@ -139,21 +139,12 @@ value: "world"
if err != nil {
panic(err)
}
cleanups := []func(){registry.Close}
env = append(env, "CUE_REGISTRY="+registry.Host()+"+insecure")
// We also set up a temporary cache directory to fetch and extract modules into.
dir, err := os.MkdirTemp("", "")
if err != nil {
panic(err)
}
env = append(env, "CUE_CACHE_DIR="+dir)
oldModulesExperiment := cueexperiment.Flags.Modules
cueexperiment.Flags.Modules = true
cleanups = append(cleanups, func() {
cueexperiment.Flags.Modules = oldModulesExperiment
})
return env, func() {
for i := len(cleanups) - 1; i >= 0; i-- {
cleanups[i]()
}
}
return env, registry.Close
}
5 changes: 2 additions & 3 deletions cue/load/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ import (
)

func init() {
// Ignore the value of CUE_EXPERIMENT for the purposes
// of these tests, which we want to test both with the experiment
// enabled and disabled.
// Ignore the value of CUE_EXPERIMENT for the purposes of these tests,
// as we want them to start off with the default experiment values.
os.Setenv("CUE_EXPERIMENT", "")

// Once we've called cueexperiment.Init, cueexperiment.Vars
Expand Down
8 changes: 2 additions & 6 deletions cue/load/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ import (
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/load"
"cuelang.org/go/internal/cueexperiment"
"cuelang.org/go/internal/cuetxtar"
"cuelang.org/go/internal/registrytest"
"cuelang.org/go/mod/modcache"
)

func TestModuleLoadWithInvalidRegistryConfig(t *testing.T) {
// When the modules experiment is enabled and there's an invalid
// registry configuration, we shouldn't get an error unless the
// module actually tries to use a registry.
qt.Patch(t, &cueexperiment.Flags.Modules, true)
// When there's an invalid registry configuration,
// we shouldn't get an error unless the module actually tries to use a registry.
t.Setenv("CUE_REGISTRY", "invalid}host:")
cacheDir := t.TempDir()
t.Setenv("CUE_CACHE_DIR", cacheDir)
Expand Down Expand Up @@ -94,7 +91,6 @@ func TestModuleFetch(t *testing.T) {
// The fetched files are read-only, so testing fails when trying
// to remove them.
defer modcache.RemoveAll(tmpDir)
qt.Patch(t, &cueexperiment.Flags.Modules, true)
ctx := cuecontext.New()
insts := t.RawInstances()
if len(insts) != 1 {
Expand Down

0 comments on commit 457a112

Please sign in to comment.