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