Skip to content

Commit 146f6f1

Browse files
authored
Merge pull request #32 from rockstaedt/1-visualize-after-5072-limit
Resolve Visualize after 50/72 limit
2 parents 49ce682 + 0b2e335 commit 146f6f1

File tree

16 files changed

+440
-157
lines changed

16 files changed

+440
-157
lines changed

cmd/handler.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cmd
2+
3+
import (
4+
"github.com/TwiN/go-color"
5+
"io"
6+
"log"
7+
"rockstaedt/commit-message-check/internal/model"
8+
)
9+
10+
type Handler struct {
11+
Config model.Config
12+
Writer io.Writer
13+
}
14+
15+
func NewHandler(config model.Config) *Handler {
16+
return &Handler{Config: config}
17+
}
18+
19+
func (h *Handler) Run(command string) int {
20+
var status int
21+
22+
switch command {
23+
case "setup":
24+
status = h.setup()
25+
case "uninstall":
26+
status = h.uninstall()
27+
case "update":
28+
status = h.update()
29+
case "validate":
30+
status = h.validate()
31+
default:
32+
log.Println("Unknown subcommand. Please check manual.")
33+
return 1
34+
}
35+
36+
return status
37+
}
38+
39+
func (h *Handler) notify(message string, txtColor ...string) {
40+
if len(txtColor) > 0 && txtColor[0] == "green" {
41+
message = color.InGreen(message)
42+
}
43+
44+
if len(txtColor) > 0 && txtColor[0] == "red" {
45+
message = color.InRed(message)
46+
}
47+
48+
if len(txtColor) > 0 && txtColor[0] == "yellow" {
49+
message = color.InYellow(message)
50+
}
51+
52+
_, err := h.Writer.Write([]byte(message + "\n"))
53+
if err != nil {
54+
log.Println("Error at writing!")
55+
}
56+
}

cmd/handler_test.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"github.com/TwiN/go-color"
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/mock"
9+
"log"
10+
"os"
11+
"rockstaedt/commit-message-check/internal/model"
12+
"rockstaedt/commit-message-check/testdata/mocks"
13+
"testing"
14+
)
15+
16+
func TestRun(t *testing.T) {
17+
buffer := &bytes.Buffer{}
18+
log.SetOutput(buffer)
19+
20+
t.Run("executes uninstall command", func(t *testing.T) {
21+
buffer.Reset()
22+
myHandler := NewHandler(model.Config{GitPath: "/"})
23+
myHandler.Writer = buffer
24+
25+
status := myHandler.Run("uninstall")
26+
27+
assert.Contains(t, buffer.String(), "Could not delete")
28+
assert.True(t, status > 0)
29+
})
30+
31+
t.Run("executes setup command", func(t *testing.T) {
32+
buffer.Reset()
33+
protectedPath := t.TempDir() + "/fake"
34+
err := os.Mkdir(protectedPath, 0000)
35+
assert.Nil(t, err)
36+
myHandler := NewHandler(model.Config{GitPath: protectedPath})
37+
myHandler.Writer = buffer
38+
39+
status := myHandler.Run("setup")
40+
41+
assert.Contains(t, buffer.String(), "Could not create")
42+
assert.True(t, status > 0)
43+
})
44+
45+
t.Run("executes update command", func(t *testing.T) {
46+
buffer.Reset()
47+
myHandler := NewHandler(model.Config{})
48+
myHandler.Writer = buffer
49+
50+
status := myHandler.Run("update")
51+
52+
assert.Contains(t, buffer.String(), "Error at retrieving")
53+
assert.True(t, status > 0)
54+
})
55+
56+
t.Run("executes validate command", func(t *testing.T) {
57+
58+
buffer.Reset()
59+
testFile := t.TempDir() + "/text.txt"
60+
err := os.WriteFile(testFile, []byte("i am a commit msg"), 0666)
61+
assert.Nil(t, err)
62+
myHandler := NewHandler(model.Config{CommitMsgFile: testFile})
63+
64+
myHandler.Run("validate")
65+
66+
assert.Equal(t, 0, buffer.Len())
67+
})
68+
69+
t.Run("prints warning when any other command", func(t *testing.T) {
70+
buffer.Reset()
71+
myHandler := NewHandler(model.Config{})
72+
73+
status := myHandler.Run("unknown")
74+
75+
want := "Unknown subcommand. Please check manual."
76+
assert.Contains(t, buffer.String(), want)
77+
assert.Equal(t, 1, status)
78+
})
79+
}
80+
81+
func TestNotify(t *testing.T) {
82+
fwm := &mocks.FakeWriterMock{}
83+
handler := NewHandler(model.Config{})
84+
handler.Writer = fwm
85+
86+
t.Run("writes a message to the writer", func(t *testing.T) {
87+
fwm.ResetCalls()
88+
fwm.On("Write", mock.Anything).Return(1, nil)
89+
90+
handler.notify("I am a message")
91+
92+
fwm.AssertCalled(t, "Write", []byte("I am a message\n"))
93+
})
94+
95+
t.Run("colorize text in", func(t *testing.T) {
96+
97+
t.Run("green", func(t *testing.T) {
98+
fwm.ResetCalls()
99+
fwm.On("Write", mock.Anything).Return(1, nil)
100+
101+
handler.notify("I am a message", "green")
102+
103+
fwm.AssertCalled(t, "Write", []byte(color.Green+"I am a message"+color.Reset+"\n"))
104+
})
105+
106+
t.Run("red", func(t *testing.T) {
107+
fwm.ResetCalls()
108+
fwm.On("Write", mock.Anything).Return(1, nil)
109+
110+
handler.notify("I am a message", "red")
111+
112+
fwm.AssertCalled(t, "Write", []byte(color.Red+"I am a message"+color.Reset+"\n"))
113+
})
114+
115+
t.Run("yellow", func(t *testing.T) {
116+
fwm.ResetCalls()
117+
fwm.On("Write", mock.Anything).Return(1, nil)
118+
119+
handler.notify("I am", "yellow")
120+
121+
fwm.AssertCalled(t, "Write", []byte(color.Yellow+"I am"+color.Reset+"\n"))
122+
})
123+
})
124+
125+
t.Run("handles error at writing", func(t *testing.T) {
126+
buffer := &bytes.Buffer{}
127+
log.SetOutput(buffer)
128+
fwm.ResetCalls()
129+
fwm.On("Write", mock.Anything).Return(0, errors.New("error at writing"))
130+
131+
handler.notify("this causes an error")
132+
133+
assert.Contains(t, buffer.String(), "Error at writing!")
134+
})
135+
}

cmd/setup.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package cmd
22

33
import (
4-
"log"
54
"rockstaedt/commit-message-check/util"
65
)
76

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

15-
log.Println("[SUCCESS]\t commit-message-check successfully installed.")
14+
h.notify("commit-message-check successfully installed.", "green")
1615
return 0
1716
}

cmd/setup_test.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,56 @@ package cmd
33
import (
44
"bytes"
55
"fmt"
6+
"github.com/TwiN/go-color"
67
"github.com/stretchr/testify/assert"
7-
"log"
88
"os"
9+
"rockstaedt/commit-message-check/internal/model"
910
"testing"
1011
)
1112

1213
func TestSetup(t *testing.T) {
1314
buffer := &bytes.Buffer{}
14-
log.SetOutput(buffer)
1515

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

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

23-
return path
24-
}
23+
handler := NewHandler(model.Config{GitPath: path})
24+
handler.Writer = buffer
2525

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

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

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

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

39-
_ = Setup(path)
39+
_ = fakeHandler.setup()
4040

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

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

50-
status := Setup(errPath)
53+
status := handler.setup()
5154

5255
assert.Equal(t, 1, status)
53-
assert.Contains(t, buffer.String(), "[ERROR]\t Could not create commit-msg script.")
56+
assert.Contains(t, buffer.String(), color.Red+"Could not create commit-msg script.")
5457
})
5558
}

cmd/uninstall.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package cmd
22

33
import (
4-
"log"
54
"rockstaedt/commit-message-check/util"
65
)
76

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

15-
log.Println("[SUCCESS]\t commit-message-check successfully uninstalled.")
14+
h.notify("commit-message-check successfully uninstalled.", "green")
1615
return 0
1716
}

cmd/uninstall_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ package cmd
33
import (
44
"bytes"
55
"fmt"
6+
"github.com/TwiN/go-color"
67
"github.com/stretchr/testify/assert"
7-
"log"
88
"os"
9+
"rockstaedt/commit-message-check/internal/model"
910
"testing"
1011
)
1112

1213
func TestUninstall(t *testing.T) {
1314
buffer := &bytes.Buffer{}
14-
log.SetOutput(buffer)
1515

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

27-
return path
27+
handler := NewHandler(model.Config{GitPath: path})
28+
handler.Writer = buffer
29+
30+
return handler
2831
}
2932

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

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

35-
status := Uninstall(path)
38+
status := handler.uninstall()
3639

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

4246
t.Run("logs a success message", func(t *testing.T) {
4347
buffer.Reset()
44-
path := createDirs()
48+
handler := createFakeHandlerWithDirs()
4549

46-
_ = Uninstall(path)
50+
_ = handler.uninstall()
4751

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

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

58-
status := Uninstall(errPath)
64+
status := handler.uninstall()
5965

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

0 commit comments

Comments
 (0)