all: do not write to testdata directories

I got tired of spurious 'git' diffs while a 'go test' was running, so
I fixed the test that produced the diffs. (We need to do that anyway
in order to run them in the module cache, plus it's just good hygiene
not to have tests interfering with each other's sources.)

Tested using:

	$ chmod -R ugo-w . && go test ./...; chmod -R u+w .

Updates golang/go#28387

Change-Id: Ie17e31aecf0e3cb022df5503d7c443000366a5c6
Reviewed-on: https://go-review.googlesource.com/c/tools/+/192577
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Bryan C. Mills 2019-08-30 11:02:08 -04:00
Родитель 6afc7fcab5
Коммит df13fa7beb
4 изменённых файлов: 79 добавлений и 31 удалений

Просмотреть файл

@ -23,17 +23,6 @@ import (
const ( const (
// Data directory, also the package directory for the test. // Data directory, also the package directory for the test.
testdata = "testdata" testdata = "testdata"
// Binaries we compile.
testcover = "./testcover.exe"
)
var (
// Files we use.
testMain = filepath.Join(testdata, "main.go")
testTest = filepath.Join(testdata, "test.go")
coverInput = filepath.Join(testdata, "test_line.go")
coverOutput = filepath.Join(testdata, "test_cover.go")
) )
var debug = false // Keeps the rewritten files around if set. var debug = false // Keeps the rewritten files around if set.
@ -48,6 +37,34 @@ var debug = false // Keeps the rewritten files around if set.
func TestCover(t *testing.T) { func TestCover(t *testing.T) {
testenv.NeedsTool(t, "go") testenv.NeedsTool(t, "go")
tmpdir, err := ioutil.TempDir("", "TestCover")
if err != nil {
t.Fatal(err)
}
defer func() {
if debug {
fmt.Printf("test files left in %s\n", tmpdir)
} else {
os.RemoveAll(tmpdir)
}
}()
testcover := filepath.Join(tmpdir, "testcover.exe")
testMain := filepath.Join(tmpdir, "main.go")
testTest := filepath.Join(tmpdir, "test.go")
coverInput := filepath.Join(tmpdir, "test_line.go")
coverOutput := filepath.Join(tmpdir, "test_cover.go")
for _, f := range []string{testMain, testTest} {
data, err := ioutil.ReadFile(filepath.Join(testdata, filepath.Base(f)))
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(f, data, 0644); err != nil {
t.Fatal(err)
}
}
// Read in the test file (testTest) and write it, with LINEs specified, to coverInput. // Read in the test file (testTest) and write it, with LINEs specified, to coverInput.
file, err := ioutil.ReadFile(testTest) file, err := ioutil.ReadFile(testTest)
if err != nil { if err != nil {
@ -62,18 +79,10 @@ func TestCover(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
// defer removal of test_line.go
if !debug {
defer os.Remove(coverInput)
}
// go build -o testcover // go build -o testcover
cmd := exec.Command("go", "build", "-o", testcover) cmd := exec.Command("go", "build", "-o", testcover)
run(cmd, t) run(cmd, t)
// defer removal of testcover
defer os.Remove(testcover)
// ./testcover -mode=count -var=coverTest -o ./testdata/test_cover.go testdata/test_line.go // ./testcover -mode=count -var=coverTest -o ./testdata/test_cover.go testdata/test_line.go
cmd = exec.Command(testcover, "-mode=count", "-var=coverTest", "-o", coverOutput, coverInput) cmd = exec.Command(testcover, "-mode=count", "-var=coverTest", "-o", coverOutput, coverInput)
run(cmd, t) run(cmd, t)

Просмотреть файл

@ -275,13 +275,15 @@ func TestGuru(t *testing.T) {
json := strings.Contains(filename, "-json/") json := strings.Contains(filename, "-json/")
queries := parseQueries(t, filename) queries := parseQueries(t, filename)
golden := filename + "lden" golden := filename + "lden"
got := filename + "t" gotfh, err := ioutil.TempFile("", filepath.Base(filename)+"t")
gotfh, err := os.Create(got)
if err != nil { if err != nil {
t.Fatalf("Create(%s) failed: %s", got, err) t.Fatal(err)
} }
defer os.Remove(got) got := gotfh.Name()
defer gotfh.Close() defer func() {
gotfh.Close()
os.Remove(got)
}()
// Run the guru on each query, redirecting its output // Run the guru on each query, redirecting its output
// and error (if any) to the foo.got file. // and error (if any) to the foo.got file.

Просмотреть файл

@ -15,6 +15,7 @@ import (
"go/parser" "go/parser"
"go/token" "go/token"
"go/types" "go/types"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -135,7 +136,11 @@ func Test(t *testing.T) {
continue continue
} }
got := filename + "t" // foo.got gotf, err := ioutil.TempFile("", filepath.Base(filename)+"t")
if err != nil {
t.Fatal(err)
}
got := gotf.Name() // foo.got
golden := filename + "lden" // foo.golden golden := filename + "lden" // foo.golden
// Write actual output to foo.got. // Write actual output to foo.got.

Просмотреть файл

@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"go/build" "go/build"
"go/token" "go/token"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -1290,7 +1291,7 @@ func TestDiff(t *testing.T) {
t.Skipf("plan9 diff tool doesn't support -u flag") t.Skipf("plan9 diff tool doesn't support -u flag")
} }
testenv.NeedsTool(t, DiffCmd) testenv.NeedsTool(t, DiffCmd)
testenv.NeedsTool(t, "go") // to locate golang.org/x/tools/refactor/rename testenv.NeedsTool(t, "go") // to locate the package to be renamed
defer func() { defer func() {
Diff = false Diff = false
@ -1299,7 +1300,42 @@ func TestDiff(t *testing.T) {
Diff = true Diff = true
stdout = new(bytes.Buffer) stdout = new(bytes.Buffer)
if err := Main(&build.Default, "", `"golang.org/x/tools/refactor/rename".justHereForTestingDiff`, "Foo"); err != nil { // Set up a fake GOPATH in a temporary directory,
// and ensure we're in GOPATH mode.
tmpdir, err := ioutil.TempDir("", "TestDiff")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
buildCtx := build.Default
buildCtx.GOPATH = tmpdir
pkgDir := filepath.Join(tmpdir, "src", "example.com", "rename")
if err := os.MkdirAll(pkgDir, 0777); err != nil {
t.Fatal(err)
}
prevWD, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(prevWD)
if err := os.Chdir(pkgDir); err != nil {
t.Fatal(err)
}
const goFile = `package rename
func justHereForTestingDiff() {
justHereForTestingDiff()
}
`
if err := ioutil.WriteFile(filepath.Join(pkgDir, "rename_test.go"), []byte(goFile), 0644); err != nil {
t.Fatal(err)
}
if err := Main(&buildCtx, "", `"example.com/rename".justHereForTestingDiff`, "Foo"); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -1315,10 +1351,6 @@ func TestDiff(t *testing.T) {
} }
} }
func justHereForTestingDiff() {
justHereForTestingDiff()
}
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Simplifying wrapper around buildutil.FakeContext for packages whose // Simplifying wrapper around buildutil.FakeContext for packages whose