this repo has no description
0
fork

Configure Feed

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

pkg: remove usages of cue.Runtime in tests

This replaces various usages of cue.Runtime in tests with the equivalent
*cue.Context methods.

`cuecontext.New` can't be used directly, as this causes an import cycle.
A `newContext` method already existed for creating a cue.Context, and
is extracted to a shared internal func for reuse across pkg/...

Also, some typos are fixed.

Updates #2480.

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: I9bcf3011096a17c3a7f81e80c57414a9b9d9b7b2
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194868
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

authored by

Noam Dolovich and committed by
Daniel Martí
51cd5ca9 27689d27

+52 -31
+4 -11
pkg/gen.go
··· 48 48 "cuelang.org/go/cue/errors" 49 49 cueformat "cuelang.org/go/cue/format" 50 50 "cuelang.org/go/internal" 51 - "cuelang.org/go/internal/core/runtime" 51 + pkginternal "cuelang.org/go/pkg/internal" 52 52 ) 53 53 54 54 const genFile = "pkg.go" ··· 209 209 // Note: we avoid using the cue/load and the cuecontext packages 210 210 // because they depend on the standard library which is what this 211 211 // command is generating - cyclic dependencies are undesirable in general. 212 - ctx := newContext() 212 + ctx := pkginternal.NewContext() 213 213 val, err := loadCUEPackage(ctx, g.dir, g.cuePkgPath) 214 214 if err != nil { 215 215 if errors.Is(err, errNoCUEFiles) { ··· 230 230 b = bytes.ReplaceAll(b, []byte("\n\n"), []byte("\n")) 231 231 // Try to use a Go string with backquotes, for readability. 232 232 // If not possible due to cueSrc itself having backquotes, 233 - // use a single-line double quoted string, removing tabs for brevity. 233 + // use a single-line double-quoted string, removing tabs for brevity. 234 234 // We don't use strconv.CanBackquote as it is for quoting as a single line. 235 235 if cueSrc := string(b); !strings.Contains(cueSrc, "`") { 236 236 fmt.Fprintf(g.w, "CUE: `%s`,\n", cueSrc) ··· 243 243 244 244 func (g *generator) processGo(pkg *packages.Package) error { 245 245 // We sort the objects by their original source code position. 246 - // Otherwise go/types defaults to sorting by name strings. 246 + // Otherwise, go/types defaults to sorting by name strings. 247 247 // We could remove this code if we were fine with sorting by name. 248 248 scope := pkg.Types.Scope() 249 249 type objWithPos struct { ··· 473 473 } 474 474 return vals[0], nil 475 475 } 476 - 477 - // Avoid using cuecontext.New because that package imports 478 - // the entire stdlib which we are generating. 479 - func newContext() *cue.Context { 480 - r := runtime.New() 481 - return (*cue.Context)(r) 482 - }
+26
pkg/internal/internal.go
··· 1 + // Copyright 2024 The 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 internal 16 + 17 + import ( 18 + "cuelang.org/go/cue" 19 + "cuelang.org/go/internal/core/runtime" 20 + ) 21 + 22 + // NewContext replaces cuecontext.New, since pkg/... can't use it due to import cycles. 23 + func NewContext() *cue.Context { 24 + r := runtime.New() 25 + return (*cue.Context)(r) 26 + }
+5 -4
pkg/tool/exec/exec_test.go
··· 22 22 23 23 "cuelang.org/go/cue" 24 24 "cuelang.org/go/internal/task" 25 + "cuelang.org/go/pkg/internal" 25 26 ) 26 27 27 28 func TestEnv(t *testing.T) { ··· 72 73 } 73 74 for _, tc := range testCases { 74 75 t.Run(tc.desc, func(t *testing.T) { 75 - var r cue.Runtime 76 - inst, err := r.Compile(tc.desc, tc.val) 77 - if err != nil { 76 + ctx := internal.NewContext() 77 + v := ctx.CompileString(tc.val, cue.Filename(tc.desc)) 78 + if err := v.Err(); err != nil { 78 79 t.Fatal(err) 79 80 } 80 81 81 82 cmd, _, err := mkCommand(&task.Context{ 82 83 Context: context.Background(), 83 - Obj: inst.Value(), 84 + Obj: v, 84 85 }) 85 86 if err != nil { 86 87 t.Fatalf("mkCommand error = %v", err)
+4 -4
pkg/tool/file/file_test.go
··· 25 25 "cuelang.org/go/cue/parser" 26 26 "cuelang.org/go/internal/task" 27 27 "cuelang.org/go/internal/value" 28 + "cuelang.org/go/pkg/internal" 28 29 ) 29 30 30 31 func parse(t *testing.T, kind, expr string) cue.Value { ··· 34 35 if err != nil { 35 36 t.Fatal(err) 36 37 } 37 - var r cue.Runtime 38 - i, err := r.CompileExpr(x) 39 - if err != nil { 38 + v := internal.NewContext().BuildExpr(x) 39 + if err := v.Err(); err != nil { 40 40 t.Fatal(err) 41 41 } 42 - return value.UnifyBuiltin(i.Value(), kind) 42 + return value.UnifyBuiltin(v, kind) 43 43 } 44 44 45 45 func TestRead(t *testing.T) {
+8 -8
pkg/tool/http/http_test.go
··· 26 26 "cuelang.org/go/cue/parser" 27 27 "cuelang.org/go/internal/task" 28 28 "cuelang.org/go/internal/value" 29 + "cuelang.org/go/pkg/internal" 29 30 ) 30 31 31 32 func newTLSServer() *httptest.Server { ··· 43 44 if err != nil { 44 45 t.Fatal(err) 45 46 } 46 - var r cue.Runtime 47 - i, err := r.CompileExpr(x) 48 - if err != nil { 47 + v := internal.NewContext().BuildExpr(x) 48 + if err := v.Err(); err != nil { 49 49 t.Fatal(err) 50 50 } 51 - return value.UnifyBuiltin(i.Value(), kind) 51 + return value.UnifyBuiltin(v, kind) 52 52 } 53 53 54 54 func TestTLS(t *testing.T) { ··· 133 133 }} 134 134 for _, tc := range testCases { 135 135 t.Run("", func(t *testing.T) { 136 - r := cue.Runtime{} 137 - inst, err := r.Compile("http headers", tc.req) 138 - if err != nil { 136 + ctx := internal.NewContext() 137 + v := ctx.CompileString(tc.req, cue.Filename("http headers")) 138 + if err := v.Err(); err != nil { 139 139 t.Fatal(err) 140 140 } 141 141 142 - h, err := parseHeaders(inst.Value(), tc.field) 142 + h, err := parseHeaders(v, tc.field) 143 143 144 144 b := &strings.Builder{} 145 145 switch {
+5 -4
pkg/tool/os/env_test.go
··· 28 28 "cuelang.org/go/cue/token" 29 29 "cuelang.org/go/internal/task" 30 30 "cuelang.org/go/internal/value" 31 + "cuelang.org/go/pkg/internal" 31 32 ) 32 33 33 34 func TestGetenv(t *testing.T) { ··· 135 136 errors.Print(os.Stderr, err, nil) 136 137 t.Fatal(err) 137 138 } 138 - var r cue.Runtime 139 - i, err := r.CompileExpr(x) 140 - if err != nil { 139 + ctx := internal.NewContext() 140 + v := ctx.BuildExpr(x) 141 + if err := v.Err(); err != nil { 141 142 t.Fatal(err) 142 143 } 143 - return value.UnifyBuiltin(i.Value(), kind) 144 + return value.UnifyBuiltin(v, kind) 144 145 }