Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 54787d1

Browse files
author
tro3
committed
Cleaning up test file functions
1 parent a15e2c2 commit 54787d1

File tree

4 files changed

+31
-34
lines changed

4 files changed

+31
-34
lines changed

lock_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ func TestReadLock(t *testing.T) {
1818
h := test.NewHelper(t)
1919
defer h.Cleanup()
2020

21-
_, err := readLock(h.GetTestFileReader("lock/error.json"))
21+
_, err := readLock(h.GetTestFile("lock/error.json"))
2222
if err == nil {
2323
t.Error("Reading lock with invalid props should have caused error, but did not")
2424
} else if !strings.Contains(err.Error(), "both a branch") {
2525
t.Errorf("Unexpected error %q; expected multiple version error", err)
2626
}
2727

28-
l, err := readLock(h.GetTestFileReader("lock/golden.json"))
28+
l, err := readLock(h.GetTestFile("lock/golden.json"))
2929
if err != nil {
3030
t.Fatalf("Should have read Lock correctly, but got err %q", err)
3131
}

manifest_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ func TestReadManifest(t *testing.T) {
1717
h := test.NewHelper(t)
1818
defer h.Cleanup()
1919

20-
_, err := readManifest(h.GetTestFileReader("manifest/error.json"))
20+
_, err := readManifest(h.GetTestFile("manifest/error.json"))
2121
if err == nil {
2222
t.Error("Reading manifest with invalid props should have caused error, but did not")
2323
} else if !strings.Contains(err.Error(), "multiple constraints") {
2424
t.Errorf("Unexpected error %q; expected multiple constraint error", err)
2525
}
2626

27-
m2, err := readManifest(h.GetTestFileReader("manifest/golden.json"))
27+
m2, err := readManifest(h.GetTestFile("manifest/golden.json"))
2828
if err != nil {
2929
t.Fatalf("Should have read Manifest correctly, but got err %q", err)
3030
}

test/test.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ type Helper struct {
4747

4848
// NewHelper initializes a new helper for testing.
4949
func NewHelper(t *testing.T) *Helper {
50-
wd, _ := os.Getwd()
50+
wd, err := os.Getwd()
51+
if err != nil {
52+
panic(err)
53+
}
5154
return &Helper{t: t, origWd: wd}
5255
}
5356

@@ -417,45 +420,39 @@ func (h *Helper) TempFile(path, contents string) {
417420
h.Must(ioutil.WriteFile(filepath.Join(h.tempdir, path), bytes, 0644))
418421
}
419422

420-
// GetTestFileBytes reads a file form the testdata directory into memory. src is
421-
// relative to ./testdata. Assumes tests take place starting in the cmd/dep
422-
// directory.
423-
func (h *Helper) GetTestFileBytes(src string) []byte {
424-
content, err := ioutil.ReadFile(filepath.Join(h.origWd, "_testdata", src))
423+
// GetTestFile reads a file from the testdata directory into memory. src is
424+
// relative to ./_testdata.
425+
func (h *Helper) GetTestFile(src string) io.ReadCloser {
426+
content, err := os.Open(filepath.Join(h.origWd, "_testdata", src))
425427
if err != nil {
426428
panic(err)
427429
}
428-
if strings.HasSuffix(src, ".go") {
429-
formatted, err := format.Source(content)
430-
if err == nil {
431-
content = formatted
432-
}
433-
}
434430
return content
435431
}
436432

437-
// GetTestFileString reads a file form the testdata directory into memory. src is
438-
// relative to ./testdata. Assumes tests take place starting in the cmd/dep
439-
// directory.
433+
// GetTestFileString reads a file from the testdata directory into memory. src is
434+
// relative to ./_testdata.
440435
func (h *Helper) GetTestFileString(src string) string {
441-
return string(h.GetTestFileBytes(src))
442-
}
443-
444-
// GetTestFileReader reads a file form the testdata directory into memory. src is
445-
// relative to ./testdata. Assumes tests take place starting in the cmd/dep
446-
// directory.
447-
func (h *Helper) GetTestFileReader(src string) io.Reader {
448-
return strings.NewReader(h.GetTestFileString(src))
436+
content, err := ioutil.ReadAll(h.GetTestFile(src))
437+
if err != nil {
438+
panic(err)
439+
}
440+
return string(content)
449441
}
450442

451443
// TempCopy copies a temporary file from testdata into the temporary directory.
452444
// dest is relative to the temp directory location, and src is relative to
453-
// ./testdata. Assumes tests take place starting in the cmd/dep directory.
445+
// ./_testdata.
454446
func (h *Helper) TempCopy(dest, src string) {
455-
content := h.GetTestFileBytes(src)
456-
h.makeTempdir()
457-
h.Must(os.MkdirAll(filepath.Join(h.tempdir, filepath.Dir(dest)), 0755))
458-
h.Must(ioutil.WriteFile(filepath.Join(h.tempdir, dest), content, 0644))
447+
in := h.GetTestFile(src)
448+
defer in.Close()
449+
h.TempDir(filepath.Dir(dest))
450+
out, err := os.Create(filepath.Join(h.tempdir, dest))
451+
if err != nil {
452+
panic(err)
453+
}
454+
defer out.Close()
455+
io.Copy(out, in)
459456
}
460457

461458
// TempDir adds a temporary directory for a run of testgo.

txn_writer_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ func TestTxnWriter(t *testing.T) {
9797
expectedManifest := h.GetTestFileString("txn_writer/expected_manifest.json")
9898
expectedLock := h.GetTestFileString("txn_writer/expected_lock.json")
9999

100-
m, err := readManifest(h.GetTestFileReader("txn_writer/expected_manifest.json"))
100+
m, err := readManifest(h.GetTestFile("txn_writer/expected_manifest.json"))
101101
h.Must(err)
102-
l, err := readLock(h.GetTestFileReader("txn_writer/expected_lock.json"))
102+
l, err := readLock(h.GetTestFile("txn_writer/expected_lock.json"))
103103
h.Must(err)
104104

105105
// Just write manifest

0 commit comments

Comments
 (0)