Skip to content

Commit

Permalink
Resolve issue with gexec executable names being unguessable
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Moran authored and ryanmoran committed Mar 8, 2021
1 parent cbc7f72 commit d2e6638
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
16 changes: 3 additions & 13 deletions pexec/executable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ func testPexec(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect

fakeCLI string
existingPath string
tmpDir string
stdout, stderr *bytes.Buffer

Expand All @@ -39,18 +37,10 @@ func testPexec(t *testing.T, context spec.G, it spec.S) {
stdout = bytes.NewBuffer(nil)
stderr = bytes.NewBuffer(nil)

executable = pexec.NewExecutable("some-executable")

fakeCLI, err = gexec.Build("github.com/paketo-buildpacks/packit/fakes/some-executable")
Expect(err).NotTo(HaveOccurred())

existingPath = os.Getenv("PATH")
os.Setenv("PATH", filepath.Dir(fakeCLI))
executable = pexec.NewExecutable(filepath.Base(fakeCLI))
})

it.After(func() {
os.Setenv("PATH", existingPath)
gexec.CleanupBuildArtifacts()
Expect(os.RemoveAll(tmpDir)).To(Succeed())
})

Expand Down Expand Up @@ -119,12 +109,12 @@ func testPexec(t *testing.T, context spec.G, it spec.S) {
context("failure cases", func() {
context("when the executable cannot be found on the path", func() {
it.Before(func() {
Expect(os.Unsetenv("PATH")).To(Succeed())
executable = pexec.NewExecutable("unknown-executable")
})

it("returns an error", func() {
err := executable.Execute(pexec.Execution{})
Expect(err).To(MatchError("exec: \"some-executable\": executable file not found in $PATH"))
Expect(err).To(MatchError("exec: \"unknown-executable\": executable file not found in $PATH"))
})
})

Expand Down
25 changes: 25 additions & 0 deletions pexec/init_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
package pexec_test

import (
"os"
"path/filepath"
"testing"

"github.com/onsi/gomega/gexec"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

. "github.com/onsi/gomega"
)

var (
existingPath string
fakeCLI string
)

func TestUnitExec(t *testing.T) {
var Expect = NewWithT(t).Expect

suite := spec.New("packit/pexec", spec.Report(report.Terminal{}))
suite("pexec", testPexec)

var err error
fakeCLI, err = gexec.Build("github.com/paketo-buildpacks/packit/fakes/some-executable")
Expect(err).NotTo(HaveOccurred())

existingPath = os.Getenv("PATH")
os.Setenv("PATH", filepath.Dir(fakeCLI))

t.Cleanup(func() {
os.Setenv("PATH", existingPath)
gexec.CleanupBuildArtifacts()
})

suite.Run(t)
}

0 comments on commit d2e6638

Please sign in to comment.