Skip to content

Commit

Permalink
pkg: remove usages of cue.Runtime in tests
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
NoamTD authored and mvdan committed May 17, 2024
1 parent 27689d2 commit 51cd5ca
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 31 deletions.
15 changes: 4 additions & 11 deletions pkg/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import (
"cuelang.org/go/cue/errors"
cueformat "cuelang.org/go/cue/format"
"cuelang.org/go/internal"
"cuelang.org/go/internal/core/runtime"
pkginternal "cuelang.org/go/pkg/internal"
)

const genFile = "pkg.go"
Expand Down Expand Up @@ -209,7 +209,7 @@ func (g *generator) processCUE() error {
// Note: we avoid using the cue/load and the cuecontext packages
// because they depend on the standard library which is what this
// command is generating - cyclic dependencies are undesirable in general.
ctx := newContext()
ctx := pkginternal.NewContext()
val, err := loadCUEPackage(ctx, g.dir, g.cuePkgPath)
if err != nil {
if errors.Is(err, errNoCUEFiles) {
Expand All @@ -230,7 +230,7 @@ func (g *generator) processCUE() error {
b = bytes.ReplaceAll(b, []byte("\n\n"), []byte("\n"))
// Try to use a Go string with backquotes, for readability.
// If not possible due to cueSrc itself having backquotes,
// use a single-line double quoted string, removing tabs for brevity.
// use a single-line double-quoted string, removing tabs for brevity.
// We don't use strconv.CanBackquote as it is for quoting as a single line.
if cueSrc := string(b); !strings.Contains(cueSrc, "`") {
fmt.Fprintf(g.w, "CUE: `%s`,\n", cueSrc)
Expand All @@ -243,7 +243,7 @@ func (g *generator) processCUE() error {

func (g *generator) processGo(pkg *packages.Package) error {
// We sort the objects by their original source code position.
// Otherwise go/types defaults to sorting by name strings.
// Otherwise, go/types defaults to sorting by name strings.
// We could remove this code if we were fine with sorting by name.
scope := pkg.Types.Scope()
type objWithPos struct {
Expand Down Expand Up @@ -473,10 +473,3 @@ func loadCUEPackage(ctx *cue.Context, dir string, pkgPath string) (cue.Value, er
}
return vals[0], nil
}

// Avoid using cuecontext.New because that package imports
// the entire stdlib which we are generating.
func newContext() *cue.Context {
r := runtime.New()
return (*cue.Context)(r)
}
26 changes: 26 additions & 0 deletions pkg/internal/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package internal

import (
"cuelang.org/go/cue"
"cuelang.org/go/internal/core/runtime"
)

// NewContext replaces cuecontext.New, since pkg/... can't use it due to import cycles.
func NewContext() *cue.Context {
r := runtime.New()
return (*cue.Context)(r)
}
9 changes: 5 additions & 4 deletions pkg/tool/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"cuelang.org/go/cue"
"cuelang.org/go/internal/task"
"cuelang.org/go/pkg/internal"
)

func TestEnv(t *testing.T) {
Expand Down Expand Up @@ -72,15 +73,15 @@ func TestEnv(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
var r cue.Runtime
inst, err := r.Compile(tc.desc, tc.val)
if err != nil {
ctx := internal.NewContext()
v := ctx.CompileString(tc.val, cue.Filename(tc.desc))
if err := v.Err(); err != nil {
t.Fatal(err)
}

cmd, _, err := mkCommand(&task.Context{
Context: context.Background(),
Obj: inst.Value(),
Obj: v,
})
if err != nil {
t.Fatalf("mkCommand error = %v", err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/tool/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"cuelang.org/go/cue/parser"
"cuelang.org/go/internal/task"
"cuelang.org/go/internal/value"
"cuelang.org/go/pkg/internal"
)

func parse(t *testing.T, kind, expr string) cue.Value {
Expand All @@ -34,12 +35,11 @@ func parse(t *testing.T, kind, expr string) cue.Value {
if err != nil {
t.Fatal(err)
}
var r cue.Runtime
i, err := r.CompileExpr(x)
if err != nil {
v := internal.NewContext().BuildExpr(x)
if err := v.Err(); err != nil {
t.Fatal(err)
}
return value.UnifyBuiltin(i.Value(), kind)
return value.UnifyBuiltin(v, kind)
}

func TestRead(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions pkg/tool/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"cuelang.org/go/cue/parser"
"cuelang.org/go/internal/task"
"cuelang.org/go/internal/value"
"cuelang.org/go/pkg/internal"
)

func newTLSServer() *httptest.Server {
Expand All @@ -43,12 +44,11 @@ func parse(t *testing.T, kind, expr string) cue.Value {
if err != nil {
t.Fatal(err)
}
var r cue.Runtime
i, err := r.CompileExpr(x)
if err != nil {
v := internal.NewContext().BuildExpr(x)
if err := v.Err(); err != nil {
t.Fatal(err)
}
return value.UnifyBuiltin(i.Value(), kind)
return value.UnifyBuiltin(v, kind)
}

func TestTLS(t *testing.T) {
Expand Down Expand Up @@ -133,13 +133,13 @@ func TestParseHeaders(t *testing.T) {
}}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
r := cue.Runtime{}
inst, err := r.Compile("http headers", tc.req)
if err != nil {
ctx := internal.NewContext()
v := ctx.CompileString(tc.req, cue.Filename("http headers"))
if err := v.Err(); err != nil {
t.Fatal(err)
}

h, err := parseHeaders(inst.Value(), tc.field)
h, err := parseHeaders(v, tc.field)

b := &strings.Builder{}
switch {
Expand Down
9 changes: 5 additions & 4 deletions pkg/tool/os/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"cuelang.org/go/cue/token"
"cuelang.org/go/internal/task"
"cuelang.org/go/internal/value"
"cuelang.org/go/pkg/internal"
)

func TestGetenv(t *testing.T) {
Expand Down Expand Up @@ -135,10 +136,10 @@ func parse(t *testing.T, kind, expr string) cue.Value {
errors.Print(os.Stderr, err, nil)
t.Fatal(err)
}
var r cue.Runtime
i, err := r.CompileExpr(x)
if err != nil {
ctx := internal.NewContext()
v := ctx.BuildExpr(x)
if err := v.Err(); err != nil {
t.Fatal(err)
}
return value.UnifyBuiltin(i.Value(), kind)
return value.UnifyBuiltin(v, kind)
}

0 comments on commit 51cd5ca

Please sign in to comment.