Skip to content

Commit d4f80a9

Browse files
author
Bryan C. Mills
committed
constraints: test against the constraints package from x/exp instead of std
Fixes golang/go#50792 Change-Id: I9abb0972c9e51c88591de1cc1ef10337f75f84d0 Reviewed-on: https://go-review.googlesource.com/c/exp/+/382834 Trust: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent bb28937 commit d4f80a9

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

constraints/constraints_test.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type TestTypes struct {
4242
var prolog = []byte(`
4343
package constrainttest
4444
45-
import "constraints"
45+
import "golang.org/x/exp/constraints"
4646
4747
type (
4848
testSigned[T constraints.Signed] struct{ f T }
@@ -71,9 +71,40 @@ func TestFailure(t *testing.T) {
7171

7272
tmpdir := t.TempDir()
7373

74-
if err := os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte("module constraintest"), 0666); err != nil {
74+
cwd, err := os.Getwd()
75+
if err != nil {
7576
t.Fatal(err)
7677
}
78+
// This package is golang.org/x/exp/constraints, so the root of the x/exp
79+
// module is the parent directory of the directory in which this test runs.
80+
expModDir := filepath.Dir(cwd)
81+
82+
modFile := fmt.Sprintf(`module constraintest
83+
84+
go 1.18
85+
86+
replace golang.org/x/exp => %s
87+
`, expModDir)
88+
if err := os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte(modFile), 0666); err != nil {
89+
t.Fatal(err)
90+
}
91+
92+
// Write the prolog as its own file so that 'go mod tidy' has something to inspect.
93+
// This will ensure that the go.mod and go.sum files include any dependencies
94+
// needed by the constraints package (which should just be some version of
95+
// x/exp itself).
96+
if err := os.WriteFile(filepath.Join(tmpdir, "prolog.go"), []byte(prolog), 0666); err != nil {
97+
t.Fatal(err)
98+
}
99+
100+
tidyCmd := exec.Command(gocmd, "mod", "tidy")
101+
tidyCmd.Dir = tmpdir
102+
tidyCmd.Env = append(os.Environ(), "PWD="+tmpdir)
103+
if out, err := tidyCmd.CombinedOutput(); err != nil {
104+
t.Fatalf("%v: %v\n%s", tidyCmd, err, out)
105+
} else {
106+
t.Logf("%v:\n%s", tidyCmd, out)
107+
}
77108

78109
// Test for types that should not satisfy a constraint.
79110
// For each pair of constraint and type, write a Go file

0 commit comments

Comments
 (0)