this repo has no description
0
fork

Configure Feed

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

cue/interpreter: move test helpers to the right file

So that the test helper funcs are not unused in the default build,
as they are only used by exe_test.go with -tags=cuewasm.

Updates #3335.

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

+87 -86
+87
cue/interpreter/wasm/exe_test.go
··· 20 20 package wasm_test 21 21 22 22 import ( 23 + "io/fs" 23 24 "os" 24 25 "path/filepath" 26 + stdruntime "runtime" 27 + "strings" 25 28 "testing" 26 29 27 30 "cuelang.org/go/cmd/cue/cmd" 31 + "cuelang.org/go/cue" 32 + "cuelang.org/go/cue/ast" 33 + "cuelang.org/go/cue/build" 34 + "cuelang.org/go/cue/cuecontext" 35 + "cuelang.org/go/cue/interpreter/wasm" 36 + "cuelang.org/go/cue/parser" 28 37 "cuelang.org/go/internal/cuetest" 29 38 30 39 "github.com/rogpeppe/go-internal/gotooltest" ··· 60 69 } 61 70 testscript.Run(t, p) 62 71 } 72 + 73 + func copyWasmFiles(t *testing.T, dstDir, srcDir string) { 74 + filepath.WalkDir(dstDir, func(path string, d fs.DirEntry, err error) error { 75 + if filepath.Ext(path) != ".wasm" { 76 + return nil 77 + } 78 + relPath := must(filepath.Rel(dstDir, path))(t) 79 + from := filepath.Join(srcDir, relPath) 80 + copyFile(t, path, from) 81 + return nil 82 + }) 83 + } 84 + 85 + func copyFile(t *testing.T, dst, src string) { 86 + buf := must(os.ReadFile(src))(t) 87 + err := os.WriteFile(dst, buf, 0664) 88 + if err != nil { 89 + t.Fatal(err) 90 + } 91 + } 92 + 93 + func check(t *testing.T, dir string, got string) { 94 + golden := filepath.Join("testdata", dir) + ".golden" 95 + 96 + if cuetest.UpdateGoldenFiles { 97 + os.WriteFile(golden, []byte(got), 0666) 98 + } 99 + 100 + want := string(must(os.ReadFile(golden))(t)) 101 + if got != want { 102 + t.Errorf("want %v, got %v", want, got) 103 + } 104 + } 105 + 106 + func loadDir(t *testing.T, name string) cue.Value { 107 + ctx := cuecontext.New(cuecontext.Interpreter(wasm.New())) 108 + bi := dirInstance(t, name) 109 + return ctx.BuildInstance(bi) 110 + } 111 + 112 + func dirInstance(t *testing.T, name string) *build.Instance { 113 + ctx := build.NewContext(build.ParseFile(loadFile)) 114 + inst := ctx.NewInstance(name, nil) 115 + 116 + files := must(os.ReadDir(name))(t) 117 + for _, f := range files { 118 + if strings.HasSuffix(f.Name(), "cue") { 119 + inst.AddFile(filepath.Join(name, f.Name()), nil) 120 + } 121 + if strings.HasSuffix(f.Name(), "wasm") { 122 + f := &build.File{ 123 + Filename: f.Name(), 124 + } 125 + inst.UnknownFiles = append(inst.UnknownFiles, f) 126 + } 127 + } 128 + inst.Complete() 129 + return inst 130 + } 131 + 132 + func loadFile(filename string, src any) (*ast.File, error) { 133 + return parser.ParseFile(filename, src, parser.ParseFuncs) 134 + } 135 + 136 + func must[T any](v T, err error) func(t *testing.T) T { 137 + fail := false 138 + if err != nil { 139 + fail = true 140 + } 141 + return func(t *testing.T) T { 142 + if fail { 143 + _, file, line, _ := stdruntime.Caller(1) 144 + file = filepath.Base(file) 145 + t.Fatalf("unexpected error at %v:%v: %v", file, line, err) 146 + } 147 + return v 148 + } 149 + }
-86
cue/interpreter/wasm/wasm_test.go
··· 16 16 17 17 import ( 18 18 "fmt" 19 - "io/fs" 20 - "os" 21 - "path/filepath" 22 - stdruntime "runtime" 23 - "strings" 24 19 "testing" 25 20 26 21 "cuelang.org/go/cue" 27 22 "cuelang.org/go/cue/ast" 28 23 "cuelang.org/go/cue/ast/astutil" 29 - "cuelang.org/go/cue/build" 30 24 "cuelang.org/go/cue/cuecontext" 31 25 "cuelang.org/go/cue/errors" 32 26 "cuelang.org/go/cue/format" 33 27 "cuelang.org/go/cue/interpreter/wasm" 34 - "cuelang.org/go/cue/parser" 35 - "cuelang.org/go/internal/cuetest" 36 28 "cuelang.org/go/internal/cuetxtar" 37 29 ) 38 30 ··· 92 84 fmt.Fprint(t, string(got)) 93 85 }) 94 86 } 95 - 96 - func copyWasmFiles(t *testing.T, dstDir, srcDir string) { 97 - filepath.WalkDir(dstDir, func(path string, d fs.DirEntry, err error) error { 98 - if filepath.Ext(path) != ".wasm" { 99 - return nil 100 - } 101 - relPath := must(filepath.Rel(dstDir, path))(t) 102 - from := filepath.Join(srcDir, relPath) 103 - copyFile(t, path, from) 104 - return nil 105 - }) 106 - } 107 - 108 - func copyFile(t *testing.T, dst, src string) { 109 - buf := must(os.ReadFile(src))(t) 110 - err := os.WriteFile(dst, buf, 0664) 111 - if err != nil { 112 - t.Fatal(err) 113 - } 114 - } 115 - 116 - func check(t *testing.T, dir string, got string) { 117 - golden := filepath.Join("testdata", dir) + ".golden" 118 - 119 - if cuetest.UpdateGoldenFiles { 120 - os.WriteFile(golden, []byte(got), 0666) 121 - } 122 - 123 - want := string(must(os.ReadFile(golden))(t)) 124 - if got != want { 125 - t.Errorf("want %v, got %v", want, got) 126 - } 127 - } 128 - 129 - func loadDir(t *testing.T, name string) cue.Value { 130 - ctx := cuecontext.New(cuecontext.Interpreter(wasm.New())) 131 - bi := dirInstance(t, name) 132 - return ctx.BuildInstance(bi) 133 - } 134 - 135 - func dirInstance(t *testing.T, name string) *build.Instance { 136 - ctx := build.NewContext(build.ParseFile(loadFile)) 137 - inst := ctx.NewInstance(name, nil) 138 - 139 - files := must(os.ReadDir(name))(t) 140 - for _, f := range files { 141 - if strings.HasSuffix(f.Name(), "cue") { 142 - inst.AddFile(filepath.Join(name, f.Name()), nil) 143 - } 144 - if strings.HasSuffix(f.Name(), "wasm") { 145 - f := &build.File{ 146 - Filename: f.Name(), 147 - } 148 - inst.UnknownFiles = append(inst.UnknownFiles, f) 149 - } 150 - } 151 - inst.Complete() 152 - return inst 153 - } 154 - 155 - func loadFile(filename string, src any) (*ast.File, error) { 156 - return parser.ParseFile(filename, src, parser.ParseFuncs) 157 - } 158 - 159 - func must[T any](v T, err error) func(t *testing.T) T { 160 - fail := false 161 - if err != nil { 162 - fail = true 163 - } 164 - return func(t *testing.T) T { 165 - if fail { 166 - _, file, line, _ := stdruntime.Caller(1) 167 - file = filepath.Base(file) 168 - t.Fatalf("unexpected error at %v:%v: %v", file, line, err) 169 - } 170 - return v 171 - } 172 - }