| OLD | NEW |
| 1 // Convenience utilities for testing. | 1 // Convenience utilities for testing. |
| 2 package testutils | 2 package testutils |
| 3 | 3 |
| 4 import ( | 4 import ( |
| 5 "encoding/json" | 5 "encoding/json" |
| 6 "fmt" | 6 "fmt" |
| 7 "io" | 7 "io" |
| 8 "io/ioutil" | 8 "io/ioutil" |
| 9 "os" | 9 "os" |
| 10 "path" | 10 "path" |
| 11 "reflect" | 11 "reflect" |
| 12 "runtime" | 12 "runtime" |
| 13 "testing" | 13 "testing" |
| 14 | 14 |
| 15 "github.com/davecgh/go-spew/spew" | 15 "github.com/davecgh/go-spew/spew" |
| 16 assert "github.com/stretchr/testify/require" | 16 assert "github.com/stretchr/testify/require" |
| 17 ) | 17 ) |
| 18 | 18 |
| 19 // SkipIfShort causes the test to be skipped when running with -short. | 19 // SkipIfShort causes the test to be skipped when running with -short. |
| 20 func SkipIfShort(t *testing.T) { | 20 func SkipIfShort(t *testing.T) { |
| 21 if testing.Short() { | 21 if testing.Short() { |
| 22 t.Skip("Skipping test with -short") | 22 t.Skip("Skipping test with -short") |
| 23 } | 23 } |
| 24 } | 24 } |
| 25 | 25 |
| 26 // AssertDeepEqual fails the test if the two objects do not pass reflect.DeepEqu
al. | 26 // AssertDeepEqual fails the test if the two objects do not pass reflect.DeepEqu
al. |
| 27 func AssertDeepEqual(t *testing.T, a, b interface{}) { | 27 func AssertDeepEqual(t *testing.T, a, b interface{}) { |
| 28 if !reflect.DeepEqual(a, b) { | 28 if !reflect.DeepEqual(a, b) { |
| 29 » » t.Fatalf("Objects to not match: \na:\n%s\n\nb:\n%s\n", spew.Spri
nt(a), spew.Sprint(b)) | 29 » » assert.FailNow(t, fmt.Sprintf("Objects do not match: \na:\n%s\n\
nb:\n%s\n", spew.Sprint(a), spew.Sprint(b))) |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 | 32 |
| 33 // TestDataDir returns the path to the caller's testdata directory, which | 33 // TestDataDir returns the path to the caller's testdata directory, which |
| 34 // is assumed to be "<path to caller dir>/testdata". | 34 // is assumed to be "<path to caller dir>/testdata". |
| 35 func TestDataDir() (string, error) { | 35 func TestDataDir() (string, error) { |
| 36 _, thisFile, _, ok := runtime.Caller(0) | 36 _, thisFile, _, ok := runtime.Caller(0) |
| 37 if !ok { | 37 if !ok { |
| 38 return "", fmt.Errorf("Could not find test data dir: runtime.Cal
ler() failed.") | 38 return "", fmt.Errorf("Could not find test data dir: runtime.Cal
ler() failed.") |
| 39 } | 39 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 115 |
| 116 // Remove attempts to remove the given file and asserts that no error is returne
d. | 116 // Remove attempts to remove the given file and asserts that no error is returne
d. |
| 117 func Remove(t assert.TestingT, fp string) { | 117 func Remove(t assert.TestingT, fp string) { |
| 118 assert.NoError(t, os.Remove(fp)) | 118 assert.NoError(t, os.Remove(fp)) |
| 119 } | 119 } |
| 120 | 120 |
| 121 // RemoveAll attempts to remove the given directory and asserts that no error is
returned. | 121 // RemoveAll attempts to remove the given directory and asserts that no error is
returned. |
| 122 func RemoveAll(t assert.TestingT, fp string) { | 122 func RemoveAll(t assert.TestingT, fp string) { |
| 123 assert.NoError(t, os.RemoveAll(fp)) | 123 assert.NoError(t, os.RemoveAll(fp)) |
| 124 } | 124 } |
| OLD | NEW |