Skip to content

Commit

Permalink
Merge pull request #7 from jbowes/filecache-mkdir
Browse files Browse the repository at this point in the history
teach filecache to make its dir
  • Loading branch information
jbowes authored Aug 11, 2021
2 parents 96a1f35 + 5fe4295 commit 1b2788c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions impl/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"encoding/json"
"os"
"path/filepath"
)

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

// Set cached release Info.
func (f *FileCacher) Set(_ context.Context, i *Info) error {
if err := os.MkdirAll(filepath.Dir(f.Path), 0750); err != nil {
return err
}

w, err := os.Create(f.Path)
if err != nil {
return err
Expand Down
32 changes: 32 additions & 0 deletions impl/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,35 @@ func TestFileCacher_roundTrip(t *testing.T) {
}

}

func TestFileCacher_errOnWrite(t *testing.T) {
ctx := context.Background()

fc := impl.FileCacher{Path: filepath.Join("/", "test-cache.json")}

err := fc.Set(ctx, &impl.Info{
CheckTime: time.Now(),
Version: "v1.1.2",
Etag: `"some-etag"`,
})

if err == nil {
t.Errorf("expected err but go none")
}
}

func TestFileCacher_errOnMkDir(t *testing.T) {
ctx := context.Background()

fc := impl.FileCacher{Path: filepath.Join("/", "whatsnew-test", "test-cache.json")}

err := fc.Set(ctx, &impl.Info{
CheckTime: time.Now(),
Version: "v1.1.2",
Etag: `"some-etag"`,
})

if err == nil {
t.Errorf("expected err but go none")
}
}

0 comments on commit 1b2788c

Please sign in to comment.