Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianfeldmann committed Jan 9, 2024
1 parent 1daad47 commit ab63d2b
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 1 deletion.
26 changes: 26 additions & 0 deletions exec/printer/default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package printer

import (
"github.com/captainhook-go/captainhook/configuration"
"github.com/captainhook-go/captainhook/events"
"github.com/captainhook-go/captainhook/hooks/app"
"github.com/captainhook-go/captainhook/test"
"testing"
)

func TestDefaultHookSuccess(t *testing.T) {
io := test.CreateFakeIO()
conf := test.CreateFakeConfig()
repo := test.CreateFakeRepo()
ctx := app.NewContext(io, conf, repo)
hook := &configuration.Hook{}
p := NewDefaultPrinter(io)

event := events.NewHookStartedEvent(ctx, hook)

p.HookStarted(event)

if len(io.Out) < 1 {
t.Errorf("Should have written something")
}
}
21 changes: 21 additions & 0 deletions git/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package config

import (
"github.com/captainhook-go/captainhook/git/types"
"testing"
)

func TestGet(t *testing.T) {
g := types.NewCmd("config")
g.AddOptions(Get("user.name"))

if len(g.Options) < 3 {
t.Errorf("Option not added correctly")
}
if g.Options[1] != "--get" {
t.Errorf("Wrong option")
}
if g.Options[2] != "user.name" {
t.Errorf("Wrong option")
}
}
18 changes: 18 additions & 0 deletions git/diff/diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package diff

import (
"github.com/captainhook-go/captainhook/git/types"
"testing"
)

func TestCached(t *testing.T) {
g := types.NewCmd("diff")
g.AddOptions(Cached)

if len(g.Options) < 2 {
t.Errorf("Option not added correctly")
}
if g.Options[1] != "--cached" {
t.Errorf("Wrong option")
}
}
11 changes: 11 additions & 0 deletions git/difftree/difftree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package difftree

import (
"testing"
)

func TestNothing(t *testing.T) {
if true == false {
t.Errorf("This is weired")
}
}
18 changes: 18 additions & 0 deletions git/log/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package log

import (
"github.com/captainhook-go/captainhook/git/types"
"testing"
)

func TestAbbrevRev(t *testing.T) {
g := types.NewCmd("log")
g.AddOptions(AbbrevCommit)

if len(g.Options) < 2 {
t.Errorf("Option not added correctly")
}
if g.Options[1] != "--abbrev-commit" {
t.Errorf("Wrong option")
}
}
18 changes: 18 additions & 0 deletions git/revparse/revparse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package revparse

import (
"github.com/captainhook-go/captainhook/git/types"
"testing"
)

func TestAbbrevRev(t *testing.T) {
g := types.NewCmd("revparse")
g.AddOptions(AbbrevRef)

if len(g.Options) < 2 {
t.Errorf("Option not added correctly")
}
if g.Options[1] != "--abbrev-ref" {
t.Errorf("Wrong option")
}
}
14 changes: 14 additions & 0 deletions git/types/message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package types

import (
"testing"
)

func TestSubject(t *testing.T) {
expected := "Foo bar"
m := NewCommitMessage("Foo bar\n\nFiz baz.", "#")

if m.Subject() != expected {
t.Errorf("Wrong subject, expected '" + expected + "' got '" + m.Subject() + "'")
}
}
9 changes: 9 additions & 0 deletions test/helper.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package test

import (
"context"
"github.com/captainhook-go/captainhook/configuration"
"github.com/captainhook-go/captainhook/git"
"github.com/captainhook-go/captainhook/git/types"
"github.com/captainhook-go/captainhook/hooks/app"
"github.com/captainhook-go/captainhook/io"
"strings"
)

func CreateFakeIO() *IOMock {
Expand All @@ -22,3 +25,9 @@ func CreateFakeRepo() *RepoMock {
func CreateFakeHookContext(inOut io.IO, conf *configuration.Configuration, repo git.Repo) *app.Context {
return app.NewContext(inOut, conf, repo)
}

func CreateFakeExecutor() types.Executor {
return func(ctx context.Context, name string, debug bool, args ...string) (string, error) {
return name + " " + strings.Join(args, " "), nil
}
}
3 changes: 2 additions & 1 deletion test/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "github.com/captainhook-go/captainhook/io"
type IOMock struct {
stdIn []string
args map[string]string
Out []string
}

func (inOut *IOMock) SetStdIn(input []string) {
Expand Down Expand Up @@ -56,7 +57,7 @@ func (inOut *IOMock) IsVerbose() bool {
}

func (inOut *IOMock) Write(message string, newline bool, verbosity int) {

inOut.Out = append(inOut.Out, message)
}

func (inOut *IOMock) Ask(message string, defaultValue string) string {
Expand Down

0 comments on commit ab63d2b

Please sign in to comment.