From 5bdd79f0bb4fcf9b9dd47b21623077033cb91991 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Mon, 4 Nov 2019 17:24:34 -0600 Subject: [PATCH 1/2] fix download caching --- downloader.go | 17 ++++++++++------- downloader_test.go | 27 +++++++++++++++++++++------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/downloader.go b/downloader.go index d28df61..15e002d 100644 --- a/downloader.go +++ b/downloader.go @@ -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 @@ -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 { @@ -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) diff --git a/downloader_test.go b/downloader_test.go index 8b944e7..ccae1db 100644 --- a/downloader_test.go +++ b/downloader_test.go @@ -10,6 +10,11 @@ import ( "github.com/stretchr/testify/require" ) +func mustCopyFile(t *testing.T, src, dst string) { + t.Helper() + require.NoError(t, copy.Copy(src, dst)) +} + func Test_downloadFile(t *testing.T) { t.Run("success", func(t *testing.T) { dir, teardown := tmpDir(t) @@ -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"))) }) @@ -92,6 +93,20 @@ func Test_downloader_validateChecksum(t *testing.T) { }) } +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) From 6a534f7911228496a6bc099191631807c952991c Mon Sep 17 00:00:00 2001 From: Will Roden Date: Mon, 4 Nov 2019 17:34:01 -0600 Subject: [PATCH 2/2] remove copy dependency --- downloader_test.go | 12 ++++-------- go.mod | 2 -- go.sum | 11 ----------- 3 files changed, 4 insertions(+), 21 deletions(-) diff --git a/downloader_test.go b/downloader_test.go index ccae1db..23c408b 100644 --- a/downloader_test.go +++ b/downloader_test.go @@ -5,14 +5,14 @@ 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, copy.Copy(src, dst)) + require.NoError(t, os.MkdirAll(filepath.Dir(dst), 0750)) + require.NoError(t, copyFile(src, dst)) } func Test_downloadFile(t *testing.T) { @@ -82,12 +82,8 @@ 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"))) }) diff --git a/go.mod b/go.mod index a3b1c12..bc3109d 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 210848a..70cdf23 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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=