diff --git a/build.go b/build.go index ecb38dbe9..7b8dbf504 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 7426eec28..b30ee0fa6 100644 --- a/build_test.go +++ b/build_test.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "math/rand" "os" + "path/filepath" "testing" "time" @@ -368,5 +369,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(), "^app directory .*does-not-exist.* does not exist$") + }) + }) + + 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(), "^provided app directory .*just-a-file.txt.* is not a directory$") + }) + }) + }) }, spec.Parallel()) } diff --git a/run_test.go b/run_test.go index 4044021fd..fb6759d19 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 000000000..e69de29bb diff --git a/testdata/some-app/.gitignore b/testdata/some-app/.gitignore new file mode 100644 index 000000000..b722e9e13 --- /dev/null +++ b/testdata/some-app/.gitignore @@ -0,0 +1 @@ +!.gitignore \ No newline at end of file