Skip to content

Commit

Permalink
Merge pull request #32 from rockstaedt/1-visualize-after-5072-limit
Browse files Browse the repository at this point in the history
Resolve Visualize after 50/72 limit
  • Loading branch information
rockstaedt authored Jul 26, 2023
2 parents 49ce682 + 0b2e335 commit 146f6f1
Show file tree
Hide file tree
Showing 16 changed files with 440 additions and 157 deletions.
56 changes: 56 additions & 0 deletions cmd/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import (
"github.com/TwiN/go-color"
"io"
"log"
"rockstaedt/commit-message-check/internal/model"
)

type Handler struct {
Config model.Config
Writer io.Writer
}

func NewHandler(config model.Config) *Handler {
return &Handler{Config: config}
}

func (h *Handler) Run(command string) int {
var status int

switch command {
case "setup":
status = h.setup()
case "uninstall":
status = h.uninstall()
case "update":
status = h.update()
case "validate":
status = h.validate()
default:
log.Println("Unknown subcommand. Please check manual.")
return 1
}

return status
}

func (h *Handler) notify(message string, txtColor ...string) {
if len(txtColor) > 0 && txtColor[0] == "green" {
message = color.InGreen(message)
}

if len(txtColor) > 0 && txtColor[0] == "red" {
message = color.InRed(message)
}

if len(txtColor) > 0 && txtColor[0] == "yellow" {
message = color.InYellow(message)
}

_, err := h.Writer.Write([]byte(message + "\n"))
if err != nil {
log.Println("Error at writing!")
}
}
135 changes: 135 additions & 0 deletions cmd/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package cmd

import (
"bytes"
"errors"
"github.com/TwiN/go-color"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"log"
"os"
"rockstaedt/commit-message-check/internal/model"
"rockstaedt/commit-message-check/testdata/mocks"
"testing"
)

func TestRun(t *testing.T) {
buffer := &bytes.Buffer{}
log.SetOutput(buffer)

t.Run("executes uninstall command", func(t *testing.T) {
buffer.Reset()
myHandler := NewHandler(model.Config{GitPath: "/"})
myHandler.Writer = buffer

status := myHandler.Run("uninstall")

assert.Contains(t, buffer.String(), "Could not delete")
assert.True(t, status > 0)
})

t.Run("executes setup command", func(t *testing.T) {
buffer.Reset()
protectedPath := t.TempDir() + "/fake"
err := os.Mkdir(protectedPath, 0000)
assert.Nil(t, err)
myHandler := NewHandler(model.Config{GitPath: protectedPath})
myHandler.Writer = buffer

status := myHandler.Run("setup")

assert.Contains(t, buffer.String(), "Could not create")
assert.True(t, status > 0)
})

t.Run("executes update command", func(t *testing.T) {
buffer.Reset()
myHandler := NewHandler(model.Config{})
myHandler.Writer = buffer

status := myHandler.Run("update")

assert.Contains(t, buffer.String(), "Error at retrieving")
assert.True(t, status > 0)
})

t.Run("executes validate command", func(t *testing.T) {

buffer.Reset()
testFile := t.TempDir() + "/text.txt"
err := os.WriteFile(testFile, []byte("i am a commit msg"), 0666)
assert.Nil(t, err)
myHandler := NewHandler(model.Config{CommitMsgFile: testFile})

myHandler.Run("validate")

assert.Equal(t, 0, buffer.Len())
})

t.Run("prints warning when any other command", func(t *testing.T) {
buffer.Reset()
myHandler := NewHandler(model.Config{})

status := myHandler.Run("unknown")

want := "Unknown subcommand. Please check manual."
assert.Contains(t, buffer.String(), want)
assert.Equal(t, 1, status)
})
}

func TestNotify(t *testing.T) {
fwm := &mocks.FakeWriterMock{}
handler := NewHandler(model.Config{})
handler.Writer = fwm

t.Run("writes a message to the writer", func(t *testing.T) {
fwm.ResetCalls()
fwm.On("Write", mock.Anything).Return(1, nil)

handler.notify("I am a message")

fwm.AssertCalled(t, "Write", []byte("I am a message\n"))
})

t.Run("colorize text in", func(t *testing.T) {

t.Run("green", func(t *testing.T) {
fwm.ResetCalls()
fwm.On("Write", mock.Anything).Return(1, nil)

handler.notify("I am a message", "green")

fwm.AssertCalled(t, "Write", []byte(color.Green+"I am a message"+color.Reset+"\n"))
})

t.Run("red", func(t *testing.T) {
fwm.ResetCalls()
fwm.On("Write", mock.Anything).Return(1, nil)

handler.notify("I am a message", "red")

fwm.AssertCalled(t, "Write", []byte(color.Red+"I am a message"+color.Reset+"\n"))
})

t.Run("yellow", func(t *testing.T) {
fwm.ResetCalls()
fwm.On("Write", mock.Anything).Return(1, nil)

handler.notify("I am", "yellow")

fwm.AssertCalled(t, "Write", []byte(color.Yellow+"I am"+color.Reset+"\n"))
})
})

t.Run("handles error at writing", func(t *testing.T) {
buffer := &bytes.Buffer{}
log.SetOutput(buffer)
fwm.ResetCalls()
fwm.On("Write", mock.Anything).Return(0, errors.New("error at writing"))

handler.notify("this causes an error")

assert.Contains(t, buffer.String(), "Error at writing!")
})
}
9 changes: 4 additions & 5 deletions cmd/setup.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package cmd

import (
"log"
"rockstaedt/commit-message-check/util"
)

func Setup(gitPath string) int {
err := util.WalkHookDirs(gitPath, util.CreateHook)
func (h *Handler) setup() int {
err := util.WalkHookDirs(h.Config.GitPath, util.CreateHook)
if err != nil {
log.Println("[ERROR]\t Could not create commit-msg script.")
h.notify("Could not create commit-msg script.", "red")
return 1
}

log.Println("[SUCCESS]\t commit-message-check successfully installed.")
h.notify("commit-message-check successfully installed.", "green")
return 0
}
31 changes: 17 additions & 14 deletions cmd/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,56 @@ package cmd
import (
"bytes"
"fmt"
"github.com/TwiN/go-color"
"github.com/stretchr/testify/assert"
"log"
"os"
"rockstaedt/commit-message-check/internal/model"
"testing"
)

func TestSetup(t *testing.T) {
buffer := &bytes.Buffer{}
log.SetOutput(buffer)

t.Run("returns 0 and", func(t *testing.T) {

createDirs := func() string {
fakeHandler := func() *Handler {
path := t.TempDir()
err := os.Mkdir(fmt.Sprintf("%s/hooks", path), os.ModePerm)
assert.Nil(t, err)

return path
}
handler := NewHandler(model.Config{GitPath: path})
handler.Writer = buffer

t.Run("creates commit-msg script in hook folder", func(t *testing.T) {
path := createDirs()
return handler
}()

status := Setup(path)
t.Run("creates commit-msg script in hook folder", func(t *testing.T) {
status := fakeHandler.setup()

assert.Equal(t, 0, status)
assert.FileExists(t, fmt.Sprintf("%s/hooks/commit-msg", path))
assert.FileExists(t, fmt.Sprintf("%s/hooks/commit-msg", fakeHandler.Config.GitPath))
})

t.Run("logs a success message", func(t *testing.T) {
buffer.Reset()
path := createDirs()

_ = Setup(path)
_ = fakeHandler.setup()

assert.Contains(t, buffer.String(), "[SUCCESS]\t commit-message-check successfully installed.")
assert.Contains(t, buffer.String(), color.Green+"commit-message-check successfully installed.")
})
})

t.Run("returns 1 when error at walking hooks and logs it", func(t *testing.T) {
buffer.Reset()
errPath := t.TempDir()
err := os.Mkdir(fmt.Sprintf("%s/hooks", errPath), 0000)
assert.Nil(t, err)
handler := NewHandler(model.Config{GitPath: errPath})
handler.Writer = buffer

status := Setup(errPath)
status := handler.setup()

assert.Equal(t, 1, status)
assert.Contains(t, buffer.String(), "[ERROR]\t Could not create commit-msg script.")
assert.Contains(t, buffer.String(), color.Red+"Could not create commit-msg script.")
})
}
9 changes: 4 additions & 5 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package cmd

import (
"log"
"rockstaedt/commit-message-check/util"
)

func Uninstall(gitPath string) int {
err := util.WalkHookDirs(gitPath, util.DeleteHook)
func (h *Handler) uninstall() int {
err := util.WalkHookDirs(h.Config.GitPath, util.DeleteHook)
if err != nil {
log.Println("[ERROR]\t Could not delete commit-msg hook.")
h.notify("Could not delete commit-msg hook.", "red")
return 1
}

log.Println("[SUCCESS]\t commit-message-check successfully uninstalled.")
h.notify("commit-message-check successfully uninstalled.", "green")
return 0
}
28 changes: 17 additions & 11 deletions cmd/uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package cmd
import (
"bytes"
"fmt"
"github.com/TwiN/go-color"
"github.com/stretchr/testify/assert"
"log"
"os"
"rockstaedt/commit-message-check/internal/model"
"testing"
)

func TestUninstall(t *testing.T) {
buffer := &bytes.Buffer{}
log.SetOutput(buffer)

createDirs := func() string {
createFakeHandlerWithDirs := func() *Handler {
path := t.TempDir()
err := os.Mkdir(fmt.Sprintf("%s/hooks", path), os.ModePerm)
assert.Nil(t, err)
Expand All @@ -24,28 +24,32 @@ func TestUninstall(t *testing.T) {
_, err = os.Create(fmt.Sprintf("%s/xyz/commit-msg", path))
assert.Nil(t, err)

return path
handler := NewHandler(model.Config{GitPath: path})
handler.Writer = buffer

return handler
}

t.Run("returns 0 and", func(t *testing.T) {

t.Run("removes all occurrences of commit-msg", func(t *testing.T) {
path := createDirs()
handler := createFakeHandlerWithDirs()

status := Uninstall(path)
status := handler.uninstall()

path := handler.Config.GitPath
assert.Equal(t, 0, status)
assert.NoFileExists(t, fmt.Sprintf("%s/hooks/commit-msg", path))
assert.FileExists(t, fmt.Sprintf("%s/xyz/commit-msg", path))
})

t.Run("logs a success message", func(t *testing.T) {
buffer.Reset()
path := createDirs()
handler := createFakeHandlerWithDirs()

_ = Uninstall(path)
_ = handler.uninstall()

assert.Contains(t, buffer.String(), "[SUCCESS]\t commit-message-check successfully uninstalled.")
assert.Contains(t, buffer.String(), color.Green+"commit-message-check successfully uninstalled.")
})
})

Expand All @@ -54,10 +58,12 @@ func TestUninstall(t *testing.T) {
errPath := t.TempDir()
err := os.Mkdir(fmt.Sprintf("%s/hooks", errPath), 0000)
assert.Nil(t, err)
handler := NewHandler(model.Config{GitPath: errPath})
handler.Writer = buffer

status := Uninstall(errPath)
status := handler.uninstall()

assert.Equal(t, 1, status)
assert.Contains(t, buffer.String(), "[ERROR]\t Could not delete commit-msg hook.")
assert.Contains(t, buffer.String(), color.Red+"Could not delete commit-msg hook.")
})
}
Loading

0 comments on commit 146f6f1

Please sign in to comment.