Skip to content

Commit

Permalink
cmd/cue: fix race in testscript tests that use go/packages
Browse files Browse the repository at this point in the history
We have recently been seeing a number of test flakes in the
get_go_types.txt which were ultimately reported as golang/go#44137.

However, it turns out this is actually caused by an interaction between
the ./... pattern and use of go/packages within a testscript test,
specifically go/packages (via cmd/go) writing temporary files and
directories to $WORK/tmp which then end up racing with the walk of $WORK
to find directories/packages that match ./...

As of v1.8.0, in a standard testscript setup a temporary directory is
established at $WORK/.tmp. We now follow that same pattern for the home
directory for consistency's sake.

Change-Id: Ia8c7ffd471f9879d3283a47db5f2a5e165f619e1
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9002
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
myitcv committed Mar 12, 2021
1 parent eb7b60d commit 7c5d28e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
27 changes: 27 additions & 0 deletions cmd/cue/cmd/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path"
"path/filepath"
goruntime "runtime"
"strings"
"testing"

Expand All @@ -36,6 +37,10 @@ import (
"cuelang.org/go/internal/cuetest"
)

const (
homeDirName = ".user-home"
)

// 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 @@ -83,9 +88,17 @@ func TestScript(t *testing.T) {
Dir: filepath.Join("testdata", "script"),
UpdateScripts: cuetest.UpdateGoldenFiles,
Setup: func(e *testscript.Env) error {
// Set up a home dir within work dir with a . prefix so that the
// Go/CUE pattern ./... does not descend into it.
home := filepath.Join(e.WorkDir, homeDirName)
if err := os.Mkdir(home, 0777); err != nil {
return err
}

e.Vars = append(e.Vars,
"GOPROXY="+srv.URL,
"GONOSUMDB=*", // GOPROXY is a private proxy
homeEnvName()+"="+home,
)
return nil
},
Expand Down Expand Up @@ -164,3 +177,17 @@ func TestMain(m *testing.M) {
"cue": MainTest,
}))
}

// homeEnvName extracts the logic from os.UserHomeDir to get the
// name of the environment variable that should be used when
// seting the user's home directory
func homeEnvName() string {
switch goruntime.GOOS {
case "windows":
return "USERPROFILE"
case "plan9":
return "home"
default:
return "HOME"
}
}
2 changes: 0 additions & 2 deletions cmd/cue/cmd/testdata/script/get_go_types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
# that is left for other tests, so that this test can focus
# on the various rules of get go.

[go1.15] [windows] skip 'flakey per golang.org/issue/44137'

# cue get go
cue get go --local ./...
cmp ./pkg3/pkg3_go_gen.cue ./pkg3/pkg3_go_gen.cue.golden
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/lib/pq v1.0.0 // indirect
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de
github.com/pkg/errors v0.8.1 // indirect
github.com/rogpeppe/go-internal v1.6.2-0.20200830194709-1115b6af0369
github.com/rogpeppe/go-internal v1.8.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.3
github.com/stretchr/testify v1.2.2
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de/go.mod h1:kJun4WP5gFuH
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -90,8 +92,8 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.6.2-0.20200830194709-1115b6af0369 h1:wdCVGtPadWC/ZuuLC7Hv58VQ5UF7V98ewE71n5mJfrM=
github.com/rogpeppe/go-internal v1.6.2-0.20200830194709-1115b6af0369/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down

0 comments on commit 7c5d28e

Please sign in to comment.