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 · 13h ago
tools_test.go · 387 lines · 10.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
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
package tools

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

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

// testProfile is a fictional editor. The template it carries is a small but
// real tools file, because Create's job is to write the profile's template and
// a test asserting on what is in it would only be reading its own fixture back.
func testProfile() profile.Profile {
	return profile.Profile{
		Name:      "Turbo Test",
		Slug:      "turbo-test",
		Language:  "Test",
		ToolsMenu: "~T~est",
		Templates: profile.Templates{Tools: testToolsTemplate},
	}
}

const testToolsTemplate = `# turbo-test tools.

[[tool]]
name = "~B~uild"
command = "make build"
output = "popup"

[[tool]]
name = "~R~un"
command = "make run"
output = "terminal"
`

// menu is the plain name of the toolchain menu the test profile uses.
var menu = DefaultMenuName(testProfile())

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

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

// load reads a project's tools, 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 tools as "name=command" strings, in order.
func summary(list List) []string {
	out := make([]string, 0, list.Len())
	for _, tool := range list.Tools() {
		out = append(out, tool.Name+"="+tool.Command)
	}
	return out
}

func TestLoadReadsTheToolsInFileOrder(t *testing.T) {
	// The menu should match the file, so someone reordering the file sees the
	// menu reorder.
	dir := project(t, `
[[tool]]
name = "Test"
command = "go test ./..."

[[tool]]
name = "Build"
command = "go build ./..."
`)

	got := summary(load(t, dir))
	want := []string{"Test=go test ./...", "Build=go build ./..."}
	if len(got) != len(want) || got[0] != want[0] || got[1] != want[1] {
		t.Errorf("got %v, want %v", got, want)
	}
}

func TestAProjectWithNoToolsFileIsNotAnError(t *testing.T) {
	list := load(t, t.TempDir())

	if list.Len() != 0 {
		t.Errorf("Len() = %d, want 0", list.Len())
	}
	if got := list.Tools(); got != nil {
		t.Errorf("Tools() = %v, want nothing", got)
	}
}

func TestAFileThatIsNotTOMLIsReported(t *testing.T) {
	// A typo must be reported rather than silently leaving the menu empty.
	dir := project(t, "[[tool]\nname = ")

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

func TestAToolWithNoNameOrNoCommandIsRefused(t *testing.T) {
	for name, contents := range map[string]string{
		"no name":    "[[tool]]\ncommand = \"go build\"\n",
		"no command": "[[tool]]\nname = \"Build\"\n",
	} {
		t.Run(name, func(t *testing.T) {
			if _, err := Load(testProfile(), project(t, contents)); err == nil {
				t.Errorf("Load() accepted a tool with %s", name)
			}
		})
	}
}

func TestACommandMayBeAWholeSequence(t *testing.T) {
	// It goes to sh -c, so one entry can be several commands.
	dir := project(t, "[[tool]]\nname = \"Check\"\ncommand = \"go vet ./... && go test ./...\"\n")

	list := load(t, dir)
	if list.Len() != 1 {
		t.Fatalf("Len() = %d", list.Len())
	}
	if got := list.Tools()[0].Command; got != "go vet ./... && go test ./..." {
		t.Errorf("Command = %q, want the sequence unchanged", got)
	}
}

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

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

func TestPathIsUnderTheEditorsOwnDirectory(t *testing.T) {
	// The directory is the editor's, so two editors in one repository keep
	// their tools apart rather than fighting over one file.
	want := filepath.Join("/src/p", ".turbo-test", "tools.toml")
	if got := Path(testProfile(), "/src/p"); got != want {
		t.Errorf("Path() = %q, want %q", got, want)
	}
}

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

	path, err := Create(testProfile(), dir)
	if err != nil {
		t.Fatalf("Create() error = %v", err)
	}
	if path != Path(testProfile(), dir) {
		t.Errorf("Create() returned %q, want %q", path, Path(testProfile(), dir))
	}
	if !Exists(testProfile(), dir) {
		t.Fatal("Create() reported success but wrote no file")
	}
	if load(t, dir).Len() == 0 {
		t.Error("the created file holds no tools")
	}
}

func TestCreateRefusesToOverwriteAnExistingFile(t *testing.T) {
	original := "[[tool]]\nname = \"Mine\"\ncommand = \"make\"\n"
	dir := project(t, original)

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

// plain strips the tilde hot-key markers from a label.
func plain(label string) string { return strings.ReplaceAll(label, "~", "") }

// hotKey returns the character between the tildes, or 0 when there is none.
func hotKey(label string) rune {
	first := strings.IndexByte(label, '~')
	if first < 0 || first+1 >= len(label) {
		return 0
	}
	return rune(label[first+1])
}

// 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 TestAToolWithNoOutputDefaultsToAPopup(t *testing.T) {
	dir := project(t, "[[tool]]\nname = \"Test\"\ncommand = \"go test ./...\"\n")

	tool := load(t, dir).Tools()[0]
	if tool.Output != "" {
		t.Errorf("Output = %q, want it left empty in the file", tool.Output)
	}
	if got := tool.Where(); got != OutputPopup {
		t.Errorf("Where() = %q, want %q", got, OutputPopup)
	}
}

func TestEveryOutputTheFileMayNameIsAccepted(t *testing.T) {
	for _, output := range outputs {
		t.Run(string(output), func(t *testing.T) {
			dir := project(t, "[[tool]]\nname = \"X\"\ncommand = \"true\"\noutput = \""+string(output)+"\"\n")

			if got := load(t, dir).Tools()[0].Where(); got != output {
				t.Errorf("Where() = %q, want %q", got, output)
			}
		})
	}
}

func TestAnUnknownOutputIsRefusedRatherThanCorrected(t *testing.T) {
	// Silently falling back would send the output somewhere the file did not
	// ask for, and "termnial" would look like it worked.
	dir := project(t, "[[tool]]\nname = \"Test\"\ncommand = \"true\"\noutput = \"termnial\"\n")

	_, err := Load(testProfile(), dir)
	if err == nil {
		t.Fatal("Load() accepted an output nobody defined")
	}
	for _, want := range []string{"Test", "termnial", "popup", "terminal", "editor"} {
		if !strings.Contains(err.Error(), want) {
			t.Errorf("the error never mentions %q: %v", want, err)
		}
	}
}

func TestAToolWithNoMenuGoesIntoTheEditorsToolchainMenu(t *testing.T) {
	dir := project(t, "[[tool]]\nname = \"Test\"\ncommand = \"go test ./...\"\n")

	if got := load(t, dir).Tools()[0].Menu; got != menu {
		t.Errorf("MenuName() = %q, want %q", got, menu)
	}
}

func TestAToolCanNameItsOwnMenu(t *testing.T) {
	dir := project(t, "[[tool]]\nname = \"Echo\"\ncommand = \"echo x\"\nmenu = \"Tools\"\n")

	if got := load(t, dir).Tools()[0].Menu; got != "Tools" {
		t.Errorf("MenuName() = %q, want Tools", got)
	}
}

func TestMenuNamesStartWithTheToolchainMenuAndFollowTheFile(t *testing.T) {
	// Go is always first, whether or not a tool named it: the item that creates
	// the tools file has to live somewhere even when there is no file.
	dir := project(t, `
[[tool]]
name = "Up"
command = "docker compose up"
menu = "Docker"

[[tool]]
name = "Echo"
command = "echo x"
menu = "Tools"

[[tool]]
name = "Down"
command = "docker compose down"
menu = "Docker"
`)

	got := load(t, dir).MenuNames()
	want := []string{menu, "Docker", "Tools"}
	if len(got) != len(want) {
		t.Fatalf("MenuNames() = %v, want %v", got, want)
	}
	for i := range want {
		if got[i] != want[i] {
			t.Errorf("menu %d = %q, want %q", i, got[i], want[i])
		}
	}
}

func TestTheToolchainMenuIsTheOnlyOneWhenNothingNamesAnother(t *testing.T) {
	dir := project(t, "[[tool]]\nname = \"Test\"\ncommand = \"go test\"\n")

	if got := load(t, dir).MenuNames(); len(got) != 1 || got[0] != menu {
		t.Errorf("MenuNames() = %v, want just %q", got, menu)
	}
}

func TestTheToolchainMenuExistsEvenWithNoToolsAtAll(t *testing.T) {
	// A project with no tools file still gets the menu, because that is where
	// the item creating one lives.
	list, err := Load(testProfile(), t.TempDir())
	if err != nil {
		t.Fatalf("Load() error = %v", err)
	}

	if got := list.MenuNames(); len(got) != 1 || got[0] != menu {
		t.Errorf("MenuNames() = %v, want just %q", got, menu)
	}
}

func TestInReturnsOneMenusToolsInFileOrder(t *testing.T) {
	dir := project(t, `
[[tool]]
name = "a"
command = "true"
menu = "Tools"

[[tool]]
name = "b"
command = "true"

[[tool]]
name = "c"
command = "true"
menu = "Tools"
`)
	list := load(t, dir)

	mine := list.In("Tools")
	if len(mine) != 2 || mine[0].Name != "a" || mine[1].Name != "c" {
		t.Errorf("In(\"Tools\") = %v", mine)
	}
	if goTools := list.In(menu); len(goTools) != 1 || goTools[0].Name != "b" {
		t.Errorf("In(%q) = %v", menu, goTools)
	}
	if none := list.In("Nowhere"); none != nil {
		t.Errorf("In(\"Nowhere\") = %v, want nothing", none)
	}
}

func TestCreateWritesTheProfilesTemplateVerbatim(t *testing.T) {
	// The commands themselves belong to the editor, not to this package: what
	// is checked here is that the file written is the one the profile carries,
	// byte for byte. What is *in* Turbo Go's template is Turbo Go's own test.
	dir := t.TempDir()

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

	if got := readFile(t, Path(testProfile(), dir)); got != testToolsTemplate {
		t.Errorf("Create() wrote:\n%s\nwant the profile's template:\n%s", got, testToolsTemplate)
	}
}

func TestDefaultMenuNameStripsTheHotKeyMarkers(t *testing.T) {
	// A tools file writes the plain name; the hot key is the editor's to
	// assign, because only it knows which letters the other menus have taken.
	tests := map[string]string{"~G~o": "Go", "Rus~t~": "Rust", "Zig": "Zig"}

	for label, want := range tests {
		if got := DefaultMenuName(profile.Profile{ToolsMenu: label}); got != want {
			t.Errorf("DefaultMenuName(%q) = %q, want %q", label, got, want)
		}
	}
}