turbo-editors/turbo-rustpublic Fork 0
f69efbe664783d7fc22522b5ebd98e6cd8f63bd1
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-rust.git
git clone ssh://git@rickub.com/turbo-editors/turbo-rust.git

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

main_test.go · 172 lines · 5.7 KBGo Blame HistoryRaw
📦 Turbo Rust 713ea5c k33g yesterday1package main
2
3import (
📦 Turbo Rust f69efbe k33g 10h ago4 "errors"
📦 Turbo Rust 713ea5c k33g yesterday5 "os"
6 "path/filepath"
📦 Turbo Rust f69efbe k33g 10h ago7 "strings"
📦 Turbo Rust 713ea5c k33g yesterday8 "testing"
9
10 "rickub.com/turbo-editors/turbo-core/app"
📦 Turbo Rust f69efbe k33g 10h ago11 "rickub.com/turbo-editors/turbo-core/configrepo"
📦 Turbo Rust 713ea5c k33g yesterday12 "rickub.com/turbo-editors/turbo-core/settings"
13 "rickub.com/turbo-editors/turbo-core/theme"
14
15 "rickub.com/turbo-editors/turbo-rust/internal/rustlang"
16)
17
18func TestTheProjectRootIsTheCrateRoot(t *testing.T) {
19 // rust-analyzer is given the crate's boundary, which is what decides the
20 // code it loads. The walk itself is turbo-core's; what is checked here is
21 // that Turbo Rust's profile asks it to look for a Cargo.toml.
22 root := t.TempDir()
23 if err := os.WriteFile(filepath.Join(root, "Cargo.toml"), []byte("[package]\nname = \"x\"\n"), 0o644); err != nil {
24 t.Fatalf("writing Cargo.toml: %v", err)
25 }
26 nested := filepath.Join(root, "src", "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.rs")
31 if err := os.WriteFile(file, []byte("pub fn f() {}\n"), 0o644); err != nil {
32 t.Fatalf("writing the file: %v", err)
33 }
34
35 if got := app.ProjectRoot(rustlang.Profile(), []string{file}); got != root {
36 t.Errorf("ProjectRoot() = %q, want the crate root %q", got, root)
37 }
38}
39
40func 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
48func 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
56func 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
64func TestLoadProjectSettingsReadsTheWorkingDirectory(t *testing.T) {
65 project := t.TempDir()
66 t.Chdir(project)
67 if err := os.MkdirAll(settings.Dir(rustlang.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(rustlang.Profile(), project), []byte(contents), 0o644); err != nil {
72 t.Fatalf("writing the settings file: %v", err)
73 }
74
75 _, loaded := loadProjectSettings(rustlang.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
85func 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(rustlang.Profile(), parent), 0o755); err != nil {
90 t.Fatalf("creating the settings directory: %v", err)
91 }
92 if err := os.WriteFile(settings.Path(rustlang.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, "src")
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(rustlang.Profile())
102
103 if loaded.Theme != "" {
104 t.Errorf("Theme = %q; settings were read from a parent directory", loaded.Theme)
105 }
106}
107
108func TestLoadProjectSettingsCarriesOnWithoutAFile(t *testing.T) {
109 t.Chdir(t.TempDir())
110
111 _, loaded := loadProjectSettings(rustlang.Profile())
112
113 if loaded != settings.Default() {
114 t.Errorf("loadProjectSettings(rustlang.Profile()) = %+v, want the defaults", loaded)
115 }
116}
117
118func 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(rustlang.Profile(), project), 0o755); err != nil {
123 t.Fatalf("creating the settings directory: %v", err)
124 }
125 if err := os.WriteFile(settings.Path(rustlang.Profile(), project), []byte("[editor\nnot toml"), 0o644); err != nil {
126 t.Fatalf("writing the settings file: %v", err)
127 }
128
129 _, loaded := loadProjectSettings(rustlang.Profile())
130
131 if loaded != settings.Default() {
132 t.Errorf("loadProjectSettings(rustlang.Profile()) = %+v, want the defaults", loaded)
133 }
134}
📦 Turbo Rust f69efbe k33g 10h ago135
136func 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-rust",
141 Files: []string{"settings.toml", "tools.toml"},
142 })
143
144 want := "Copied /work/.turbo-rust 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
150func 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
158func TestLoadConfigLeavesAProjectThatHasAConfigurationAlone(t *testing.T) {
159 // Checked before anything is fetched, so this needs neither git nor a
160 // network: the project's own .turbo-rust is a decision, not a default.
161 project := t.TempDir()
162 if err := os.MkdirAll(filepath.Join(project, ".turbo-rust"), 0o755); err != nil {
163 t.Fatal(err)
164 }
165 t.Chdir(project)
166
167 err := loadConfig(rustlang.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}