Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
Merge 6daec70 into 18cf1d5
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonSlovoka committed Mar 29, 2022
2 parents 18cf1d5 + 6daec70 commit 4a231d4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/asticode/go-astilectron
go 1.13

require (
github.com/asticode/go-astikit v0.15.0
github.com/asticode/go-astikit v0.29.1
github.com/stretchr/testify v1.4.0
)
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
github.com/asticode/go-astikit v0.15.0 h1:mpy55njSQ9SS9gP9vmPAfo08YpRt+4pr5/1emK6DFF0=
github.com/asticode/go-astikit v0.15.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0=
github.com/asticode/go-astikit v0.29.1 h1:w27sLYXK84mDwArf/Vw1BiD5dfD5PBDB+iHoIcpYq0w=
github.com/asticode/go-astikit v0.29.1/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
65 changes: 51 additions & 14 deletions provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Provisioner interface {
}

// mover is a function that moves a package
type mover func(ctx context.Context, p Paths) error
type mover func(ctx context.Context, p Paths) (func() error, error)

// defaultProvisioner represents the default provisioner
type defaultProvisioner struct {
Expand All @@ -39,17 +39,29 @@ func newDefaultProvisioner(l astikit.StdLogger) (dp *defaultProvisioner) {
},
})
dp = &defaultProvisioner{l: astikit.AdaptStdLogger(l)}
dp.moverAstilectron = func(ctx context.Context, p Paths) (err error) {
dp.moverAstilectron = func(ctx context.Context, p Paths) (closeFunc func() error, err error) {
if err = Download(ctx, dp.l, d, p.AstilectronDownloadSrc(), p.AstilectronDownloadDst()); err != nil {
return fmt.Errorf("downloading %s into %s failed: %w", p.AstilectronDownloadSrc(), p.AstilectronDownloadDst(), err)
return nil, fmt.Errorf("downloading %s into %s failed: %w", p.AstilectronDownloadSrc(), p.AstilectronDownloadDst(), err)
}
return
return func() (err error) {
dp.l.Debugf("removing %s", p.AstilectronDownloadDst())
if err = os.Remove(p.AstilectronDownloadDst()); err != nil {
return fmt.Errorf("removing %s failed: %w", p.AstilectronDownloadDst(), err)
}
return nil
}, err
}
dp.moverElectron = func(ctx context.Context, p Paths) (err error) {
dp.moverElectron = func(ctx context.Context, p Paths) (closeFunc func() error, err error) {
if err = Download(ctx, dp.l, d, p.ElectronDownloadSrc(), p.ElectronDownloadDst()); err != nil {
return fmt.Errorf("downloading %s into %s failed: %w", p.ElectronDownloadSrc(), p.ElectronDownloadDst(), err)
return nil, fmt.Errorf("downloading %s into %s failed: %w", p.ElectronDownloadSrc(), p.ElectronDownloadDst(), err)
}
return
return func() (err error) {
dp.l.Debugf("removing %s", p.ElectronDownloadDst())
if err = os.Remove(p.ElectronDownloadDst()); err != nil {
return fmt.Errorf("removing %s failed: %w", p.ElectronDownloadDst(), err)
}
return nil
}, err
}
return
}
Expand Down Expand Up @@ -183,10 +195,23 @@ func (p *defaultProvisioner) provisionPackage(ctx context.Context, paths Paths,
}

// Move
if err = m(ctx, paths); err != nil {
var closeFunc func() error
if closeFunc, err = m(ctx, paths); err != nil {
return fmt.Errorf("moving %s failed: %w", name, err)
}

// Make sure to close
defer func() {
if closeFunc == nil {
return
}
if err := closeFunc(); err != nil {
// Only log the error
p.l.Error(fmt.Errorf("closing failed: %w", err))
return
}
}()

// Create directory
p.l.Debugf("Creating directory %s", pathDirectory)
if err = os.MkdirAll(pathDirectory, 0755); err != nil {
Expand Down Expand Up @@ -330,17 +355,29 @@ type Disembedder func(src string) ([]byte, error)
// NewDisembedderProvisioner creates a provisioner that can provision based on embedded data
func NewDisembedderProvisioner(d Disembedder, pathAstilectron, pathElectron string, l astikit.StdLogger) Provisioner {
dp := &defaultProvisioner{l: astikit.AdaptStdLogger(l)}
dp.moverAstilectron = func(ctx context.Context, p Paths) (err error) {
dp.moverAstilectron = func(ctx context.Context, p Paths) (closeFunc func() error, err error) {
if err = Disembed(ctx, dp.l, d, pathAstilectron, p.AstilectronDownloadDst()); err != nil {
return fmt.Errorf("disembedding %s into %s failed: %w", pathAstilectron, p.AstilectronDownloadDst(), err)
return nil, fmt.Errorf("disembedding %s into %s failed: %w", pathAstilectron, p.AstilectronDownloadDst(), err)
}
return
return func() (err error) {
dp.l.Debugf("removing %s", p.AstilectronDownloadDst())
if err = os.Remove(p.AstilectronDownloadDst()); err != nil {
return fmt.Errorf("removing %s failed: %w", p.AstilectronDownloadDst(), err)
}
return nil
}, err
}
dp.moverElectron = func(ctx context.Context, p Paths) (err error) {
dp.moverElectron = func(ctx context.Context, p Paths) (closeFunc func() error, err error) {
if err = Disembed(ctx, dp.l, d, pathElectron, p.ElectronDownloadDst()); err != nil {
return fmt.Errorf("disembedding %s into %s failed: %w", pathElectron, p.ElectronDownloadDst(), err)
return nil, fmt.Errorf("disembedding %s into %s failed: %w", pathElectron, p.ElectronDownloadDst(), err)
}
return
return func() (err error) {
dp.l.Debugf("removing %s", p.ElectronDownloadDst())
if err = os.Remove(p.ElectronDownloadDst()); err != nil {
return fmt.Errorf("removing %s failed: %w", p.ElectronDownloadDst(), err)
}
return nil
}, err
}
return dp
}
45 changes: 43 additions & 2 deletions provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package astilectron

import (
"context"
"github.com/asticode/go-astikit"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http/httptest"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func testProvisionerSuccessful(t *testing.T, p Paths, osName, arch, versionAstilectron, versionElectron string) {
Expand Down Expand Up @@ -119,3 +119,44 @@ func TestNewDisembedderProvisioner(t *testing.T) {
assert.NoError(t, err)
testProvisionerSuccessful(t, *p, "linux", "amd64", DefaultVersionAstilectron, DefaultVersionElectron)
}

func TestRemoveDownloadDst(t *testing.T) {
var o = Options{
DataDirectoryPath: mockedTempPath(),
}

// Make sure the test directory doesn't exist.
if err := os.RemoveAll(o.DataDirectoryPath); err != nil && !os.IsNotExist(err) {
t.Fatalf("main: removing %s failed: %s", o.DataDirectoryPath, err)
}
defer os.RemoveAll(o.DataDirectoryPath)

a, err := New(astikit.AdaptTestLogger(t), o)
if err != nil {
t.Fatalf("main: creating astilectron failed: %s", err)
}

p := a.Paths()

if err = a.provision(); err != nil {
t.Fatalf("main: provisionning failed: %s", err)
}

// Check UnZip successful
if _, err := os.Stat(p.AstilectronDirectory()); os.IsNotExist(err) {
t.Fatalf("%v", err)
}

if _, err := os.Stat(p.ElectronDirectory()); os.IsNotExist(err) {
t.Fatalf("%v", err)
}

// Check Zip doesn't exist
if _, err := os.Stat(p.AstilectronDownloadDst()); !os.IsNotExist(err) {
t.Fatalf("%v", err)
}

if _, err := os.Stat(p.ElectronDownloadDst()); !os.IsNotExist(err) {
t.Fatalf("%v", err)
}
}

0 comments on commit 4a231d4

Please sign in to comment.