Skip to content

Commit

Permalink
Merge pull request #165 from jromero/feature/93
Browse files Browse the repository at this point in the history
Resolve symbolic links for app dir
  • Loading branch information
ekcasey authored May 6, 2019
2 parents 0a5888a + 77c7e7f commit 8ab44f6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
23 changes: 18 additions & 5 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,32 @@ func (c *Client) validateImageReference(imageName string) (name.Reference, error
}

func (c *Client) processAppDir(appDir string) (string, error) {
var (
resolvedAppDir = appDir
err error
)

if appDir == "" {
var err error
appDir, err = os.Getwd()
if err != nil {
if appDir, err = os.Getwd(); err != nil {
return "", err
}
}
if fi, err := os.Stat(appDir); err != nil {

if resolvedAppDir, err = filepath.EvalSymlinks(appDir); err != nil {
return "", err
}

if resolvedAppDir, err = filepath.Abs(resolvedAppDir); err != nil {
return "", err
}

if fi, err := os.Stat(resolvedAppDir); err != nil {
return "", err
} else if !fi.IsDir() {
return "", fmt.Errorf("%s is not a directory", appDir)
}
return filepath.Abs(appDir)

return resolvedAppDir, nil
}

func (c *Client) processBuildpacks(buildpacks []string) ([]buildpack.Buildpack, builder.GroupMetadata, error) {
Expand Down
69 changes: 69 additions & 0 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,75 @@ func testBuild(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)
h.AssertEq(t, fakeLifecycle.Opts.AppDir, absPath)
})

when("appDir is a symlink", func() {
var (
appDirName = "some-app"
absoluteAppDir string
tmpDir string
err error
)

it.Before(func() {
tmpDir, err = ioutil.TempDir("", "build-symlink-test")
h.AssertNil(t, err)

appDirPath := filepath.Join(tmpDir, appDirName)
h.AssertNil(t, os.MkdirAll(filepath.Join(tmpDir, appDirName), 0666))

absoluteAppDir, err = filepath.Abs(appDirPath)
h.AssertNil(t, err)

absoluteAppDir, err = filepath.EvalSymlinks(appDirPath)
h.AssertNil(t, err)
})

it.After(func() {
_ = os.RemoveAll(tmpDir)
})

it("resolves relative symbolic links", func() {
relLink := filepath.Join(tmpDir, "some-app.link")
h.AssertNil(t, os.Symlink(filepath.Join(".", appDirName), relLink))

h.AssertNil(t, subject.Build(context.TODO(), BuildOptions{
Image: "some/app",
AppDir: relLink,
}))

h.AssertEq(t, fakeLifecycle.Opts.AppDir, absoluteAppDir)
})

it("resolves absolute symbolic links", func() {
relLink := filepath.Join(tmpDir, "some-app.link")
h.AssertNil(t, os.Symlink(absoluteAppDir, relLink))

h.AssertNil(t, subject.Build(context.TODO(), BuildOptions{
Image: "some/app",
AppDir: relLink,
}))

h.AssertEq(t, fakeLifecycle.Opts.AppDir, absoluteAppDir)
})

it("resolves symbolic links recursively", func() {
linkRef1 := absoluteAppDir
absoluteLink1 := filepath.Join(tmpDir, "some-app-abs-1.link")

linkRef2 := "some-app-abs-1.link"
symbolicLink := filepath.Join(tmpDir, "some-app-rel-2.link")

h.AssertNil(t, os.Symlink(linkRef1, absoluteLink1))
h.AssertNil(t, os.Symlink(linkRef2, symbolicLink))

h.AssertNil(t, subject.Build(context.TODO(), BuildOptions{
Image: "some/app",
AppDir: symbolicLink,
}))

h.AssertEq(t, fakeLifecycle.Opts.AppDir, absoluteAppDir)
})
})
})

when("Builder option", func() {
Expand Down

0 comments on commit 8ab44f6

Please sign in to comment.