| 1 | package testutils |
| 2 | |
| 3 | import ( |
| 4 | "log" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | ) |
| 8 | |
| 9 | func MustOpen(name string) *os.File { |
| 10 | f, err := os.Open(name) |
| 11 | if err != nil { |
| 12 | log.Panicf("opening %s: %s", name, err) |
| 13 | } |
| 14 | return f |
| 15 | } |
| 16 | |
| 17 | // Searches for a file in a dir, then the parent dir, etc. |
| 18 | // If the file is not found, an empty string is returned. |
| 19 | func FindUp(name, dir string) string { |
| 20 | curDir := dir |
| 21 | for { |
| 22 | entries, err := os.ReadDir(curDir) |
| 23 | if err != nil { |
| 24 | panic(err) |
| 25 | } |
| 26 | for _, e := range entries { |
| 27 | if name == e.Name() { |
| 28 | return filepath.Join(curDir, name) |
| 29 | } |
| 30 | } |
| 31 | newDir := filepath.Dir(curDir) |
| 32 | if newDir == curDir { |
| 33 | return "" |
| 34 | } |
| 35 | curDir = newDir |
| 36 | } |
| 37 | } |