package main import ( "os" "path/filepath" "testing" "rickub.com/turbo-editors/turbo-core/app" "rickub.com/turbo-editors/turbo-core/settings" "rickub.com/turbo-editors/turbo-core/theme" "rickub.com/turbo-editors/turbo-go/internal/golang" ) func TestTheProjectRootIsTheModuleRoot(t *testing.T) { // gopls is given the module's boundary, which is what decides the package // set it loads. The walk itself is turbo-core's; what is checked here is // that Turbo Go's profile asks it to look for a go.mod. root := t.TempDir() if err := os.WriteFile(filepath.Join(root, "go.mod"), []byte("module x\n"), 0o644); err != nil { t.Fatalf("writing go.mod: %v", err) } nested := filepath.Join(root, "internal", "deep") if err := os.MkdirAll(nested, 0o755); err != nil { t.Fatalf("creating the tree: %v", err) } file := filepath.Join(nested, "deep.go") if err := os.WriteFile(file, []byte("package deep\n"), 0o644); err != nil { t.Fatalf("writing the file: %v", err) } if got := app.ProjectRoot(golang.Profile(), []string{file}); got != root { t.Errorf("ProjectRoot() = %q, want the module root %q", got, root) } } func TestThemeNamePrefersTheFlagOverTheProject(t *testing.T) { got := themeName(options{theme: "borland-light"}, settings.Settings{Theme: "turbo-dark"}) if got != "borland-light" { t.Errorf("themeName() = %q; an explicit -theme must win over the project's", got) } } func TestThemeNameUsesTheProjectWhenNoFlagWasGiven(t *testing.T) { got := themeName(options{}, settings.Settings{Theme: "turbo-dark"}) if got != "turbo-dark" { t.Errorf("themeName() = %q, want the project's theme", got) } } func TestThemeNameFallsBackToTheDefault(t *testing.T) { got := themeName(options{}, settings.Settings{}) if got != theme.DefaultName { t.Errorf("themeName() = %q, want %q", got, theme.DefaultName) } } func TestLoadProjectSettingsReadsTheWorkingDirectory(t *testing.T) { project := t.TempDir() t.Chdir(project) if err := os.MkdirAll(settings.Dir(golang.Profile(), project), 0o755); err != nil { t.Fatalf("creating the settings directory: %v", err) } contents := "[editor]\ntheme = \"turbo-dark\"\nautosave = true\n" if err := os.WriteFile(settings.Path(golang.Profile(), project), []byte(contents), 0o644); err != nil { t.Fatalf("writing the settings file: %v", err) } _, loaded := loadProjectSettings(golang.Profile()) if loaded.Theme != "turbo-dark" { t.Errorf("Theme = %q, want turbo-dark", loaded.Theme) } if !loaded.Autosave { t.Error("Autosave = false, want the file's true") } } func TestLoadProjectSettingsDoesNotWalkUpToAParent(t *testing.T) { // "The project is where you started the editor" is the rule; a settings // file one directory up belongs to a different project. parent := t.TempDir() if err := os.MkdirAll(settings.Dir(golang.Profile(), parent), 0o755); err != nil { t.Fatalf("creating the settings directory: %v", err) } if err := os.WriteFile(settings.Path(golang.Profile(), parent), []byte("[editor]\ntheme = \"turbo-dark\"\n"), 0o644); err != nil { t.Fatalf("writing the settings file: %v", err) } child := filepath.Join(parent, "internal") if err := os.Mkdir(child, 0o755); err != nil { t.Fatalf("creating the child directory: %v", err) } t.Chdir(child) _, loaded := loadProjectSettings(golang.Profile()) if loaded.Theme != "" { t.Errorf("Theme = %q; settings were read from a parent directory", loaded.Theme) } } func TestLoadProjectSettingsCarriesOnWithoutAFile(t *testing.T) { t.Chdir(t.TempDir()) _, loaded := loadProjectSettings(golang.Profile()) if loaded != settings.Default() { t.Errorf("loadProjectSettings(golang.Profile()) = %+v, want the defaults", loaded) } } func TestABrokenSettingsFileDoesNotStopTheEditor(t *testing.T) { // The editor is how you would fix the file, so it has to open. project := t.TempDir() t.Chdir(project) if err := os.MkdirAll(settings.Dir(golang.Profile(), project), 0o755); err != nil { t.Fatalf("creating the settings directory: %v", err) } if err := os.WriteFile(settings.Path(golang.Profile(), project), []byte("[editor\nnot toml"), 0o644); err != nil { t.Fatalf("writing the settings file: %v", err) } _, loaded := loadProjectSettings(golang.Profile()) if loaded != settings.Default() { t.Errorf("loadProjectSettings(golang.Profile()) = %+v, want the defaults", loaded) } }