| 📦 Turbo Go 3d7798b k33g 8h ago | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "rickub.com/turbo-editors/turbo-core/app" |
| 9 | "rickub.com/turbo-editors/turbo-core/settings" |
| 10 | "rickub.com/turbo-editors/turbo-core/theme" |
| 11 | |
| 12 | "rickub.com/turbo-editors/turbo-go/internal/golang" |
| 13 | ) |
| 14 | |
| 15 | func TestTheProjectRootIsTheModuleRoot(t *testing.T) { |
| 16 | // gopls is given the module's boundary, which is what decides the package |
| 17 | // set it loads. The walk itself is turbo-core's; what is checked here is |
| 18 | // that Turbo Go's profile asks it to look for a go.mod. |
| 19 | root := t.TempDir() |
| 20 | if err := os.WriteFile(filepath.Join(root, "go.mod"), []byte("module x\n"), 0o644); err != nil { |
| 21 | t.Fatalf("writing go.mod: %v", err) |
| 22 | } |
| 23 | nested := filepath.Join(root, "internal", "deep") |
| 24 | if err := os.MkdirAll(nested, 0o755); err != nil { |
| 25 | t.Fatalf("creating the tree: %v", err) |
| 26 | } |
| 27 | file := filepath.Join(nested, "deep.go") |
| 28 | if err := os.WriteFile(file, []byte("package deep\n"), 0o644); err != nil { |
| 29 | t.Fatalf("writing the file: %v", err) |
| 30 | } |
| 31 | |
| 32 | if got := app.ProjectRoot(golang.Profile(), []string{file}); got != root { |
| 33 | t.Errorf("ProjectRoot() = %q, want the module root %q", got, root) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestThemeNamePrefersTheFlagOverTheProject(t *testing.T) { |
| 38 | got := themeName(options{theme: "borland-light"}, settings.Settings{Theme: "turbo-dark"}) |
| 39 | |
| 40 | if got != "borland-light" { |
| 41 | t.Errorf("themeName() = %q; an explicit -theme must win over the project's", got) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestThemeNameUsesTheProjectWhenNoFlagWasGiven(t *testing.T) { |
| 46 | got := themeName(options{}, settings.Settings{Theme: "turbo-dark"}) |
| 47 | |
| 48 | if got != "turbo-dark" { |
| 49 | t.Errorf("themeName() = %q, want the project's theme", got) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestThemeNameFallsBackToTheDefault(t *testing.T) { |
| 54 | got := themeName(options{}, settings.Settings{}) |
| 55 | |
| 56 | if got != theme.DefaultName { |
| 57 | t.Errorf("themeName() = %q, want %q", got, theme.DefaultName) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestLoadProjectSettingsReadsTheWorkingDirectory(t *testing.T) { |
| 62 | project := t.TempDir() |
| 63 | t.Chdir(project) |
| 64 | if err := os.MkdirAll(settings.Dir(golang.Profile(), project), 0o755); err != nil { |
| 65 | t.Fatalf("creating the settings directory: %v", err) |
| 66 | } |
| 67 | contents := "[editor]\ntheme = \"turbo-dark\"\nautosave = true\n" |
| 68 | if err := os.WriteFile(settings.Path(golang.Profile(), project), []byte(contents), 0o644); err != nil { |
| 69 | t.Fatalf("writing the settings file: %v", err) |
| 70 | } |
| 71 | |
| 72 | _, loaded := loadProjectSettings(golang.Profile()) |
| 73 | |
| 74 | if loaded.Theme != "turbo-dark" { |
| 75 | t.Errorf("Theme = %q, want turbo-dark", loaded.Theme) |
| 76 | } |
| 77 | if !loaded.Autosave { |
| 78 | t.Error("Autosave = false, want the file's true") |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestLoadProjectSettingsDoesNotWalkUpToAParent(t *testing.T) { |
| 83 | // "The project is where you started the editor" is the rule; a settings |
| 84 | // file one directory up belongs to a different project. |
| 85 | parent := t.TempDir() |
| 86 | if err := os.MkdirAll(settings.Dir(golang.Profile(), parent), 0o755); err != nil { |
| 87 | t.Fatalf("creating the settings directory: %v", err) |
| 88 | } |
| 89 | if err := os.WriteFile(settings.Path(golang.Profile(), parent), []byte("[editor]\ntheme = \"turbo-dark\"\n"), 0o644); err != nil { |
| 90 | t.Fatalf("writing the settings file: %v", err) |
| 91 | } |
| 92 | child := filepath.Join(parent, "internal") |
| 93 | if err := os.Mkdir(child, 0o755); err != nil { |
| 94 | t.Fatalf("creating the child directory: %v", err) |
| 95 | } |
| 96 | t.Chdir(child) |
| 97 | |
| 98 | _, loaded := loadProjectSettings(golang.Profile()) |
| 99 | |
| 100 | if loaded.Theme != "" { |
| 101 | t.Errorf("Theme = %q; settings were read from a parent directory", loaded.Theme) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestLoadProjectSettingsCarriesOnWithoutAFile(t *testing.T) { |
| 106 | t.Chdir(t.TempDir()) |
| 107 | |
| 108 | _, loaded := loadProjectSettings(golang.Profile()) |
| 109 | |
| 110 | if loaded != settings.Default() { |
| 111 | t.Errorf("loadProjectSettings(golang.Profile()) = %+v, want the defaults", loaded) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestABrokenSettingsFileDoesNotStopTheEditor(t *testing.T) { |
| 116 | // The editor is how you would fix the file, so it has to open. |
| 117 | project := t.TempDir() |
| 118 | t.Chdir(project) |
| 119 | if err := os.MkdirAll(settings.Dir(golang.Profile(), project), 0o755); err != nil { |
| 120 | t.Fatalf("creating the settings directory: %v", err) |
| 121 | } |
| 122 | if err := os.WriteFile(settings.Path(golang.Profile(), project), []byte("[editor\nnot toml"), 0o644); err != nil { |
| 123 | t.Fatalf("writing the settings file: %v", err) |
| 124 | } |
| 125 | |
| 126 | _, loaded := loadProjectSettings(golang.Profile()) |
| 127 | |
| 128 | if loaded != settings.Default() { |
| 129 | t.Errorf("loadProjectSettings(golang.Profile()) = %+v, want the defaults", loaded) |
| 130 | } |
| 131 | } |