| // Convenience utilities for testing. |
| package testutils |
| |
| import ( |
| "encoding/json" |
| "fmt" |
| "io" |
| "io/ioutil" |
| "os" |
| "path" |
| "runtime" |
| ) |
| |
| // TestDataDir returns the path to the caller's testdata directory, which |
| // is assumed to be "<path to caller dir>/testdata". |
| func TestDataDir() (string, error) { |
| _, thisFile, _, ok := runtime.Caller(0) |
| if !ok { |
| return "", fmt.Errorf("Could not find test data dir: runtime.Caller() failed.") |
| } |
| for skip := 0; ; skip++ { |
| _, file, _, ok := runtime.Caller(skip) |
| if !ok { |
| return "", fmt.Errorf("Could not find test data dir: runtime.Caller() failed.") |
| } |
| if file != thisFile { |
| return path.Join(path.Dir(file), "testdata"), nil |
| } |
| } |
| } |
| |
| func readFile(filename string) (io.Reader, error) { |
| dir, err := TestDataDir() |
| if err != nil { |
| return nil, fmt.Errorf("Could not read %s: %v", filename, err) |
| } |
| f, err := os.Open(path.Join(dir, filename)) |
| if err != nil { |
| return nil, fmt.Errorf("Could not read %s: %v", filename, err) |
| } |
| return f, nil |
| } |
| |
| // ReadFile reads a file from the caller's testdata directory. |
| func ReadFile(filename string) (string, error) { |
| f, err := readFile(filename) |
| if err != nil { |
| return "", err |
| } |
| b, err := ioutil.ReadAll(f) |
| if err != nil { |
| return "", fmt.Errorf("Could not read %s: %v", filename, err) |
| } |
| return string(b), nil |
| } |
| |
| // MustReadFile reads a file from the caller's testdata directory and panics on |
| // error. |
| func MustReadFile(filename string) string { |
| s, err := ReadFile(filename) |
| if err != nil { |
| panic(err) |
| } |
| return s |
| } |
| |
| // ReadJsonFile reads a JSON file from the caller's testdata directory into the |
| // given interface. |
| func ReadJsonFile(filename string, dest interface{}) error { |
| f, err := readFile(filename) |
| if err != nil { |
| return err |
| } |
| return json.NewDecoder(f).Decode(dest) |
| } |
| |
| // MustReadJsonFile reads a JSON file from the caller's testdata directory into |
| // the given interface and panics on error. |
| func MustReadJsonFile(filename string, dest interface{}) { |
| if err := ReadJsonFile(filename, dest); err != nil { |
| panic(err) |
| } |
| } |