Skip to content

Commit 1b2788c

Browse files
authored
Merge pull request #7 from jbowes/filecache-mkdir
teach filecache to make its dir
2 parents 96a1f35 + 5fe4295 commit 1b2788c

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

impl/file.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"encoding/json"
1010
"os"
11+
"path/filepath"
1112
)
1213

1314
// FileCacher is the default Cacher used in whatsnew.
@@ -31,6 +32,10 @@ func (f *FileCacher) Get(context.Context) (*Info, error) {
3132

3233
// Set cached release Info.
3334
func (f *FileCacher) Set(_ context.Context, i *Info) error {
35+
if err := os.MkdirAll(filepath.Dir(f.Path), 0750); err != nil {
36+
return err
37+
}
38+
3439
w, err := os.Create(f.Path)
3540
if err != nil {
3641
return err

impl/file_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,35 @@ func TestFileCacher_roundTrip(t *testing.T) {
5757
}
5858

5959
}
60+
61+
func TestFileCacher_errOnWrite(t *testing.T) {
62+
ctx := context.Background()
63+
64+
fc := impl.FileCacher{Path: filepath.Join("/", "test-cache.json")}
65+
66+
err := fc.Set(ctx, &impl.Info{
67+
CheckTime: time.Now(),
68+
Version: "v1.1.2",
69+
Etag: `"some-etag"`,
70+
})
71+
72+
if err == nil {
73+
t.Errorf("expected err but go none")
74+
}
75+
}
76+
77+
func TestFileCacher_errOnMkDir(t *testing.T) {
78+
ctx := context.Background()
79+
80+
fc := impl.FileCacher{Path: filepath.Join("/", "whatsnew-test", "test-cache.json")}
81+
82+
err := fc.Set(ctx, &impl.Info{
83+
CheckTime: time.Now(),
84+
Version: "v1.1.2",
85+
Etag: `"some-etag"`,
86+
})
87+
88+
if err == nil {
89+
t.Errorf("expected err but go none")
90+
}
91+
}

0 commit comments

Comments
 (0)