turbo-editors/turbo-moonbitpublic Fork 0
v1.0.3
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-moonbit.git
git clone ssh://git@rickub.com/turbo-editors/turbo-moonbit.git

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

📦 Turbo MoonBit ded61de · on v1.0.3 · k33g · 3h ago
main_test.go · 198 lines · 6.4 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main

import (
	"errors"
	"flag"
	"os"
	"path/filepath"
	"slices"
	"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-moonbit/internal/moonbitlang"
)

// withArgs runs the command line parser against a fixed argument list, and
// restores the real one afterwards so tests do not affect each other.
func withArgs(t *testing.T, args ...string) options {
	t.Helper()

	realArgs, realFlags := os.Args, flag.CommandLine
	t.Cleanup(func() { os.Args, flag.CommandLine = realArgs, realFlags })

	os.Args = append([]string{"turbo-moonbit"}, args...)
	flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
	return parseFlags()
}

func TestFilesAreWhatIsLeftAfterTheFlags(t *testing.T) {
	opts := withArgs(t, "-no-lsp", "main.mbt", "lib/x.mbt")

	if !opts.noLSP {
		t.Error("-no-lsp was not read")
	}
	if want := []string{"main.mbt", "lib/x.mbt"}; !slices.Equal(opts.files, want) {
		t.Errorf("files = %v, want %v", opts.files, want)
	}
}

func TestTheThemeFlagDefaultsToEmptyRatherThanToAName(t *testing.T) {
	// Empty is what lets "was -theme given?" still be answered afterwards, and
	// that is what lets the project's settings fill it in without overriding an
	// explicit choice.
	if opts := withArgs(t); opts.theme != "" {
		t.Errorf("theme = %q with no flag, want the empty string", opts.theme)
	}
}

func TestTheThemeFlagBeatsTheProjectSettings(t *testing.T) {
	project := settings.Default()
	project.Theme = "cobalt"

	if got := themeName(options{theme: "monochrome"}, project); got != "monochrome" {
		t.Errorf("themeName() = %q, want the flag's %q", got, "monochrome")
	}
}

func TestTheProjectSettingsBeatTheBuiltInDefault(t *testing.T) {
	project := settings.Default()
	project.Theme = "cobalt"

	if got := themeName(options{}, project); got != "cobalt" {
		t.Errorf("themeName() = %q, want the project's %q", got, "cobalt")
	}
}

func TestTheBuiltInDefaultIsUsedWhenNobodySaysOtherwise(t *testing.T) {
	if got := themeName(options{}, settings.Default()); got == "" {
		t.Error("themeName() = \"\" with nothing set, want the library's default")
	}
}

func TestTheProjectRootIsTheNearestMoonModule(t *testing.T) {
	// app.ProjectRoot walks up looking for the profile's RootMarkers. This is
	// what decides the directory moon-lsp is started in, and starting it
	// anywhere else is how a server answers nothing for a whole session.
	root := t.TempDir()
	nested := filepath.Join(root, "cmd", "main")
	if err := os.MkdirAll(nested, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(root, "moon.mod"), []byte(`name = "u/m"`), 0o644); err != nil {
		t.Fatal(err)
	}

	file := filepath.Join(nested, "main.mbt")
	if got := app.ProjectRoot(moonbitlang.Profile(), []string{file}); got != root {
		t.Errorf("ProjectRoot(%q) = %q, want %q", file, got, root)
	}
}

func TestTheLegacyJSONModuleFileIsAlsoARoot(t *testing.T) {
	root := t.TempDir()
	if err := os.WriteFile(filepath.Join(root, "moon.mod.json"), []byte(`{"name":"u/m"}`), 0o644); err != nil {
		t.Fatal(err)
	}

	file := filepath.Join(root, "main.mbt")
	if got := app.ProjectRoot(moonbitlang.Profile(), []string{file}); got != root {
		t.Errorf("ProjectRoot(%q) = %q, want %q", file, got, root)
	}
}

func TestTheNearestModuleWinsOverTheOneAboveIt(t *testing.T) {
	// A workspace holds several modules. The server belongs to the one the
	// file is in, not to the outermost directory that happens to have a
	// manifest.
	outer := t.TempDir()
	inner := filepath.Join(outer, "member")
	if err := os.MkdirAll(inner, 0o755); err != nil {
		t.Fatal(err)
	}
	for _, dir := range []string{outer, inner} {
		if err := os.WriteFile(filepath.Join(dir, "moon.mod"), []byte(`name = "u/m"`), 0o644); err != nil {
			t.Fatal(err)
		}
	}

	file := filepath.Join(inner, "lib.mbt")
	if got := app.ProjectRoot(moonbitlang.Profile(), []string{file}); got != inner {
		t.Errorf("ProjectRoot(%q) = %q, want the nearer %q", file, got, inner)
	}
}

func TestLoadingSettingsFromADirectoryWithNoneGivesTheDefaults(t *testing.T) {
	// A directory somebody merely started the editor in has said nothing, and
	// the editor must not write to it. The starter file turns autosave on; the
	// default leaves it off.
	dir := t.TempDir()
	t.Chdir(dir)

	project, loaded := loadProjectSettings(moonbitlang.Profile())
	if project == "" {
		t.Error("loadProjectSettings returned no project directory")
	}
	if loaded.Autosave {
		t.Error("autosave is on with no settings file, want it off")
	}
}

func TestABrokenSettingsFileDoesNotStopTheEditorOpening(t *testing.T) {
	// A broken settings file must not stop the editor opening, because the
	// editor is how you would fix it.
	dir := t.TempDir()
	p := moonbitlang.Profile()
	if err := os.MkdirAll(filepath.Join(dir, p.ProjectDir()), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(settings.Path(p, dir), []byte("this is not ["), 0o644); err != nil {
		t.Fatal(err)
	}
	t.Chdir(dir)

	if _, loaded := loadProjectSettings(p); loaded != settings.Default() {
		t.Errorf("loadProjectSettings() = %+v with a broken file, 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-moonbit",
		Files:  []string{"settings.toml", "tools.toml"},
	})

	want := "Copied /work/.turbo-moonbit 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-moonbit is a decision, not a default.
	project := t.TempDir()
	if err := os.MkdirAll(filepath.Join(project, ".turbo-moonbit"), 0o755); err != nil {
		t.Fatal(err)
	}
	t.Chdir(project)

	err := loadConfig(moonbitlang.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)
	}
}