Skip to content

Commit

Permalink
constraints: test against the constraints package from x/exp instead …
Browse files Browse the repository at this point in the history
…of std

Fixes golang/go#50792

Change-Id: I9abb0972c9e51c88591de1cc1ef10337f75f84d0
Reviewed-on: https://go-review.googlesource.com/c/exp/+/382834
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
  • Loading branch information
Bryan C. Mills committed Feb 3, 2022
1 parent bb28937 commit d4f80a9
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions constraints/constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d4f80a9

Please sign in to comment.