121 lines
4.5 KiB
Go
121 lines
4.5 KiB
Go
package cli
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
)
|
|
|
|
func TestMacOSPluginDisableAndVMInventory(t *testing.T) {
|
|
root := t.TempDir()
|
|
args := []string{"--data-root", root, "--lang", "en-US"}
|
|
runCLI := func(command ...string) error {
|
|
_, err := run(append(args, command...))
|
|
return err
|
|
}
|
|
t.Setenv("REIMS_CTL_CONFIG_DIR", filepath.Join(root, "config", "reims-ctl"))
|
|
if err := runCLI("plugin", "disable", "builtin.guest.macos"); err != nil {
|
|
t.Fatalf("disable without a runtime or VMs: %v", err)
|
|
}
|
|
for _, cmd := range [][]string{{"vm", "create", "--start", "mac"}, {"vm", "type", "mac", "test"}} {
|
|
if err := runCLI(cmd...); err == nil || err.Error() != i18n.T(i18n.EnUS, "err.macos.disabled") {
|
|
t.Fatalf("%v while disabled: %v", cmd, err)
|
|
}
|
|
}
|
|
|
|
vmDir := filepath.Join(t.TempDir(), "folder-different-from-vm-name")
|
|
cfg := &config.Config{DataRoot: root}
|
|
registryDir := filepath.Join(cfg.XDGConfigDir(), "reims-ctl")
|
|
if err := os.MkdirAll(registryDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
registry := filepath.Join(registryDir, "vms.toml")
|
|
content := fmt.Sprintf("[[vm]]\nname = %q\ndir = %q\n", "Mac Work", vmDir)
|
|
if err := os.WriteFile(registry, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mgr, router, err := openCLIPluginManager(cfg, true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer mgr.Close()
|
|
vms, err := router.ListAllVMs(t.Context())
|
|
if err != nil || len(vms) != 1 || vms[0].Name != "Mac Work" || vms[0].Directory != vmDir ||
|
|
vms[0].DriverID != driver.BuiltinReimsMacOSDriverID || !vms[0].DriverUnavailable {
|
|
t.Fatalf("disabled list = %+v, %v", vms, err)
|
|
}
|
|
if _, vm, err := router.ResolveVM(t.Context(), "mac work"); err == nil || vm.Directory != vmDir || !vm.DriverUnavailable {
|
|
t.Fatalf("disabled lookup = %+v, %v", vm, err)
|
|
}
|
|
|
|
if err := runCLI("plugin", "enable", "builtin.guest.macos"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := runCLI("vm", "list"); !errors.Is(err, rt.ErrNoActiveRuntime) {
|
|
t.Fatalf("list with registered VMs and no runtime = %v", err)
|
|
}
|
|
if err := runCLI("plugin", "disable", "builtin.guest.macos"); !errors.Is(err, rt.ErrNoActiveRuntime) {
|
|
t.Fatalf("disabling with registered VMs and unknown state = %v", err)
|
|
}
|
|
|
|
// An installed runtime with a missing control executable must also fail its
|
|
// enumeration rather than show an empty VM list.
|
|
runtimeDir := filepath.Join(cfg.RuntimesDir(), "test")
|
|
if err := os.MkdirAll(runtimeDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(runtimeDir, "manifest.json"), []byte(`{"commit":"test"}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := runCLI("vm", "list"); err == nil || !strings.Contains(err.Error(), "reims-ctl list") {
|
|
t.Fatalf("list with an unusable runtime = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCLIPluginListEnableDisableAndDoctor(t *testing.T) {
|
|
dataRoot := t.TempDir()
|
|
// `plugin list` should succeed and include builtins.
|
|
if code := Main([]string{"--data-root", dataRoot, "plugin", "list", "--json"}); code != 0 {
|
|
t.Fatalf("plugin list exited %d", code)
|
|
}
|
|
// Disable and re-enable builtin clipboard plugin via CLI.
|
|
if code := Main([]string{"--data-root", dataRoot, "plugin", "disable", "builtin.bridge.clipboard"}); code != 0 {
|
|
t.Fatalf("plugin disable clipboard exited %d", code)
|
|
}
|
|
if code := Main([]string{"--data-root", dataRoot, "plugin", "enable", "builtin.bridge.clipboard"}); code != 0 {
|
|
t.Fatalf("plugin enable clipboard exited %d", code)
|
|
}
|
|
// Builtin cannot be removed.
|
|
if code := Main([]string{"--data-root", dataRoot, "plugin", "remove", "builtin.bridge.clipboard"}); code == 0 {
|
|
t.Fatal("expected removing builtin plugin to fail")
|
|
}
|
|
// Safe mode window flag round-trips cleanly.
|
|
w, ok := ParseWindowArgs([]string{"--safe-mode"})
|
|
if !ok || !w.SafeMode {
|
|
t.Fatalf("ParseWindowArgs(--safe-mode) = %+v, %v", w, ok)
|
|
}
|
|
// Dev link + doctor on a local directory.
|
|
devDir := t.TempDir()
|
|
_ = os.WriteFile(filepath.Join(devDir, "plugin.json"), []byte(`{
|
|
"schema": 1,
|
|
"id": "dev.sample.theme",
|
|
"version": "0.1.0",
|
|
"name": {"en-US": "Dev Theme"},
|
|
"engines": {"pluginApi": "^1.0.0"}
|
|
}`), 0o644)
|
|
if code := Main([]string{"--data-root", dataRoot, "plugin", "link", devDir}); code != 0 {
|
|
t.Fatalf("plugin link exited %d", code)
|
|
}
|
|
if code := Main([]string{"--data-root", dataRoot, "plugin", "doctor", devDir}); code != 0 {
|
|
t.Fatalf("plugin doctor dir exited %d", code)
|
|
}
|
|
}
|