From d4f80a91470e45b31d944fe5f8383717bd92019f Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Thu, 3 Feb 2022 11:20:37 -0500 Subject: [PATCH] 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 Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: David Chase --- constraints/constraints_test.go | 35 +++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/constraints/constraints_test.go b/constraints/constraints_test.go index a285ee64e..5bc43a922 100644 --- a/constraints/constraints_test.go +++ b/constraints/constraints_test.go @@ -42,7 +42,7 @@ type TestTypes struct { var prolog = []byte(` package constrainttest -import "constraints" +import "golang.org/x/exp/constraints" type ( testSigned[T constraints.Signed] struct{ f T } @@ -71,9 +71,40 @@ func TestFailure(t *testing.T) { tmpdir := t.TempDir() - if err := os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte("module constraintest"), 0666); err != nil { + cwd, err := os.Getwd() + if err != nil { t.Fatal(err) } + // This package is golang.org/x/exp/constraints, so the root of the x/exp + // module is the parent directory of the directory in which this test runs. + expModDir := filepath.Dir(cwd) + + modFile := fmt.Sprintf(`module constraintest + +go 1.18 + +replace golang.org/x/exp => %s +`, expModDir) + if err := os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte(modFile), 0666); err != nil { + t.Fatal(err) + } + + // Write the prolog as its own file so that 'go mod tidy' has something to inspect. + // This will ensure that the go.mod and go.sum files include any dependencies + // needed by the constraints package (which should just be some version of + // x/exp itself). + if err := os.WriteFile(filepath.Join(tmpdir, "prolog.go"), []byte(prolog), 0666); err != nil { + t.Fatal(err) + } + + tidyCmd := exec.Command(gocmd, "mod", "tidy") + tidyCmd.Dir = tmpdir + tidyCmd.Env = append(os.Environ(), "PWD="+tmpdir) + if out, err := tidyCmd.CombinedOutput(); err != nil { + t.Fatalf("%v: %v\n%s", tidyCmd, err, out) + } else { + t.Logf("%v:\n%s", tidyCmd, out) + } // Test for types that should not satisfy a constraint. // For each pair of constraint and type, write a Go file