turbo-editors/turbo-corepublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on main · k33g · 4h ago
profile_test.go · 105 lines · 3.1 KBGo Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
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")
	}
}