From d17779fa59ad2a7b0255eb79c94256428c40c089 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Tue, 2 Feb 2021 10:07:29 -0800 Subject: [PATCH] nocmp/test: Test with Go modules (#83) The nocmp integration test creates a temporary directory with a copy of nocmp.go and a Go file that should not compile if our implementation of nocmp is correct. This worked fine without a GOPATH set previously, but with Go 1.16, it seems that we need to have the GOPATH environment variable set (ref https://github.com/golang/go/commit/cdbd4d49d8b). Change the nocmp integration test to rely on Go modules for the test. We can point GOPATH to an arbitrary temporary directory because we don't rely on anything in it. Fixes #82 --- nocmp_test.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/nocmp_test.go b/nocmp_test.go index d4be166..e248087 100644 --- a/nocmp_test.go +++ b/nocmp_test.go @@ -114,6 +114,9 @@ func TestNocmpCopy(t *testing.T) { }) } +// Fake go.mod with no dependencies. +const _exampleGoMod = `module example.com/nocmp` + const _badFile = `package atomic import "fmt" @@ -135,26 +138,29 @@ func TestNocmpIntegration(t *testing.T) { require.NoError(t, err, "unable to set up temporary directory") defer os.RemoveAll(tempdir) - src := filepath.Join(tempdir, "src") - require.NoError(t, os.Mkdir(src, 0755), "unable to make source directory") - nocmp, err := ioutil.ReadFile("nocmp.go") require.NoError(t, err, "unable to read nocmp.go") require.NoError(t, - ioutil.WriteFile(filepath.Join(src, "nocmp.go"), nocmp, 0644), + ioutil.WriteFile(filepath.Join(tempdir, "go.mod"), []byte(_exampleGoMod), 0644), + "unable to write go.mod") + + require.NoError(t, + ioutil.WriteFile(filepath.Join(tempdir, "nocmp.go"), nocmp, 0644), "unable to write nocmp.go") require.NoError(t, - ioutil.WriteFile(filepath.Join(src, "bad.go"), []byte(_badFile), 0644), + ioutil.WriteFile(filepath.Join(tempdir, "bad.go"), []byte(_badFile), 0644), "unable to write bad.go") var stderr bytes.Buffer cmd := exec.Command("go", "build") - cmd.Dir = src + cmd.Dir = tempdir // Forget OS build enviroment and set up a minimal one for "go build" - // to run. + // to run. We need GOPATH and GOCACHE set for the compiler to run but + // we don't do anything with them. cmd.Env = []string{ + "GOPATH=" + filepath.Join(tempdir, "gopath"), "GOCACHE=" + filepath.Join(tempdir, "gocache"), } cmd.Stderr = &stderr