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 tests on runner #304

Merged
merged 3 commits into from
Aug 6, 2022
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
Next Next commit
chore: Add tests on runner
Signed-off-by: Valentin Kiselev <[email protected]>
  • Loading branch information
mrexox committed Aug 6, 2022
commit d0ef29f1061172472ca3f7d8f2cef8ba7eb00b95
4 changes: 3 additions & 1 deletion internal/lefthook/runner/execute_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"github.com/creack/pty"
)

func Execute(root string, args []string) (*bytes.Buffer, error) {
type CommandExecutor struct{}

func (e CommandExecutor) Execute(root string, args []string) (*bytes.Buffer, error) {
command := exec.Command("sh", "-c", strings.Join(args, " "))
rootDir, _ := filepath.Abs(root)
command.Dir = rootDir
Expand Down
4 changes: 3 additions & 1 deletion internal/lefthook/runner/execute_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"path/filepath"
)

func Execute(root string, args []string) (*bytes.Buffer, error) {
type CommandExecutor struct{}

func (e CommandExecutor) Execute(root string, args []string) (*bytes.Buffer, error) {
command := exec.Command(args[0], args[1:]...)
rootDir, _ := filepath.Abs(root)
command.Dir = rootDir
Expand Down
11 changes: 11 additions & 0 deletions internal/lefthook/runner/executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package runner

import (
"bytes"
)

// Executor provides an interface for command execution.
// It is used here for testing purpose mostly.
type Executor interface {
Execute(root string, args []string) (*bytes.Buffer, error)
}
4 changes: 3 additions & 1 deletion internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Runner struct {
args []string
failed bool
resultChan chan Result
exec Executor
}

func NewRunner(
Expand All @@ -45,6 +46,7 @@ func NewRunner(
hook: hook,
args: args,
resultChan: resultChan,
exec: CommandExecutor{},
}
}

Expand Down Expand Up @@ -270,7 +272,7 @@ func prepareFiles(command *config.Command, files []string) string {
}

func (r *Runner) run(name, root, failText string, args []string) {
out, err := Execute(root, args)
out, err := r.exec.Execute(root, args)

var execName string
if err != nil {
Expand Down
266 changes: 266 additions & 0 deletions internal/lefthook/runner/runner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
package runner

import (
"bytes"
"errors"
"fmt"
"path/filepath"
"testing"

"github.com/spf13/afero"

"github.com/evilmartians/lefthook/internal/config"
"github.com/evilmartians/lefthook/internal/git"
)

type TestExecutor struct{}

func (e TestExecutor) Execute(root string, args []string) (out *bytes.Buffer, err error) {
out = bytes.NewBuffer(make([]byte, 0))

if args[0] == "success" {
err = nil
} else {
err = errors.New(args[0])
}

return
}

func TestRunAll(t *testing.T) {
root, err := filepath.Abs("src")
if err != nil {
t.Errorf("unexpected error: %s", err)
}

gitPath := filepath.Join(root, ".git")
repo := &git.Repository{
HooksPath: filepath.Join(gitPath, "hooks"),
RootPath: root,
GitPath: gitPath,
InfoPath: filepath.Join(gitPath, "info"),
}

for i, tt := range [...]struct {
name string
args []string
scriptDirs []string
existingFiles []string
hook *config.Hook
success, fail []Result
}{
{
name: "empty hook",
hook: &config.Hook{
Commands: map[string]*config.Command{},
Scripts: map[string]*config.Script{},
Piped: true,
},
},
{
name: "with simple command",
hook: &config.Hook{
Commands: map[string]*config.Command{
"test": &config.Command{
Run: "success",
},
},
Scripts: map[string]*config.Script{},
},
success: []Result{{Name: "test", Status: StatusOk}},
},
{
name: "with multiple commands ran in parallel",
hook: &config.Hook{
Parallel: true,
Commands: map[string]*config.Command{
"test": &config.Command{
Run: "success",
},
"lint": &config.Command{
Run: "success",
},
"type-check": &config.Command{
Run: "fail",
},
},
Scripts: map[string]*config.Script{},
},
success: []Result{
{Name: "lint", Status: StatusOk},
{Name: "test", Status: StatusOk},
},
fail: []Result{{Name: "type-check", Status: StatusErr}},
},
{
name: "with exclude tags",
hook: &config.Hook{
ExcludeTags: []string{"tests"},
Commands: map[string]*config.Command{
"test": &config.Command{
Run: "success",
Tags: []string{"tests"},
},
"lint": &config.Command{
Run: "success",
Tags: []string{"linters"},
},
},
Scripts: map[string]*config.Script{},
},
success: []Result{{Name: "lint", Status: StatusOk}},
},
{
name: "with skip boolean option",
hook: &config.Hook{
Commands: map[string]*config.Command{
"test": &config.Command{
Run: "success",
Skip: true,
},
"lint": &config.Command{
Run: "success",
},
},
Scripts: map[string]*config.Script{},
},
success: []Result{{Name: "lint", Status: StatusOk}},
},
{
name: "with skip merge",
existingFiles: []string{
filepath.Join(gitPath, "MERGE_HEAD"),
},
hook: &config.Hook{
Commands: map[string]*config.Command{
"test": &config.Command{
Run: "success",
Skip: "merge",
},
"lint": &config.Command{
Run: "success",
},
},
Scripts: map[string]*config.Script{},
},
success: []Result{{Name: "lint", Status: StatusOk}},
},
{
name: "with skip rebase and merge in an array",
existingFiles: []string{
filepath.Join(gitPath, "rebase-merge"),
filepath.Join(gitPath, "rebase-apply"),
},
hook: &config.Hook{
Commands: map[string]*config.Command{
"test": &config.Command{
Run: "success",
Skip: []interface{}{"merge", "rebase"},
},
"lint": &config.Command{
Run: "success",
},
},
Scripts: map[string]*config.Script{},
},
success: []Result{{Name: "lint", Status: StatusOk}},
},
{
name: "with fail test",
hook: &config.Hook{
Commands: map[string]*config.Command{
"test": &config.Command{
Run: "fail",
FailText: "try 'success'",
},
},
Scripts: map[string]*config.Script{},
},
fail: []Result{{Name: "test", Status: StatusErr, Text: "try 'success'"}},
},
{
name: "with simple scripts",
scriptDirs: []string{
filepath.Join(root, config.DefaultSourceDir),
},
existingFiles: []string{
filepath.Join(root, config.DefaultSourceDir, "script.sh"),
filepath.Join(root, config.DefaultSourceDir, "failing.js"),
},
hook: &config.Hook{
Commands: map[string]*config.Command{},
Scripts: map[string]*config.Script{
"script.sh": &config.Script{
Runner: "success",
},
"failing.js": &config.Script{
Runner: "fail",
FailText: "install node",
},
},
},
success: []Result{{Name: "script.sh", Status: StatusOk}},
fail: []Result{{Name: "failing.js", Status: StatusErr, Text: "install node"}},
},
} {
fs := afero.NewMemMapFs()
repo.Fs = fs
resultChan := make(chan Result, len(tt.hook.Commands)+len(tt.hook.Scripts))
executor := TestExecutor{}
runner := &Runner{
fs: fs,
repo: repo,
hook: tt.hook,
args: tt.args,
resultChan: resultChan,
exec: executor,
}

for _, file := range tt.existingFiles {
if err := fs.MkdirAll(filepath.Base(file), 0o755); err != nil {
t.Errorf("unexpected error: %s", err)
}
if err := afero.WriteFile(fs, file, []byte{}, 0o755); err != nil {
t.Errorf("unexpected error: %s", err)
}
}

t.Run(fmt.Sprintf("%d: %s", i, tt.name), func(t *testing.T) {
runner.RunAll(tt.scriptDirs)
close(resultChan)

var success, fail []Result
for res := range resultChan {
if res.Status == StatusOk {
success = append(success, res)
} else {
fail = append(fail, res)
}
}

if !resultsEqual(success, tt.success) {
t.Errorf("success results are not matching")
}

if !resultsEqual(fail, tt.fail) {
t.Errorf("fail results are not matching")
}
})
}
}

func resultsEqual(a, b []Result) bool {
if len(a) != len(b) {
return false
}

for i, item := range a {
if item.Name != b[i].Name ||
item.Status != b[i].Status ||
item.Text != b[i].Text {
return false
}
}

return true
}