Skip to content

Commit

Permalink
TestStacktraceFiltersVendorZap: Fix for modules
Browse files Browse the repository at this point in the history
This fixes TestStacktraceFiltersVendorZap to work with Go modules.

For context, the test previously did the following:

- Set up a temporary GOPATH
- Copy stacktrace_ext_test.go into it
- Symlink Zap and relevant dependencies into a vendor/ directory in the
  temporary GOPATH
- Run specific tests from stacktrace_ext_test.go with Zap inside the
  vendor/ directory

We're no longer using GOPATH or vendor/ directories, but this behavior
of Zap when *it* is inside a vendor/ directory should be retained.

To that end, this changes TestStacktraceFiltersVendorZap to continue to
run with the following changes:

- Use `go mod download -json` to retrieve the locations of all of Zap's
  dependencies on-disk, and symlink them into the temporary GOPATH's
  vendor/.
- Explicitly set `GO111MODULE=off` before running `go test` because we
  may be running in an environment where `GO111MODULE=on` is set.
  • Loading branch information
abhinav committed Oct 30, 2019
1 parent 4c619d4 commit 9bec370
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions stacktrace_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package zap_test

import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -84,33 +85,35 @@ func TestStacktraceFiltersZapMarshal(t *testing.T) {
}

func TestStacktraceFiltersVendorZap(t *testing.T) {
// We need to simulate a zap as a vendor library, so we're going to create a fake GOPATH
// and run the above test which will contain zap in the vendor directory.
// We already have the dependencies downloaded so this should be
// instant.
deps := downloadDependencies(t)

// We need to simulate a zap as a vendor library, so we're going to
// create a fake GOPATH and run the above test which will contain zap
// in the vendor directory.
withGoPath(t, func(goPath string) {
curDir, err := os.Getwd()
zapDir, err := os.Getwd()
require.NoError(t, err, "Failed to get current directory")

testDir := filepath.Join(goPath, "src/go.uber.org/zap_test/")
vendorDir := filepath.Join(testDir, "vendor")
require.NoError(t, os.MkdirAll(testDir, 0777), "Failed to create source director")

curFile := getSelfFilename(t)
//copyFile(t, curFile, filepath.Join(testDir, curFile))
setupSymlink(t, curFile, filepath.Join(testDir, curFile))

// Set up symlinks for zap, and for any test dependencies.
setupSymlink(t, curDir, filepath.Join(vendorDir, "go.uber.org/zap"))
for _, testDep := range []string{"github.com/stretchr/testify"} {
target := filepath.Join(curDir, "vendor", testDep)
_, err := os.Stat(target)
require.NoError(t, err, "Required dependency (%v) not installed in vendor", target)
setupSymlink(t, target, filepath.Join(vendorDir, testDep))
setupSymlink(t, zapDir, filepath.Join(vendorDir, "go.uber.org/zap"))
for _, dep := range deps {
setupSymlink(t, dep.Dir, filepath.Join(vendorDir, dep.ImportPath))
}

// Now run the above test which ensures we filter out zap stacktraces, but this time
// zap is in a vendor
// Now run the above test which ensures we filter out zap
// stacktraces, but this time zap is in a vendor
cmd := exec.Command("go", "test", "-v", "-run", "TestStacktraceFiltersZap")
cmd.Dir = testDir
cmd.Env = append(os.Environ(), "GO111MODULE=off")
out, err := cmd.CombinedOutput()
require.NoError(t, err, "Failed to run test in vendor directory, output: %s", out)
assert.Contains(t, string(out), "PASS")
Expand Down Expand Up @@ -162,3 +165,27 @@ func setupSymlink(t *testing.T, src, dst string) {

require.NoError(t, os.Symlink(srcAbs, dst), "Failed to set up symlink")
}

type dependency struct {
ImportPath string `json:"Path"` // import path of the dependency
Dir string `json:"Dir"` // location on disk
}

// Downloads all dependencies for the current Go module and reports their
// module paths and locations on disk.
func downloadDependencies(t *testing.T) []dependency {
cmd := exec.Command("go", "mod", "download", "-json")

stdout, err := cmd.Output()
require.NoError(t, err, "Failed to run 'go mod download'")

var deps []dependency
dec := json.NewDecoder(bytes.NewBuffer(stdout))
for dec.More() {
var d dependency
require.NoError(t, dec.Decode(&d), "Failed to decode dependency")
deps = append(deps, d)
}

return deps
}

0 comments on commit 9bec370

Please sign in to comment.