Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve symbolic links for app dir #165

Merged
merged 3 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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