From 776b67d2cfd58561a853e5dc8984867a34b52869 Mon Sep 17 00:00:00 2001 From: Javier Romero Date: Sat, 13 Apr 2019 11:00:43 -0500 Subject: [PATCH] Display error when --path is invalid Signed-off-by: Javier Romero --- build.go | 10 ++++++++++ build_test.go | 37 ++++++++++++++++++++++++++++++++++++ run_test.go | 4 ++-- testdata/just-a-file.txt | 0 testdata/some-app/.gitignore | 1 + 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 testdata/just-a-file.txt create mode 100644 testdata/some-app/.gitignore diff --git a/build.go b/build.go index ecb38dbe99..7b8dbf504b 100644 --- a/build.go +++ b/build.go @@ -123,6 +123,16 @@ func (bf *BuildFactory) BuildConfigFromFlags(ctx context.Context, f *BuildFlags) if err != nil { return nil, err } + stat, err := os.Stat(appDir) + if err != nil { + if os.IsNotExist(err) { + return nil, errors.Errorf("app directory %s does not exist", style.Symbol(appDir)) + } else { + return nil, err + } + } else if !stat.IsDir() { + return nil, errors.Errorf("provided app directory %s is not a directory", style.Symbol(appDir)) + } f.RepoName = calculateRepositoryName(appDir, f) diff --git a/build_test.go b/build_test.go index 7426eec287..ff08346463 100644 --- a/build_test.go +++ b/build_test.go @@ -4,9 +4,11 @@ import ( "bytes" "context" "errors" + "fmt" "io/ioutil" "math/rand" "os" + "path/filepath" "testing" "time" @@ -368,5 +370,40 @@ PATH }) h.AssertNotEq(t, os.Getenv("PATH"), "") }) + + when("app path is invalid", func() { + when("app directory does not exist", func() { + it("return error", func() { + appDir := filepath.Join("testdata", "does-not-exist") + + config, err := factory.BuildConfigFromFlags(context.TODO(), &pack.BuildFlags{ + RepoName: "some/app", + Builder: "some/builder", + AppDir: appDir, + }) + + h.AssertNil(t, config) + h.AssertNotNil(t, err) + h.AssertContainsMatch(t, err.Error(), fmt.Sprintf("^app directory .*%s.* does not exist$", appDir)) + }) + }) + + when("app directory is a file", func() { + it("return error", func() { + appDir := filepath.Join("testdata", "just-a-file.txt") + + config, err := factory.BuildConfigFromFlags(context.TODO(), &pack.BuildFlags{ + RepoName: "some/app", + Builder: "some/builder", + AppDir: appDir, + }) + + h.AssertNil(t, config) + h.AssertNotNil(t, err) + + h.AssertContainsMatch(t, err.Error(), fmt.Sprintf("^provided app directory .*%s.* is not a directory$", appDir)) + }) + }) + }) }, spec.Parallel()) } diff --git a/run_test.go b/run_test.go index 4044021fda..fb6759d19d 100644 --- a/run_test.go +++ b/run_test.go @@ -81,7 +81,7 @@ func testRun(t *testing.T, when spec.G, it spec.S) { run, err := factory.RunConfigFromFlags(context.TODO(), &pack.RunFlags{ BuildFlags: pack.BuildFlags{ - AppDir: "acceptance/testdata/node_app", + AppDir: "testdata/some-app", Builder: "some/builder", RunImage: "some/run", }, @@ -89,7 +89,7 @@ func testRun(t *testing.T, when spec.G, it spec.S) { }) h.AssertNil(t, err) - absAppDir, _ := filepath.Abs("acceptance/testdata/node_app") + absAppDir, _ := filepath.Abs("testdata/some-app") absAppDirMd5 := fmt.Sprintf("pack.local/run/%x", md5.Sum([]byte(absAppDir))) h.AssertEq(t, run.RepoName, absAppDirMd5) h.AssertEq(t, run.Ports, []string{"1370"}) diff --git a/testdata/just-a-file.txt b/testdata/just-a-file.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testdata/some-app/.gitignore b/testdata/some-app/.gitignore new file mode 100644 index 0000000000..b722e9e13e --- /dev/null +++ b/testdata/some-app/.gitignore @@ -0,0 +1 @@ +!.gitignore \ No newline at end of file