turbo-editors/turbo-corepublic Fork 0
v1.0.2
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 v1.0.2 · k33g · 17h ago
tree_test.go · 324 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
package filetree

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

// makeTree builds a directory from a list of paths; one ending in a slash
// becomes a directory, and intermediate directories are made as needed.
func makeTree(t *testing.T, paths ...string) string {
	t.Helper()

	root := t.TempDir()
	for _, path := range paths {
		full := filepath.Join(root, strings.TrimSuffix(path, "/"))
		if strings.HasSuffix(path, "/") {
			if err := os.MkdirAll(full, 0o755); err != nil {
				t.Fatalf("creating %s: %v", path, err)
			}
			continue
		}
		if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
			t.Fatalf("creating the parent of %s: %v", path, err)
		}
		if err := os.WriteFile(full, nil, 0o644); err != nil {
			t.Fatalf("creating %s: %v", path, err)
		}
	}
	return root
}

// names renders the visible rows as "  ".Depth + name, which reads like the
// tree does on screen and makes a failure legible.
func names(t *Tree) []string {
	rows := t.Rows()
	out := make([]string, len(rows))
	for i, row := range rows {
		name := row.Node.Name()
		if row.Node.IsDir() {
			name += "/"
		}
		out[i] = strings.Repeat("  ", row.Depth) + name
	}
	return out
}

// wantRows compares the visible rows against what they should be.
func wantRows(t *testing.T, tree *Tree, want ...string) {
	t.Helper()

	got := names(tree)
	if len(got) != len(want) {
		t.Fatalf("the tree shows\n%s\nwant\n%s", strings.Join(got, "\n"), strings.Join(want, "\n"))
	}
	for i := range want {
		if got[i] != want[i] {
			t.Errorf("row %d is %q, want %q (whole tree:\n%s)", i, got[i], want[i], strings.Join(got, "\n"))
		}
	}
}

// find returns the node with a name, failing the test when there is none.
func find(t *testing.T, tree *Tree, name string) *Node {
	t.Helper()

	for _, row := range tree.Rows() {
		if row.Node.Name() == name {
			return row.Node
		}
	}
	t.Fatalf("no visible row is called %q; the tree shows:\n%s", name, strings.Join(names(tree), "\n"))
	return nil
}

// open returns a tree on a directory, failing the test if it cannot be read.
func open(t *testing.T, root string) *Tree {
	t.Helper()

	tree, err := New(root)
	if err != nil {
		t.Fatalf("New(%q) error = %v", root, err)
	}
	return tree
}

func TestANewTreeShowsTheRootsContents(t *testing.T) {
	tree := open(t, makeTree(t, "main.go", "go.mod"))

	wantRows(t, tree, "go.mod", "main.go")
}

func TestDirectoriesComeBeforeFilesEachSorted(t *testing.T) {
	tree := open(t, makeTree(t, "zebra.go", "alpha.go", "ui/", "app/"))

	wantRows(t, tree, "app/", "ui/", "alpha.go", "zebra.go")
}

func TestTheGitDirectoryIsHidden(t *testing.T) {
	tree := open(t, makeTree(t, ".git/config", "main.go"))

	wantRows(t, tree, "main.go")
}

func TestEveryOtherDotEntryIsShown(t *testing.T) {
	// .turbo-go holds the project's own settings, which the editor encourages
	// people to edit; a tree that hid it would make them unreachable from it.
	tree := open(t, makeTree(t, ".turbo-go/settings.toml", ".gitignore", "main.go"))

	wantRows(t, tree, ".turbo-go/", ".gitignore", "main.go")
}

func TestADirectoryIsNotReadUntilItIsExpanded(t *testing.T) {
	tree := open(t, makeTree(t, "internal/app/app.go"))

	if got := find(t, tree, "internal").Children(); len(got) != 0 {
		t.Errorf("a closed directory already holds %d children", len(got))
	}
	wantRows(t, tree, "internal/")
}

func TestExpandingADirectoryShowsItsContents(t *testing.T) {
	tree := open(t, makeTree(t, "internal/app/app.go", "main.go"))

	find(t, tree, "internal").Expand()

	wantRows(t, tree, "internal/", "  app/", "main.go")
}

func TestExpandingNestsAsDeepAsItIsOpened(t *testing.T) {
	tree := open(t, makeTree(t, "internal/app/app.go", "main.go"))

	find(t, tree, "internal").Expand()
	find(t, tree, "app").Expand()

	wantRows(t, tree, "internal/", "  app/", "    app.go", "main.go")
}

func TestCollapsingHidesTheContentsWithoutForgettingThem(t *testing.T) {
	tree := open(t, makeTree(t, "internal/app.go"))
	node := find(t, tree, "internal")
	node.Expand()

	node.Collapse()
	wantRows(t, tree, "internal/")

	if len(node.Children()) == 0 {
		t.Error("collapsing threw away what had been read")
	}
	node.Expand()
	wantRows(t, tree, "internal/", "  app.go")
}

func TestToggleOpensThenCloses(t *testing.T) {
	tree := open(t, makeTree(t, "internal/app.go"))
	node := find(t, tree, "internal")

	node.Toggle()
	if !node.Expanded() {
		t.Error("Toggle() did not open a closed directory")
	}
	node.Toggle()
	if node.Expanded() {
		t.Error("Toggle() did not close an open directory")
	}
}

func TestExpandingAFileDoesNothing(t *testing.T) {
	tree := open(t, makeTree(t, "main.go"))
	node := find(t, tree, "main.go")

	node.Expand()

	if node.Expanded() {
		t.Error("a file reports itself as expanded")
	}
	wantRows(t, tree, "main.go")
}

func TestANodeCarriesItsAbsolutePath(t *testing.T) {
	root := makeTree(t, "internal/app.go")
	tree := open(t, root)
	find(t, tree, "internal").Expand()

	if got, want := find(t, tree, "app.go").Path(), filepath.Join(root, "internal", "app.go"); got != want {
		t.Errorf("Path() = %q, want %q", got, want)
	}
}

func TestTheRootIsNamedAfterItsDirectory(t *testing.T) {
	root := makeTree(t, "main.go")

	tree := open(t, root)

	if got := tree.Root().Name(); got != filepath.Base(root) {
		t.Errorf("Root().Name() = %q, want %q", got, filepath.Base(root))
	}
	if !tree.Root().IsDir() {
		t.Error("the root does not call itself a directory")
	}
}

func TestNewRefusesAFileAndAMissingPath(t *testing.T) {
	root := makeTree(t, "main.go")

	if _, err := New(filepath.Join(root, "main.go")); err == nil {
		t.Error("New() accepted a file as the root of a tree")
	}
	if _, err := New(filepath.Join(root, "nowhere")); err == nil {
		t.Error("New() accepted a path that does not exist")
	}
}

func TestAnUnreadableDirectoryShowsAsEmptyRatherThanFailing(t *testing.T) {
	if os.Geteuid() == 0 {
		t.Skip("running as root, which can read a directory with no permissions")
	}
	root := makeTree(t, "locked/secret.go", "main.go")
	locked := filepath.Join(root, "locked")
	if err := os.Chmod(locked, 0o000); err != nil {
		t.Fatalf("removing the permissions on %s: %v", locked, err)
	}
	t.Cleanup(func() { os.Chmod(locked, 0o755) })

	tree := open(t, root)
	find(t, tree, "locked").Expand()

	// One unreadable directory is not a reason to refuse the rest of the tree.
	wantRows(t, tree, "locked/", "main.go")
}

func TestRefreshShowsAFileMadeAfterTheTreeWasRead(t *testing.T) {
	root := makeTree(t, "main.go")
	tree := open(t, root)

	if err := os.WriteFile(filepath.Join(root, "other.go"), nil, 0o644); err != nil {
		t.Fatalf("creating the new file: %v", err)
	}
	wantRows(t, tree, "main.go") // not yet

	tree.Refresh()

	wantRows(t, tree, "main.go", "other.go")
}

func TestRefreshDropsAFileThatHasGone(t *testing.T) {
	root := makeTree(t, "main.go", "other.go")
	tree := open(t, root)

	if err := os.Remove(filepath.Join(root, "other.go")); err != nil {
		t.Fatalf("removing the file: %v", err)
	}
	tree.Refresh()

	wantRows(t, tree, "main.go")
}

func TestRefreshKeepsWhatWasOpenOpen(t *testing.T) {
	root := makeTree(t, "internal/app/app.go", "main.go")
	tree := open(t, root)
	find(t, tree, "internal").Expand()
	find(t, tree, "app").Expand()

	if err := os.WriteFile(filepath.Join(root, "internal", "app", "new.go"), nil, 0o644); err != nil {
		t.Fatalf("creating the new file: %v", err)
	}
	tree.Refresh()

	// The whole shape survives, and the new file appears in the right branch.
	wantRows(t, tree, "internal/", "  app/", "    app.go", "    new.go", "main.go")
}

func TestRefreshLeavesUnopenedDirectoriesUnread(t *testing.T) {
	// Refreshing a large project must cost what is on screen, not a walk of
	// everything under it.
	root := makeTree(t, "internal/app/app.go")
	tree := open(t, root)

	tree.Refresh()

	if got := find(t, tree, "internal").Children(); len(got) != 0 {
		t.Errorf("refreshing read a directory nobody opened: %d children", len(got))
	}
}

func TestRefreshTakesAwayTheBranchOfADeletedDirectory(t *testing.T) {
	root := makeTree(t, "internal/app/app.go", "main.go")
	tree := open(t, root)
	find(t, tree, "internal").Expand()

	if err := os.RemoveAll(filepath.Join(root, "internal")); err != nil {
		t.Fatalf("removing the directory: %v", err)
	}
	tree.Refresh()

	wantRows(t, tree, "main.go")
}

func TestAnEmptyProjectHasNoRows(t *testing.T) {
	tree := open(t, t.TempDir())

	if got := len(tree.Rows()); got != 0 {
		t.Errorf("an empty project shows %d rows", got)
	}
}

// writeFile creates an empty file, failing the test if it cannot.
func writeFile(t *testing.T, path string) {
	t.Helper()

	if err := os.WriteFile(path, nil, 0o644); err != nil {
		t.Fatalf("creating %s: %v", path, err)
	}
}

// removeFile deletes a file, failing the test if it cannot.
func removeFile(t *testing.T, path string) {
	t.Helper()

	if err := os.Remove(path); err != nil {
		t.Fatalf("removing %s: %v", path, err)
	}
}