1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
|
// Copyright 2018 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package load
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"text/template"
"unicode"
"github.com/go-quicktest/qt"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/format"
"cuelang.org/go/internal/tdtest"
)
func init() {
// The user running `go test` might have a broken environment,
// such as an invalid $CUE_REGISTRY like the one below,
// or a broken $DOCKER_CONFIG/config.json due to syntax errors.
// Go tests should be hermetic by explicitly setting load.Config.Env;
// catch any that do not by leaving a broken $CUE_REGISTRY in os.Environ.
os.Setenv("CUE_REGISTRY", "inline:{")
}
// TestLoad is an end-to-end test.
func TestLoad(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
testdataDir := testdata("testmod")
dirCfg := &Config{
Dir: testdataDir,
Tools: true,
}
badModCfg := &Config{
Dir: testdata("badmod"),
}
type loadTest struct {
name string
cfg *Config
args []string
want string
}
testCases := []loadTest{{
name: "BadModuleFile",
cfg: badModCfg,
args: []string{"."},
want: `err: module: 2 errors in empty disjunction:
module: conflicting values 123 and "" (mismatched types int and string):
$CWD/testdata/badmod/cue.mod/module.cue:2:9
cuelang.org/go/mod/modfile/schema.cue:56:22
module: conflicting values 123 and string (mismatched types int and string):
$CWD/testdata/badmod/cue.mod/module.cue:2:9
cuelang.org/go/mod/modfile/schema.cue:98:12
path: ""
module: ""
root: ""
dir: ""
display:""`,
}, {
name: "DefaultPackage",
// 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@v0
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod
display:.
files:
$CWD/testdata/testmod/test.cue
imports:
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`}, {
name: "DefaultPackageWithExplicitDotArgument",
// 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: []string{"."},
want: `path: mod.test/test@v0
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod
display:.
files:
$CWD/testdata/testmod/test.cue
imports:
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`}, {
name: "RelativeImportPathWildcard",
cfg: dirCfg,
args: []string{"./other/..."},
want: `err: import failed: relative import paths not allowed ("./file"):
$CWD/testdata/testmod/other/main.cue:6:2
path: ""
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: ""
display:""`}, {
name: "NoMatchingPackageName",
cfg: dirCfg,
args: []string{"./anon"},
want: `err: build constraints exclude all CUE files in ./anon:
$CWD/testdata/testmod/anon/anon.cue: no package name
path: mod.test/test/anon@v0
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/anon
display:./anon`}, {
name: "RelativeImportPathSingle",
cfg: dirCfg,
args: []string{"./other"},
want: `err: import failed: relative import paths not allowed ("./file"):
$CWD/testdata/testmod/other/main.cue:6:2
path: mod.test/test/other@v0:main
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/other
display:./other
files:
$CWD/testdata/testmod/other/main.cue`}, {
name: "RelativePathSuccess",
cfg: dirCfg,
args: []string{"./hello"},
want: `path: mod.test/test/hello@v0:test
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/hello
display:./hello
files:
$CWD/testdata/testmod/test.cue
$CWD/testdata/testmod/hello/test.cue
imports:
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`}, {
name: "ExplicitPackageIdentifier",
cfg: dirCfg,
args: []string{"mod.test/test/hello:test"},
want: `path: mod.test/test/hello:test
module: mod.test/test@v0
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
imports:
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`}, {
name: "NoPackageName",
cfg: dirCfg,
args: []string{"mod.test/test/hello:nonexist"},
want: `err: cannot find package "mod.test/test/hello": no files in package directory with package name "nonexist"
path: mod.test/test/hello:nonexist
module: ""
root: ""
dir: ""
display:mod.test/test/hello:nonexist`,
}, {
name: "ExplicitNonPackageFiles",
cfg: dirCfg,
args: []string{"./anon.cue", "./other/anon.cue"},
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`,
}, {
name: "AbsoluteFileIsNormalized", // TODO(rogpeppe) what is this actually testing?
cfg: dirCfg,
// Absolute file is normalized.
args: []string{filepath.Join(cwd, testdata("testmod", "anon.cue"))},
want: `path: ""
module: ""
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod
display:command-line-arguments
files:
$CWD/testdata/testmod/anon.cue`}, {
name: "StandardInput",
cfg: dirCfg,
args: []string{"-"},
want: `path: ""
module: ""
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod
display:command-line-arguments
files:
-`}, {
name: "BadIdentifier",
cfg: dirCfg,
args: []string{"foo.com/bad-identifier"},
want: `err: cannot determine package name for "foo.com/bad-identifier"; set it explicitly with ':'
cannot find package "foo.com/bad-identifier": cannot find module providing package foo.com/bad-identifier
path: foo.com/bad-identifier
module: ""
root: ""
dir: ""
display:foo.com/bad-identifier`,
}, {
name: "NonexistentStdlibImport",
cfg: dirCfg,
args: []string{"nonexisting"},
want: `err: standard library import path "nonexisting" cannot be imported as a CUE package
path: nonexisting
module: ""
root: ""
dir: ""
display:nonexisting`,
}, {
name: "ExistingStdlibImport",
cfg: dirCfg,
args: []string{"strconv"},
want: `err: standard library import path "strconv" cannot be imported as a CUE package
path: strconv
module: ""
root: ""
dir: ""
display:strconv`,
}, {
name: "EmptyPackageDirectory",
cfg: dirCfg,
args: []string{"./empty"},
want: `err: no CUE files in ./empty
path: mod.test/test/empty@v0
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/empty
display:./empty`,
}, {
name: "PackageWithImports",
cfg: dirCfg,
args: []string{"./imports"},
want: `path: mod.test/test/imports@v0
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/imports
display:./imports
files:
$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`}, {
name: "PackageWithImportsWithSkipImportsConfig",
cfg: &Config{
Dir: testdataDir,
Tools: true,
SkipImports: true,
},
args: []string{"./imports"},
want: `path: mod.test/test/imports@v0
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/imports
display:./imports
files:
$CWD/testdata/testmod/imports/imports.cue`}, {
name: "OnlyToolFiles",
cfg: dirCfg,
args: []string{"./toolonly"},
want: `path: mod.test/test/toolonly@v0:foo
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/toolonly
display:./toolonly
files:
$CWD/testdata/testmod/toolonly/foo_tool.cue`}, {
name: "OnlyToolFilesWithToolsDisabledInConfig",
cfg: &Config{
Dir: testdataDir,
},
args: []string{"./toolonly"},
want: `err: build constraints exclude all CUE files in ./toolonly:
$CWD/testdata/testmod/test.cue: package is test, want foo
$CWD/testdata/testmod/toolonly/foo_tool.cue: _tool.cue files excluded in non-cmd mode
path: mod.test/test/toolonly@v0:foo
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/toolonly
display:./toolonly`}, {
name: "WithBoolTag",
cfg: &Config{
Dir: testdataDir,
Tags: []string{"prod"},
},
args: []string{"./tags"},
want: `path: mod.test/test/tags@v0
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/tags
display:./tags
files:
$CWD/testdata/testmod/tags/prod.cue`}, {
name: "WithAttrValTag",
cfg: &Config{
Dir: testdataDir,
Tags: []string{"prod", "foo=bar"},
},
args: []string{"./tags"},
want: `path: mod.test/test/tags@v0
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/tags
display:./tags
files:
$CWD/testdata/testmod/tags/prod.cue`}, {
name: "UnusedTag",
cfg: &Config{
Dir: testdataDir,
Tags: []string{"prod"},
},
args: []string{"./tagsbad"},
want: `err: tag "prod" not used in any file
previous declaration here:
$CWD/testdata/testmod/tagsbad/prod.cue:1:1
multiple @if attributes:
$CWD/testdata/testmod/tagsbad/prod.cue:2:1
path: mod.test/test/tagsbad@v0
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/tagsbad
display:./tagsbad`}, {
name: "ImportCycle",
cfg: &Config{
Dir: testdataDir,
},
args: []string{"./cycle"},
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
path: mod.test/test/cycle@v0
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/cycle
display:./cycle
files:
$CWD/testdata/testmod/cycle/cycle.cue`}, {
name: "AcceptLegacyModuleWithLegacyModule",
cfg: &Config{
Dir: testdata("testmod_legacy"),
AcceptLegacyModules: true,
},
want: `path: test.example/foo@v0
module: test.example/foo@v0
root: $CWD/testdata/testmod_legacy
dir: $CWD/testdata/testmod_legacy
display:.
files:
$CWD/testdata/testmod_legacy/foo.cue`}, {
name: "AcceptLegacyModuleWithNonLegacyModule",
cfg: &Config{
Dir: testdataDir,
Tools: true,
AcceptLegacyModules: true,
},
args: []string{"./imports"},
want: `path: mod.test/test/imports@v0
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/imports
display:./imports
files:
$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`}, {
name: "MismatchedModulePathInConfig",
cfg: &Config{
Dir: testdataDir,
Tools: true,
Module: "wrong.test@v0",
},
args: []string{"./imports"},
want: `err: inconsistent modules: got "mod.test/test@v0", want "wrong.test@v0"
path: ""
module: wrong.test@v0
root: ""
dir: ""
display:""`}, {
name: "ModulePathInConfigWithoutMajorVersion",
cfg: &Config{
Dir: testdataDir,
Tools: true,
Module: "mod.test/test",
},
args: []string{"./imports"},
want: `err: inconsistent modules: got "mod.test/test@v0", want "mod.test/test"
path: ""
module: mod.test/test
root: ""
dir: ""
display:""`}, {
name: "ModulePathInConfigWithoutMajorVersionAndMismatchedPath",
cfg: &Config{
Dir: testdataDir,
Tools: true,
Module: "mod.test/wrong",
},
args: []string{"./imports"},
want: `err: inconsistent modules: got "mod.test/test@v0", want "mod.test/wrong"
path: ""
module: mod.test/wrong
root: ""
dir: ""
display:""`}, {
name: "ExplicitPackageWithUnqualifiedImportPath#1",
cfg: &Config{
Dir: filepath.Join(testdataDir, "multi"),
Package: "main",
},
args: []string{"."},
want: `path: mod.test/test/multi@v0:main
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/multi
display:.
files:
$CWD/testdata/testmod/multi/file.cue`}, {
name: "ExplicitPackageWithUnqualifiedImportPath#2",
// This test replicates the failure reported in https://cuelang.org/issue/3213
cfg: &Config{
Dir: filepath.Join(testdataDir, "multi2"),
Package: "other",
},
args: []string{"."},
want: `path: mod.test/test/multi2@v0:other
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/multi2
display:.
files:
$CWD/testdata/testmod/multi2/other.cue
imports:
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`}, {
name: "ExplicitPackageWithUnqualifiedImportPath#3",
cfg: &Config{
Dir: filepath.Join(testdataDir, "multi3"),
Package: "other",
},
args: []string{"."},
want: `path: mod.test/test/multi3@v0:other
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/multi3
display:.
files:
$CWD/testdata/testmod/multi3/other.cue
imports:
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/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: `path: mod.test/test/multi5@v0:_
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/multi5
display:.
files:
$CWD/testdata/testmod/multi5/nopackage.cue`}, {
// Check that imports are only considered from files
// that match the build paths.
name: "BuildTagsWithImports#1",
cfg: &Config{
Dir: filepath.Join(testdataDir, "tagswithimports"),
Tags: []string{"prod"},
},
args: []string{"."},
want: `path: mod.test/test/tagswithimports@v0
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/tagswithimports
display:.
files:
$CWD/testdata/testmod/tagswithimports/prod.cue
imports:
mod.test/test/hello:test: $CWD/testdata/testmod/test.cue $CWD/testdata/testmod/hello/test.cue
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`}, {
// Check that imports are only considered from files
// that match the build paths. When we don't have the prod
// tag, the bad import path mentioned in testdata/testmod/tagswithimports/nonprod.cue
// surfaces in the errors.
name: "BuildTagsWithImports#2",
cfg: &Config{
Dir: filepath.Join(testdataDir, "tagswithimports"),
},
args: []string{"."},
want: `err: mod.test/test/tagswithimports@v0: import failed: cannot find package "bad-import.example/foo": cannot find module providing package bad-import.example/foo:
$CWD/testdata/testmod/tagswithimports/nonprod.cue:5:8
path: mod.test/test/tagswithimports@v0
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/tagswithimports
display:.
files:
$CWD/testdata/testmod/tagswithimports/nonprod.cue`}, {
name: "ModuleFileNonDirectory",
cfg: &Config{
Dir: testdata("testmod_legacymodfile"),
},
args: []string{"."},
want: `err: cue.mod files are no longer supported; use cue.mod/module.cue
path: ""
module: ""
root: ""
dir: ""
display:""`}, {
// This test checks that files in parent directories
// do not result in irrelevant instances appearing
// in the result of Instances.
name: "Issue3306",
cfg: &Config{
Dir: testdataDir,
Package: "*",
SkipImports: true,
},
args: []string{"./issue3306/..."},
want: `path: mod.test/test/issue3306@v0:x
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/issue3306
display:./issue3306
files:
$CWD/testdata/testmod/issue3306/x.cue
path: mod.test/test/issue3306/a@v0:a
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/issue3306/a
display:./issue3306/a
files:
$CWD/testdata/testmod/issue3306/a/a.cue
path: mod.test/test/issue3306/a@v0:b
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/issue3306/a
display:./issue3306/a
files:
$CWD/testdata/testmod/issue3306/a/b.cue
path: mod.test/test/issue3306/a@v0:x
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/issue3306/a
display:./issue3306/a
files:
$CWD/testdata/testmod/issue3306/x.cue
$CWD/testdata/testmod/issue3306/a/x.cue
path: mod.test/test/issue3306/x@v0:x
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/issue3306/x
display:./issue3306/x
files:
$CWD/testdata/testmod/issue3306/x.cue
$CWD/testdata/testmod/issue3306/x/x.cue`}, {
// This test checks that when we use Package: "*",
// we can still use imported packages.
name: "AllPackagesWithImports",
cfg: &Config{
Dir: testdataDir,
Package: "*",
},
args: []string{"."},
want: `path: mod.test/test@v0:_
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod
display:.
files:
$CWD/testdata/testmod/anon.cue
path: mod.test/test@v0:test
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod
display:.
files:
$CWD/testdata/testmod/test.cue
imports:
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`}, {
// This tests that we can load a CUE package by pointing Dir to it
// even when the package's directory name ends with ".cue".
name: "DirWithCUEFileExtension",
cfg: &Config{
Dir: filepath.Join(testdataDir, "testdir.cue"),
},
args: []string{"."},
want: `path: mod.test/test/testdir.cue@v0:testdir
module: mod.test/test@v0
root: $CWD/testdata/testmod
dir: $CWD/testdata/testmod/testdir.cue
display:.
files:
$CWD/testdata/testmod/testdir.cue/test.cue`,
}}
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)
}
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)
t.Equal(got, tc.want)
})
}
var pkgInfo = template.Must(template.New("pkg").Funcs(template.FuncMap{
"errordetails": func(err error) string {
s := errors.Details(err, &errors.Config{
ToSlash: true,
})
s = strings.TrimSuffix(s, "\n")
return s
}}).Parse(`
{{- range . -}}
{{- if .Err}}err: {{errordetails .Err}}{{end}}
path: {{if .ImportPath}}{{.ImportPath}}{{else}}""{{end}}
module: {{with .Module}}{{.}}{{else}}""{{end}}
root: {{with .Root}}{{.}}{{else}}""{{end}}
dir: {{with .Dir}}{{.}}{{else}}""{{end}}
display:{{with .DisplayPath}}{{.}}{{else}}""{{end}}
{{if .Files -}}
files:
{{- range .Files}}
{{.Filename}}
{{- end -}}
{{- end}}
{{if .Imports -}}
imports:
{{- range .Dependencies}}
{{.ImportPath}}:{{range .Files}} {{.Filename}}{{end}}
{{- end}}
{{end -}}
{{- end -}}
`))
func TestOverlays(t *testing.T) {
cwd, _ := os.Getwd()
abs := func(path string) string {
return filepath.Join(cwd, path)
}
c := &Config{
Overlay: map[string]Source{
// Not necessary, but nice to add.
abs("cue.mod/module.cue"): FromString(`module: "mod.test", language: version: "v0.9.0"`),
abs("dir/top.cue"): FromBytes([]byte(`
package top
msg: "Hello"
`)),
abs("dir/b/foo.cue"): FromString(`
package foo
a: <= 5
`),
abs("dir/b/bar.cue"): FromString(`
package foo
a: >= 5
`),
},
}
want := []string{
`{msg:"Hello"}`,
`{a:>=5&<=5}`,
}
rmSpace := func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}
ctx := cuecontext.New()
insts, err := ctx.BuildInstances(Instances([]string{"./dir/..."}, c))
if err != nil {
t.Fatal(err)
}
for i, inst := range insts {
if err := inst.Err(); err != nil {
t.Error(err)
continue
}
b, err := format.Node(inst.Value().Syntax(cue.Final()))
if err != nil {
t.Error(err)
continue
}
if got := string(bytes.Map(rmSpace, b)); got != want[i] {
t.Errorf("%s: got %s; want %s", inst.BuildInstance().Dir, got, want[i])
}
}
}
func TestLoadOrder(t *testing.T) {
testDir := t.TempDir()
letters := "abcdefghij"
for _, c := range letters {
contents := fmt.Sprintf(`
package %s
x: 1
`, string(c))
err := os.WriteFile(filepath.Join(testDir, string(c)+".cue"), []byte(contents), 0o666)
qt.Assert(t, qt.IsNil(err))
}
insts := Instances([]string{"."}, &Config{
Package: "*",
Dir: testDir,
})
var actualFiles = []string{}
for _, inst := range insts {
for _, f := range inst.BuildFiles {
if strings.Contains(f.Filename, testDir) {
actualFiles = append(actualFiles, filepath.Base(f.Filename))
}
}
}
var expectedFiles []string
for _, c := range letters {
expectedFiles = append(expectedFiles, string(c)+".cue")
}
qt.Assert(t, qt.DeepEquals(actualFiles, expectedFiles))
}
func TestLoadInstancesConcurrent(t *testing.T) {
// This test is designed to fail when run with the race detector
// if there's an underlying race condition.
// See https://cuelang.org/issue/1746
race(t, func() error {
_, err := getInst(".", testdata("testmod", "hello"))
return err
})
}
func race(t *testing.T, f func() error) {
var wg sync.WaitGroup
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
if err := f(); err != nil {
t.Error(err)
}
wg.Done()
}()
}
wg.Wait()
}
|