From 02aca2055108b764cfbfebf65357c75a964d1f0b Mon Sep 17 00:00:00 2001 From: David Jahn Date: Tue, 18 Jul 2017 14:11:13 -0400 Subject: [PATCH] create stemcell dir if it does not exist [#148885503](https://www.pivotaltracker.com/story/show/148885503) Signed-off-by: Natalie Arellano --- main.go | 8 ++++++-- main_test.go | 57 +++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 4c07dbde..546b962f 100644 --- a/main.go +++ b/main.go @@ -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)) @@ -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 diff --git a/main_test.go b/main_test.go index 73d12d2e..d42cc8ec 100644 --- a/main_test.go +++ b/main_test.go @@ -57,17 +57,49 @@ 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() @@ -75,12 +107,13 @@ func TestMissingOutputDirectoryAddsToErrorMessage(t *testing.T) { 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) } }