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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
package main
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"rickub.com/turbo-editors/turbo-core/app"
"rickub.com/turbo-editors/turbo-core/configrepo"
"rickub.com/turbo-editors/turbo-core/settings"
"rickub.com/turbo-editors/turbo-core/theme"
"rickub.com/turbo-editors/turbo-rust/internal/rustlang"
)
func TestTheProjectRootIsTheCrateRoot(t *testing.T) {
// rust-analyzer is given the crate's boundary, which is what decides the
// code it loads. The walk itself is turbo-core's; what is checked here is
// that Turbo Rust's profile asks it to look for a Cargo.toml.
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "Cargo.toml"), []byte("[package]\nname = \"x\"\n"), 0o644); err != nil {
t.Fatalf("writing Cargo.toml: %v", err)
}
nested := filepath.Join(root, "src", "deep")
if err := os.MkdirAll(nested, 0o755); err != nil {
t.Fatalf("creating the tree: %v", err)
}
file := filepath.Join(nested, "deep.rs")
if err := os.WriteFile(file, []byte("pub fn f() {}\n"), 0o644); err != nil {
t.Fatalf("writing the file: %v", err)
}
if got := app.ProjectRoot(rustlang.Profile(), []string{file}); got != root {
t.Errorf("ProjectRoot() = %q, want the crate 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(rustlang.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(rustlang.Profile(), project), []byte(contents), 0o644); err != nil {
t.Fatalf("writing the settings file: %v", err)
}
_, loaded := loadProjectSettings(rustlang.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(rustlang.Profile(), parent), 0o755); err != nil {
t.Fatalf("creating the settings directory: %v", err)
}
if err := os.WriteFile(settings.Path(rustlang.Profile(), parent), []byte("[editor]\ntheme = \"turbo-dark\"\n"), 0o644); err != nil {
t.Fatalf("writing the settings file: %v", err)
}
child := filepath.Join(parent, "src")
if err := os.Mkdir(child, 0o755); err != nil {
t.Fatalf("creating the child directory: %v", err)
}
t.Chdir(child)
_, loaded := loadProjectSettings(rustlang.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(rustlang.Profile())
if loaded != settings.Default() {
t.Errorf("loadProjectSettings(rustlang.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(rustlang.Profile(), project), 0o755); err != nil {
t.Fatalf("creating the settings directory: %v", err)
}
if err := os.WriteFile(settings.Path(rustlang.Profile(), project), []byte("[editor\nnot toml"), 0o644); err != nil {
t.Fatalf("writing the settings file: %v", err)
}
_, loaded := loadProjectSettings(rustlang.Profile())
if loaded != settings.Default() {
t.Errorf("loadProjectSettings(rustlang.Profile()) = %+v, want the defaults", loaded)
}
}
func TestDescribeLoadNamesTheSourceAndEveryFile(t *testing.T) {
got := describeLoad(configrepo.Result{
Remote: "https://git.rickub.com/turbo-editors/configs.git",
Ref: "main",
Dir: "/work/.turbo-rust",
Files: []string{"settings.toml", "tools.toml"},
})
want := "Copied /work/.turbo-rust from https://git.rickub.com/turbo-editors/configs.git (main):\n settings.toml\n tools.toml\n"
if got != want {
t.Errorf("describeLoad() =\n%q\nwant\n%q", got, want)
}
}
func TestDescribeLoadOmitsTheRefWhenTheDefaultBranchWasUsed(t *testing.T) {
got := describeLoad(configrepo.Result{Remote: "r", Dir: "d"})
if strings.Contains(got, "(") {
t.Errorf("describeLoad() = %q, want no parenthesised ref", got)
}
}
func TestLoadConfigLeavesAProjectThatHasAConfigurationAlone(t *testing.T) {
// Checked before anything is fetched, so this needs neither git nor a
// network: the project's own .turbo-rust is a decision, not a default.
project := t.TempDir()
if err := os.MkdirAll(filepath.Join(project, ".turbo-rust"), 0o755); err != nil {
t.Fatal(err)
}
t.Chdir(project)
err := loadConfig(rustlang.Profile(), "https://rickub.com/turbo-editors/configs/tree/main/golang-init")
if !errors.Is(err, configrepo.ErrExists) {
t.Errorf("loadConfig() error = %v, want ErrExists", err)
}
}
|