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
This commit is contained in:
Abhinav Gupta
2021-02-02 10:07:29 -08:00
committed by GitHub
parent 12f27ba263
commit d17779fa59

View File

@@ -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