Skip to content

Commit

Permalink
minor bug fixes to file installer
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMcKenzie committed Dec 3, 2015
1 parent 52d13c1 commit 98b9328
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
3 changes: 2 additions & 1 deletion dropship/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
)

// Installer is an interface that allows different methods of writing
Expand All @@ -13,7 +14,7 @@ type Installer interface {
}

func moveOld(dest string) error {
return os.Rename(dest, filepath.Join(dest, ".old"))
return os.Rename(dest, strings.Join([]string{dest, "old"}, "."))
}

func cleanup(dest string, err error) error {
Expand Down
10 changes: 7 additions & 3 deletions dropship/installer_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import (
type FileInstaller struct{}

func (i FileInstaller) Install(dest string, f io.Reader) (count int, err error) {
err = moveOld(dest)
if err != nil {
return 0, err
// if file exists lets move it so we can recover on failure
if _, err := os.Stat(dest); err == nil {
err = moveOld(dest)
if err != nil {
return 0, err
}
defer cleanup(dest, err)
}

if f == nil {
Expand Down
10 changes: 9 additions & 1 deletion dropship/installer_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dropship
import (
"bytes"
"io"
"io/ioutil"
"os"
"testing"
)

Expand All @@ -21,7 +23,13 @@ func TestInstallFile(t *testing.T) {

var fileInstaller FileInstaller
for _, test := range cases {
count, err := fileInstaller.Install("/tmp/test.txt", test.file)
dir, err := ioutil.TempDir(".", "test")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir)

count, err := fileInstaller.Install(dir+"/test.txt", test.file)
if err != test.err {
t.Errorf("Install: Expected error to equal %v got: %v", test.err, err)
}
Expand Down

0 comments on commit 98b9328

Please sign in to comment.