internal/cuetest: move Kubernetes related helper to where they are used
Currently cuetest.Run and friends live in cuetest, but are only used by
doc/tutorial/kubernetes.
In a later CL we centralise some testing helpers, specifically a
variable that determines whether golden files in testscript or cuetxtar
tests should be updated. The addition of this variable (and addition of
dependencies on cuetest) creates a cycle cmd/cue/cmd -> cuetest ->
cmd/cue/cmd (this latter dependency is created by cuetest.Run).
On the basis cuetest.Run and friends are only used by the Kubernetes
tutorial test, we move them there (for now).
Change-Id: I8ecacc85a61da16c4bfc09050bcfdf470a6703c5
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8681
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/doc/tutorial/kubernetes/tut_test.go b/doc/tutorial/kubernetes/tut_test.go
index a9b7a70..2156e08 100644
--- a/doc/tutorial/kubernetes/tut_test.go
+++ b/doc/tutorial/kubernetes/tut_test.go
@@ -16,6 +16,7 @@
import (
"bytes"
+ "context"
"flag"
"fmt"
"io"
@@ -30,6 +31,7 @@
"github.com/kylelemons/godebug/diff"
+ "cuelang.org/go/cmd/cue/cmd"
"cuelang.org/go/cue/load"
"cuelang.org/go/internal/copy"
"cuelang.org/go/internal/cuetest"
@@ -72,7 +74,7 @@
t.Fatal(err)
}
- cuetest.Run(t, dir, "cue mod init", &cuetest.Config{
+ run(t, dir, "cue mod init", &config{
// Stdin: strings.NewReader(input),
})
@@ -167,7 +169,7 @@
break
}
- cuetest.Run(t, wd, cmd, &cuetest.Config{
+ run(t, wd, cmd, &config{
Stdin: strings.NewReader(input),
Stdout: os.Stdout,
})
@@ -265,16 +267,11 @@
return filepath.Ext(filename) == ".cue" && !strings.Contains(filename, "_tool")
}
-func logf(t *testing.T, format string, args ...interface{}) {
- t.Logf(format, args...)
- log.Printf(format, args...)
-}
-
func TestEval(t *testing.T) {
for _, dir := range []string{"quick", "manual"} {
t.Run(dir, func(t *testing.T) {
buf := &bytes.Buffer{}
- cuetest.Run(t, dir, "cue eval ./...", &cuetest.Config{
+ run(t, dir, "cue eval ./...", &config{
Stdout: buf,
})
@@ -309,3 +306,94 @@
})
}
}
+
+type config struct {
+ Stdin io.Reader
+ Stdout io.Writer
+ Golden string
+}
+
+// run executes the given command in the given directory and reports any
+// errors comparing it to the gold standard.
+func run(t *testing.T, dir, command string, cfg *config) {
+ if cfg == nil {
+ cfg = &config{}
+ }
+
+ old, err := os.Getwd()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err = os.Chdir(dir); err != nil {
+ t.Fatal(err)
+ }
+ defer func() { os.Chdir(old) }()
+
+ logf(t, "Executing command: %s", command)
+
+ command = strings.TrimSpace(command[4:])
+ args := splitArgs(t, command)
+ logf(t, "Args: %q", args)
+
+ buf := &bytes.Buffer{}
+ if cfg.Golden != "" {
+ if cfg.Stdout != nil {
+ t.Fatal("cannot set Golden and Stdout")
+ }
+ cfg.Stdout = buf
+ }
+ cmd, err := cmd.New(args)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if cfg.Stdout != nil {
+ cmd.SetOutput(cfg.Stdout)
+ } else {
+ cmd.SetOutput(buf)
+ }
+ if cfg.Stdin != nil {
+ cmd.SetInput(cfg.Stdin)
+ }
+ if err = cmd.Run(context.Background()); err != nil {
+ if cfg.Stdout == nil {
+ logf(t, "Output:\n%s", buf.String())
+ }
+ logf(t, "Execution failed: %v", err)
+ }
+
+ if cfg.Golden == "" {
+ return
+ }
+
+ pattern := fmt.Sprintf("//.*%s.*", regexp.QuoteMeta(dir))
+ re, err := regexp.Compile(pattern)
+ if err != nil {
+ t.Fatal(err)
+ }
+ got := re.ReplaceAllString(buf.String(), "")
+ got = strings.TrimSpace(got)
+
+ want := strings.TrimSpace(cfg.Golden)
+ if got != want {
+ t.Errorf("files differ:\n%s", diff.Diff(got, want))
+ }
+}
+
+func logf(t *testing.T, format string, args ...interface{}) {
+ t.Helper()
+ t.Logf(format, args...)
+}
+
+func splitArgs(t *testing.T, s string) (args []string) {
+ c := cuetest.NewChunker(t, []byte(s))
+ for {
+ found := c.Find(" '")
+ args = append(args, strings.Split(c.Text(), " ")...)
+ if !found {
+ break
+ }
+ c.Next("", "' ")
+ args = append(args, c.Text())
+ }
+ return args
+}
diff --git a/internal/cuetest/sim.go b/internal/cuetest/sim.go
deleted file mode 100644
index 51aa95d..0000000
--- a/internal/cuetest/sim.go
+++ /dev/null
@@ -1,121 +0,0 @@
-// Copyright 2019 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 cuetest
-
-import (
- "bytes"
- "context"
- "fmt"
- "io"
- "os"
- "regexp"
- "strings"
- "testing"
-
- "github.com/kylelemons/godebug/diff"
-
- "cuelang.org/go/cmd/cue/cmd"
-)
-
-type Config struct {
- Stdin io.Reader
- Stdout io.Writer
- Golden string
-}
-
-// Run executes the given command in the given directory and reports any
-// errors comparing it to the gold standard.
-func Run(t *testing.T, dir, command string, cfg *Config) {
- if cfg == nil {
- cfg = &Config{}
- }
-
- old, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- if err = os.Chdir(dir); err != nil {
- t.Fatal(err)
- }
- defer func() { os.Chdir(old) }()
-
- logf(t, "Executing command: %s", command)
-
- command = strings.TrimSpace(command[4:])
- args := SplitArgs(t, command)
- logf(t, "Args: %q", args)
-
- buf := &bytes.Buffer{}
- if cfg.Golden != "" {
- if cfg.Stdout != nil {
- t.Fatal("cannot set Golden and Stdout")
- }
- cfg.Stdout = buf
- }
- cmd, err := cmd.New(args)
- if err != nil {
- t.Fatal(err)
- }
- if cfg.Stdout != nil {
- cmd.SetOutput(cfg.Stdout)
- } else {
- cmd.SetOutput(buf)
- }
- if cfg.Stdin != nil {
- cmd.SetInput(cfg.Stdin)
- }
- if err = cmd.Run(context.Background()); err != nil {
- if cfg.Stdout == nil {
- logf(t, "Output:\n%s", buf.String())
- }
- logf(t, "Execution failed: %v", err)
- }
-
- if cfg.Golden == "" {
- return
- }
-
- pattern := fmt.Sprintf("//.*%s.*", regexp.QuoteMeta(dir))
- re, err := regexp.Compile(pattern)
- if err != nil {
- t.Fatal(err)
- }
- got := re.ReplaceAllString(buf.String(), "")
- got = strings.TrimSpace(got)
-
- want := strings.TrimSpace(cfg.Golden)
- if got != want {
- t.Errorf("files differ:\n%s", diff.Diff(got, want))
- }
-}
-
-func logf(t *testing.T, format string, args ...interface{}) {
- t.Helper()
- t.Logf(format, args...)
-}
-
-func SplitArgs(t *testing.T, s string) (args []string) {
- c := NewChunker(t, []byte(s))
- for {
- found := c.Find(" '")
- args = append(args, strings.Split(c.Text(), " ")...)
- if !found {
- break
- }
- c.Next("", "' ")
- args = append(args, c.Text())
- }
- return args
-}