Skip to content

Commit

Permalink
create stemcell dir if it does not exist
Browse files Browse the repository at this point in the history
[#148885503](https://www.pivotaltracker.com/story/show/148885503)

Signed-off-by: Natalie Arellano <narellano@pivotal.io>
  • Loading branch information
davidjahn authored and natalieparellano committed Jul 18, 2017
1 parent 95ce977 commit 02aca20
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ func ValidateFlags() []error {
add(errors.New("missing required argument 'output'"))
}
fi, err := os.Stat(OutputDir)
if err != nil || fi == nil {
if err != nil && os.IsNotExist(err) {
if err = os.Mkdir(OutputDir, 0700); err != nil {
add(err)
}
} else if err != nil || fi == nil {
add(fmt.Errorf("error opening output directory (%s): %s\n", OutputDir, err))
} else if !fi.IsDir() {
add(fmt.Errorf("output argument (%s): is not a directory\n", OutputDir))
Expand All @@ -195,7 +199,7 @@ func ValidateFlags() []error {
name := filepath.Join(OutputDir, StemcellFilename(Version, OSVersion))
Debugf("validating that stemcell filename (%s) does not exist", name)
if _, err := os.Stat(name); !os.IsNotExist(err) {
add(fmt.Errorf("file (%s) already exists - refusing to overwrite", name))
add(fmt.Errorf("error with output file (%s): %v (file may already exist)", name, err))
}

return errs
Expand Down
57 changes: 45 additions & 12 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,63 @@ func TestValidateVersion(t *testing.T) {
}
}

func errorArrayContains(errors []error, s string) bool {
for _, e := range errors {
if strings.Contains(e.Error(), s) {
return true
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false
return true, err
}

func TestMissingOutputDirectoryAddsToErrorMessage(t *testing.T) {
testCommand := "stembuild -vhd arbitrary.vhd -delta arbitrary.patch -v 1200.666 -output idontexist"
func TestMissingOutputDirectoryCreatesDirectory(t *testing.T) {
// Setup output directory
testOutputDir, err := TempDir("testOutputDir-")
if err != nil {
t.Fatal(err)
}
os.RemoveAll(testOutputDir)
dirExists, _ := exists(testOutputDir)
if dirExists {
t.Errorf("%s already exists, not a valid test", testOutputDir)
}
// defer os.RemoveAll(testOutputDir)

// Setup input vhd and vmdk
testInputDir, err := TempDir("testInputDir-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(testInputDir)
testEmptyFilePath := filepath.Join(testInputDir, "testEmptyFile.txt")
testEmptyFile, err := os.Create(testEmptyFilePath)
if err != nil {
t.Fatal(err)
}
testEmptyFile.Close()

testCommand := fmt.Sprintf(
"stembuild -vhd %s -delta %s -v 1200.666 -output %s",
testEmptyFilePath,
testEmptyFilePath,
testOutputDir,
)
testArgs := strings.Split(testCommand, " ")
os.Args = testArgs
Init()
ParseFlags()

errs := ValidateFlags()

if len(errs) == 0 {
t.Errorf("expected error, but got nothing")
if len(errs) != 0 {
t.Errorf("expected no errors, but got errors: %s", errs)
}

if !errorArrayContains(errs, "idontexist") {
t.Errorf("expected error string to contain output directory but got: %v", errs)
dirExists, _ = exists(testOutputDir)
if !dirExists {
t.Errorf("%s was not created", testOutputDir)
}
}

Expand Down

0 comments on commit 02aca20

Please sign in to comment.