turbo-editors/turbo-corepublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on main · k33g · 4h ago
file_test.go · 341 lines · 8.7 KBGo Blame HistoryRaw
  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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package buffer

import (
	"errors"
	"os"
	"path/filepath"
	"testing"
)

func TestOpenReadsTheFile(t *testing.T) {
	path := filepath.Join(t.TempDir(), "main.go")
	writeTestFile(t, path, "package main\n\nfunc main() {}\n")

	b, err := Open(path)
	if err != nil {
		t.Fatalf("Open() error = %v", err)
	}

	if got := b.LineCount(); got != 3 {
		t.Errorf("LineCount() = %d, want 3", got)
	}
	if got := b.Path(); got != path {
		t.Errorf("Path() = %q, want %q", got, path)
	}
	if b.Modified() {
		t.Error("a freshly opened buffer must not be modified")
	}
	if b.CanUndo() {
		t.Error("opening a file must not leave anything to undo")
	}
}

func TestOpenAMissingFileGivesAnEmptyBuffer(t *testing.T) {
	path := filepath.Join(t.TempDir(), "new.go")

	b, err := Open(path)
	if err != nil {
		t.Fatalf("Open() error = %v, want nil for a file that does not exist yet", err)
	}

	if got := b.LineCount(); got != 1 {
		t.Errorf("LineCount() = %d, want 1", got)
	}
	if got := b.Path(); got != path {
		t.Errorf("Path() = %q, want the requested path", got)
	}
	if b.Modified() {
		t.Error("an empty new buffer must not be modified")
	}
}

func TestOpenADirectoryFails(t *testing.T) {
	if _, err := Open(t.TempDir()); err == nil {
		t.Fatal("Open() error = nil, want a failure when the path is a directory")
	}
}

func TestSaveWritesTheTextBack(t *testing.T) {
	path := filepath.Join(t.TempDir(), "main.go")
	writeTestFile(t, path, "package main\n")

	b, err := Open(path)
	if err != nil {
		t.Fatalf("Open() error = %v", err)
	}
	b.MoveBufferEnd()
	b.Insert("\nfunc main() {}")

	if err := b.Save(); err != nil {
		t.Fatalf("Save() error = %v", err)
	}
	if b.Modified() {
		t.Error("Save must clear the modified flag")
	}

	want := "package main\nfunc main() {}\n"
	if got := readTestFile(t, path); got != want {
		t.Errorf("file content = %q, want %q", got, want)
	}
}

func TestSaveRoundTripsAnUnmodifiedFileByteForByte(t *testing.T) {
	tests := []struct {
		name    string
		content string
	}{
		{"unix endings", "a\nb\n"},
		{"windows endings", "a\r\nb\r\n"},
		{"no trailing newline", "a\nb"},
		{"empty file", ""},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			path := filepath.Join(t.TempDir(), "f.txt")
			writeTestFile(t, path, tc.content)

			b, err := Open(path)
			if err != nil {
				t.Fatalf("Open() error = %v", err)
			}
			if err := b.Save(); err != nil {
				t.Fatalf("Save() error = %v", err)
			}

			if got := readTestFile(t, path); got != tc.content {
				t.Errorf("file content = %q, want %q", got, tc.content)
			}
		})
	}
}

func TestSaveWithoutAPathReportsErrNoPath(t *testing.T) {
	b := NewFromString("orphan")

	err := b.Save()

	if !errors.Is(err, ErrNoPath) {
		t.Errorf("Save() error = %v, want ErrNoPath", err)
	}
}

func TestSaveAsAdoptsTheNewPath(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "copy.go")
	b := NewFromString("package main\n")

	if err := b.SaveAs(path); err != nil {
		t.Fatalf("SaveAs() error = %v", err)
	}

	if got := b.Path(); got != path {
		t.Errorf("Path() = %q, want %q", got, path)
	}
	if got := readTestFile(t, path); got != "package main\n" {
		t.Errorf("file content = %q", got)
	}
	if entries, _ := os.ReadDir(dir); len(entries) != 1 {
		t.Errorf("the directory holds %d entries, want 1 — the temporary file must be gone", len(entries))
	}
}

func TestSaveKeepsTheExistingFilePermissions(t *testing.T) {
	path := filepath.Join(t.TempDir(), "script.go")
	writeTestFile(t, path, "package main\n")
	if err := os.Chmod(path, 0o640); err != nil {
		t.Fatalf("Chmod() error = %v", err)
	}

	b, err := Open(path)
	if err != nil {
		t.Fatalf("Open() error = %v", err)
	}
	if err := b.Save(); err != nil {
		t.Fatalf("Save() error = %v", err)
	}

	info, err := os.Stat(path)
	if err != nil {
		t.Fatalf("Stat() error = %v", err)
	}
	if got := info.Mode().Perm(); got != 0o640 {
		t.Errorf("permissions = %o, want 640 — the atomic rename must not tighten them", got)
	}
}

func TestSaveIntoAMissingDirectoryFails(t *testing.T) {
	b := NewFromString("x")

	err := b.SaveAs(filepath.Join(t.TempDir(), "nope", "f.go"))

	if err == nil {
		t.Fatal("SaveAs() error = nil, want a failure when the directory does not exist")
	}
	if b.Path() != "" {
		t.Errorf("Path() = %q, want it left alone after a failed save", b.Path())
	}
}

func TestModeOfFallsBackForAMissingFile(t *testing.T) {
	if got := modeOf(filepath.Join(t.TempDir(), "absent")); got != defaultFileMode {
		t.Errorf("modeOf(missing) = %o, want %o", got, defaultFileMode)
	}
}

// writeTestFile creates a file with the given content, failing the test if it
// cannot.
func writeTestFile(t *testing.T, path, content string) {
	t.Helper()
	if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
		t.Fatalf("writing %s: %v", path, err)
	}
}

// readTestFile returns the content of a file, failing the test if it cannot.
func readTestFile(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)
}

func TestReloadPicksUpAChangeMadeOnDisk(t *testing.T) {
	// What a formatter run from the Go menu leaves behind.
	path := filepath.Join(t.TempDir(), "main.go")
	writeTestFile(t, path, "package main\n")
	b, err := Open(path)
	if err != nil {
		t.Fatalf("Open() error = %v", err)
	}

	writeTestFile(t, path, "package main\n\nfunc main() {}\n")
	changed, err := b.Reload()

	if err != nil {
		t.Fatalf("Reload() error = %v", err)
	}
	if !changed {
		t.Error("Reload() reported no change for a file that was rewritten")
	}
	if got := b.Text(); got != "package main\n\nfunc main() {}\n" {
		t.Errorf("the buffer holds %q", got)
	}
	if b.Modified() {
		t.Error("a reloaded buffer reports itself modified")
	}
}

func TestReloadReportsNoChangeWhenTheFileIsTheSame(t *testing.T) {
	// A caller uses this to tell a file something rewrote from one it did not.
	path := filepath.Join(t.TempDir(), "main.go")
	writeTestFile(t, path, "package main\n")
	b, _ := Open(path)

	changed, err := b.Reload()

	if err != nil {
		t.Fatalf("Reload() error = %v", err)
	}
	if changed {
		t.Error("Reload() reported a change for a file nobody touched")
	}
}

func TestReloadRefusesToThrowAwayUnsavedWork(t *testing.T) {
	path := filepath.Join(t.TempDir(), "main.go")
	writeTestFile(t, path, "package main\n")
	b, _ := Open(path)
	b.Insert("// mine")
	before := b.Text()

	writeTestFile(t, path, "something else\n")
	_, err := b.Reload()

	if !errors.Is(err, ErrModified) {
		t.Fatalf("Reload() error = %v, want ErrModified", err)
	}
	if got := b.Text(); got != before {
		t.Errorf("the buffer was reloaded anyway: %q", got)
	}
}

func TestReloadKeepsTheCursorWhereItWas(t *testing.T) {
	// A formatter moves lines about; putting the cursor back at the top would
	// lose the reader's place for no reason.
	path := filepath.Join(t.TempDir(), "main.go")
	writeTestFile(t, path, "one\ntwo\nthree\n")
	b, _ := Open(path)
	b.SetCursor(Position{Line: 1, Col: 2})

	if _, err := b.Reload(); err != nil {
		t.Fatalf("Reload() error = %v", err)
	}
	writeTestFile(t, path, "one\nTWO!\nthree\n")
	if _, err := b.Reload(); err != nil {
		t.Fatalf("Reload() error = %v", err)
	}

	if got := b.Cursor(); got.Line != 1 || got.Col != 2 {
		t.Errorf("Cursor() = %+v, want line 1 column 2", got)
	}
}

func TestReloadClampsACursorPastTheEndOfTheNewFile(t *testing.T) {
	path := filepath.Join(t.TempDir(), "main.go")
	writeTestFile(t, path, "one\ntwo\nthree\nfour\n")
	b, _ := Open(path)
	b.SetCursor(Position{Line: 3, Col: 4})

	writeTestFile(t, path, "one\n")
	if _, err := b.Reload(); err != nil {
		t.Fatalf("Reload() error = %v", err)
	}

	cursor := b.Cursor()
	if cursor.Line >= b.LineCount() {
		t.Errorf("Cursor() = %+v, past the end of a %d-line file", cursor, b.LineCount())
	}
}

func TestReloadForgetsTheUndoHistory(t *testing.T) {
	// Undoing back past a reload would restore text the file no longer has.
	path := filepath.Join(t.TempDir(), "main.go")
	writeTestFile(t, path, "one\n")
	b, _ := Open(path)
	b.Insert("x")
	if err := b.Save(); err != nil {
		t.Fatalf("Save() error = %v", err)
	}

	writeTestFile(t, path, "reformatted\n")
	if _, err := b.Reload(); err != nil {
		t.Fatalf("Reload() error = %v", err)
	}
	b.Undo()

	if got := b.Text(); got != "reformatted\n" {
		t.Errorf("undo after a reload gave %q", got)
	}
}

func TestReloadWithNoPathSaysSo(t *testing.T) {
	if _, err := New().Reload(); !errors.Is(err, ErrNoPath) {
		t.Errorf("Reload() error = %v, want ErrNoPath", err)
	}
}

func TestReloadOfAFileThatHasGoneIsAnError(t *testing.T) {
	path := filepath.Join(t.TempDir(), "main.go")
	writeTestFile(t, path, "package main\n")
	b, _ := Open(path)

	if err := os.Remove(path); err != nil {
		t.Fatalf("removing the file: %v", err)
	}
	if _, err := b.Reload(); err == nil {
		t.Error("Reload() accepted a file that no longer exists")
	}
}