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 <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/script_test.go b/cmd/cue/cmd/script_test.go
index 004b5e4..43012c9 100644
--- a/cmd/cue/cmd/script_test.go
+++ b/cmd/cue/cmd/script_test.go
@@ -26,6 +26,7 @@
 	"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 @@
 // 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 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 0000000..0fc3b23
--- /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 0000000..549e027
--- /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