| 📦 Turbo Go 3d7798b k33g yesterday | 1 | package main |
| 2 | |
| 3 | import ( |
| 📦 Turbo Go 85cf23f k33g 14h ago | 4 | "errors" |
| 📦 Turbo Go 3d7798b k33g yesterday | 5 | "os" |
| 6 | "path/filepath" |
| 📦 Turbo Go 85cf23f k33g 14h ago | 7 | "strings" |
| 📦 Turbo Go 3d7798b k33g yesterday | 8 | "testing" |
| 9 | |
| 10 | "rickub.com/turbo-editors/turbo-core/app" |
| 📦 Turbo Go 85cf23f k33g 14h ago | 11 | "rickub.com/turbo-editors/turbo-core/configrepo" |
| 📦 Turbo Go 3d7798b k33g yesterday | 12 | "rickub.com/turbo-editors/turbo-core/settings" |
| 13 | "rickub.com/turbo-editors/turbo-core/theme" |
| 14 | |
| 15 | "rickub.com/turbo-editors/turbo-go/internal/golang" |
| 16 | ) |
| 17 | |
| 18 | func TestTheProjectRootIsTheModuleRoot(t *testing.T) { |
| 19 | // gopls is given the module's boundary, which is what decides the package |
| 20 | // set it loads. The walk itself is turbo-core's; what is checked here is |
| 21 | // that Turbo Go's profile asks it to look for a go.mod. |
| 22 | root := t.TempDir() |
| 23 | if err := os.WriteFile(filepath.Join(root, "go.mod"), []byte("module x\n"), 0o644); err != nil { |
| 24 | t.Fatalf("writing go.mod: %v", err) |
| 25 | } |
| 26 | nested := filepath.Join(root, "internal", "deep") |
| 27 | if err := os.MkdirAll(nested, 0o755); err != nil { |
| 28 | t.Fatalf("creating the tree: %v", err) |
| 29 | } |
| 30 | file := filepath.Join(nested, "deep.go") |
| 31 | if err := os.WriteFile(file, []byte("package deep\n"), 0o644); err != nil { |
| 32 | t.Fatalf("writing the file: %v", err) |
| 33 | } |
| 34 | |
| 35 | if got := app.ProjectRoot(golang.Profile(), []string{file}); got != root { |
| 36 | t.Errorf("ProjectRoot() = %q, want the module root %q", got, root) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestThemeNamePrefersTheFlagOverTheProject(t *testing.T) { |
| 41 | got := themeName(options{theme: "borland-light"}, settings.Settings{Theme: "turbo-dark"}) |
| 42 | |
| 43 | if got != "borland-light" { |
| 44 | t.Errorf("themeName() = %q; an explicit -theme must win over the project's", got) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestThemeNameUsesTheProjectWhenNoFlagWasGiven(t *testing.T) { |
| 49 | got := themeName(options{}, settings.Settings{Theme: "turbo-dark"}) |
| 50 | |
| 51 | if got != "turbo-dark" { |
| 52 | t.Errorf("themeName() = %q, want the project's theme", got) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestThemeNameFallsBackToTheDefault(t *testing.T) { |
| 57 | got := themeName(options{}, settings.Settings{}) |
| 58 | |
| 59 | if got != theme.DefaultName { |
| 60 | t.Errorf("themeName() = %q, want %q", got, theme.DefaultName) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestLoadProjectSettingsReadsTheWorkingDirectory(t *testing.T) { |
| 65 | project := t.TempDir() |
| 66 | t.Chdir(project) |
| 67 | if err := os.MkdirAll(settings.Dir(golang.Profile(), project), 0o755); err != nil { |
| 68 | t.Fatalf("creating the settings directory: %v", err) |
| 69 | } |
| 70 | contents := "[editor]\ntheme = \"turbo-dark\"\nautosave = true\n" |
| 71 | if err := os.WriteFile(settings.Path(golang.Profile(), project), []byte(contents), 0o644); err != nil { |
| 72 | t.Fatalf("writing the settings file: %v", err) |
| 73 | } |
| 74 | |
| 75 | _, loaded := loadProjectSettings(golang.Profile()) |
| 76 | |
| 77 | if loaded.Theme != "turbo-dark" { |
| 78 | t.Errorf("Theme = %q, want turbo-dark", loaded.Theme) |
| 79 | } |
| 80 | if !loaded.Autosave { |
| 81 | t.Error("Autosave = false, want the file's true") |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestLoadProjectSettingsDoesNotWalkUpToAParent(t *testing.T) { |
| 86 | // "The project is where you started the editor" is the rule; a settings |
| 87 | // file one directory up belongs to a different project. |
| 88 | parent := t.TempDir() |
| 89 | if err := os.MkdirAll(settings.Dir(golang.Profile(), parent), 0o755); err != nil { |
| 90 | t.Fatalf("creating the settings directory: %v", err) |
| 91 | } |
| 92 | if err := os.WriteFile(settings.Path(golang.Profile(), parent), []byte("[editor]\ntheme = \"turbo-dark\"\n"), 0o644); err != nil { |
| 93 | t.Fatalf("writing the settings file: %v", err) |
| 94 | } |
| 95 | child := filepath.Join(parent, "internal") |
| 96 | if err := os.Mkdir(child, 0o755); err != nil { |
| 97 | t.Fatalf("creating the child directory: %v", err) |
| 98 | } |
| 99 | t.Chdir(child) |
| 100 | |
| 101 | _, loaded := loadProjectSettings(golang.Profile()) |
| 102 | |
| 103 | if loaded.Theme != "" { |
| 104 | t.Errorf("Theme = %q; settings were read from a parent directory", loaded.Theme) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestLoadProjectSettingsCarriesOnWithoutAFile(t *testing.T) { |
| 109 | t.Chdir(t.TempDir()) |
| 110 | |
| 111 | _, loaded := loadProjectSettings(golang.Profile()) |
| 112 | |
| 113 | if loaded != settings.Default() { |
| 114 | t.Errorf("loadProjectSettings(golang.Profile()) = %+v, want the defaults", loaded) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestABrokenSettingsFileDoesNotStopTheEditor(t *testing.T) { |
| 119 | // The editor is how you would fix the file, so it has to open. |
| 120 | project := t.TempDir() |
| 121 | t.Chdir(project) |
| 122 | if err := os.MkdirAll(settings.Dir(golang.Profile(), project), 0o755); err != nil { |
| 123 | t.Fatalf("creating the settings directory: %v", err) |
| 124 | } |
| 125 | if err := os.WriteFile(settings.Path(golang.Profile(), project), []byte("[editor\nnot toml"), 0o644); err != nil { |
| 126 | t.Fatalf("writing the settings file: %v", err) |
| 127 | } |
| 128 | |
| 129 | _, loaded := loadProjectSettings(golang.Profile()) |
| 130 | |
| 131 | if loaded != settings.Default() { |
| 132 | t.Errorf("loadProjectSettings(golang.Profile()) = %+v, want the defaults", loaded) |
| 133 | } |
| 134 | } |
| 📦 Turbo Go 85cf23f k33g 14h ago | 135 | |
| 136 | func TestDescribeLoadNamesTheSourceAndEveryFile(t *testing.T) { |
| 137 | got := describeLoad(configrepo.Result{ |
| 138 | Remote: "https://git.rickub.com/turbo-editors/configs.git", |
| 139 | Ref: "main", |
| 140 | Dir: "/work/.turbo-go", |
| 141 | Files: []string{"settings.toml", "tools.toml"}, |
| 142 | }) |
| 143 | |
| 144 | want := "Copied /work/.turbo-go from https://git.rickub.com/turbo-editors/configs.git (main):\n settings.toml\n tools.toml\n" |
| 145 | if got != want { |
| 146 | t.Errorf("describeLoad() =\n%q\nwant\n%q", got, want) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func TestDescribeLoadOmitsTheRefWhenTheDefaultBranchWasUsed(t *testing.T) { |
| 151 | got := describeLoad(configrepo.Result{Remote: "r", Dir: "d"}) |
| 152 | |
| 153 | if strings.Contains(got, "(") { |
| 154 | t.Errorf("describeLoad() = %q, want no parenthesised ref", got) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestLoadConfigLeavesAProjectThatHasAConfigurationAlone(t *testing.T) { |
| 159 | // Checked before anything is fetched, so this needs neither git nor a |
| 160 | // network: the project's own .turbo-go is a decision, not a default. |
| 161 | project := t.TempDir() |
| 162 | if err := os.MkdirAll(filepath.Join(project, ".turbo-go"), 0o755); err != nil { |
| 163 | t.Fatal(err) |
| 164 | } |
| 165 | t.Chdir(project) |
| 166 | |
| 167 | err := loadConfig(golang.Profile(), "https://rickub.com/turbo-editors/configs/tree/main/golang-init") |
| 168 | |
| 169 | if !errors.Is(err, configrepo.ErrExists) { |
| 170 | t.Errorf("loadConfig() error = %v, want ErrExists", err) |
| 171 | } |
| 172 | } |