Skip to content

Commit

Permalink
cmd/cue: add tests for module path errors
Browse files Browse the repository at this point in the history
This adds some tests for some errors that will be improved in
the next CL in this chain.

We also add tests for some of the module path checking functions
that were not properly unit tested previously so we can be sure
that their behavior does not change (an earlier iteration _did_
unexpectedly change the behavior of `CheckImportPath`).

As the test table is shared between several test functions (useful,
as it's nice to see the results for each check function across all
the input data), we needed to update the `tdtest` package to support
tables defined at the top level. We include this change in this CL
because then it's clear that it actually works.

For #3022

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: I7cd5da04858c7610dcadfa6a28dff8a0f2e6ea77
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198135
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
rogpeppe committed Jul 22, 2024
1 parent 8986233 commit f7e48bb
Show file tree
Hide file tree
Showing 3 changed files with 518 additions and 29 deletions.
23 changes: 23 additions & 0 deletions cmd/cue/cmd/testdata/script/modinit_badpath.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Check that the error messages for a bad module path are reasonably
# self-explanatory. See https://cuelang.org/issue/3022

! exec cue mod init github.com/FooBar
cmp stderr want-stderr-1

! exec cue mod init github.com/foo/.bar
cmp stderr want-stderr-2

! exec cue mod init github.com/foo/bar.
cmp stderr want-stderr-3

! exec cue mod init github.com/foo..bar
cmp stderr want-stderr-4

-- want-stderr-1 --
malformed module path "github.com/FooBar": non-conforming path "github.com/FooBar"
-- want-stderr-2 --
malformed module path "github.com/foo/.bar": leading dot in path element
-- want-stderr-3 --
malformed module path "github.com/foo/bar.": trailing dot in path element
-- want-stderr-4 --
malformed module path "github.com/foo..bar": non-conforming path "github.com/foo..bar"
31 changes: 23 additions & 8 deletions internal/tdtest/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,15 @@ func (s *set[T]) getInfo(file string) *info {
def := ti.Uses[ident]
pos := def.Pos()

// - locate the CompositLit in the AST based on position.
v, ok := findVar(pos, f).(*ast.CompositeLit)
// - locate the CompositeLit in the AST based on position.
v0 := findVar(pos, f)
if v0 == nil {
t.Fatalf("cannot find composite literal in source code")
}
v, ok := v0.(*ast.CompositeLit)
if !ok {
// generics should avoid this.
t.Fatalf("expected composite literal, found %T", v)
t.Fatalf("expected composite literal, found %T", v0)
}
info.table = v

Expand Down Expand Up @@ -256,12 +260,23 @@ func (i *info) findCalls(block *ast.BlockStmt, names ...string) []*callInfo {
return a
}

func findVar(pos token.Pos, n ast.Node) (ret ast.Expr) {
ast.Inspect(n, func(n ast.Node) bool {
if as, ok := n.(*ast.AssignStmt); ok {
for i, v := range as.Lhs {
func findVar(pos token.Pos, n0 ast.Node) (ret ast.Expr) {
ast.Inspect(n0, func(n ast.Node) bool {
if n == nil {
return true
}
switch n := n.(type) {
case *ast.AssignStmt:
for i, v := range n.Lhs {
if v.Pos() == pos {
ret = n.Rhs[i]
}
}
return false
case *ast.ValueSpec:
for i, v := range n.Names {
if v.Pos() == pos {
ret = as.Rhs[i]
ret = n.Values[i]
}
}
return false
Expand Down
Loading

0 comments on commit f7e48bb

Please sign in to comment.