turbo-editors/turbo-pythonpublic Fork 0
v1.0.0
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-python.git
git clone ssh://git@rickub.com/turbo-editors/turbo-python.git

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

main_test.go · 156 lines · 5.1 KBGo Blame HistoryRaw
📦 Turbo Python 6fc62ea k33g 10h 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-python/internal/pythonlang"
13)
14
15// Each of the three markers finds the root on its own, and the walk stops at
16// the nearest directory holding any of them. pylsp is started there, and it is
17// what decides the code the server loads.
18func TestTheProjectRootIsFoundByAnyOfTheThreeMarkers(t *testing.T) {
19 markers := map[string]string{
20 "pyproject.toml": "[project]\nname = \"x\"\n",
21 "setup.py": "from setuptools import setup\n\nsetup()\n",
22 "setup.cfg": "[metadata]\nname = x\n",
23 }
24
25 for marker, contents := range markers {
26 t.Run(marker, func(t *testing.T) {
27 root := t.TempDir()
28 writeFile(t, filepath.Join(root, marker), contents)
29 file := filepath.Join(root, "src", "package", "deep.py")
30 writeFile(t, file, "def f() -> None:\n pass\n")
31
32 if got := app.ProjectRoot(pythonlang.Profile(), []string{file}); got != root {
33 t.Errorf("ProjectRoot() = %q, want the project root %q", got, root)
34 }
35 })
36 }
37}
38
39// A directory with none of the three is its own root: the walk must not climb
40// past a project into whatever is above it.
41func TestAFileInNoProjectIsItsOwnRoot(t *testing.T) {
42 root := t.TempDir()
43 file := filepath.Join(root, "script.py")
44 writeFile(t, file, "print(1)\n")
45
46 if got := app.ProjectRoot(pythonlang.Profile(), []string{file}); got != root {
47 t.Errorf("ProjectRoot() = %q, want the file's own directory %q", got, root)
48 }
49}
50
51// writeFile creates a file, making its directory first.
52func writeFile(t *testing.T, path, contents string) {
53 t.Helper()
54 if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
55 t.Fatalf("creating %s: %v", filepath.Dir(path), err)
56 }
57 if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
58 t.Fatalf("writing %s: %v", path, err)
59 }
60}
61
62func TestThemeNamePrefersTheFlagOverTheProject(t *testing.T) {
63 got := themeName(options{theme: "borland-light"}, settings.Settings{Theme: "turbo-dark"})
64
65 if got != "borland-light" {
66 t.Errorf("themeName() = %q; an explicit -theme must win over the project's", got)
67 }
68}
69
70func TestThemeNameUsesTheProjectWhenNoFlagWasGiven(t *testing.T) {
71 got := themeName(options{}, settings.Settings{Theme: "turbo-dark"})
72
73 if got != "turbo-dark" {
74 t.Errorf("themeName() = %q, want the project's theme", got)
75 }
76}
77
78func TestThemeNameFallsBackToTheDefault(t *testing.T) {
79 got := themeName(options{}, settings.Settings{})
80
81 if got != theme.DefaultName {
82 t.Errorf("themeName() = %q, want %q", got, theme.DefaultName)
83 }
84}
85
86func TestLoadProjectSettingsReadsTheWorkingDirectory(t *testing.T) {
87 project := t.TempDir()
88 t.Chdir(project)
89 if err := os.MkdirAll(settings.Dir(pythonlang.Profile(), project), 0o755); err != nil {
90 t.Fatalf("creating the settings directory: %v", err)
91 }
92 contents := "[editor]\ntheme = \"turbo-dark\"\nautosave = true\n"
93 if err := os.WriteFile(settings.Path(pythonlang.Profile(), project), []byte(contents), 0o644); err != nil {
94 t.Fatalf("writing the settings file: %v", err)
95 }
96
97 _, loaded := loadProjectSettings(pythonlang.Profile())
98
99 if loaded.Theme != "turbo-dark" {
100 t.Errorf("Theme = %q, want turbo-dark", loaded.Theme)
101 }
102 if !loaded.Autosave {
103 t.Error("Autosave = false, want the file's true")
104 }
105}
106
107func TestLoadProjectSettingsDoesNotWalkUpToAParent(t *testing.T) {
108 // "The project is where you started the editor" is the rule; a settings
109 // file one directory up belongs to a different project.
110 parent := t.TempDir()
111 if err := os.MkdirAll(settings.Dir(pythonlang.Profile(), parent), 0o755); err != nil {
112 t.Fatalf("creating the settings directory: %v", err)
113 }
114 if err := os.WriteFile(settings.Path(pythonlang.Profile(), parent), []byte("[editor]\ntheme = \"turbo-dark\"\n"), 0o644); err != nil {
115 t.Fatalf("writing the settings file: %v", err)
116 }
117 child := filepath.Join(parent, "src")
118 if err := os.Mkdir(child, 0o755); err != nil {
119 t.Fatalf("creating the child directory: %v", err)
120 }
121 t.Chdir(child)
122
123 _, loaded := loadProjectSettings(pythonlang.Profile())
124
125 if loaded.Theme != "" {
126 t.Errorf("Theme = %q; settings were read from a parent directory", loaded.Theme)
127 }
128}
129
130func TestLoadProjectSettingsCarriesOnWithoutAFile(t *testing.T) {
131 t.Chdir(t.TempDir())
132
133 _, loaded := loadProjectSettings(pythonlang.Profile())
134
135 if loaded != settings.Default() {
136 t.Errorf("loadProjectSettings(pythonlang.Profile()) = %+v, want the defaults", loaded)
137 }
138}
139
140func TestABrokenSettingsFileDoesNotStopTheEditor(t *testing.T) {
141 // The editor is how you would fix the file, so it has to open.
142 project := t.TempDir()
143 t.Chdir(project)
144 if err := os.MkdirAll(settings.Dir(pythonlang.Profile(), project), 0o755); err != nil {
145 t.Fatalf("creating the settings directory: %v", err)
146 }
147 if err := os.WriteFile(settings.Path(pythonlang.Profile(), project), []byte("[editor\nnot toml"), 0o644); err != nil {
148 t.Fatalf("writing the settings file: %v", err)
149 }
150
151 _, loaded := loadProjectSettings(pythonlang.Profile())
152
153 if loaded != settings.Default() {
154 t.Errorf("loadProjectSettings(pythonlang.Profile()) = %+v, want the defaults", loaded)
155 }
156}