this repo has no description
0
fork

Configure Feed

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

encoding/gocode: simplify testdata/gen.go

We can use cue/load directly to load all CUE packages in one go.
This saves us having to manually read the directory and loop.
Moreover, we can use a simpler cue/load.Config as well.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I2290bfa4f6e4352e54df8dc5dad0c45ef01f5f19
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201335
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+8 -25
+8 -25
encoding/gocode/testdata/gen.go
··· 19 19 "os" 20 20 "path/filepath" 21 21 22 - "cuelang.org/go/cue" 22 + "cuelang.org/go/cue/cuecontext" 23 23 "cuelang.org/go/cue/load" 24 24 "cuelang.org/go/encoding/gocode" 25 25 _ "cuelang.org/go/pkg" 26 26 ) 27 27 28 28 func main() { 29 - dirs, err := os.ReadDir("testdata") 30 - if err != nil { 31 - log.Fatal(err) 32 - } 33 - 34 - cwd, err := os.Getwd() 35 - if err != nil { 36 - log.Fatal(err) 37 - } 38 - 39 - for _, d := range dirs { 40 - if !d.IsDir() || d.Name() == "cue.mod" { 41 - continue 42 - } 43 - dir := filepath.Join(cwd, "testdata") 44 - pkg := "." + string(filepath.Separator) + d.Name() 45 - inst := cue.Build(load.Instances([]string{pkg}, &load.Config{ 46 - Dir: dir, 47 - ModuleRoot: dir, 48 - Module: "cuelang.org/go/encoding/gocode/testdata@v0", 49 - }))[0] 29 + insts := load.Instances([]string{"./..."}, &load.Config{ 30 + Dir: "testdata", 31 + }) 32 + ctx := cuecontext.New() 33 + for _, inst := range insts { 50 34 if err := inst.Err; err != nil { 51 35 log.Fatal(err) 52 36 } 53 37 54 - goPkg := "./testdata/" + d.Name() 55 - b, err := gocode.Generate(goPkg, inst, nil) 38 + b, err := gocode.Generate(inst.Dir, ctx.BuildInstance(inst), nil) 56 39 if err != nil { 57 40 log.Fatal(err) 58 41 } 59 42 60 - goFile := filepath.Join("testdata", d.Name(), "cue_gen.go") 43 + goFile := filepath.Join(inst.Dir, "cue_gen.go") 61 44 if err := os.WriteFile(goFile, b, 0666); err != nil { 62 45 log.Fatal(err) 63 46 }