-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from rockstaedt/1-visualize-after-5072-limit
Resolve Visualize after 50/72 limit
- Loading branch information
Showing
16 changed files
with
440 additions
and
157 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
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!") | ||
} | ||
} |
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,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!") | ||
}) | ||
} |
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 |
---|---|---|
@@ -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 | ||
} |
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 |
---|---|---|
@@ -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 | ||
} |
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
Oops, something went wrong.