Skip to content

Commit

Permalink
cmd/cue: add test for bad path lookup
Browse files Browse the repository at this point in the history
I have observed that `cue mod tidy` can fail when
looking up package paths that are not valid module
paths. This CL adds a test case for that error,
so we can fix it in a subsequent CL.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I9b0d08a0d578ff44b1938e6f9ce21e705ceb11b3
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199211
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
rogpeppe committed Aug 13, 2024
1 parent bddcb54 commit 9a9dd17
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
31 changes: 28 additions & 3 deletions cmd/cue/cmd/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
"testing"
Expand All @@ -36,6 +37,7 @@ import (
"cuelabs.dev/go/oci/ociregistry/ociclient"
"cuelabs.dev/go/oci/ociregistry/ocimem"
"cuelabs.dev/go/oci/ociregistry/ociref"
"cuelabs.dev/go/oci/ociregistry/ociserver"
"github.com/google/shlex"
"github.com/rogpeppe/go-internal/goproxytest"
"github.com/rogpeppe/go-internal/gotooltest"
Expand Down Expand Up @@ -287,20 +289,43 @@ func TestScript(t *testing.T) {
if data, err := os.ReadFile(filepath.Join(e.WorkDir, "_registry"+regID+"_prefix")); err == nil {
prefix = strings.TrimSpace(string(data))
}
useProxy := false
proxyFile := filepath.Join(e.WorkDir, "_registry"+regID+"_proxy")
if data, err := os.ReadFile(proxyFile); err == nil {
useProxy, err = strconv.ParseBool(strings.TrimSpace(string(data)))
if err != nil {
return fmt.Errorf("invalid contents of proxy file %q: %v", proxyFile, err)
}
}
reg, err := registrytest.New(os.DirFS(registryDir), prefix)
if err != nil {
return fmt.Errorf("cannot start test registry server: %v", err)
}
e.Defer(reg.Close)
if prefix != "" {
prefix = "/" + prefix
}
regHost := reg.Host()
if useProxy {
// Use a proxy for the registry, mirroring the way that the Central Registry
// works.
proxyClient, err := ociclient.New(regHost, &ociclient.Options{
Insecure: true,
})
if err != nil {
return fmt.Errorf("cannot create oci proxy client")
}
reg2 := httptest.NewServer(ociserver.New(proxyClient, nil))
reg2URL, _ := url.Parse(reg2.URL)
regHost = reg2URL.Host
e.Defer(reg2.Close)
}
e.Vars = append(e.Vars,
"CUE_REGISTRY"+regID+"="+reg.Host()+prefix+"+insecure",
"CUE_REGISTRY"+regID+"="+regHost+prefix+"+insecure",
// This enables some tests to construct their own malformed
// CUE_REGISTRY values that still refer to the test registry.
"DEBUG_REGISTRY"+regID+"_HOST="+reg.Host(),
"DEBUG_REGISTRY"+regID+"_HOST="+regHost,
)
e.Defer(reg.Close)
}
return nil
},
Expand Down
56 changes: 56 additions & 0 deletions cmd/cue/cmd/testdata/script/modtidy_badpath.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# When the module resolution logic queries a registry for possible
# module matches, it tries many possible prefixes. Some prefixes
# might not be legitimate repository names even though they
# are OK to use as package names. In such a case, a registry
# might not return a 404 error, but some other kind of error.
# The OCI proxy is one such registry.

# TODO fix this so it works OK.
! exec cue mod tidy
cmp stderr want-stderr

# TODO enable these assertions.
# cmp cue.mod/module.cue want-module
# exec cue export
# cmp stdout want-stdout

-- want-stderr --
failed to resolve "test.example/foo/BarBaz": module test.example/foo/BarBaz: invalid OCI request: name invalid: invalid repository name
-- want-module --
module: "cue.example@v0"
language: {
version: "v0.9.2"
}
deps: {
"test.example/foo@v0": {
v: "v0.0.1"
default: true
}
}
-- cue.mod/module.cue --
module: "cue.example@v0"
language: version: "v0.9.2"

-- want-stdout --
{
"self": "test.example/foo/BarBaz"
}
-- main.cue --
package main

// Note: test.example/foo/BarBaz is an illegal module path
// (because it contains upper case) but is OK as a package path.
import "test.example/foo/BarBaz"

BarBaz

-- _registry_proxy --
true
-- _registry/test.example_foo_v0.0.1/cue.mod/module.cue --
module: "test.example/foo@v0"
language: version: "v0.9.2"

-- _registry/test.example_foo_v0.0.1/BarBaz/x.cue --
package BarBaz

self: "test.example/foo/BarBaz"

0 comments on commit 9a9dd17

Please sign in to comment.