| 📦 Turbo Rust 713ea5c k33g 14h ago | 1 | package rustlang |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "rickub.com/turbo-editors/turbo-core/settings" |
| 10 | "rickub.com/turbo-editors/turbo-core/snippets" |
| 11 | "rickub.com/turbo-editors/turbo-core/syntax" |
| 12 | "rickub.com/turbo-editors/turbo-core/tools" |
| 13 | ) |
| 14 | |
| 15 | // The starter files Turbo Rust writes are the one part of a project's |
| 16 | // .turbo-rust directory that is about Rust, so this is where what is *in* them |
| 17 | // is checked. That the file written is the profile's template at all is |
| 18 | // turbo-core's test. |
| 19 | |
| 20 | // noUserSnippets points the user's own snippets at an empty directory, so a |
| 21 | // test never reads whoever is running it. |
| 22 | func noUserSnippets(t *testing.T) { |
| 23 | t.Helper() |
| 24 | t.Setenv(Profile().SnippetDirEnvVar(), t.TempDir()) |
| 25 | } |
| 26 | |
| 27 | // loadTools reads a project's tools, failing the test if it cannot. |
| 28 | func loadTools(t *testing.T, dir string) tools.List { |
| 29 | t.Helper() |
| 30 | |
| 31 | list, err := tools.Load(Profile(), dir) |
| 32 | if err != nil { |
| 33 | t.Fatalf("tools.Load(%q) error = %v", dir, err) |
| 34 | } |
| 35 | return list |
| 36 | } |
| 37 | |
| 38 | // loadSnippets reads a project's snippets, failing the test if it cannot. |
| 39 | func loadSnippets(t *testing.T, dir string) snippets.List { |
| 40 | t.Helper() |
| 41 | |
| 42 | list, err := snippets.Load(Profile(), dir) |
| 43 | if err != nil { |
| 44 | t.Fatalf("snippets.Load(%q) error = %v", dir, err) |
| 45 | } |
| 46 | return list |
| 47 | } |
| 48 | |
| 49 | // readFile returns a file's contents. |
| 50 | func readFile(t *testing.T, path string) string { |
| 51 | t.Helper() |
| 52 | |
| 53 | data, err := os.ReadFile(path) |
| 54 | if err != nil { |
| 55 | t.Fatalf("reading %s: %v", path, err) |
| 56 | } |
| 57 | return string(data) |
| 58 | } |
| 59 | |
| 60 | // plain strips the tilde hot-key markers from a label. |
| 61 | func plain(label string) string { return strings.ReplaceAll(label, "~", "") } |
| 62 | |
| 63 | // hotKey returns the character between the tildes, or 0 when there is none. |
| 64 | func hotKey(label string) rune { |
| 65 | first := strings.IndexByte(label, '~') |
| 66 | if first < 0 || first+1 >= len(label) { |
| 67 | return 0 |
| 68 | } |
| 69 | return rune(label[first+1]) |
| 70 | } |
| 71 | |
| 72 | func TestTheCreatedToolsFileHoldsTheCargoCommandsAndTheExampleBesideThem(t *testing.T) { |
| 73 | // These are what a Rust project runs before it commits, and they are the |
| 74 | // reason the file exists at all. |
| 75 | dir := t.TempDir() |
| 76 | if _, err := tools.Create(Profile(), dir); err != nil { |
| 77 | t.Fatalf("tools.Create() error = %v", err) |
| 78 | } |
| 79 | |
| 80 | byName := map[string]string{} |
| 81 | for _, tool := range loadTools(t, dir).Tools() { |
| 82 | byName[plain(tool.Name)] = tool.Command |
| 83 | } |
| 84 | |
| 85 | want := map[string]string{ |
| 86 | "Format": "cargo fmt", |
| 87 | "Lint": "cargo clippy --all-targets", |
| 88 | "Build": "cargo build", |
| 89 | "Test": "cargo test", |
| 90 | "Run": "cargo run", |
| 91 | "Echo": "echo 🎉 tada!", |
| 92 | } |
| 93 | for name, command := range want { |
| 94 | if got := byName[name]; got != command { |
| 95 | t.Errorf("%s runs %q, want %q", name, got, command) |
| 96 | } |
| 97 | } |
| 98 | for name := range byName { |
| 99 | if _, ok := want[name]; !ok { |
| 100 | t.Errorf("the created file holds a tool this test does not know about: %q", name) |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestTheCreatedToolsCarryHotKeys(t *testing.T) { |
| 106 | // Five items in a menu are worth reaching with one keystroke each. |
| 107 | dir := t.TempDir() |
| 108 | if _, err := tools.Create(Profile(), dir); err != nil { |
| 109 | t.Fatalf("tools.Create() error = %v", err) |
| 110 | } |
| 111 | |
| 112 | seen := map[rune]string{} |
| 113 | for _, tool := range loadTools(t, dir).Tools() { |
| 114 | key := hotKey(tool.Name) |
| 115 | if key == 0 { |
| 116 | t.Errorf("%q has no hot key", tool.Name) |
| 117 | continue |
| 118 | } |
| 119 | if other, clash := seen[key]; clash { |
| 120 | t.Errorf("%q and %q both answer to %c", other, tool.Name, key) |
| 121 | } |
| 122 | seen[key] = tool.Name |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func TestTheCreatedToolsFileNamesAnOutputForEveryTool(t *testing.T) { |
| 127 | // The key is the interesting part of the format, and a file where it only |
| 128 | // appears once is a file where nobody notices it exists. |
| 129 | dir := t.TempDir() |
| 130 | if _, err := tools.Create(Profile(), dir); err != nil { |
| 131 | t.Fatalf("tools.Create() error = %v", err) |
| 132 | } |
| 133 | |
| 134 | for _, tool := range loadTools(t, dir).Tools() { |
| 135 | if tool.Output == "" { |
| 136 | t.Errorf("%q leaves its output to the default rather than saying it", tool.Name) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestEachToolGoesWhereItsOwnOutputBelongs(t *testing.T) { |
| 142 | // `cargo run` starts a program that may read the keyboard, and a popup |
| 143 | // cannot answer one. Echo is a terminal too, as the worked example of a |
| 144 | // tool in a menu of its own. The rest say something short and are read |
| 145 | // once. |
| 146 | dir := t.TempDir() |
| 147 | if _, err := tools.Create(Profile(), dir); err != nil { |
| 148 | t.Fatalf("tools.Create() error = %v", err) |
| 149 | } |
| 150 | |
| 151 | want := map[string]tools.Output{ |
| 152 | "Format": tools.OutputPopup, |
| 153 | "Lint": tools.OutputPopup, |
| 154 | "Build": tools.OutputPopup, |
| 155 | "Test": tools.OutputPopup, |
| 156 | "Run": tools.OutputTerminal, |
| 157 | "Echo": tools.OutputTerminal, |
| 158 | } |
| 159 | for _, tool := range loadTools(t, dir).Tools() { |
| 160 | name := plain(tool.Name) |
| 161 | if got := tool.Where(); got != want[name] { |
| 162 | t.Errorf("%s goes to %q, want %q", name, got, want[name]) |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func TestTheCreatedToolsFileExplainsItself(t *testing.T) { |
| 168 | dir := t.TempDir() |
| 169 | if _, err := tools.Create(Profile(), dir); err != nil { |
| 170 | t.Fatalf("tools.Create() error = %v", err) |
| 171 | } |
| 172 | |
| 173 | contents := readFile(t, tools.Path(Profile(), dir)) |
| 174 | for _, want := range []string{"[[tool]]", "sh -c", "hot key", "popup", "terminal", "editor"} { |
| 175 | if !strings.Contains(contents, want) { |
| 176 | t.Errorf("the created file never mentions %q:\n%s", want, contents) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func TestTheCreatedToolsFileNamesTheRustMenuNotTheGoOne(t *testing.T) { |
| 182 | // The comments explain which menu a tool lands in by naming it, and naming |
| 183 | // the wrong editor's menu is the copy-and-paste mistake this catches. |
| 184 | dir := t.TempDir() |
| 185 | if _, err := tools.Create(Profile(), dir); err != nil { |
| 186 | t.Fatalf("tools.Create() error = %v", err) |
| 187 | } |
| 188 | |
| 189 | contents := readFile(t, tools.Path(Profile(), dir)) |
| 190 | if !strings.Contains(contents, "Rust menu") { |
| 191 | t.Errorf("the created file never names the Rust menu:\n%s", contents) |
| 192 | } |
| 193 | if strings.Contains(contents, "Go menu") || strings.Contains(contents, "turbo-go") { |
| 194 | t.Errorf("the created file still talks about Turbo Go:\n%s", contents) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | func TestTheCreatedToolsFileShowsHowToUseAnotherMenu(t *testing.T) { |
| 199 | dir := t.TempDir() |
| 200 | if _, err := tools.Create(Profile(), dir); err != nil { |
| 201 | t.Fatalf("tools.Create() error = %v", err) |
| 202 | } |
| 203 | |
| 204 | contents := readFile(t, tools.Path(Profile(), dir)) |
| 205 | for _, want := range []string{"menu says which menu", `menu = "Tools"`} { |
| 206 | if !strings.Contains(contents, want) { |
| 207 | t.Errorf("the created file never shows %q:\n%s", want, contents) |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | func TestTheCreatedSnippetsFileHoldsUsableRustSnippets(t *testing.T) { |
| 213 | noUserSnippets(t) |
| 214 | dir := t.TempDir() |
| 215 | if _, err := snippets.Create(Profile(), dir); err != nil { |
| 216 | t.Fatalf("snippets.Create() error = %v", err) |
| 217 | } |
| 218 | |
| 219 | groups := loadSnippets(t, dir).Groups(string(Language)) |
| 220 | if len(groups) == 0 { |
| 221 | t.Fatal("the created file offers nothing at all in a Rust file") |
| 222 | } |
| 223 | for _, group := range groups { |
| 224 | for _, snippet := range group.Snippets { |
| 225 | if snippet.Name == "" || snippet.Body == "" { |
| 226 | t.Errorf("the created file holds an unusable snippet %+v", snippet) |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | func TestTheCreatedSnippetsIndentWithSpacesTheWayRustfmtDoes(t *testing.T) { |
| 233 | // Rust indents with four spaces. A tab that crept in would land in |
| 234 | // somebody's file and be reformatted out on the next `cargo fmt`, which is |
| 235 | // a diff nobody asked for. |
| 236 | noUserSnippets(t) |
| 237 | dir := t.TempDir() |
| 238 | if _, err := snippets.Create(Profile(), dir); err != nil { |
| 239 | t.Fatalf("snippets.Create() error = %v", err) |
| 240 | } |
| 241 | |
| 242 | for _, group := range loadSnippets(t, dir).Groups(string(Language)) { |
| 243 | for _, snippet := range group.Snippets { |
| 244 | if strings.Contains(snippet.Body, "\t") { |
| 245 | t.Errorf("%q indents with a tab:\n%q", snippet.Name, snippet.Body) |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | func TestTheCreatedSnippetsFileExplainsItself(t *testing.T) { |
| 252 | noUserSnippets(t) |
| 253 | dir := t.TempDir() |
| 254 | if _, err := snippets.Create(Profile(), dir); err != nil { |
| 255 | t.Fatalf("snippets.Create() error = %v", err) |
| 256 | } |
| 257 | |
| 258 | contents := readFile(t, snippets.ProjectPath(Profile(), dir)) |
| 259 | for _, want := range []string{"[[snippet]]", "languages", "group", "General", snippets.UserPath(Profile())} { |
| 260 | if !strings.Contains(contents, want) { |
| 261 | t.Errorf("the created file never mentions %q:\n%s", want, contents) |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | func TestTheCreatedSettingsFileExplainsItself(t *testing.T) { |
| 267 | project := t.TempDir() |
| 268 | if _, err := settings.Create(Profile(), project, "turbo-classic"); err != nil { |
| 269 | t.Fatalf("settings.Create() error = %v", err) |
| 270 | } |
| 271 | |
| 272 | contents := readFile(t, settings.Path(Profile(), project)) |
| 273 | for _, want := range []string{"theme", "autosave", "autosave_delay", "-list-themes"} { |
| 274 | if !strings.Contains(contents, want) { |
| 275 | t.Errorf("the created file never mentions %q:\n%s", want, contents) |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | func TestEveryTemplateNamesThisEditorAndNotTheOther(t *testing.T) { |
| 281 | // The three templates started as Turbo Go's. A leftover "turbo-go" in a |
| 282 | // file written into somebody's Rust project is the whole class of mistake |
| 283 | // this catches, and it is invisible to every other test here. |
| 284 | templates := map[string]string{ |
| 285 | "settings": settingsTemplate, |
| 286 | "snippets": snippetsTemplate, |
| 287 | "tools": toolsTemplate, |
| 288 | } |
| 289 | |
| 290 | for name, template := range templates { |
| 291 | t.Run(name, func(t *testing.T) { |
| 292 | if strings.Contains(template, "turbo-go") { |
| 293 | t.Errorf("the %s template still says turbo-go:\n%s", name, template) |
| 294 | } |
| 295 | if !strings.Contains(template, Slug) { |
| 296 | t.Errorf("the %s template never names %s:\n%s", name, Slug, template) |
| 297 | } |
| 298 | }) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | func TestTheSnippetsTemplateTakesExactlyTwoBlanks(t *testing.T) { |
| 303 | // profile.Templates says Snippets is formatted with the ungrouped group's |
| 304 | // name and the user's path, in that order. A third %s, or a stray one in a |
| 305 | // comment, comes out as %!s(MISSING) in somebody's project. |
| 306 | if got := strings.Count(snippetsTemplate, "%s"); got != 2 { |
| 307 | t.Errorf("the snippets template has %d %%s, want 2", got) |
| 308 | } |
| 309 | if got := strings.Count(settingsTemplate, "%q"); got != 2 { |
| 310 | t.Errorf("the settings template has %d %%q, want 2", got) |
| 311 | } |
| 312 | if strings.Contains(toolsTemplate, "%") { |
| 313 | t.Errorf("the tools template takes no arguments but contains a %%:\n%s", toolsTemplate) |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | func TestTheCreatedToolsFileExplainsHowToAskForAValue(t *testing.T) { |
| 318 | // A parameterised tool is only discoverable if the file people get says the |
| 319 | // syntax exists. The double-brace warning is here too, because somebody |
| 320 | // reading this file may well have an awk one-liner in mind. |
| 321 | dir := t.TempDir() |
| 322 | if _, err := tools.Create(Profile(), dir); err != nil { |
| 323 | t.Fatalf("tools.Create() error = %v", err) |
| 324 | } |
| 325 | |
| 326 | contents := readFile(t, tools.Path(Profile(), dir)) |
| 327 | for _, want := range []string{ |
| 328 | "{{label}}", |
| 329 | "cargo new --bin {{crate name}}", |
| 330 | "{{extra flags...}}", |
| 331 | "Double braces, not single", |
| 332 | } { |
| 333 | if !strings.Contains(contents, want) { |
| 334 | t.Errorf("the created file never mentions %q:\n%s", want, contents) |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | func TestTheCreatedToolsFileStillLoadsWithItsPlaceholderExamples(t *testing.T) { |
| 340 | // The examples live in comments, so none of them may become a real tool — |
| 341 | // and the loader must not trip over the braces in the prose either. |
| 342 | dir := t.TempDir() |
| 343 | if _, err := tools.Create(Profile(), dir); err != nil { |
| 344 | t.Fatalf("tools.Create() error = %v", err) |
| 345 | } |
| 346 | |
| 347 | for _, tool := range loadTools(t, dir).Tools() { |
| 348 | if got := tool.Placeholders(); got != nil { |
| 349 | t.Errorf("%q asks for %v; none of the five starter commands takes a value", tool.Name, got) |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | func TestTheCreatedSnippetsFileListsEveryLanguageTheEditorKnows(t *testing.T) { |
| 355 | // The comment is where a user finds out what they may write in a languages |
| 356 | // key. One that omits a language the editor colours sends them looking for |
| 357 | // a feature that is already there. |
| 358 | noUserSnippets(t) |
| 359 | dir := t.TempDir() |
| 360 | if _, err := snippets.Create(Profile(), dir); err != nil { |
| 361 | t.Fatalf("snippets.Create() error = %v", err) |
| 362 | } |
| 363 | |
| 364 | contents := readFile(t, snippets.ProjectPath(Profile(), dir)) |
| 365 | for _, language := range syntax.Registered() { |
| 366 | if !strings.Contains(contents, string(language)) { |
| 367 | t.Errorf("the created file never mentions the %q language:\n%s", language, contents) |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | func TestTheCreatedSettingsFileTurnsAutosaveOn(t *testing.T) { |
| 373 | // A project that has gone to the trouble of creating a settings file has |
| 374 | // said what it wants. The file is the visible, editable place to say |
| 375 | // otherwise, which is why the default lives here and not in the library. |
| 376 | project := t.TempDir() |
| 377 | if _, err := settings.Create(Profile(), project, "turbo-classic"); err != nil { |
| 378 | t.Fatalf("settings.Create() error = %v", err) |
| 379 | } |
| 380 | |
| 381 | loaded, err := settings.Load(Profile(), project) |
| 382 | if err != nil { |
| 383 | t.Fatalf("settings.Load() error = %v", err) |
| 384 | } |
| 385 | if !loaded.Autosave { |
| 386 | t.Errorf("the created settings file leaves autosave off:\n%s", readFile(t, settings.Path(Profile(), project))) |
| 387 | } |
| 388 | if loaded.AutosaveDelay != settings.DefaultAutosaveDelay { |
| 389 | t.Errorf("AutosaveDelay = %v, want the library default %v", loaded.AutosaveDelay, settings.DefaultAutosaveDelay) |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | func TestAProjectWithNoSettingsFileStillDoesNotAutosave(t *testing.T) { |
| 394 | // The other half of the decision. Turning autosave on for a project that |
| 395 | // never opted in would mean the editor writing to disk in any directory it |
| 396 | // is started in, which is a different and much larger claim. |
| 397 | if settings.Default().Autosave { |
| 398 | t.Error("settings.Default() autosaves; a project with no settings file never opted in") |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // The three embedded templates and the blanks profile.Templates says each one |
| 403 | // takes. Kept together so that adding a verb to a .tmpl file without saying so |
| 404 | // here fails, which is the guard the constants used to get for free by sitting |
| 405 | // next to the contract. |
| 406 | var embeddedTemplates = []struct { |
| 407 | name string |
| 408 | body string |
| 409 | verb string |
| 410 | blanks int |
| 411 | filledBy []any |
| 412 | }{ |
| 413 | {"settings.toml.tmpl", settingsTemplate, "%q", 2, []any{"turbo-classic", "2s"}}, |
| 414 | {"snippets.toml.tmpl", snippetsTemplate, "%s", 2, []any{"General", "/tmp/snippets.toml"}}, |
| 415 | {"tools.toml.tmpl", toolsTemplate, "%", 0, nil}, |
| 416 | } |
| 417 | |
| 418 | func TestEveryTemplateIsEmbeddedAndNotEmpty(t *testing.T) { |
| 419 | // go:embed fails to compile when a file is missing, but an empty file |
| 420 | // compiles happily and writes an empty starter file into somebody's |
| 421 | // project. |
| 422 | for _, template := range embeddedTemplates { |
| 423 | if len(template.body) == 0 { |
| 424 | t.Errorf("%s embedded as nothing", template.name) |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | func TestEveryTemplateTakesTheBlanksItsContractPromises(t *testing.T) { |
| 430 | // profile.Templates documents the count and the verb of each. The |
| 431 | // templates now live in files of their own, so nothing but this notices a |
| 432 | // verb added, removed, or changed. |
| 433 | for _, template := range embeddedTemplates { |
| 434 | if got := strings.Count(template.body, template.verb); got != template.blanks { |
| 435 | t.Errorf("%s holds %d %q, want %d", template.name, got, template.verb, template.blanks) |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | func TestFillingATemplateLeavesNoFormattingMarker(t *testing.T) { |
| 441 | // Go writes %!q(MISSING) or %!(EXTRA …) into the output rather than |
| 442 | // failing, so a template with the wrong number of blanks produces a file |
| 443 | // that is written, opened, and wrong. |
| 444 | for _, template := range embeddedTemplates { |
| 445 | filled := template.body |
| 446 | if template.filledBy != nil { |
| 447 | filled = fmt.Sprintf(template.body, template.filledBy...) |
| 448 | } |
| 449 | if strings.Contains(filled, "%!") { |
| 450 | t.Errorf("%s filled to:\n%s", template.name, filled) |
| 451 | } |
| 452 | } |
| 453 | } |