this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/cuetxtar: add method for getting a single instance

This CL renames `Test.ValidInstances` to just `Instances`. There doesn't
seem much to be gained from the "Valid" prefix: functions
only return valid results and we've already got the "Raw" prefix
to distinguish the two methods.

Currently the `ValidInstances` method is used in many places
to get just the single instance at the top level of the txtar archive,
but this is somewhat opaque as it relies on the implicit behaviour
that passing no arguments implies the single package in the
top level directory. This CL adds an `Instance` method to return just a single instance.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I4d48697159cf200a3d6b2f2eda2a260b7922fd33
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/541050
Reviewed-by: Paul Jolly <paul@myitcv.io>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+36 -41
+2 -2
cue/ast/astutil/resolve_test.go
··· 32 32 } 33 33 34 34 test.Run(t, func(t *cuetxtar.Test) { 35 - a := t.ValidInstances() 35 + a := t.Instance() 36 36 37 - for _, f := range a[0].Files { 37 + for _, f := range a.Files { 38 38 if filepath.Ext(f.Filename) != ".cue" { 39 39 continue 40 40 }
+2 -2
internal/core/adt/eval_test.go
··· 52 52 r := runtime.New() 53 53 54 54 test.Run(t, func(t *cuetxtar.Test) { 55 - a := t.ValidInstances() 55 + a := t.Instance() 56 56 57 - v, err := r.Build(nil, a[0]) 57 + v, err := r.Build(nil, a) 58 58 if err != nil { 59 59 t.WriteErrors(err) 60 60 return
+3 -3
internal/core/compile/compile_test.go
··· 49 49 test.Run(t, func(t *cuetxtar.Test) { 50 50 // TODO: use high-level API. 51 51 52 - a := t.ValidInstances() 52 + a := t.Instance() 53 53 54 - v, err := compile.Files(nil, r, "", a[0].Files...) 54 + v, err := compile.Files(nil, r, "", a.Files...) 55 55 56 56 // Write the results. 57 57 t.WriteErrors(err) ··· 60 60 return 61 61 } 62 62 63 - for i, f := range a[0].Files { 63 + for i, f := range a.Files { 64 64 if i > 0 { 65 65 fmt.Fprintln(t) 66 66 }
+6 -7
internal/core/dep/dep_test.go
··· 20 20 "testing" 21 21 22 22 "cuelang.org/go/cue" 23 + "cuelang.org/go/cue/cuecontext" 23 24 "cuelang.org/go/internal/core/adt" 24 25 "cuelang.org/go/internal/core/debug" 25 26 "cuelang.org/go/internal/core/dep" ··· 35 36 } 36 37 37 38 test.Run(t, func(t *cuetxtar.Test) { 38 - a := t.ValidInstances() 39 - 40 - inst := cue.Build(a)[0].Value() 41 - if inst.Err() != nil { 42 - t.Fatal(inst.Err()) 39 + val := cuecontext.New().BuildInstance(t.Instance()) 40 + if val.Err() != nil { 41 + t.Fatal(val.Err()) 43 42 } 44 43 45 - ctxt := eval.NewContext(value.ToInternal(inst)) 44 + ctxt := eval.NewContext(value.ToInternal(val)) 46 45 47 46 testCases := []struct { 48 47 name string ··· 63 62 }} 64 63 65 64 for _, tc := range testCases { 66 - v := inst.LookupPath(cue.ParsePath(tc.root)) 65 + v := val.LookupPath(cue.ParsePath(tc.root)) 67 66 68 67 _, n := value.ToInternal(v) 69 68 w := t.Writer(tc.name)
+2 -2
internal/core/export/export_test.go
··· 45 45 r := runtime.New() 46 46 47 47 test.Run(t, func(t *cuetxtar.Test) { 48 - a := t.ValidInstances() 48 + a := t.Instance() 49 49 50 - v, errs := compile.Files(nil, r, "", a[0].Files...) 50 + v, errs := compile.Files(nil, r, "", a.Files...) 51 51 if errs != nil { 52 52 t.Fatal(errs) 53 53 }
+2 -2
internal/core/export/extract_test.go
··· 35 35 r := runtime.New() 36 36 37 37 test.Run(t, func(t *cuetxtar.Test) { 38 - a := t.ValidInstances() 38 + a := t.Instance() 39 39 40 - v, err := compile.Files(nil, r, "", a[0].Files...) 40 + v, err := compile.Files(nil, r, "", a.Files...) 41 41 if err != nil { 42 42 t.Fatal(err) 43 43 }
+1 -1
internal/core/export/self_test.go
··· 43 43 r := cuecontext.New() 44 44 45 45 test.Run(t, func(t *cuetxtar.Test) { 46 - a := t.ValidInstances() 46 + a := t.Instances() 47 47 48 48 v := buildFile(t.T, r, a[0]) 49 49
+3 -3
internal/core/export/value_test.go
··· 43 43 r := runtime.New() 44 44 45 45 test.Run(t, func(t *cuetxtar.Test) { 46 - a := t.ValidInstances() 46 + a := t.Instance() 47 47 48 - pkgID := a[0].ID() 48 + pkgID := a.ID() 49 49 50 - v, err := r.Build(nil, a[0]) 50 + v, err := r.Build(nil, a) 51 51 if err != nil { 52 52 t.Fatal(err) 53 53 }
+4 -4
internal/cuetxtar/txtar.go
··· 63 63 // A Test embeds *[testing.T] and should be used to report errors. 64 64 // 65 65 // Entries within the txtar file define CUE files (available via the 66 - // ValidInstances and RawInstances methods) and expected output 66 + // Instances and RawInstances methods) and expected output 67 67 // (or "golden") files (names starting with "out/\(testname)"). The "main" golden 68 68 // file is "out/\(testname)" itself, used when [Test] is used directly as an [io.Writer] 69 69 // and with [Test.WriteFile]. ··· 231 231 } 232 232 233 233 // Instance returns the single instance representing the 234 - // top directory in the txtar file. 234 + // root directory in the txtar file. 235 235 func (t *Test) Instance() *build.Instance { 236 - return t.ValidInstances()[0] 236 + return t.Instances()[0] 237 237 } 238 238 239 239 // Instances returns the valid instances for this .txtar file or skips the 240 240 // test if there is an error loading the instances. 241 - func (t *Test) ValidInstances(args ...string) []*build.Instance { 241 + func (t *Test) Instances(args ...string) []*build.Instance { 242 242 t.Helper() 243 243 244 244 a := t.RawInstances(args...)
+2 -2
pkg/internal/builtintest/testing.go
··· 35 35 r := runtime.New() 36 36 37 37 test.Run(t, func(t *cuetxtar.Test) { 38 - a := t.ValidInstances() 38 + a := t.Instance() 39 39 40 - v, errs := r.Build(nil, a[0]) 40 + v, errs := r.Build(nil, a) 41 41 if errs != nil { 42 42 t.Fatal(errs) 43 43 }
+1 -1
tools/fix/fixall_test.go
··· 31 31 } 32 32 33 33 test.Run(t, func(t *cuetxtar.Test) { 34 - a := t.ValidInstances("./...") 34 + a := t.Instances("./...") 35 35 err := Instances(a) 36 36 t.WriteErrors(err) 37 37 for _, b := range a {
+2 -5
tools/flow/flow_test.go
··· 42 42 } 43 43 44 44 test.Run(t, func(t *cuetxtar.Test) { 45 - a := t.ValidInstances() 46 - 47 - insts, err := cuecontext.New().BuildInstances(a) 48 - if err != nil { 45 + v := cuecontext.New().BuildInstance(t.Instance()) 46 + if err := v.Err(); err != nil { 49 47 t.Fatal(err) 50 48 } 51 - v := insts[0] 52 49 53 50 seqNum = 0 54 51
+6 -7
tools/trim/trim_test.go
··· 282 282 } 283 283 284 284 test.Run(t, func(t *cuetxtar.Test) { 285 - a := t.ValidInstances() 286 285 287 - inst := cue.Build(a[:1])[0] 288 - if inst.Err != nil { 289 - t.Fatal(inst.Err) 290 - } 286 + a := t.Instance() 287 + val := cuecontext.New().BuildInstance(a) 288 + // Note: don't check val.Err because there are deliberate 289 + // errors in some tests. 291 290 292 - files := a[0].Files 291 + files := a.Files 293 292 294 - err := Files(files, inst, &Config{Trace: trace}) 293 + err := Files(files, val, &Config{Trace: trace}) 295 294 if err != nil { 296 295 t.WriteErrors(errors.Promote(err, "")) 297 296 }