From dfce25c5ebaa99db30edb3f55c96e9c19c9e3cd7 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Mon, 25 Jan 2021 10:57:20 +0000 Subject: [PATCH] cmd/cue: add goproxytest instance for script tests This allows us to define modules as txtar archives that are served via a github.com/rogpeppe/go-internal/goproxytest.Server instance from within cmd/cue/cmd/testdata/mod that speaks the module download protocol. cmd/cue/cmd testscript tests are run with a GOPROXY value that is this local server, allowing us to support go get requests that are resolved locally, with modules that can be shared across testscript scripts. This will be used by various cue get go tests. Whilst we don't have full CUE module support, we can also use this proxy server as means of go get-ting CUE modules (that have a light Go module shim). The example.com/blah module defined in this change includes a .cue file as part of the Go module that forms part of a later cue get go test that verifies the vendoring of .cue files within the cue get go package arguments. Change-Id: I52351c02697ee3963576b1d9391e0aa56dada132 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8291 Reviewed-by: CUE cueckoo Reviewed-by: Marcel van Lohuizen --- cmd/cue/cmd/script_test.go | 17 +++++++++-- .../testdata/mod/example.com_blah_v1.0.0.txt | 18 ++++++++++++ cmd/cue/cmd/testdata/script/goproxytest.txt | 29 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 cmd/cue/cmd/testdata/mod/example.com_blah_v1.0.0.txt create mode 100644 cmd/cue/cmd/testdata/script/goproxytest.txt diff --git a/cmd/cue/cmd/script_test.go b/cmd/cue/cmd/script_test.go index 004b5e42e..43012c917 100644 --- a/cmd/cue/cmd/script_test.go +++ b/cmd/cue/cmd/script_test.go @@ -26,6 +26,7 @@ import ( "strings" "testing" + "github.com/rogpeppe/go-internal/goproxytest" "github.com/rogpeppe/go-internal/gotooltest" "github.com/rogpeppe/go-internal/testscript" "github.com/rogpeppe/go-internal/txtar" @@ -37,7 +38,8 @@ import ( // TestLatest checks that the examples match the latest language standard, // even if still valid in backwards compatibility mode. func TestLatest(t *testing.T) { - filepath.Walk("testdata/script", func(fullpath string, info os.FileInfo, err error) error { + root := filepath.Join("testdata", "script") + filepath.Walk(root, func(fullpath string, info os.FileInfo, err error) error { if !strings.HasSuffix(fullpath, ".txt") || strings.HasPrefix(filepath.Base(fullpath), "fix") { return nil @@ -72,9 +74,20 @@ func TestLatest(t *testing.T) { } func TestScript(t *testing.T) { + srv, err := goproxytest.NewServer(filepath.Join("testdata", "mod"), "") + if err != nil { + t.Fatalf("cannot start proxy: %v", err) + } p := testscript.Params{ - Dir: "testdata/script", + Dir: filepath.Join("testdata", "script"), UpdateScripts: *update, + Setup: func(e *testscript.Env) error { + e.Vars = append(e.Vars, + "GOPROXY="+srv.URL, + "GONOSUMDB=*", // GOPROXY is a private proxy + ) + return nil + }, } if err := gotooltest.Setup(&p); err != nil { t.Fatal(err) diff --git a/cmd/cue/cmd/testdata/mod/example.com_blah_v1.0.0.txt b/cmd/cue/cmd/testdata/mod/example.com_blah_v1.0.0.txt new file mode 100644 index 000000000..0fc3b23a5 --- /dev/null +++ b/cmd/cue/cmd/testdata/mod/example.com_blah_v1.0.0.txt @@ -0,0 +1,18 @@ +-- .mod -- +module example.com/blah + +-- .info -- +{"Version":"v1.0.0","Time":"2018-10-22T18:45:39Z"} + +-- go.mod -- +module example.com/blah + +-- blah.go -- +package blah + +// Some fruit +const Name = "Orange" +-- blah.cue -- +package blah + +Type: "Fruit" diff --git a/cmd/cue/cmd/testdata/script/goproxytest.txt b/cmd/cue/cmd/testdata/script/goproxytest.txt new file mode 100644 index 000000000..549e0276c --- /dev/null +++ b/cmd/cue/cmd/testdata/script/goproxytest.txt @@ -0,0 +1,29 @@ +# Basic test to ensure that the goproxytest instance used by various testscript +# tests works as expected. + +go get example.com/blah +go mod tidy +cmp go.mod go.mod.golden + +-- go.mod -- +module rubbish + +go 1.14 +-- main.go -- +package main + +import ( + "fmt" + + "example.com/blah" +) + +func main() { + fmt.Println(blah.Orange) +} +-- go.mod.golden -- +module rubbish + +go 1.14 + +require example.com/blah v1.0.0