| 🛟 Updated. 28d5985 k33g 14h ago | 1 | package profile |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // goProfile is the identity Turbo Go builds itself with, which is what makes |
| 10 | // the expectations below the real ones rather than invented. |
| 11 | func goProfile() Profile { |
| 12 | return Profile{Name: "Turbo Go", Slug: "turbo-go", Language: "Go"} |
| 13 | } |
| 14 | |
| 15 | func TestProjectDirIsTheSlugWithADotInFront(t *testing.T) { |
| 16 | if got := goProfile().ProjectDir(); got != ".turbo-go" { |
| 17 | t.Errorf("ProjectDir() = %q, want %q", got, ".turbo-go") |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | func TestTheEnvironmentVariableNamesAreTheOnesTurboGoAlreadyUsed(t *testing.T) { |
| 22 | // These three names are not free to change: a user who has set |
| 23 | // TURBO_GO_THEME_DIR did so against a released binary. |
| 24 | tests := []struct { |
| 25 | name string |
| 26 | got string |
| 27 | want string |
| 28 | }{ |
| 29 | {"user directory", goProfile().DirEnvVar(), "TURBO_GO_DIR"}, |
| 30 | {"theme directory", goProfile().ThemeDirEnvVar(), "TURBO_GO_THEME_DIR"}, |
| 31 | {"snippet directory", goProfile().SnippetDirEnvVar(), "TURBO_GO_SNIPPET_DIR"}, |
| 32 | } |
| 33 | |
| 34 | for _, test := range tests { |
| 35 | t.Run(test.name, func(t *testing.T) { |
| 36 | if test.got != test.want { |
| 37 | t.Errorf("got %q, want %q", test.got, test.want) |
| 38 | } |
| 39 | }) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestUserDirHonoursItsEnvironmentOverride(t *testing.T) { |
| 44 | dir := t.TempDir() |
| 45 | t.Setenv(goProfile().DirEnvVar(), dir) |
| 46 | |
| 47 | if got := goProfile().UserDir(); got != dir { |
| 48 | t.Errorf("UserDir() = %q, want %q", got, dir) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestUserDirFallsBackToTheConfigDirectory(t *testing.T) { |
| 53 | t.Setenv(goProfile().DirEnvVar(), "") |
| 54 | |
| 55 | got := goProfile().UserDir() |
| 56 | // A system with no configuration directory at all gives "", which is a |
| 57 | // state the editor handles rather than an error. |
| 58 | if got != "" && filepath.Base(got) != "turbo-go" { |
| 59 | t.Errorf("UserDir() = %q, want it to end in turbo-go", got) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestThemeDirSitsUnderTheUserDirectory(t *testing.T) { |
| 64 | dir := t.TempDir() |
| 65 | t.Setenv(goProfile().DirEnvVar(), dir) |
| 66 | t.Setenv(goProfile().ThemeDirEnvVar(), "") |
| 67 | |
| 68 | want := filepath.Join(dir, "themes") |
| 69 | if got := goProfile().ThemeDir(); got != want { |
| 70 | t.Errorf("ThemeDir() = %q, want %q", got, want) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestTheThemeOverrideNamesTheThemeDirectoryItself(t *testing.T) { |
| 75 | // Not the directory above it: a test setting this wants to say exactly |
| 76 | // where its theme files are, without having to make a themes/ inside it. |
| 77 | dir := t.TempDir() |
| 78 | t.Setenv(goProfile().ThemeDirEnvVar(), dir) |
| 79 | |
| 80 | if got := goProfile().ThemeDir(); got != dir { |
| 81 | t.Errorf("ThemeDir() = %q, want %q", got, dir) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestThemeDirIsEmptyWhenThereIsNowhereToPutIt(t *testing.T) { |
| 86 | t.Setenv(goProfile().ThemeDirEnvVar(), "") |
| 87 | t.Setenv(goProfile().DirEnvVar(), "") |
| 88 | // UserConfigDir reads these; emptying them is what makes it fail. |
| 89 | t.Setenv("XDG_CONFIG_HOME", "") |
| 90 | t.Setenv("HOME", "") |
| 91 | if os.Getenv("HOME") != "" { |
| 92 | t.Skip("this system does not take its configuration directory from HOME") |
| 93 | } |
| 94 | |
| 95 | if got := goProfile().ThemeDir(); got != "" { |
| 96 | t.Errorf("ThemeDir() = %q, want %q", got, "") |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestASlugWithDashesBecomesAnEnvironmentVariableWithUnderscores(t *testing.T) { |
| 101 | p := Profile{Slug: "turbo-rust"} |
| 102 | if got := p.ThemeDirEnvVar(); got != "TURBO_RUST_THEME_DIR" { |
| 103 | t.Errorf("ThemeDirEnvVar() = %q, want %q", got, "TURBO_RUST_THEME_DIR") |
| 104 | } |
| 105 | } |