Skip to content

Commit 1acaf6d

Browse files
authored
Merge pull request #16 from oalders/copilot/fix-debounce-functionality
Fix cache directory creation when ~/.cache/debounce doesn't exist
2 parents d737a22 + 05a6fe2 commit 1acaf6d

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
github.com/alecthomas/assert/v2 v2.10.0 h1:jjRCHsj6hBJhkmhznrCzoNpbA3zqy0fYiUcYZP/GkPY=
2+
github.com/alecthomas/assert/v2 v2.10.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
23
github.com/alecthomas/kong v1.2.1 h1:E8jH4Tsgv6wCRX2nGrdPyHDUCSG83WH2qE4XLACD33Q=
34
github.com/alecthomas/kong v1.2.1/go.mod h1:rKTSFhbdp3Ryefn8x5MOEprnRFQ7nlmMC01GKhehhBM=
45
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
6+
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
57
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
68
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
79
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
10+
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
811
github.com/oalders/is v0.5.4-0.20240716215244-33e0acf2ac19 h1:9BaO8F7ovJseyo119hfahMm1UDU98/cEQ2LN8m+TCmY=
912
github.com/oalders/is v0.5.4-0.20240716215244-33e0acf2ac19/go.mod h1:11gTLN1BHxrg8PYn+73Iuspq7kOsQVbl4ccOlj8rHj0=
1013
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

run/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func Run(args *types.DebounceCommand, home string) (bool, []byte, error) { //nol
3737
}
3838
} else {
3939
cacheDir = filepath.Join(home, ".cache", "debounce")
40-
err := MaybeMakeCacheDir(home, cacheDir)
40+
err := MaybeMakeCacheDir(home, filepath.Join(".cache", "debounce"))
4141
if err != nil {
4242
return false, []byte{}, err
4343
}

run/run_test.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package run_test
22

33
import (
44
"os"
5-
"path"
5+
"path/filepath"
66
"testing"
77

88
"github.com/oalders/debounce/run"
@@ -43,7 +43,7 @@ func TestEnsureDir(t *testing.T) {
4343
err = run.MaybeMakeCacheDir(tempDir, dirName)
4444
require.NoError(t, err, "first attempt to make dir")
4545

46-
_, err = os.Stat(path.Join(tempDir, dirName))
46+
_, err = os.Stat(filepath.Join(tempDir, dirName))
4747
require.NoError(t, err)
4848

4949
err = run.MaybeMakeCacheDir(tempDir, dirName)
@@ -76,3 +76,35 @@ func TestRunWithNonExistentCacheDir(t *testing.T) {
7676
assert.False(t, success)
7777
assert.Empty(t, output)
7878
}
79+
80+
func TestRunCreatesDefaultCacheDir(t *testing.T) {
81+
t.Parallel()
82+
// Create a temporary home directory
83+
tempHome, err := os.MkdirTemp("", "test-home")
84+
require.NoError(t, err)
85+
defer os.RemoveAll(tempHome)
86+
87+
// Don't create the cache dir ahead of time - let Run create it
88+
args := &types.DebounceCommand{
89+
Quantity: "1",
90+
Unit: "s",
91+
Command: []string{"echo", "Hello, World!"},
92+
Debug: false,
93+
}
94+
95+
// Run should succeed even when cache dir doesn't exist
96+
success, output, err := run.Run(args, tempHome)
97+
assert.NoError(t, err)
98+
assert.True(t, success)
99+
assert.Equal(t, string(output), "Hello, World!\n")
100+
101+
// Verify the cache directory was created
102+
cacheDir := filepath.Join(tempHome, ".cache", "debounce")
103+
_, err = os.Stat(cacheDir)
104+
assert.NoError(t, err, "cache directory should be created")
105+
106+
// Verify the cache file was created
107+
entries, err := os.ReadDir(cacheDir)
108+
require.NoError(t, err)
109+
assert.Len(t, entries, 1, "should have created one cache file")
110+
}

0 commit comments

Comments
 (0)