package profile import ( "os" "path/filepath" "testing" ) // goProfile is the identity Turbo Go builds itself with, which is what makes // the expectations below the real ones rather than invented. func goProfile() Profile { return Profile{Name: "Turbo Go", Slug: "turbo-go", Language: "Go"} } func TestProjectDirIsTheSlugWithADotInFront(t *testing.T) { if got := goProfile().ProjectDir(); got != ".turbo-go" { t.Errorf("ProjectDir() = %q, want %q", got, ".turbo-go") } } func TestTheEnvironmentVariableNamesAreTheOnesTurboGoAlreadyUsed(t *testing.T) { // These three names are not free to change: a user who has set // TURBO_GO_THEME_DIR did so against a released binary. tests := []struct { name string got string want string }{ {"user directory", goProfile().DirEnvVar(), "TURBO_GO_DIR"}, {"theme directory", goProfile().ThemeDirEnvVar(), "TURBO_GO_THEME_DIR"}, {"snippet directory", goProfile().SnippetDirEnvVar(), "TURBO_GO_SNIPPET_DIR"}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { if test.got != test.want { t.Errorf("got %q, want %q", test.got, test.want) } }) } } func TestUserDirHonoursItsEnvironmentOverride(t *testing.T) { dir := t.TempDir() t.Setenv(goProfile().DirEnvVar(), dir) if got := goProfile().UserDir(); got != dir { t.Errorf("UserDir() = %q, want %q", got, dir) } } func TestUserDirFallsBackToTheConfigDirectory(t *testing.T) { t.Setenv(goProfile().DirEnvVar(), "") got := goProfile().UserDir() // A system with no configuration directory at all gives "", which is a // state the editor handles rather than an error. if got != "" && filepath.Base(got) != "turbo-go" { t.Errorf("UserDir() = %q, want it to end in turbo-go", got) } } func TestThemeDirSitsUnderTheUserDirectory(t *testing.T) { dir := t.TempDir() t.Setenv(goProfile().DirEnvVar(), dir) t.Setenv(goProfile().ThemeDirEnvVar(), "") want := filepath.Join(dir, "themes") if got := goProfile().ThemeDir(); got != want { t.Errorf("ThemeDir() = %q, want %q", got, want) } } func TestTheThemeOverrideNamesTheThemeDirectoryItself(t *testing.T) { // Not the directory above it: a test setting this wants to say exactly // where its theme files are, without having to make a themes/ inside it. dir := t.TempDir() t.Setenv(goProfile().ThemeDirEnvVar(), dir) if got := goProfile().ThemeDir(); got != dir { t.Errorf("ThemeDir() = %q, want %q", got, dir) } } func TestThemeDirIsEmptyWhenThereIsNowhereToPutIt(t *testing.T) { t.Setenv(goProfile().ThemeDirEnvVar(), "") t.Setenv(goProfile().DirEnvVar(), "") // UserConfigDir reads these; emptying them is what makes it fail. t.Setenv("XDG_CONFIG_HOME", "") t.Setenv("HOME", "") if os.Getenv("HOME") != "" { t.Skip("this system does not take its configuration directory from HOME") } if got := goProfile().ThemeDir(); got != "" { t.Errorf("ThemeDir() = %q, want %q", got, "") } } func TestASlugWithDashesBecomesAnEnvironmentVariableWithUnderscores(t *testing.T) { p := Profile{Slug: "turbo-rust"} if got := p.ThemeDirEnvVar(); got != "TURBO_RUST_THEME_DIR" { t.Errorf("ThemeDirEnvVar() = %q, want %q", got, "TURBO_RUST_THEME_DIR") } }