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

Add WorkingDir() to return image working directory #141

Merged
merged 1 commit into from
Apr 25, 2022
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
4 changes: 2 additions & 2 deletions fakes/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ func (i *Image) ReusedLayers() []string {
return i.reusedLayers
}

func (i *Image) WorkingDir() string {
return i.workingDir
func (i *Image) WorkingDir() (string, error) {
return i.workingDir, nil
}

func (i *Image) AddPreviousLayer(sha, path string) {
Expand Down
1 change: 1 addition & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Image interface {
SetLabel(string, string) error
RemoveLabel(string) error
Env(key string) (string, error)
WorkingDir() (string, error)
Entrypoint() ([]string, error)
SetEnv(string, string) error
SetEntrypoint(...string) error
Expand Down
4 changes: 4 additions & 0 deletions local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ func (i *Image) Env(key string) (string, error) {
return "", nil
}

func (i *Image) WorkingDir() (string, error) {
return i.inspect.Config.WorkingDir, nil
}

func (i *Image) Entrypoint() ([]string, error) {
return i.inspect.Config.Entrypoint, nil
}
Expand Down
54 changes: 54 additions & 0 deletions local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,60 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
})
})

when("#WorkingDir", func() {
when("image exists", func() {
var repoName = newTestImageName()

it.Before(func() {
existingImage, err := local.NewImage(repoName, dockerClient)
h.AssertNil(t, err)

h.AssertNil(t, existingImage.SetWorkingDir("/testWorkingDir"))
h.AssertNil(t, existingImage.Save())
})

it.After(func() {
h.AssertNil(t, h.DockerRmi(dockerClient, repoName))
})

it("returns the WorkingDir value", func() {
img, err := local.NewImage(repoName, dockerClient, local.FromBaseImage(repoName))
h.AssertNil(t, err)

val, err := img.WorkingDir()
h.AssertNil(t, err)

h.AssertEq(t, val, "/testWorkingDir")
})

it("returns empty string for missing WorkingDir", func() {
existingImage, err := local.NewImage(repoName, dockerClient)
h.AssertNil(t, err)
h.AssertNil(t, existingImage.Save())

img, err := local.NewImage(repoName, dockerClient, local.FromBaseImage(repoName))
h.AssertNil(t, err)

val, err := img.WorkingDir()
h.AssertNil(t, err)
var expected string
h.AssertEq(t, val, expected)
})
})

when("image NOT exists", func() {
it("returns empty string", func() {
img, err := local.NewImage(newTestImageName(), dockerClient)
h.AssertNil(t, err)

val, err := img.WorkingDir()
h.AssertNil(t, err)
var expected string
h.AssertEq(t, val, expected)
})
})
})

when("#Entrypoint", func() {
when("image exists", func() {
var repoName = newTestImageName()
Expand Down
11 changes: 11 additions & 0 deletions remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,17 @@ func (i *Image) Env(key string) (string, error) {
return "", nil
}

func (i *Image) WorkingDir() (string, error) {
cfg, err := i.image.ConfigFile()
if err != nil {
return "", errors.Wrapf(err, "getting config file for image %q", i.repoName)
}
if cfg == nil {
return "", fmt.Errorf("missing config for image %q", i.repoName)
}
return cfg.Config.WorkingDir, nil
}

func (i *Image) Entrypoint() ([]string, error) {
cfg, err := i.image.ConfigFile()
if err != nil {
Expand Down
50 changes: 50 additions & 0 deletions remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,56 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
})
})

when("#WorkingDir", func() {
when("image exists", func() {
var repoName = newTestImageName()

it.Before(func() {
baseImage, err := remote.NewImage(repoName, authn.DefaultKeychain)
h.AssertNil(t, err)

h.AssertNil(t, baseImage.SetWorkingDir("/testWorkingDir"))
h.AssertNil(t, baseImage.Save())
})

it("returns the WorkingDir value", func() {
img, err := remote.NewImage(repoName, authn.DefaultKeychain, remote.FromBaseImage(repoName))
h.AssertNil(t, err)

val, err := img.WorkingDir()
h.AssertNil(t, err)

h.AssertEq(t, val, "/testWorkingDir")
})

it("returns empty string for a missing WorkingDir", func() {
baseImage, err := remote.NewImage(repoName, authn.DefaultKeychain)
h.AssertNil(t, err)
h.AssertNil(t, baseImage.Save())

img, err := remote.NewImage(repoName, authn.DefaultKeychain, remote.FromBaseImage(repoName))
h.AssertNil(t, err)

val, err := img.WorkingDir()
h.AssertNil(t, err)
var expected string
h.AssertEq(t, val, expected)
})
})

when("image NOT exists", func() {
it("returns empty string", func() {
img, err := remote.NewImage(repoName, authn.DefaultKeychain)
h.AssertNil(t, err)

val, err := img.WorkingDir()
h.AssertNil(t, err)
var expected string
h.AssertEq(t, val, expected)
})
})
})

when("#Entrypoint", func() {
when("image exists with entrypoint", func() {
var repoName = newTestImageName()
Expand Down