Skip to content

Commit

Permalink
Merge pull request #12 from WillAbides/cachecheck
Browse files Browse the repository at this point in the history
fix download caching
  • Loading branch information
WillAbides authored Nov 4, 2019
2 parents b0a6329 + 6a534f7 commit f430152
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 33 deletions.
17 changes: 10 additions & 7 deletions downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func (d *Downloader) extract(downloadDir, extractDir string) error {
if err != nil {
return err
}
err = os.RemoveAll(extractDir)
if err != nil {
return err
}
err = os.MkdirAll(extractDir, 0750)
if err != nil {
return err
Expand All @@ -104,11 +108,7 @@ func (d *Downloader) extract(downloadDir, extractDir string) error {
if err != nil {
return copyFile(tarPath, filepath.Join(extractDir, dlName))
}
err = archiver.Unarchive(tarPath, extractDir)
if err != nil {
return err
}
return rm(tarPath)
return archiver.Unarchive(tarPath, extractDir)
}

func (d *Downloader) download(downloadDir string) error {
Expand Down Expand Up @@ -181,8 +181,11 @@ func (d *Downloader) Install(opts InstallOpts) error {
downloadDir := filepath.Join(cellarDir, "downloads", d.downloadsSubName())
extractDir := filepath.Join(cellarDir, "extracts", d.extractsSubName())

if fileExists(d.binPath(opts.TargetDir)) && !opts.Force {
return nil
if opts.Force {
err := os.RemoveAll(downloadDir)
if err != nil {
return err
}
}

err := d.download(downloadDir)
Expand Down
37 changes: 24 additions & 13 deletions downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import (
"path/filepath"
"testing"

"github.com/otiai10/copy"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func mustCopyFile(t *testing.T, src, dst string) {
t.Helper()
require.NoError(t, os.MkdirAll(filepath.Dir(dst), 0750))
require.NoError(t, copyFile(src, dst))
}

func Test_downloadFile(t *testing.T) {
t.Run("success", func(t *testing.T) {
dir, teardown := tmpDir(t)
Expand Down Expand Up @@ -52,12 +57,8 @@ func Test_downloader_validateChecksum(t *testing.T) {
URL: "foo/foo.tar.gz",
Checksum: "f7fa712caea646575c920af17de3462fe9d08d7fe062b9a17010117d5fa4ed88",
}
err := copy.Copy(
fooPath,
filepath.Join(dir, "foo.tar.gz"),
)
require.NoError(t, err)
err = d.validateChecksum(dir)
mustCopyFile(t, fooPath, filepath.Join(dir, "foo.tar.gz"))
err := d.validateChecksum(dir)
assert.NoError(t, err)
assert.True(t, fileExists(filepath.Join(dir, "foo.tar.gz")))
})
Expand All @@ -81,17 +82,27 @@ func Test_downloader_validateChecksum(t *testing.T) {
URL: "foo/foo.tar.gz",
Checksum: "deadbeef",
}
err := copy.Copy(
fooPath,
filepath.Join(dir, "foo.tar.gz"),
)
require.NoError(t, err)
err = d.validateChecksum(dir)
mustCopyFile(t, fooPath, filepath.Join(dir, "foo.tar.gz"))
err := d.validateChecksum(dir)
assert.Error(t, err)
assert.False(t, fileExists(filepath.Join(dir, "foo.tar.gz")))
})
}

func TestDownloader_extract(t *testing.T) {
dir, teardown := tmpDir(t)
defer teardown()
d := &Downloader{
URL: "foo/foo.tar.gz",
Checksum: "f7fa712caea646575c920af17de3462fe9d08d7fe062b9a17010117d5fa4ed88",
}
downloadDir := filepath.Join(dir, "download")
extractDir := filepath.Join(dir, "extract")
mustCopyFile(t, fooPath, filepath.Join(downloadDir, "foo.tar.gz"))
err := d.extract(downloadDir, extractDir)
assert.NoError(t, err)
}

func TestDownloader_Install(t *testing.T) {
t.Run("move", func(t *testing.T) {
dir, teardown := tmpDir(t)
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ require (
github.com/golang/snappy v0.0.1 // indirect
github.com/mholt/archiver v3.1.1+incompatible
github.com/nwaples/rardecode v1.0.0 // indirect
github.com/otiai10/copy v1.0.1
github.com/otiai10/curr v0.0.0-20190513014714-f5a3d24e5776 // indirect
github.com/pierrec/lz4 v2.3.0+incompatible // indirect
github.com/stretchr/testify v1.4.0
github.com/udhos/equalfile v0.3.0
Expand Down
11 changes: 0 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
bou.ke/monkey v1.0.1 h1:zEMLInw9xvNakzUUPjfS4Ds6jYPqCFx3m7bRmG5NH2U=
bou.ke/monkey v1.0.1/go.mod h1:FgHuK96Rv2Nlf+0u1OOVDpCMdsWyOFmeeketDHE7LIg=
github.com/alecthomas/kong v0.2.1 h1:V1tLBhyQBC4rsbXbcOvm3GBaytJSwRNX69fp1WJxbqQ=
github.com/alecthomas/kong v0.2.1/go.mod h1:+inYUSluD+p4L8KdviBSgzcqEjUQOfC5fQDRFuc36lI=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand Down Expand Up @@ -27,15 +25,6 @@ github.com/mholt/archiver v3.1.1+incompatible/go.mod h1:Dh2dOXnSdiLxRiPoVfIr/fI1
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/nwaples/rardecode v1.0.0 h1:r7vGuS5akxOnR4JQSkko62RJ1ReCMXxQRPtxsiFMBOs=
github.com/nwaples/rardecode v1.0.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
github.com/otiai10/copy v1.0.1 h1:gtBjD8aq4nychvRZ2CyJvFWAw0aja+VHazDdruZKGZA=
github.com/otiai10/copy v1.0.1/go.mod h1:8bMCJrAqOtN/d9oyh5HR7HhLQMvcGMpGdwRDYsfOCHc=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/curr v0.0.0-20190513014714-f5a3d24e5776 h1:o59bHXu8Ejas8Kq6pjoVJQ9/neN66SM8AKh6wI42BBs=
github.com/otiai10/curr v0.0.0-20190513014714-f5a3d24e5776/go.mod h1:3HNVkVOU7vZeFXocWuvtcS0XSFLcf2XUSDHkq9t1jU4=
github.com/otiai10/mint v1.2.3 h1:PsrRBmrxR68kyNu6YlqYHbNlItc5vOkuS6LBEsNttVA=
github.com/otiai10/mint v1.2.3/go.mod h1:YnfyPNhBvnY8bW4SGQHCs/aAFhkgySlMZbrF5U0bOVw=
github.com/otiai10/mint v1.2.4 h1:DxYL0itZyPaR5Z9HILdxSoHx+gNs6Yx+neOGS3IVUk0=
github.com/otiai10/mint v1.2.4/go.mod h1:d+b7n/0R3tdyUYYylALXpWQ/kTN+QobSq/4SRGBkR3M=
github.com/pierrec/lz4 v2.3.0+incompatible h1:CZzRn4Ut9GbUkHlQ7jqBXeZQV41ZSKWFc302ZU6lUTk=
github.com/pierrec/lz4 v2.3.0+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
Expand Down

0 comments on commit f430152

Please sign in to comment.