Skip to content

Commit f7e48bb

Browse files
committed
cmd/cue: add tests for module path errors
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]>
1 parent 8986233 commit f7e48bb

File tree

3 files changed

+518
-29
lines changed

3 files changed

+518
-29
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Check that the error messages for a bad module path are reasonably
2+
# self-explanatory. See https://cuelang.org/issue/3022
3+
4+
! exec cue mod init github.com/FooBar
5+
cmp stderr want-stderr-1
6+
7+
! exec cue mod init github.com/foo/.bar
8+
cmp stderr want-stderr-2
9+
10+
! exec cue mod init github.com/foo/bar.
11+
cmp stderr want-stderr-3
12+
13+
! exec cue mod init github.com/foo..bar
14+
cmp stderr want-stderr-4
15+
16+
-- want-stderr-1 --
17+
malformed module path "github.com/FooBar": non-conforming path "github.com/FooBar"
18+
-- want-stderr-2 --
19+
malformed module path "github.com/foo/.bar": leading dot in path element
20+
-- want-stderr-3 --
21+
malformed module path "github.com/foo/bar.": trailing dot in path element
22+
-- want-stderr-4 --
23+
malformed module path "github.com/foo..bar": non-conforming path "github.com/foo..bar"

internal/tdtest/update.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,15 @@ func (s *set[T]) getInfo(file string) *info {
144144
def := ti.Uses[ident]
145145
pos := def.Pos()
146146

147-
// - locate the CompositLit in the AST based on position.
148-
v, ok := findVar(pos, f).(*ast.CompositeLit)
147+
// - locate the CompositeLit in the AST based on position.
148+
v0 := findVar(pos, f)
149+
if v0 == nil {
150+
t.Fatalf("cannot find composite literal in source code")
151+
}
152+
v, ok := v0.(*ast.CompositeLit)
149153
if !ok {
150154
// generics should avoid this.
151-
t.Fatalf("expected composite literal, found %T", v)
155+
t.Fatalf("expected composite literal, found %T", v0)
152156
}
153157
info.table = v
154158

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

259-
func findVar(pos token.Pos, n ast.Node) (ret ast.Expr) {
260-
ast.Inspect(n, func(n ast.Node) bool {
261-
if as, ok := n.(*ast.AssignStmt); ok {
262-
for i, v := range as.Lhs {
263+
func findVar(pos token.Pos, n0 ast.Node) (ret ast.Expr) {
264+
ast.Inspect(n0, func(n ast.Node) bool {
265+
if n == nil {
266+
return true
267+
}
268+
switch n := n.(type) {
269+
case *ast.AssignStmt:
270+
for i, v := range n.Lhs {
271+
if v.Pos() == pos {
272+
ret = n.Rhs[i]
273+
}
274+
}
275+
return false
276+
case *ast.ValueSpec:
277+
for i, v := range n.Names {
263278
if v.Pos() == pos {
264-
ret = as.Rhs[i]
279+
ret = n.Values[i]
265280
}
266281
}
267282
return false

0 commit comments

Comments
 (0)