turbo-editors/turbo-corepublic Fork 0
v0.9.0
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 v0.9.0 · k33g · 15h ago
snippets_test.go · 408 lines · 11.2 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package snippets

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

	"codeberg.org/turbo-editors/turbo-core/profile"
)

// project builds a project directory holding a snippets file.
func project(t *testing.T, contents string) string {
	t.Helper()

	dir := t.TempDir()
	if contents == "" {
		return dir
	}
	path := ProjectPath(testProfile(), dir)
	if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
		t.Fatalf("creating the snippets directory: %v", err)
	}
	if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
		t.Fatalf("writing the snippets file: %v", err)
	}
	return dir
}

// testProfile is a fictional editor. Its template is a real snippets file, but
// a small one: what is in Turbo Go's template is Turbo Go's own test.
func testProfile() profile.Profile {
	return profile.Profile{
		Name:      "Turbo Test",
		Slug:      "turbo-test",
		Templates: profile.Templates{Snippets: testSnippetsTemplate},
	}
}

// testSnippetsTemplate takes the name of the ungrouped group and the user's own
// snippets path, in that order, as profile.Templates says it must.
const testSnippetsTemplate = `# turbo-test snippets.
#
# A snippet with no group goes into %s.
# Your own snippets live in:
#   %s

[[snippet]]
name = "guard"
group = "Test"
languages = ["test"]
body = """
if broken {
	return
}"""

[[snippet]]
name = "TODO"
body = "TODO: "
`

// noUserSnippets points the user's snippets at an empty directory, so a test
// never reads whoever is running it.
func noUserSnippets(t *testing.T) {
	t.Helper()
	t.Setenv(testProfile().SnippetDirEnvVar(), t.TempDir())
}

// userSnippets writes a user-level snippets file and points the package at it.
func userSnippets(t *testing.T, contents string) {
	t.Helper()

	dir := t.TempDir()
	t.Setenv(testProfile().SnippetDirEnvVar(), dir)
	if err := os.WriteFile(filepath.Join(dir, FileName), []byte(contents), 0o644); err != nil {
		t.Fatalf("writing the user snippets file: %v", err)
	}
}

// load reads a project's snippets, failing the test if it cannot.
func load(t *testing.T, dir string) List {
	t.Helper()

	list, err := Load(testProfile(), dir)
	if err != nil {
		t.Fatalf("Load(testProfile(), %q) error = %v", dir, err)
	}
	return list
}

// summary renders the groups as "Group/name" strings, in order.
func summary(groups []Group) []string {
	var out []string
	for _, group := range groups {
		for _, snippet := range group.Snippets {
			out = append(out, group.Name+"/"+snippet.Name)
		}
	}
	return out
}

// wantGroups compares the grouped snippets against what they should be.
func wantGroups(t *testing.T, groups []Group, want ...string) {
	t.Helper()

	got := summary(groups)
	if len(got) != len(want) {
		t.Fatalf("got\n  %v\nwant\n  %v", got, want)
	}
	for i := range want {
		if got[i] != want[i] {
			t.Errorf("entry %d = %q, want %q (all: %v)", i, got[i], want[i], got)
		}
	}
}

func TestLoadReadsAProjectsSnippets(t *testing.T) {
	noUserSnippets(t)
	dir := project(t, `
[[snippet]]
name = "if err"
group = "Go"
body = "if err != nil {}"

[[snippet]]
name = "TODO"
body = "TODO: "
`)

	list := load(t, dir)

	if list.Len() != 2 {
		t.Fatalf("Len() = %d, want 2", list.Len())
	}
	wantGroups(t, list.Groups("go"), "Go/if err", "General/TODO")
}

func TestASnippetWithNoGroupGoesIntoTheGeneralOne(t *testing.T) {
	noUserSnippets(t)
	dir := project(t, "[[snippet]]\nname = \"x\"\nbody = \"y\"\n")

	wantGroups(t, load(t, dir).Groups(""), ungroupedName+"/x")
}

func TestGroupsKeepTheOrderOfTheFile(t *testing.T) {
	// The menu should match the file, so someone reordering the file sees the
	// menu reorder.
	noUserSnippets(t)
	dir := project(t, `
[[snippet]]
name = "b"
group = "Second"
body = "x"

[[snippet]]
name = "a"
group = "First"
body = "x"

[[snippet]]
name = "c"
group = "Second"
body = "x"
`)

	wantGroups(t, load(t, dir).Groups(""), "Second/b", "Second/c", "First/a")
}

func TestALanguageFiltersTheSnippetsOffered(t *testing.T) {
	noUserSnippets(t)
	dir := project(t, `
[[snippet]]
name = "go thing"
group = "Go"
languages = ["go"]
body = "x"

[[snippet]]
name = "shell thing"
group = "Shell"
languages = ["bash"]
body = "x"

[[snippet]]
name = "anywhere"
body = "x"
`)
	list := load(t, dir)

	wantGroups(t, list.Groups("go"), "Go/go thing", "General/anywhere")
	wantGroups(t, list.Groups("bash"), "Shell/shell thing", "General/anywhere")
	wantGroups(t, list.Groups("markdown"), "General/anywhere")
}

func TestAGroupLeftEmptyByFilteringDoesNotAppear(t *testing.T) {
	noUserSnippets(t)
	dir := project(t, "[[snippet]]\nname = \"x\"\ngroup = \"Go\"\nlanguages = [\"go\"]\nbody = \"y\"\n")

	if groups := load(t, dir).Groups("markdown"); len(groups) != 0 {
		t.Errorf("Groups() = %v, want no group at all", summary(groups))
	}
}

func TestASnippetForSeveralLanguagesAppliesToEach(t *testing.T) {
	noUserSnippets(t)
	dir := project(t, "[[snippet]]\nname = \"x\"\nlanguages = [\"go\", \"bash\"]\nbody = \"y\"\n")
	list := load(t, dir)

	for _, language := range []string{"go", "bash"} {
		if got := len(list.Groups(language)); got != 1 {
			t.Errorf("Groups(%q) gave %d groups, want 1", language, got)
		}
	}
	if got := len(list.Groups("html")); got != 0 {
		t.Errorf("Groups(\"html\") gave %d groups, want none", got)
	}
}

func TestTheUsersSnippetsAndTheProjectsAreBothOffered(t *testing.T) {
	userSnippets(t, "[[snippet]]\nname = \"mine\"\ngroup = \"Mine\"\nbody = \"x\"\n")
	dir := project(t, "[[snippet]]\nname = \"theirs\"\ngroup = \"Theirs\"\nbody = \"x\"\n")

	// The user's come first, so a project adds to what you already have.
	wantGroups(t, load(t, dir).Groups(""), "Mine/mine", "Theirs/theirs")
}

func TestTheProjectWinsWhenANameClashes(t *testing.T) {
	// The project's file is the more specific statement of the two.
	userSnippets(t, "[[snippet]]\nname = \"header\"\ngroup = \"Go\"\nbody = \"mine\"\n")
	dir := project(t, "[[snippet]]\nname = \"header\"\ngroup = \"Go\"\nbody = \"theirs\"\n")

	groups := load(t, dir).Groups("")
	if len(groups) != 1 || len(groups[0].Snippets) != 1 {
		t.Fatalf("got %v, want one snippet", summary(groups))
	}
	if got := groups[0].Snippets[0].Body; got != "theirs" {
		t.Errorf("body = %q, want the project's", got)
	}
}

func TestTheSameNameInADifferentGroupIsADifferentSnippet(t *testing.T) {
	userSnippets(t, "[[snippet]]\nname = \"header\"\ngroup = \"Go\"\nbody = \"a\"\n")
	dir := project(t, "[[snippet]]\nname = \"header\"\ngroup = \"Shell\"\nbody = \"b\"\n")

	wantGroups(t, load(t, dir).Groups(""), "Go/header", "Shell/header")
}

func TestAProjectWithNoSnippetsFileIsNotAnError(t *testing.T) {
	noUserSnippets(t)

	list := load(t, t.TempDir())

	if list.Len() != 0 {
		t.Errorf("Len() = %d, want 0", list.Len())
	}
	if got := len(list.Groups("go")); got != 0 {
		t.Errorf("Groups() gave %d groups", got)
	}
}

func TestAFileThatIsNotTOMLIsReported(t *testing.T) {
	// A typo must be reported rather than silently dropping every snippet.
	noUserSnippets(t)
	dir := project(t, "[[snippet]\nname = ")

	_, err := Load(testProfile(), dir)
	if err == nil {
		t.Fatal("Load(testProfile()) accepted a file that is not TOML")
	}
	if !strings.Contains(err.Error(), ProjectPath(testProfile(), dir)) {
		t.Errorf("the error does not name the file: %v", err)
	}
}

func TestASnippetWithNoNameOrNoBodyIsRefused(t *testing.T) {
	noUserSnippets(t)

	for name, contents := range map[string]string{
		"no name": "[[snippet]]\nbody = \"x\"\n",
		"no body": "[[snippet]]\nname = \"x\"\n",
	} {
		t.Run(name, func(t *testing.T) {
			if _, err := Load(testProfile(), project(t, contents)); err == nil {
				t.Errorf("Load(testProfile()) accepted a snippet with %s", name)
			}
		})
	}
}

func TestADirectoryWhereTheFileGoesCountsAsAbsent(t *testing.T) {
	noUserSnippets(t)
	dir := t.TempDir()
	if err := os.MkdirAll(ProjectPath(testProfile(), dir), 0o755); err != nil {
		t.Fatalf("creating a directory where the file goes: %v", err)
	}

	if Exists(testProfile(), dir) {
		t.Error("Exists(testProfile()) called a directory a snippets file")
	}
}

func TestCreateWritesAFileThatLoadsBack(t *testing.T) {
	noUserSnippets(t)
	dir := t.TempDir()

	path, err := Create(testProfile(), dir)
	if err != nil {
		t.Fatalf("Create(testProfile()) error = %v", err)
	}
	if path != ProjectPath(testProfile(), dir) {
		t.Errorf("Create(testProfile()) returned %q, want %q", path, ProjectPath(testProfile(), dir))
	}
	if !Exists(testProfile(), dir) {
		t.Fatal("Create(testProfile()) reported success but wrote no file")
	}

	list := load(t, dir)
	if list.Len() == 0 {
		t.Fatal("the created file holds no snippets")
	}
	// The examples in it must be usable, not just parseable.
	for _, group := range list.Groups("go") {
		for _, snippet := range group.Snippets {
			if snippet.Name == "" || snippet.Body == "" {
				t.Errorf("the created file holds an unusable snippet %+v", snippet)
			}
		}
	}
}

func TestCreateRefusesToOverwriteAnExistingFile(t *testing.T) {
	noUserSnippets(t)
	original := "[[snippet]]\nname = \"mine\"\nbody = \"hand written\"\n"
	dir := project(t, original)

	_, err := Create(testProfile(), dir)
	if !errors.Is(err, ErrExists) {
		t.Fatalf("Create(testProfile()) error = %v, want ErrExists", err)
	}
	if got := readFile(t, ProjectPath(testProfile(), dir)); got != original {
		t.Errorf("the existing file was changed:\n%s", got)
	}
}

func TestUserDirFollowsTheEnvironmentVariable(t *testing.T) {
	t.Setenv(testProfile().SnippetDirEnvVar(), "/somewhere")

	if got := UserDir(testProfile()); got != "/somewhere" {
		t.Errorf("UserDir(testProfile()) = %q", got)
	}
	if got, want := UserPath(testProfile()), filepath.Join("/somewhere", FileName); got != want {
		t.Errorf("UserPath(testProfile()) = %q, want %q", got, want)
	}
}

// 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)
}

func TestCreateWritesTheProfilesTemplateWithItsBlanksFilledIn(t *testing.T) {
	// Two things the template asks for and cannot know itself: the name of the
	// group an ungrouped snippet falls into, and where the user's own file is.
	// What is *in* Turbo Go's template is Turbo Go's own test.
	userDir := t.TempDir()
	t.Setenv(testProfile().SnippetDirEnvVar(), userDir)
	dir := t.TempDir()

	if _, err := Create(testProfile(), dir); err != nil {
		t.Fatalf("Create() error = %v", err)
	}

	contents := readFile(t, ProjectPath(testProfile(), dir))
	for _, want := range []string{ungroupedName, UserPath(testProfile())} {
		if !strings.Contains(contents, want) {
			t.Errorf("the created file never mentions %q:\n%s", want, contents)
		}
	}
}

func TestCreateSaysSoWhenThereIsNowhereForTheUsersOwnFile(t *testing.T) {
	// A system with no configuration directory leaves UserPath empty, and a
	// comment reading "Your own snippets go in:" followed by nothing is worse
	// than one that says there is nowhere.
	t.Setenv(testProfile().SnippetDirEnvVar(), "")
	t.Setenv("XDG_CONFIG_HOME", "")
	t.Setenv("HOME", "")
	if UserPath(testProfile()) != "" {
		t.Skip("this system still reports a configuration directory")
	}
	dir := t.TempDir()

	if _, err := Create(testProfile(), dir); err != nil {
		t.Fatalf("Create() error = %v", err)
	}

	contents := readFile(t, ProjectPath(testProfile(), dir))
	if !strings.Contains(contents, "no configuration directory") {
		t.Errorf("the created file does not say there is nowhere to put user snippets:\n%s", contents)
	}
}