Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cmd/cue: add goproxytest instance for script tests
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
myitcv committed Jan 29, 2021
1 parent b8d9972 commit dfce25c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
17 changes: 15 additions & 2 deletions cmd/cue/cmd/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions cmd/cue/cmd/testdata/mod/example.com_blah_v1.0.0.txt
Original file line number Diff line number Diff line change
@@ -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"
29 changes: 29 additions & 0 deletions cmd/cue/cmd/testdata/script/goproxytest.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dfce25c

Please sign in to comment.