Skip to content

Commit

Permalink
internal/cuetest: consolidate testscript/cuetxtar helpers
Browse files Browse the repository at this point in the history
Currently -update testing flags and the use of CUE_UPDATE to update the
golden files of testscript/cuetxtar scripts is littered throughout
various files/packages.

In this change we consolidate the use of -update flags to update golden
files into a single use of the CUE_UPDATE environment variable

We also defines a "long" build tag, along with an accompanying
testscript Condition function so that [long] can be used as a condition
in testscript scripts (used in a later CL)

Change-Id: If0898d4997010257d00ecf9b3bfdc1125796cbc4
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8601
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
myitcv committed Feb 13, 2021
1 parent bf6c3fc commit cb4fae9
Show file tree
Hide file tree
Showing 28 changed files with 215 additions and 94 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ jobs:
key: ${{ runner.os }}-${{ matrix.go-version }}-go-${{ hashFiles('**/go.sum')
}}
restore-keys: ${{ runner.os }}-${{ matrix.go-version }}-go-
- if: ${{ github.ref == 'refs/heads/master' }}
name: Set go build tags
run: go env -w GOFLAGS=-tags=long
- if: matrix.go-version == '1.14.14' && matrix.os != 'windows-2019'
name: Generate
run: go generate ./...
Expand Down
3 changes: 0 additions & 3 deletions cmd/cue/cmd/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cmd

import (
"flag"
"os"
"testing"

Expand All @@ -24,8 +23,6 @@ import (

var _ = errors.Print

var update = flag.Bool("update", os.Getenv("CUE_UPDATE") != "", "update the test files")

func printConfig(t *testing.T) *errors.Config {
t.Helper()

Expand Down
4 changes: 3 additions & 1 deletion cmd/cue/cmd/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/parser"
"cuelang.org/go/internal/cuetest"
)

// TestLatest checks that the examples match the latest language standard,
Expand Down Expand Up @@ -80,14 +81,15 @@ func TestScript(t *testing.T) {
}
p := testscript.Params{
Dir: filepath.Join("testdata", "script"),
UpdateScripts: *update,
UpdateScripts: cuetest.UpdateGoldenFiles,
Setup: func(e *testscript.Env) error {
e.Vars = append(e.Vars,
"GOPROXY="+srv.URL,
"GONOSUMDB=*", // GOPROXY is a private proxy
)
return nil
},
Condition: cuetest.Condition,
}
if err := gotooltest.Setup(&p); err != nil {
t.Fatal(err)
Expand Down
13 changes: 12 additions & 1 deletion cmd/cue/cmd/testdata/script/cmd_github.txt
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ jobs:
key: ${{ runner.os }}-${{ matrix.go-version }}-go-${{ hashFiles('**/go.sum')
}}
restore-keys: ${{ runner.os }}-${{ matrix.go-version }}-go-
- if: ${{ github.ref == 'refs/heads/master' }}
name: Set go build tags
run: go env -w GOFLAGS=-tags=long
- if: matrix.go-version == '1.14.14' && matrix.os != 'windows-2019'
name: Generate
run: go generate ./...
Expand Down Expand Up @@ -1171,7 +1174,10 @@ test: _#bashWorkflow & {
needs: "start"
strategy: _#testStrategy
"runs-on": "${{ matrix.os }}"
steps: [_#writeCookiesFile, _#installGo, _#checkoutCode, _#cacheGoModules, _#goGenerate, _#goTest, _#goTestRace & {
steps: [_#writeCookiesFile, _#installGo, _#checkoutCode, _#cacheGoModules, _#setGoBuildTags & {
_#tags: "long"
if: "${{ \(_#isMaster) }}"
}, _#goGenerate, _#goTest, _#goTestRace & {
if: "${{ \(_#isMaster) || \(_#isCLCITestBranch) && matrix.go-version == '\(_#latestStableGo)' && matrix.os == '\(_#linuxMachine)' }}"
}, _#goReleaseCheck, _#checkGitClean, _#pullThroughProxy, _#failCLBuild]
}
Expand Down Expand Up @@ -1401,6 +1407,11 @@ _#testStrategy: {
os: [_#linuxMachine, _#macosMachine, _#windowsMachine]
}
}
_#setGoBuildTags: _#step & {
_#tags: string
name: "Set go build tags"
run: "go env -w GOFLAGS=-tags=\(_#tags)"
}
_#installGo: _#step & {
name: "Install Go"
uses: "actions/setup-go@v2"
Expand Down
8 changes: 3 additions & 5 deletions cue/format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package format

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"path/filepath"
Expand All @@ -30,6 +29,7 @@ import (
"cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
"cuelang.org/go/internal/cuetest"
)

var (
Expand All @@ -41,8 +41,6 @@ const (
dataDir = "testdata"
)

var update = flag.Bool("update", false, "update golden files")

type checkMode uint

const (
Expand Down Expand Up @@ -136,7 +134,7 @@ func runcheck(t *testing.T, source, golden string, mode checkMode) {
}

// update golden files if necessary
if *update {
if cuetest.UpdateGoldenFiles {
if err := ioutil.WriteFile(golden, res, 0644); err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -193,7 +191,7 @@ type entry struct {
mode checkMode
}

// Use go test -update to create/update the respective golden files.
// Set CUE_UPDATE=1 to create/update the respective golden files.
var data = []entry{
{"comments.input", "comments.golden", simplify},
{"simplify.input", "simplify.golden", simplify},
Expand Down
75 changes: 56 additions & 19 deletions cue/testdata/eval/github.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ test: _#bashWorkflow & {
needs: "start"
strategy: _#testStrategy
"runs-on": "${{ matrix.os }}"
steps: [_#writeCookiesFile, _#installGo, _#checkoutCode, _#cacheGoModules, _#goGenerate, _#goTest, _#goTestRace & {
steps: [_#writeCookiesFile, _#installGo, _#checkoutCode, _#cacheGoModules, _#setGoBuildTags & {
_#tags: "long"
if: "${{ \(_#isMaster) }}"
}, _#goGenerate, _#goTest, _#goTestRace & {
if: "${{ \(_#isMaster) || \(_#isCLCITestBranch) && matrix.go-version == '\(_#latestStableGo)' && matrix.os == '\(_#linuxMachine)' }}"
}, _#goReleaseCheck, _#checkGitClean, _#pullThroughProxy, _#failCLBuild]
}
Expand Down Expand Up @@ -282,6 +285,11 @@ _#testStrategy: {
os: [_#linuxMachine, _#macosMachine, _#windowsMachine]
}
}
_#setGoBuildTags: _#step & {
_#tags: string
name: "Set go build tags"
run: "go env -w GOFLAGS=-tags=\(_#tags)"
}
_#installGo: _#step & {
name: "Install Go"
uses: "actions/setup-go@v2"
Expand Down Expand Up @@ -1152,33 +1160,39 @@ import "strings"
}
}
4: (#struct){
_#tags(:ci): (string){ "long" }
name: (string){ "Set go build tags" }
run: (string){ "go env -w GOFLAGS=-tags=long" }
if: (string){ "${{ github.ref == 'refs/heads/master' }}" }
}
5: (#struct){
name: (string){ "Generate" }
run: (string){ "go generate ./..." }
if: (string){ "matrix.go-version == '1.14.14' && matrix.os != 'windows-2019'" }
}
5: (#struct){
6: (#struct){
name: (string){ "Test" }
run: (string){ "go test ./..." }
}
6: (#struct){
7: (#struct){
name: (string){ "Test with -race" }
run: (string){ "go test -race ./..." }
if: (string){ "${{ github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/ci/') && matrix.go-version == '1.15.8' && matrix.os == 'ubuntu-18.04' }}" }
}
7: (#struct){
8: (#struct){
name: (string){ "gorelease check" }
run: (string){ "go run golang.org/x/exp/cmd/gorelease" }
}
8: (#struct){
9: (#struct){
name: (string){ "Check that git is clean post generate and tests" }
run: (string){ "test -z \"$(git status --porcelain)\" || (git status; git diff; false)" }
}
9: (#struct){
10: (#struct){
name: (string){ "Pull this commit through the proxy on master" }
run: (string){ "v=$(git rev-parse HEAD)\ncd $(mktemp -d)\ngo mod init mod.com\nGOPROXY=https://proxy.golang.org go get -d cuelang.org/go/cmd/cue@$v" }
if: (string){ "${{ github.ref == 'refs/heads/master' }}" }
}
10: (#struct){
11: (#struct){
if: (string){ "${{ startsWith(github.ref, 'refs/heads/ci/') && failure() }}" }
name: (string){ "Post any failures for this matrix entry" }
run: (string){ "curl -f -s -H \"Content-Type: application/json\" --request POST --data '{\"message\":\"Build failed for ${{ runner.os }}-${{ matrix.go-version }}; see ${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }} for more details\",\"labels\":{\"Code-Review\":-1}}' -b ~/.gitcookies https://cue-review.googlesource.com/a/changes/$(basename $(dirname $GITHUB_REF))/revisions/$(basename $GITHUB_REF)/review" }
Expand Down Expand Up @@ -1264,7 +1278,7 @@ import "strings"
}
res: (_|_){
// [incomplete] invalid interpolation: cannot convert incomplete value "string" to JSON:
// ./workflows.cue:122:9
// ./workflows.cue:125:9
}
}
}
Expand Down Expand Up @@ -1386,7 +1400,7 @@ import "strings"
_#type(:ci): (string){ string }
if: (_|_){
// [incomplete] workflows.1.schema._#dispatchJob.if: invalid interpolation: non-concrete value string (type string):
// ./workflows.cue:134:14
// ./workflows.cue:137:14
}
}
name: (string){ "Repository Dispatch" }
Expand Down Expand Up @@ -1770,33 +1784,39 @@ import "strings"
}
}
4: (#struct){
_#tags(:ci): (string){ "long" }
name: (string){ "Set go build tags" }
run: (string){ "go env -w GOFLAGS=-tags=long" }
if: (string){ "${{ github.ref == 'refs/heads/master' }}" }
}
5: (#struct){
name: (string){ "Generate" }
run: (string){ "go generate ./..." }
if: (string){ "matrix.go-version == '1.14.14' && matrix.os != 'windows-2019'" }
}
5: (#struct){
6: (#struct){
name: (string){ "Test" }
run: (string){ "go test ./..." }
}
6: (#struct){
7: (#struct){
name: (string){ "Test with -race" }
run: (string){ "go test -race ./..." }
if: (string){ "${{ github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/ci/') && matrix.go-version == '1.15.8' && matrix.os == 'ubuntu-18.04' }}" }
}
7: (#struct){
8: (#struct){
name: (string){ "gorelease check" }
run: (string){ "go run golang.org/x/exp/cmd/gorelease" }
}
8: (#struct){
9: (#struct){
name: (string){ "Check that git is clean post generate and tests" }
run: (string){ "test -z \"$(git status --porcelain)\" || (git status; git diff; false)" }
}
9: (#struct){
10: (#struct){
name: (string){ "Pull this commit through the proxy on master" }
run: (string){ "v=$(git rev-parse HEAD)\ncd $(mktemp -d)\ngo mod init mod.com\nGOPROXY=https://proxy.golang.org go get -d cuelang.org/go/cmd/cue@$v" }
if: (string){ "${{ github.ref == 'refs/heads/master' }}" }
}
10: (#struct){
11: (#struct){
if: (string){ "${{ startsWith(github.ref, 'refs/heads/ci/') && failure() }}" }
name: (string){ "Post any failures for this matrix entry" }
run: (string){ "curl -f -s -H \"Content-Type: application/json\" --request POST --data '{\"message\":\"Build failed for ${{ runner.os }}-${{ matrix.go-version }}; see ${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }} for more details\",\"labels\":{\"Code-Review\":-1}}' -b ~/.gitcookies https://cue-review.googlesource.com/a/changes/$(basename $(dirname $GITHUB_REF))/revisions/$(basename $GITHUB_REF)/review" }
Expand Down Expand Up @@ -1882,7 +1902,7 @@ import "strings"
}
res: (_|_){
// [incomplete] invalid interpolation: cannot convert incomplete value "string" to JSON:
// ./workflows.cue:122:9
// ./workflows.cue:125:9
}
}
}
Expand Down Expand Up @@ -2001,7 +2021,7 @@ import "strings"
_#type(:ci): (string){ string }
if: (_|_){
// [incomplete] repository_dispatch._#dispatchJob.if: invalid interpolation: non-concrete value string (type string):
// ./workflows.cue:134:14
// ./workflows.cue:137:14
}
}
name: (string){ "Repository Dispatch" }
Expand Down Expand Up @@ -2356,6 +2376,14 @@ import "strings"
}
}
}
_#setGoBuildTags(:ci): (#struct){
_#tags(:ci): (string){ string }
name: (string){ "Set go build tags" }
run: (_|_){
// [incomplete] _#setGoBuildTags.run: invalid interpolation: non-concrete value string (type string):
// ./workflows.cue:283:10
}
}
_#installGo(:ci): (#struct){
name: (string){ "Install Go" }
uses: (string){ "actions/setup-go@v2" }
Expand Down Expand Up @@ -2407,7 +2435,7 @@ import "strings"
_#cueckooCopybaraImage(:ci): (string){ "cueckoo/copybara:afc4ae03eed00b0c9d7415141cd1b5dfa583da7c" }
_#copybaraCmd(:ci): (_|_){
// [incomplete] _#copybaraCmd: invalid interpolation: non-concrete value string (type string):
// ./workflows.cue:342:2
// ./workflows.cue:350:2
_#cmd(:ci): (string){ string }
}
_#copybaraSteps(:ci): (#list){
Expand All @@ -2421,7 +2449,7 @@ import "strings"
name: (string){ string }
run: (_|_){
// [incomplete] _#copybaraSteps.1.run: invalid interpolation: non-concrete value string (type string):
// ./workflows.cue:342:2
// ./workflows.cue:350:2
_#cmd(:ci): (string){ string }
}
}
Expand Down Expand Up @@ -2493,6 +2521,10 @@ import "strings"
〈3;_#installGo〉,
〈3;_#checkoutCode〉,
〈3;_#cacheGoModules〉,
(〈3;_#setGoBuildTags〉 & {
_#tags: "long"
if: "${{ \(〈3;_#isMaster〉) }}"
}),
〈3;_#goGenerate〉,
〈3;_#goTest〉,
(〈3;_#goTestRace〉 & {
Expand Down Expand Up @@ -2769,6 +2801,11 @@ import "strings"
]
}
}
_#setGoBuildTags: (〈0;_#step〉 & {
_#tags: string
name: "Set go build tags"
run: "go env -w GOFLAGS=-tags=\(〈0;_#tags〉)"
})
_#installGo: (〈0;_#step〉 & {
name: "Install Go"
uses: "actions/setup-go@v2"
Expand Down
6 changes: 2 additions & 4 deletions doc/tutorial/basics/script_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package basics

import (
"flag"
"os"
"path"
"path/filepath"
Expand All @@ -13,10 +12,9 @@ import (

"cuelang.org/go/cmd/cue/cmd"
"cuelang.org/go/cue/parser"
"cuelang.org/go/internal/cuetest"
)

var update = flag.Bool("update", false, "update the test files")

// TestLatest checks that the examples match the latest language standard,
// even if still valid in backwards compatibility mode.
func TestLatest(t *testing.T) {
Expand Down Expand Up @@ -54,7 +52,7 @@ func TestScript(t *testing.T) {
}
testscript.Run(t, testscript.Params{
Dir: path,
UpdateScripts: *update,
UpdateScripts: cuetest.UpdateGoldenFiles,
})
return nil
})
Expand Down

0 comments on commit cb4fae9

Please sign in to comment.