Skip to content

Commit

Permalink
cue/load: use tdtest for TestLoad
Browse files Browse the repository at this point in the history
This makes the subsequent test difference in the next CL easier
to read.

For #2330.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I304c27366ac26183cbc95303d26dd3bbf0c1c78c
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1168846
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Paul Jolly <paul@myitcv.io>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
rogpeppe committed Sep 13, 2023
1 parent 28f34a2 commit 7389fe9
Showing 1 changed file with 60 additions and 89 deletions.
149 changes: 60 additions & 89 deletions cue/load/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@ import (
"bytes"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"text/template"
"unicode"

"github.com/google/go-cmp/cmp"

"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/format"
"cuelang.org/go/internal/str"
"cuelang.org/go/internal/tdtest"
)

// TestLoad is an end-to-end test.
Expand All @@ -48,16 +46,17 @@ func TestLoad(t *testing.T) {
Dir: testMod("badmod"),
}

args := str.StringList
testCases := []struct {
type loadTest struct {
cfg *Config
args []string
want string
}{{
}

args := str.StringList
testCases := []loadTest{{
cfg: badModCfg,
args: args("."),
want: `
err: module: invalid module.cue file: 2 errors in empty disjunction:
want: `err: module: invalid module.cue file: 2 errors in empty disjunction:
module: invalid module.cue file: conflicting values 123 and "" (mismatched types int and string):
$cueroot/cue/load/moduleschema.cue:4:20
$CWD/testdata/badmod/cue.mod/module.cue:2:9
Expand All @@ -69,16 +68,14 @@ path: ""
module: ""
root: ""
dir: ""
display:""
`,
display:""`,
}, {
// Even though the directory is called testdata, the last path in
// the module is test. So "package test" is correctly the default
// package of this directory.
cfg: dirCfg,
args: nil,
want: `
path: mod.test/test
want: `path: mod.test/test
module: mod.test/test
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod
Expand All @@ -93,8 +90,7 @@ imports:
// package of this directory.
cfg: dirCfg,
args: args("."),
want: `
path: mod.test/test
want: `path: mod.test/test
module: mod.test/test
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod
Expand All @@ -108,8 +104,7 @@ imports:
// - path incorrect, should be mod.test/test/other:main.
cfg: dirCfg,
args: args("./other/..."),
want: `
err: import failed: relative import paths not allowed ("./file"):
want: `err: import failed: relative import paths not allowed ("./file"):
$CWD/testdata/testmod/other/main.cue:6:2
path: ""
module: mod.test/test
Expand All @@ -119,9 +114,8 @@ display:""`,
}, {
cfg: dirCfg,
args: args("./anon"),
want: `
err: build constraints exclude all CUE files in ./anon:
anon/anon.cue: no package name
want: `err: build constraints exclude all CUE files in ./anon:
anon/anon.cue: no package name
path: mod.test/test/anon
module: mod.test/test
root: $CWD/testdata/testmod
Expand All @@ -132,55 +126,51 @@ display:./anon`,
// - paths are incorrect, should be mod.test/test/other:main.
cfg: dirCfg,
args: args("./other"),
want: `
err: import failed: relative import paths not allowed ("./file"):
want: `err: import failed: relative import paths not allowed ("./file"):
$CWD/testdata/testmod/other/main.cue:6:2
path: mod.test/test/other:main
module: mod.test/test
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/other
display:./other
files:
$CWD/testdata/testmod/other/main.cue`,
$CWD/testdata/testmod/other/main.cue`,
}, {
// TODO:
// - incorrect path, should be mod.test/test/hello:test
cfg: dirCfg,
args: args("./hello"),
want: `
path: mod.test/test/hello:test
want: `path: mod.test/test/hello:test
module: mod.test/test
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/hello
display:./hello
files:
$CWD/testdata/testmod/test.cue
$CWD/testdata/testmod/hello/test.cue
$CWD/testdata/testmod/test.cue
$CWD/testdata/testmod/hello/test.cue
imports:
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`,
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`,
}, {
// TODO:
// - incorrect path, should be mod.test/test/hello:test
cfg: dirCfg,
args: args("mod.test/test/hello:test"),
want: `
path: mod.test/test/hello:test
want: `path: mod.test/test/hello:test
module: mod.test/test
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/hello
display:mod.test/test/hello:test
files:
$CWD/testdata/testmod/test.cue
$CWD/testdata/testmod/hello/test.cue
$CWD/testdata/testmod/test.cue
$CWD/testdata/testmod/hello/test.cue
imports:
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`,
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`,
}, {
// TODO:
// - incorrect path, should be mod.test/test/hello:test
cfg: dirCfg,
args: args("mod.test/test/hello:nonexist"),
want: `
err: build constraints exclude all CUE files in mod.test/test/hello:nonexist:
want: `err: build constraints exclude all CUE files in mod.test/test/hello:nonexist:
anon.cue: no package name
test.cue: package is test, want nonexist
hello/test.cue: package is test, want nonexist
Expand All @@ -192,21 +182,19 @@ display:mod.test/test/hello:nonexist`,
}, {
cfg: dirCfg,
args: args("./anon.cue", "./other/anon.cue"),
want: `
path: ""
want: `path: ""
module: ""
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod
display:command-line-arguments
files:
$CWD/testdata/testmod/anon.cue
$CWD/testdata/testmod/other/anon.cue`,
$CWD/testdata/testmod/anon.cue
$CWD/testdata/testmod/other/anon.cue`,
}, {
cfg: dirCfg,
// Absolute file is normalized.
args: args(filepath.Join(testMod("testmod"), "anon.cue")),
want: `
path: ""
want: `path: ""
module: ""
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod
Expand All @@ -216,8 +204,7 @@ files:
}, {
cfg: dirCfg,
args: args("-"),
want: `
path: ""
want: `path: ""
module: ""
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod
Expand All @@ -228,8 +215,7 @@ files:
// NOTE: dir should probably be set to $CWD/testdata, but either way.
cfg: dirCfg,
args: args("non-existing"),
want: `
err: cannot find package "non-existing"
want: `err: cannot find package "non-existing"
path: non-existing
module: mod.test/test
root: $CWD/testdata/testmod
Expand All @@ -238,8 +224,7 @@ display:non-existing`,
}, {
cfg: dirCfg,
args: args("./empty"),
want: `
err: no CUE files in ./empty
want: `err: no CUE files in ./empty
path: mod.test/test/empty
module: mod.test/test
root: $CWD/testdata/testmod
Expand All @@ -248,35 +233,32 @@ display:./empty`,
}, {
cfg: dirCfg,
args: args("./imports"),
want: `
path: mod.test/test/imports
want: `path: mod.test/test/imports
module: mod.test/test
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/imports
display:./imports
files:
$CWD/testdata/testmod/imports/imports.cue
$CWD/testdata/testmod/imports/imports.cue
imports:
mod.test/catch: $CWD/testdata/testmod/cue.mod/pkg/mod.test/catch/catch.cue
mod.test/helper:helper1: $CWD/testdata/testmod/cue.mod/pkg/mod.test/helper/helper1.cue`,
mod.test/catch: $CWD/testdata/testmod/cue.mod/pkg/mod.test/catch/catch.cue
mod.test/helper:helper1: $CWD/testdata/testmod/cue.mod/pkg/mod.test/helper/helper1.cue`,
}, {
cfg: dirCfg,
args: args("./toolonly"),
want: `
path: mod.test/test/toolonly:foo
want: `path: mod.test/test/toolonly:foo
module: mod.test/test
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/toolonly
display:./toolonly
files:
$CWD/testdata/testmod/toolonly/foo_tool.cue`,
$CWD/testdata/testmod/toolonly/foo_tool.cue`,
}, {
cfg: &Config{
Dir: testdataDir,
},
args: args("./toolonly"),
want: `
err: build constraints exclude all CUE files in ./toolonly:
want: `err: build constraints exclude all CUE files in ./toolonly:
anon.cue: no package name
test.cue: package is test, want foo
toolonly/foo_tool.cue: _tool.cue files excluded in non-cmd mode
Expand All @@ -291,36 +273,33 @@ display:./toolonly`,
Tags: []string{"prod"},
},
args: args("./tags"),
want: `
path: mod.test/test/tags
want: `path: mod.test/test/tags
module: mod.test/test
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/tags
display:./tags
files:
$CWD/testdata/testmod/tags/prod.cue`,
$CWD/testdata/testmod/tags/prod.cue`,
}, {
cfg: &Config{
Dir: testdataDir,
Tags: []string{"prod", "foo=bar"},
},
args: args("./tags"),
want: `
path: mod.test/test/tags
want: `path: mod.test/test/tags
module: mod.test/test
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/tags
display:./tags
files:
$CWD/testdata/testmod/tags/prod.cue`,
$CWD/testdata/testmod/tags/prod.cue`,
}, {
cfg: &Config{
Dir: testdataDir,
Tags: []string{"prod"},
},
args: args("./tagsbad"),
want: `
err: tag "prod" not used in any file
want: `err: tag "prod" not used in any file
previous declaration here:
$CWD/testdata/testmod/tagsbad/prod.cue:1:1
multiple @if attributes:
Expand All @@ -335,8 +314,7 @@ display:./tagsbad`,
Dir: testdataDir,
},
args: args("./cycle"),
want: `
err: import failed: import failed: import failed: package import cycle not allowed:
want: `err: import failed: import failed: import failed: package import cycle not allowed:
$CWD/testdata/testmod/cycle/cycle.cue:3:8
$CWD/testdata/testmod/cue.mod/pkg/mod.test/cycle/bar/bar.cue:3:8
$CWD/testdata/testmod/cue.mod/pkg/mod.test/cycle/foo/foo.cue:3:8
Expand All @@ -348,32 +326,25 @@ display:./cycle
files:
$CWD/testdata/testmod/cycle/cycle.cue`,
}}
for i, tc := range testCases {
t.Run(strconv.Itoa(i)+"/"+strings.Join(tc.args, ":"), func(t *testing.T) {
pkgs := Instances(tc.args, tc.cfg)
tdtest.Run(t, testCases, func(t *tdtest.T, tc *loadTest) {
pkgs := Instances(tc.args, tc.cfg)

buf := &bytes.Buffer{}
err := pkgInfo.Execute(buf, pkgs)
if err != nil {
t.Fatal(err)
}
buf := &bytes.Buffer{}
err := pkgInfo.Execute(buf, pkgs)
if err != nil {
t.Fatal(err)
}

got := strings.TrimSpace(buf.String())
got = strings.Replace(got, cwd, "$CWD", -1)
// Errors are printed with slashes, so replace
// the slash-separated form of CWD too.
got = strings.Replace(got, filepath.ToSlash(cwd), "$CWD", -1)
// Make test work with Windows.
got = strings.Replace(got, string(filepath.Separator), "/", -1)
got := strings.TrimSpace(buf.String())
got = strings.Replace(got, cwd, "$CWD", -1)
// Errors are printed with slashes, so replace
// the slash-separated form of CWD too.
got = strings.Replace(got, filepath.ToSlash(cwd), "$CWD", -1)
// Make test work with Windows.
got = strings.Replace(got, string(filepath.Separator), "/", -1)

want := strings.TrimSpace(tc.want)
want = strings.Replace(want, "\t", " ", -1)
if got != want {
t.Errorf("\n%s", cmp.Diff(want, got))
t.Logf("\n%s", got)
}
})
}
t.Equal(got, tc.want)
})
}

var pkgInfo = template.Must(template.New("pkg").Funcs(template.FuncMap{
Expand Down

0 comments on commit 7389fe9

Please sign in to comment.