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 1 commit
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
31 changes: 26 additions & 5 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,41 @@ func (c *Client) validateImageReference(imageName string) (name.Reference, error
return ref, nil
}

func mapAppDirError(dir string, err error) error {
jromero marked this conversation as resolved.
Show resolved Hide resolved
if os.IsNotExist(err) {
return errors.Errorf("app directory %s does not exist", style.Symbol(dir))
} else {
return err
}
}

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 "", mapAppDirError(appDir, err)
}

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

if fi, err := os.Stat(resolvedAppDir); err != nil {
return "", mapAppDirError(appDir, 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
63 changes: 63 additions & 0 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,69 @@ func testBuild(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, err)
h.AssertEq(t, fakeLifecycle.Opts.AppDir, absPath)
})

it("resolves relative symbolic links", func() {
appDir := filepath.Join("testdata", "some-app")
absoluteAppDir, err := filepath.Abs(appDir)
h.AssertNil(t, err)

relLinkRef := "./some-app"
relLink := filepath.Join("testdata", "some-app.link")

_ = os.Remove(relLink)
h.AssertNil(t, os.Symlink(relLinkRef, relLink))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to make temporary file for testing we should make them in a temp dir instead of in the repo. We generally only use the testdata dir if we are going to commit the files.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Full disclosure: I did feel a tad dirty doing it this way.

I initially attempted to follow the prior practice of using fixtures but when it came time to submit the PR I remembered that git doesn't play nice with symlinks. I obviously should have choose a different path. Thanks for the redirection. Hope it's better now.


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() {
appDir := filepath.Join("testdata", "some-app")
absoluteAppDir, err := filepath.Abs(appDir)
h.AssertNil(t, err)

relLinkRef := absoluteAppDir
relLink := filepath.Join("testdata", "some-app.link")

_ = os.Remove(relLink)
h.AssertNil(t, os.Symlink(relLinkRef, 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() {
appDir := filepath.Join("testdata", "some-app")
absoluteAppDir, err := filepath.Abs(appDir)
h.AssertNil(t, err)

linkRef1 := absoluteAppDir
absoluteLink1 := filepath.Join("testdata", "some-app-abs-1.link")

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

_ = os.Remove(absoluteLink1)
_ = os.Remove(relLink2)

h.AssertNil(t, os.Symlink(linkRef1, absoluteLink1))
jromero marked this conversation as resolved.
Show resolved Hide resolved
h.AssertNil(t, os.Symlink(linkRef2, relLink2))

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

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

when("Builder option", func() {
Expand Down
1 change: 1 addition & 0 deletions testdata/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.link
jromero marked this conversation as resolved.
Show resolved Hide resolved