Skip to content

Commit b80a17d

Browse files
indacoclaude
andauthored
refactor: split large test files (#203)
Co-authored-by: Claude Opus 4.5 <[email protected]>
1 parent 5a04d3a commit b80a17d

19 files changed

+3217
-3096
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 ENABLE COMMAND */
19+
/* ------------------------------------------------------------------------- */
20+
21+
func TestExtensionEnableCmd_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: false`
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", "enable", "--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" enabled.`
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 enabled 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: true") {
56+
t.Errorf("expected config to contain 'enabled: true', got:\n%s", string(data))
57+
}
58+
}
59+
60+
func TestExtensionEnableCmd_MissingName(t *testing.T) {
61+
if os.Getenv("TEST_EXTENSION_ENABLE_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: false`
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", "enable",
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=TestExtensionEnableCmd_MissingName")
87+
cmd.Env = append(os.Environ(), "TEST_EXTENSION_ENABLE_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 enable"
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

Comments
 (0)