|
| 1 | +package extension |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/indaco/sley/internal/config" |
| 13 | + "github.com/indaco/sley/internal/testutils" |
| 14 | + "github.com/urfave/cli/v3" |
| 15 | +) |
| 16 | + |
| 17 | +/* ------------------------------------------------------------------------- */ |
| 18 | +/* EXTENSION DISABLE COMMAND */ |
| 19 | +/* ------------------------------------------------------------------------- */ |
| 20 | + |
| 21 | +func TestExtensionDisableCmd_Success(t *testing.T) { |
| 22 | + tmpDir := t.TempDir() |
| 23 | + configPath := filepath.Join(tmpDir, ".sley.yaml") |
| 24 | + |
| 25 | + content := `extensions: |
| 26 | + - name: mock-extension |
| 27 | + path: /some/path |
| 28 | + enabled: true` |
| 29 | + if err := os.WriteFile(configPath, []byte(content), 0644); err != nil { |
| 30 | + t.Fatalf("failed to write config file: %v", err) |
| 31 | + } |
| 32 | + |
| 33 | + cfg := &config.Config{Path: configPath} |
| 34 | + appCli := testutils.BuildCLIForTests(cfg.Path, []*cli.Command{Run()}) |
| 35 | + |
| 36 | + output, err := testutils.CaptureStdout(func() { |
| 37 | + testutils.RunCLITest(t, appCli, []string{ |
| 38 | + "sley", "extension", "disable", "--name", "mock-extension", |
| 39 | + }, tmpDir) |
| 40 | + }) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("CLI run failed: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + expected := `Extension "mock-extension" disabled.` |
| 46 | + if !strings.Contains(output, expected) { |
| 47 | + t.Errorf("expected output to contain %q, got:\n%s", expected, output) |
| 48 | + } |
| 49 | + |
| 50 | + // Verify the extension is now disabled in the config |
| 51 | + data, readErr := os.ReadFile(configPath) |
| 52 | + if readErr != nil { |
| 53 | + t.Fatalf("failed to read config: %v", readErr) |
| 54 | + } |
| 55 | + if !strings.Contains(string(data), "enabled: false") { |
| 56 | + t.Errorf("expected config to contain 'enabled: false', got:\n%s", string(data)) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestExtensionDisableCmd_MissingName(t *testing.T) { |
| 61 | + if os.Getenv("TEST_EXTENSION_DISABLE_MISSING_NAME") == "1" { |
| 62 | + tmp := t.TempDir() |
| 63 | + configPath := filepath.Join(tmp, ".sley.yaml") |
| 64 | + content := `extensions: |
| 65 | + - name: mock-extension |
| 66 | + path: /some/path |
| 67 | + enabled: true` |
| 68 | + if err := os.WriteFile(configPath, []byte(content), 0644); err != nil { |
| 69 | + fmt.Fprintln(os.Stderr, "failed to write config:", err) |
| 70 | + os.Exit(1) |
| 71 | + } |
| 72 | + |
| 73 | + cfg := &config.Config{Path: configPath} |
| 74 | + appCli := testutils.BuildCLIForTests(cfg.Path, []*cli.Command{Run()}) |
| 75 | + |
| 76 | + err := appCli.Run(context.Background(), []string{ |
| 77 | + "sley", "extension", "disable", |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + fmt.Fprintln(os.Stderr, err) |
| 81 | + os.Exit(1) |
| 82 | + } |
| 83 | + os.Exit(0) |
| 84 | + } |
| 85 | + |
| 86 | + cmd := exec.Command(os.Args[0], "-test.run=TestExtensionDisableCmd_MissingName") |
| 87 | + cmd.Env = append(os.Environ(), "TEST_EXTENSION_DISABLE_MISSING_NAME=1") |
| 88 | + output, err := cmd.CombinedOutput() |
| 89 | + |
| 90 | + if err == nil { |
| 91 | + t.Fatal("expected non-zero exit status") |
| 92 | + } |
| 93 | + |
| 94 | + expected := "please provide an extension name to disable" |
| 95 | + if !strings.Contains(string(output), expected) { |
| 96 | + t.Errorf("expected output to contain %q, got:\n%s", expected, output) |
| 97 | + } |
| 98 | +} |
0 commit comments