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

Commit

Permalink
Merge pull request #14 from remove-bg/create-output-directory
Browse files Browse the repository at this point in the history
Create output directory if required
  • Loading branch information
groe authored Jun 7, 2020
2 parents a5c2b7d + c46f103 commit 530c6ef
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
5 changes: 5 additions & 0 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func NewProcessor(apiKey string, version string) Processor {
}

func (p Processor) Process(rawInputPaths []string, settings Settings) {
err := p.Storage.MkdirP(settings.OutputDirectory)
if err != nil {
log.Fatal(err)
}

inputPaths, err := p.Storage.ExpandPaths(rawInputPaths)
if err != nil {
log.Fatal(err)
Expand Down
7 changes: 7 additions & 0 deletions processor/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ var _ = Describe("Processor", func() {
Expect(clientArg1).To(Equal("dir/image1.jpg"))
})

It("create the output directory", func() {
subject.Process([]string{"dir/*.jpg"}, testSettings)

Expect(fakeStorage.MkdirPCallCount()).To(Equal(1))
Expect(fakeStorage.MkdirPArgsForCall(0)).To(Equal(testSettings.OutputDirectory))
})

It("coordinates the HTTP request and writing the result", func() {
fakeClient.RemoveFromFileReturnsOnCall(0, []byte("Processed1"), mimePng, nil)
fakeClient.RemoveFromFileReturnsOnCall(1, []byte("Processed2"), mimePng, nil)
Expand Down
9 changes: 9 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type StorageInterface interface {
Write(path string, data []byte) error
FileExists(path string) bool
ExpandPaths(originalPaths []string) ([]string, error)
MkdirP(path string) error
}

type FileStorage struct {
Expand Down Expand Up @@ -52,3 +53,11 @@ func (FileStorage) ExpandPaths(originalPaths []string) ([]string, error) {

return resolvedPaths, nil
}

func (FileStorage) MkdirP(path string) error {
if len(path) == 0 {
return nil
}

return os.MkdirAll(path, 0755)
}
26 changes: 25 additions & 1 deletion storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package storage_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"io/ioutil"
"os"
"path"
"runtime"

Expand Down Expand Up @@ -91,4 +92,27 @@ var _ = Describe("FileStorage", func() {
})
})
})

Describe("MkdirP", func() {
var tmpDir string

BeforeEach(func() {
dir, err := ioutil.TempDir("", "mkdirp-spec")
Expect(err).ToNot(HaveOccurred())

tmpDir = dir
})

AfterEach(func() {
os.RemoveAll(tmpDir)
})

It("creates deeply nested directories, if they don't exist", func() {
outputDir := path.Join(tmpDir, "nested1/nested2")

Expect(outputDir).ToNot(BeADirectory())
Expect(subject.MkdirP(outputDir)).Should(Succeed())
Expect(outputDir).To(BeADirectory())
})
})
})
73 changes: 73 additions & 0 deletions storage/storagefakes/fake_storage_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 530c6ef

Please sign in to comment.