| 🛟 Updated. 28d5985 k33g 16h ago | 1 | package projectfile |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "runtime" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestWriteCreatesTheFileAndItsDirectory(t *testing.T) { |
| 12 | path := filepath.Join(t.TempDir(), ".turbo-go", "tools.toml") |
| 13 | |
| 14 | if err := Write(path, []byte("[[tool]]\n")); err != nil { |
| 15 | t.Fatalf("Write() error = %v", err) |
| 16 | } |
| 17 | |
| 18 | if got := readFile(t, path); got != "[[tool]]\n" { |
| 19 | t.Errorf("the file holds %q", got) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestWriteReplacesAnExistingFile(t *testing.T) { |
| 24 | path := filepath.Join(t.TempDir(), "settings.toml") |
| 25 | if err := os.WriteFile(path, []byte("old"), 0o644); err != nil { |
| 26 | t.Fatalf("writing the first version: %v", err) |
| 27 | } |
| 28 | |
| 29 | if err := Write(path, []byte("new")); err != nil { |
| 30 | t.Fatalf("Write() error = %v", err) |
| 31 | } |
| 32 | |
| 33 | if got := readFile(t, path); got != "new" { |
| 34 | t.Errorf("the file holds %q, want the new contents", got) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestTheFileIsReadableByEveryone(t *testing.T) { |
| 39 | // These files are meant to be committed and read by a team. CreateTemp |
| 40 | // makes a file owner-readable only, and the rename would carry that |
| 41 | // through unless the mode is set on the way. |
| 42 | if runtime.GOOS == "windows" { |
| 43 | t.Skip("file modes do not work this way on Windows") |
| 44 | } |
| 45 | path := filepath.Join(t.TempDir(), "tools.toml") |
| 46 | |
| 47 | if err := Write(path, []byte("x")); err != nil { |
| 48 | t.Fatalf("Write() error = %v", err) |
| 49 | } |
| 50 | |
| 51 | info, err := os.Stat(path) |
| 52 | if err != nil { |
| 53 | t.Fatalf("stat: %v", err) |
| 54 | } |
| 55 | if got := info.Mode().Perm(); got != fileMode { |
| 56 | t.Errorf("the file is %o, want %o", got, fileMode) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestWriteLeavesNoTemporaryFileBehind(t *testing.T) { |
| 61 | // A failed or finished write must not litter .turbo-go with half-written |
| 62 | // files somebody then has to explain. |
| 63 | dir := t.TempDir() |
| 64 | path := filepath.Join(dir, "tools.toml") |
| 65 | |
| 66 | if err := Write(path, []byte("x")); err != nil { |
| 67 | t.Fatalf("Write() error = %v", err) |
| 68 | } |
| 69 | |
| 70 | entries, err := os.ReadDir(dir) |
| 71 | if err != nil { |
| 72 | t.Fatalf("reading the directory: %v", err) |
| 73 | } |
| 74 | for _, entry := range entries { |
| 75 | if strings.HasPrefix(entry.Name(), ".projectfile-") { |
| 76 | t.Errorf("a temporary file was left behind: %s", entry.Name()) |
| 77 | } |
| 78 | } |
| 79 | if len(entries) != 1 { |
| 80 | t.Errorf("the directory holds %d entries, want just the file", len(entries)) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestWriteNamesTheFileInItsError(t *testing.T) { |
| 85 | // A path that cannot be a directory: the parent is a regular file. |
| 86 | dir := t.TempDir() |
| 87 | blocker := filepath.Join(dir, "blocker") |
| 88 | if err := os.WriteFile(blocker, nil, 0o644); err != nil { |
| 89 | t.Fatalf("writing the blocker: %v", err) |
| 90 | } |
| 91 | path := filepath.Join(blocker, "tools.toml") |
| 92 | |
| 93 | err := Write(path, []byte("x")) |
| 94 | |
| 95 | if err == nil { |
| 96 | t.Fatal("Write() accepted a path under a regular file") |
| 97 | } |
| 98 | if !strings.Contains(err.Error(), path) { |
| 99 | t.Errorf("the error does not name the file: %v", err) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestWriteAcceptsEmptyContents(t *testing.T) { |
| 104 | path := filepath.Join(t.TempDir(), "empty.toml") |
| 105 | |
| 106 | if err := Write(path, nil); err != nil { |
| 107 | t.Fatalf("Write() error = %v", err) |
| 108 | } |
| 109 | if got := readFile(t, path); got != "" { |
| 110 | t.Errorf("the file holds %q, want nothing", got) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // readFile returns a file's contents. |
| 115 | func readFile(t *testing.T, path string) string { |
| 116 | t.Helper() |
| 117 | |
| 118 | data, err := os.ReadFile(path) |
| 119 | if err != nil { |
| 120 | t.Fatalf("reading %s: %v", path, err) |
| 121 | } |
| 122 | return string(data) |
| 123 | } |