on main help sign in
fix: skip non-existent command dirs instead of creating them (#2534)
73c2a9dlif
internal/commands/commands.go+2−9
@@ -130,8 +130,8 @@ func loadAll(sources []commandSource) ([]CustomCommand, error) {
130}
130}
131
131
132func loadFromSource(source commandSource) ([]CustomCommand, error) {
132func loadFromSource(source commandSource) ([]CustomCommand, error) {
133− if err := ensureDir(source.path); err != nil {
133+ if _, err := os.Stat(source.path); os.IsNotExist(err) {
134− return nil, err
134+ return nil, nil
135 }
135 }
136
136
137 var commands []CustomCommand
137 var commands []CustomCommand
@@ -216,13 +216,6 @@ func getXDGCommandsDir() string {
216 return ""
216 return ""
217}
217}
218
218
219−func ensureDir(path string) error {
220− if _, err := os.Stat(path); os.IsNotExist(err) {
221− return os.MkdirAll(path, 0o755)
222− }
223− return nil
224−}
225−
226func isMarkdownFile(name string) bool {
219func isMarkdownFile(name string) bool {
227 return strings.HasSuffix(strings.ToLower(name), ".md")
220 return strings.HasSuffix(strings.ToLower(name), ".md")
228}
221}
internal/commands/commands_test.gonew file+53
@@ -0,0 +1,53 @@
1+package commands
2+
3+import (
4+ "os"
5+ "path/filepath"
6+ "testing"
7+
8+ "github.com/stretchr/testify/require"
9+)
10+
11+func TestLoadFromSource_NonExistentDir(t *testing.T) {
12+ t.Parallel()
13+
14+ dir := filepath.Join(t.TempDir(), "does-not-exist")
15+
16+ cmds, err := loadFromSource(commandSource{path: dir, prefix: userCommandPrefix})
17+ require.NoError(t, err)
18+ require.Empty(t, cmds)
19+
20+ // directory must NOT have been created
21+ _, statErr := os.Stat(dir)
22+ require.True(t, os.IsNotExist(statErr))
23+}
24+
25+func TestLoadFromSource_ExistingDir(t *testing.T) {
26+ t.Parallel()
27+
28+ dir := t.TempDir()
29+ require.NoError(t, os.WriteFile(filepath.Join(dir, "hello.md"), []byte("say hello"), 0o644))
30+
31+ cmds, err := loadFromSource(commandSource{path: dir, prefix: userCommandPrefix})
32+ require.NoError(t, err)
33+ require.Len(t, cmds, 1)
34+ require.Equal(t, "user:hello", cmds[0].ID)
35+ require.Equal(t, "say hello", cmds[0].Content)
36+}
37+
38+func TestLoadAll_MixedSources(t *testing.T) {
39+ t.Parallel()
40+
41+ existing := t.TempDir()
42+ require.NoError(t, os.WriteFile(filepath.Join(existing, "cmd.md"), []byte("content"), 0o644))
43+
44+ missing := filepath.Join(t.TempDir(), "nope")
45+
46+ cmds, err := loadAll([]commandSource{
47+ {path: existing, prefix: userCommandPrefix},
48+ {path: missing, prefix: projectCommandPrefix},
49+ })
50+ require.NoError(t, err)
51+ require.Len(t, cmds, 1)
52+ require.Equal(t, "user:cmd", cmds[0].ID)
53+}