Skip to content

Commit

Permalink
test: improve executable not found checks
Browse files Browse the repository at this point in the history
extracted from #3795
  • Loading branch information
caarlos0 committed Jun 16, 2023
1 parent 3eecfdd commit e65098c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
10 changes: 8 additions & 2 deletions internal/pipe/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package build
import (
"errors"
"os"
"os/exec"
"path/filepath"
"testing"

Expand Down Expand Up @@ -209,13 +210,18 @@ func TestRunPipeFailingHooks(t *testing.T) {
ctx := testctx.NewWithCfg(cfg, testctx.WithCurrentTag("2.4.5"))
ctx.Config.Builds[0].Hooks.Pre = []config.Hook{{Cmd: "exit 1"}}
ctx.Config.Builds[0].Hooks.Post = []config.Hook{{Cmd: "echo post"}}
require.EqualError(t, Pipe{}.Run(ctx), "pre hook failed: failed to run 'exit 1': exec: \"exit\": executable file not found in $PATH")

err := Pipe{}.Run(ctx)
require.ErrorIs(t, err, exec.ErrNotFound)
require.Contains(t, err.Error(), "pre hook failed")
})
t.Run("post-hook", func(t *testing.T) {
ctx := testctx.NewWithCfg(cfg, testctx.WithCurrentTag("2.4.5"))
ctx.Config.Builds[0].Hooks.Pre = []config.Hook{{Cmd: "echo pre"}}
ctx.Config.Builds[0].Hooks.Post = []config.Hook{{Cmd: "exit 1"}}
require.EqualError(t, Pipe{}.Run(ctx), `post hook failed: failed to run 'exit 1': exec: "exit": executable file not found in $PATH`)
err := Pipe{}.Run(ctx)
require.ErrorIs(t, err, exec.ErrNotFound)
require.Contains(t, err.Error(), "post hook failed")
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func process(ctx *context.Context, docker config.Docker, artifacts []*artifact.A
if err := imagers[docker.Use].Build(ctx, tmp, images, buildFlags); err != nil {
if isFileNotFoundError(err.Error()) {
var files []string
_ = filepath.Walk(tmp, func(path string, info fs.FileInfo, err error) error {
_ = filepath.Walk(tmp, func(_ string, info fs.FileInfo, _ error) error {
if info.IsDir() {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/pipe/sign/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func TestSignArtifacts(t *testing.T) {
user string
}{
{
desc: "sign cmd not found",
expectedErrMsg: `sign: not-a-valid-cmd failed: exec: "not-a-valid-cmd": executable file not found in $PATH: `,
desc: "sign cmd not found",
expectedErrIs: exec.ErrNotFound,
ctx: testctx.NewWithCfg(config.Project{
Signs: []config.Sign{
{
Expand Down
8 changes: 6 additions & 2 deletions internal/pipe/universalbinary/universalbinary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,18 @@ func TestRun(t *testing.T) {
ctx := ctx5
ctx.Config.UniversalBinaries[0].Hooks.Pre = []config.Hook{{Cmd: "exit 1"}}
ctx.Config.UniversalBinaries[0].Hooks.Post = []config.Hook{{Cmd: "echo post"}}
require.EqualError(t, Pipe{}.Run(ctx), "pre hook failed: failed to run 'exit 1': exec: \"exit\": executable file not found in $PATH")
err := Pipe{}.Run(ctx)
require.ErrorIs(t, err, exec.ErrNotFound)
require.Contains(t, err.Error(), "pre hook failed")
})

t.Run("failing post-hook", func(t *testing.T) {
ctx := ctx5
ctx.Config.UniversalBinaries[0].Hooks.Pre = []config.Hook{{Cmd: "echo pre"}}
ctx.Config.UniversalBinaries[0].Hooks.Post = []config.Hook{{Cmd: "exit 1"}}
require.EqualError(t, Pipe{}.Run(ctx), "post hook failed: failed to run 'exit 1': exec: \"exit\": executable file not found in $PATH")
err := Pipe{}.Run(ctx)
require.ErrorIs(t, err, exec.ErrNotFound)
require.Contains(t, err.Error(), "post hook failed")
})

t.Run("hook with env tmpl", func(t *testing.T) {
Expand Down

0 comments on commit e65098c

Please sign in to comment.