cuelang.org/go@v0.10.1/cue/interpreter/wasm/wasm_test.go (about) 1 // Copyright 2023 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package wasm_test 16 17 import ( 18 "fmt" 19 "io/fs" 20 "os" 21 "path/filepath" 22 stdruntime "runtime" 23 "strings" 24 "testing" 25 26 "cuelang.org/go/cue" 27 "cuelang.org/go/cue/ast" 28 "cuelang.org/go/cue/ast/astutil" 29 "cuelang.org/go/cue/build" 30 "cuelang.org/go/cue/cuecontext" 31 "cuelang.org/go/cue/errors" 32 "cuelang.org/go/cue/format" 33 "cuelang.org/go/cue/interpreter/wasm" 34 "cuelang.org/go/cue/parser" 35 "cuelang.org/go/internal/cuetest" 36 "cuelang.org/go/internal/cuetxtar" 37 ) 38 39 // TestWasm tests Wasm using the API. 40 func TestWasm(t *testing.T) { 41 test := cuetxtar.TxTarTest{ 42 Root: "./testdata/cue", 43 Name: "wasm", 44 } 45 46 test.Run(t, func(t *cuetxtar.Test) { 47 ctx := cuecontext.New(cuecontext.Interpreter(wasm.New())) 48 49 if t.HasTag("cuecmd") { 50 // if #cuecmd is set the test is only for the CUE command, 51 // not the API, so skip it. 52 return 53 } 54 55 bi := t.Instance() 56 57 v := ctx.BuildInstance(bi) 58 err := v.Validate() 59 60 if t.HasTag("error") { 61 // if #error is set we're checking for a specific error, 62 // so only print the error then bail out. 63 for _, err := range errors.Errors(err) { 64 t.WriteErrors(err) 65 } 66 return 67 } 68 69 // we got an unexpected error. print both the error 70 // and the CUE value, to aid debugging. 71 if err != nil { 72 fmt.Fprintln(t, "Errors:") 73 for _, err := range errors.Errors(err) { 74 t.WriteErrors(err) 75 } 76 fmt.Fprintln(t, "") 77 fmt.Fprintln(t, "Result:") 78 } 79 80 syntax := v.Syntax( 81 cue.Attributes(false), cue.Final(), cue.Definitions(true), cue.ErrorsAsValues(true), 82 ) 83 file, err := astutil.ToFile(syntax.(ast.Expr)) 84 if err != nil { 85 t.Fatal(err) 86 } 87 88 got, err := format.Node(file, format.UseSpaces(4), format.TabIndent(false)) 89 if err != nil { 90 t.Fatal(err) 91 } 92 fmt.Fprint(t, string(got)) 93 }) 94 } 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), 0644) 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 }