Skip to content

Commit

Permalink
cmd/cue,cue/load: add tests for underscore packages
Browse files Browse the repository at this point in the history
This change adds some tests that cover CUE's behavior
with respect to underscore package identifiers.
See issue #3167 for some previous discussion
on this area.

For #3167
For #3244

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Ib99400373e49065be6cbe50642c771a19df571d5
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196719
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
rogpeppe committed Jun 25, 2024
1 parent b70e543 commit 9295a20
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 3 deletions.
2 changes: 0 additions & 2 deletions cmd/cue/cmd/testdata/script/load_pkg.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ cd ..
-- data.cue --
foo:3
-- t.cue --
package _

foo: int
bar: 3
-- kube.cue --
Expand Down
34 changes: 34 additions & 0 deletions cmd/cue/cmd/testdata/script/load_underscore.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Test whether an import with a _ qualifer is allowed or not.

# TODO(rog): this should fail; see https://cuelang.org/issue/3167
exec cue export ./foo
cmp stdout want-stdout

# Check that it also fails with an explicit _ qualifier
# specified on the command line.
# TODO(rog): this should fail similarly.
exec cue export other.example/m:_
cmp stdout want-stdout
-- want-stdout --
{
"x": 20
}
-- cue.mod/gen/other.example/m/m.cue --
package _
x: 20

-- cue.mod/module.cue --
module: "test.example/foo"
language: version: "v0.9.0"

-- foo/foo.cue --
// Note: the package name does not match the directory name,
// which means that this is only resolvable with an explicit
// qualifier when used as an import path, but is allowed
// on the cue command line because there's only one
// package in the directory.
package bar

import m "other.example/m:_"

x: m.x
35 changes: 35 additions & 0 deletions cmd/cue/cmd/testdata/script/load_with_unnamed_pkg.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Test that the command-line "allow single package
# even when the package name does not match the import
# path" logic works correctly in the presence of an
# non-package CUE file, which can never be part
# of a package.
# See https://cuelang.org/issue/3244

cd foo
# TODO(rog): this should succeed
! exec cue export
cmp stderr $WORK/want-stderr
-- want-stderr --
test.example/foo@v0: import failed: no dependency found for package "other.example/m":
./foo.cue:8:8
-- cue.mod/gen/other.example/m/m.cue --
package m
x: 20

-- cue.mod/module.cue --
module: "test.example"
language: version: "v0.9.0"

-- foo/foo.cue --
// Note: the package name does not match the directory name,
// which means that this is only resolvable with an explicit
// qualifier when used as an import path, but is allowed
// on the cue command line because there's only one
// package in the directory.
package bar

import "other.example/m"

x: m.x
-- foo/unnamed.cue --
import ()
33 changes: 32 additions & 1 deletion cue/load/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,38 @@ display:.
files:
$CWD/testdata/testmod/multi3/other.cue
imports:
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`}}
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`}, {
// Test that we can explicitly ask for non-package
// CUE files by setting Config.Package to "_".
name: "ExplicitPackageWithUnqualifiedImportPath#4",
cfg: &Config{
Dir: filepath.Join(testdataDir, "multi4"),
Package: "_",
},
args: []string{"."},
want: `path: mod.test/test/multi4@v0:_
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/multi4
display:.
files:
$CWD/testdata/testmod/anon.cue
$CWD/testdata/testmod/multi4/nopackage1.cue
$CWD/testdata/testmod/multi4/nopackage2.cue`}, {
// Test what happens when there's a single CUE file
// with an explicit `package _` directive.
name: "ExplicitPackageWithUnqualifiedImportPath#5",
cfg: &Config{
Dir: filepath.Join(testdataDir, "multi5"),
Package: "_",
},
args: []string{"."},
want: `err: found packages "main" (file.cue) and "_" (nopackage.cue) in "multi5"
path: ""
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: ""
display:""`}}
tdtest.Run(t, testCases, func(t *tdtest.T, tc *loadTest) {
pkgs := Instances(tc.args, tc.cfg)

Expand Down
2 changes: 2 additions & 0 deletions cue/load/testdata/testmod/multi/nopackage1.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x: 1

5 changes: 5 additions & 0 deletions cue/load/testdata/testmod/multi4/file.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Test data - not compiled.

package main

{}
5 changes: 5 additions & 0 deletions cue/load/testdata/testmod/multi4/file_appengine.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Test data - not compiled.

package test_package

{}
2 changes: 2 additions & 0 deletions cue/load/testdata/testmod/multi4/nopackage1.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x: 1

7 changes: 7 additions & 0 deletions cue/load/testdata/testmod/multi4/nopackage2.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// The underscore package should be treated the same as an omitted package
// clause.

package _

x: 1

5 changes: 5 additions & 0 deletions cue/load/testdata/testmod/multi5/file.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Test data - not compiled.

package main

{}
5 changes: 5 additions & 0 deletions cue/load/testdata/testmod/multi5/multi/file.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Test data - not compiled.

package main

{}
5 changes: 5 additions & 0 deletions cue/load/testdata/testmod/multi5/multi/file_appengine.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Test data - not compiled.

package test_package

{}
2 changes: 2 additions & 0 deletions cue/load/testdata/testmod/multi5/multi/nopackage1.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x: 1

7 changes: 7 additions & 0 deletions cue/load/testdata/testmod/multi5/multi/nopackage2.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// The underscore package should be treated the same as an omitted package
// clause.

package _

x: 1

7 changes: 7 additions & 0 deletions cue/load/testdata/testmod/multi5/nopackage.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// The underscore package should be treated the same as an omitted package
// clause.

package _

x: 1

0 comments on commit 9295a20

Please sign in to comment.