From 1400d20bb9409981e9d505457c4fceb2ab92d9b0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 27 Jun 2013 22:14:23 -0400 Subject: [PATCH] builder/virtualbox: Error if output directory already exists --- builder/virtualbox/builder.go | 4 ++++ builder/virtualbox/builder_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/builder/virtualbox/builder.go b/builder/virtualbox/builder.go index 69ff257e8..4bbc04b1e 100644 --- a/builder/virtualbox/builder.go +++ b/builder/virtualbox/builder.go @@ -164,6 +164,10 @@ func (b *Builder) Prepare(raws ...interface{}) error { } } + if _, err := os.Stat(b.config.OutputDir); err == nil { + errs = append(errs, errors.New("Output directory already exists. It must not exist.")) + } + if b.config.RawBootWait != "" { b.config.BootWait, err = time.ParseDuration(b.config.RawBootWait) if err != nil { diff --git a/builder/virtualbox/builder_test.go b/builder/virtualbox/builder_test.go index 46163c44f..e509cbcc9 100644 --- a/builder/virtualbox/builder_test.go +++ b/builder/virtualbox/builder_test.go @@ -224,6 +224,31 @@ func TestBuilderPrepare_ISOUrl(t *testing.T) { } } +func TestBuilderPrepare_OutputDir(t *testing.T) { + var b Builder + config := testConfig() + + // Test with existing dir + dir, err := ioutil.TempDir("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(dir) + + config["output_directory"] = dir + err = b.Prepare(config) + if err == nil { + t.Fatal("should have error") + } + + // Test with a good one + config["output_directory"] = "i-hope-i-dont-exist" + err = b.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } +} + func TestBuilderPrepare_ShutdownTimeout(t *testing.T) { var b Builder config := testConfig()