turbo-editors/turbo-rustpublic Fork 0
v1.0.1
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 · 131 lines · 4.3 KBGo Blame HistoryRaw
📦 Turbo Rust 713ea5c k33g 9h ago1package main
2
3import (
4 "os"
5 "path/filepath"
6 "testing"
7
8 "rickub.com/turbo-editors/turbo-core/app"
9 "rickub.com/turbo-editors/turbo-core/settings"
10 "rickub.com/turbo-editors/turbo-core/theme"
11
12 "rickub.com/turbo-editors/turbo-rust/internal/rustlang"
13)
14
15func TestTheProjectRootIsTheCrateRoot(t *testing.T) {
16 // rust-analyzer is given the crate's boundary, which is what decides the
17 // code it loads. The walk itself is turbo-core's; what is checked here is
18 // that Turbo Rust's profile asks it to look for a Cargo.toml.
19 root := t.TempDir()
20 if err := os.WriteFile(filepath.Join(root, "Cargo.toml"), []byte("[package]\nname = \"x\"\n"), 0o644); err != nil {
21 t.Fatalf("writing Cargo.toml: %v", err)
22 }
23 nested := filepath.Join(root, "src", "deep")
24 if err := os.MkdirAll(nested, 0o755); err != nil {
25 t.Fatalf("creating the tree: %v", err)
26 }
27 file := filepath.Join(nested, "deep.rs")
28 if err := os.WriteFile(file, []byte("pub fn f() {}\n"), 0o644); err != nil {
29 t.Fatalf("writing the file: %v", err)
30 }
31
32 if got := app.ProjectRoot(rustlang.Profile(), []string{file}); got != root {
33 t.Errorf("ProjectRoot() = %q, want the crate root %q", got, root)
34 }
35}
36
37func TestThemeNamePrefersTheFlagOverTheProject(t *testing.T) {
38 got := themeName(options{theme: "borland-light"}, settings.Settings{Theme: "turbo-dark"})
39
40 if got != "borland-light" {
41 t.Errorf("themeName() = %q; an explicit -theme must win over the project's", got)
42 }
43}
44
45func TestThemeNameUsesTheProjectWhenNoFlagWasGiven(t *testing.T) {
46 got := themeName(options{}, settings.Settings{Theme: "turbo-dark"})
47
48 if got != "turbo-dark" {
49 t.Errorf("themeName() = %q, want the project's theme", got)
50 }
51}
52
53func TestThemeNameFallsBackToTheDefault(t *testing.T) {
54 got := themeName(options{}, settings.Settings{})
55
56 if got != theme.DefaultName {
57 t.Errorf("themeName() = %q, want %q", got, theme.DefaultName)
58 }
59}
60
61func TestLoadProjectSettingsReadsTheWorkingDirectory(t *testing.T) {
62 project := t.TempDir()
63 t.Chdir(project)
64 if err := os.MkdirAll(settings.Dir(rustlang.Profile(), project), 0o755); err != nil {
65 t.Fatalf("creating the settings directory: %v", err)
66 }
67 contents := "[editor]\ntheme = \"turbo-dark\"\nautosave = true\n"
68 if err := os.WriteFile(settings.Path(rustlang.Profile(), project), []byte(contents), 0o644); err != nil {
69 t.Fatalf("writing the settings file: %v", err)
70 }
71
72 _, loaded := loadProjectSettings(rustlang.Profile())
73
74 if loaded.Theme != "turbo-dark" {
75 t.Errorf("Theme = %q, want turbo-dark", loaded.Theme)
76 }
77 if !loaded.Autosave {
78 t.Error("Autosave = false, want the file's true")
79 }
80}
81
82func TestLoadProjectSettingsDoesNotWalkUpToAParent(t *testing.T) {
83 // "The project is where you started the editor" is the rule; a settings
84 // file one directory up belongs to a different project.
85 parent := t.TempDir()
86 if err := os.MkdirAll(settings.Dir(rustlang.Profile(), parent), 0o755); err != nil {
87 t.Fatalf("creating the settings directory: %v", err)
88 }
89 if err := os.WriteFile(settings.Path(rustlang.Profile(), parent), []byte("[editor]\ntheme = \"turbo-dark\"\n"), 0o644); err != nil {
90 t.Fatalf("writing the settings file: %v", err)
91 }
92 child := filepath.Join(parent, "src")
93 if err := os.Mkdir(child, 0o755); err != nil {
94 t.Fatalf("creating the child directory: %v", err)
95 }
96 t.Chdir(child)
97
98 _, loaded := loadProjectSettings(rustlang.Profile())
99
100 if loaded.Theme != "" {
101 t.Errorf("Theme = %q; settings were read from a parent directory", loaded.Theme)
102 }
103}
104
105func TestLoadProjectSettingsCarriesOnWithoutAFile(t *testing.T) {
106 t.Chdir(t.TempDir())
107
108 _, loaded := loadProjectSettings(rustlang.Profile())
109
110 if loaded != settings.Default() {
111 t.Errorf("loadProjectSettings(rustlang.Profile()) = %+v, want the defaults", loaded)
112 }
113}
114
115func TestABrokenSettingsFileDoesNotStopTheEditor(t *testing.T) {
116 // The editor is how you would fix the file, so it has to open.
117 project := t.TempDir()
118 t.Chdir(project)
119 if err := os.MkdirAll(settings.Dir(rustlang.Profile(), project), 0o755); err != nil {
120 t.Fatalf("creating the settings directory: %v", err)
121 }
122 if err := os.WriteFile(settings.Path(rustlang.Profile(), project), []byte("[editor\nnot toml"), 0o644); err != nil {
123 t.Fatalf("writing the settings file: %v", err)
124 }
125
126 _, loaded := loadProjectSettings(rustlang.Profile())
127
128 if loaded != settings.Default() {
129 t.Errorf("loadProjectSettings(rustlang.Profile()) = %+v, want the defaults", loaded)
130 }
131}