Skip to content

Commit

Permalink
cue/load: create test data with broken cue dynamically
Browse files Browse the repository at this point in the history
In https://cuelang.org/cl/1193030, multiple cue files were added in
order to test that load.Instances returns build instances sorted by
directory and package name.
Each cue file belongs to a different package, causing commands like
`cue vet ./...` to fail with the following error:

    found packages "a" (a.cue) and "b" (b.cue) in
    "cue/load/testdata/testsort"

To resolve this these cue files are created dynamically as part of the
test, instead of being stored statically on disk.

Change-Id: I94661c5446183ad300b37ddf46312821dbd8a66b
Signed-off-by: Noam Dolovich <[email protected]>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195592
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
  • Loading branch information
NoamTD authored and mvdan committed Jun 3, 2024
1 parent 7490a3d commit 1ea6c49
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 34 deletions.
21 changes: 17 additions & 4 deletions cue/load/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package load

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -467,22 +468,34 @@ func TestOverlays(t *testing.T) {
}

func TestLoadOrder(t *testing.T) {
testDataDir := testdata("testsort")
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), 0o644)
qt.Assert(t, qt.IsNil(err))
}

insts := Instances([]string{"."}, &Config{
Package: "*",
Dir: testDataDir,
Dir: testDir,
})

var actualFiles = []string{}
for _, inst := range insts {
for _, f := range inst.BuildFiles {
if strings.Contains(f.Filename, testDataDir) {
if strings.Contains(f.Filename, testDir) {
actualFiles = append(actualFiles, filepath.Base(f.Filename))
}
}
}
var expectedFiles []string
for _, c := range "abcdefghij" {
for _, c := range letters {
expectedFiles = append(expectedFiles, string(c)+".cue")
}
qt.Assert(t, qt.DeepEquals(actualFiles, expectedFiles))
Expand Down
3 changes: 0 additions & 3 deletions cue/load/testdata/testsort/a.cue

This file was deleted.

3 changes: 0 additions & 3 deletions cue/load/testdata/testsort/b.cue

This file was deleted.

3 changes: 0 additions & 3 deletions cue/load/testdata/testsort/c.cue

This file was deleted.

3 changes: 0 additions & 3 deletions cue/load/testdata/testsort/d.cue

This file was deleted.

3 changes: 0 additions & 3 deletions cue/load/testdata/testsort/e.cue

This file was deleted.

3 changes: 0 additions & 3 deletions cue/load/testdata/testsort/f.cue

This file was deleted.

3 changes: 0 additions & 3 deletions cue/load/testdata/testsort/g.cue

This file was deleted.

3 changes: 0 additions & 3 deletions cue/load/testdata/testsort/h.cue

This file was deleted.

3 changes: 0 additions & 3 deletions cue/load/testdata/testsort/i.cue

This file was deleted.

3 changes: 0 additions & 3 deletions cue/load/testdata/testsort/j.cue

This file was deleted.

0 comments on commit 1ea6c49

Please sign in to comment.