-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: Add tests on runner Signed-off-by: Valentin Kiselev <mrexox@evilmartians.com> * fix: Linting issues Signed-off-by: Valentin Kiselev <mrexox@evilmartians.com> * chore: Update goreleaser ldflags with commit hash Signed-off-by: Valentin Kiselev <mrexox@evilmartians.com>
- Loading branch information
Showing
9 changed files
with
302 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | ||
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": { | ||
Run: "success", | ||
}, | ||
"lint": { | ||
Run: "success", | ||
}, | ||
"type-check": { | ||
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": { | ||
Run: "success", | ||
Tags: []string{"tests"}, | ||
}, | ||
"lint": { | ||
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": { | ||
Run: "success", | ||
Skip: true, | ||
}, | ||
"lint": { | ||
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": { | ||
Run: "success", | ||
Skip: "merge", | ||
}, | ||
"lint": { | ||
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": { | ||
Run: "success", | ||
Skip: []interface{}{"merge", "rebase"}, | ||
}, | ||
"lint": { | ||
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": { | ||
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": { | ||
Runner: "success", | ||
}, | ||
"failing.js": { | ||
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 | ||
} |