From 85fcda510a9b327682b1ec0298de827b442f19d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 18:09:13 +0200 Subject: [PATCH 01/45] Define test cases for new handler --- cmd/handler_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 cmd/handler_test.go diff --git a/cmd/handler_test.go b/cmd/handler_test.go new file mode 100644 index 0000000..20af960 --- /dev/null +++ b/cmd/handler_test.go @@ -0,0 +1,26 @@ +package cmd + +import "testing" + +func TestHandler(t *testing.T) { + + t.Run("executes uninstall command", func(t *testing.T) { + t.Skip() + }) + + t.Run("executes setup command", func(t *testing.T) { + t.Skip() + }) + + t.Run("executes update command", func(t *testing.T) { + t.Skip() + }) + + t.Run("executes validate command", func(t *testing.T) { + t.Skip() + }) + + t.Run("prints warning when any other command", func(t *testing.T) { + t.Skip() + }) +} From 29439d535b42d7f9b640815d0c6c081d21c189bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 18:14:01 +0200 Subject: [PATCH 02/45] Implement happy handler --- cmd/handler.go | 13 +++++++++++++ cmd/handler_test.go | 8 ++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 cmd/handler.go diff --git a/cmd/handler.go b/cmd/handler.go new file mode 100644 index 0000000..816e410 --- /dev/null +++ b/cmd/handler.go @@ -0,0 +1,13 @@ +package cmd + +type Handler struct { + Command string +} + +func NewHandler(command string) *Handler { + return &Handler{Command: command} +} + +func (h *Handler) Run() { + +} diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 20af960..c58eea9 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -1,11 +1,15 @@ package cmd -import "testing" +import ( + "testing" +) func TestHandler(t *testing.T) { t.Run("executes uninstall command", func(t *testing.T) { - t.Skip() + myHandler := NewHandler("uninstall") + + myHandler.Run() }) t.Run("executes setup command", func(t *testing.T) { From 3bcc41b102a0ba16fe6a97178b85e14fc558f954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 18:19:29 +0200 Subject: [PATCH 03/45] Rename structor to broaden purpose --- cmd/update.go | 6 +++--- cmd/update_test.go | 14 +++++++------- internal/model/{update_config.go => config.go} | 2 +- main.go | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) rename internal/model/{update_config.go => config.go} (82%) diff --git a/cmd/update.go b/cmd/update.go index e7b6800..1f1d4b1 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -15,7 +15,7 @@ type responseData struct { TagName string `json:"tag_name"` } -func Update(config *model.UpdateConfig) int { +func Update(config *model.Config) int { config.LatestVersion = getLatestTag(config.TagUrl) if config.LatestVersion == "" { log.Println("Error at retrieving latest version.") @@ -49,7 +49,7 @@ func getLatestTag(url string) string { return data.TagName } -func downloadScript(config *model.UpdateConfig) int { +func downloadScript(config *model.Config) int { file, err := os.Create(config.DownloadPath + "/commit-message-check") if err != nil { return 1 @@ -69,7 +69,7 @@ func downloadScript(config *model.UpdateConfig) int { return 0 } -func getBinaryUrl(config *model.UpdateConfig) string { +func getBinaryUrl(config *model.Config) string { return fmt.Sprintf( "%s/commit-message-check-%s-%s-%s", config.BinaryBaseUrl, diff --git a/cmd/update_test.go b/cmd/update_test.go index 8bc85ae..c9e9a3e 100644 --- a/cmd/update_test.go +++ b/cmd/update_test.go @@ -23,7 +23,7 @@ func TestUpdate(t *testing.T) { buffer.Reset() ts := httptest.NewServer(getHandlerFor(`{"tag_name":"v1.0.0"}`)) defer ts.Close() - config := &model.UpdateConfig{Version: "v1.0.0", TagUrl: ts.URL} + config := &model.Config{Version: "v1.0.0", TagUrl: ts.URL} status := Update(config) @@ -35,7 +35,7 @@ func TestUpdate(t *testing.T) { ts := httptest.NewServer(getHandlerFor(`{"tag_name":"v1.1.0"}`)) defer ts.Close() tempDir := t.TempDir() - config := &model.UpdateConfig{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: tempDir} + config := &model.Config{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: tempDir} _ = Update(config) @@ -47,7 +47,7 @@ func TestUpdate(t *testing.T) { buffer.Reset() ts := httptest.NewServer(getHandlerFor("", 500)) defer ts.Close() - config := &model.UpdateConfig{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: ""} + config := &model.Config{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: ""} status := Update(config) @@ -129,7 +129,7 @@ func TestDownloadScript(t *testing.T) { } })) defer ts.Close() - config := &model.UpdateConfig{LatestVersion: "v1.1.1", DownloadPath: tempDir, BinaryBaseUrl: ts.URL} + config := &model.Config{LatestVersion: "v1.1.1", DownloadPath: tempDir, BinaryBaseUrl: ts.URL} status := downloadScript(config) @@ -140,7 +140,7 @@ func TestDownloadScript(t *testing.T) { }) t.Run("returns 1 when error at creating file", func(t *testing.T) { - config := &model.UpdateConfig{DownloadPath: getProtectedPath(t)} + config := &model.Config{DownloadPath: getProtectedPath(t)} status := downloadScript(config) @@ -149,7 +149,7 @@ func TestDownloadScript(t *testing.T) { t.Run("return 2 when http protocol error", func(t *testing.T) { tempDir := t.TempDir() - config := &model.UpdateConfig{DownloadPath: tempDir, BinaryBaseUrl: "/xxx"} + config := &model.Config{DownloadPath: tempDir, BinaryBaseUrl: "/xxx"} status := downloadScript(config) @@ -162,7 +162,7 @@ func TestDownloadScript(t *testing.T) { w.WriteHeader(500) })) defer ts.Close() - config := &model.UpdateConfig{DownloadPath: tempDir, BinaryBaseUrl: ts.URL} + config := &model.Config{DownloadPath: tempDir, BinaryBaseUrl: ts.URL} status := downloadScript(config) diff --git a/internal/model/update_config.go b/internal/model/config.go similarity index 82% rename from internal/model/update_config.go rename to internal/model/config.go index 6fd24e8..b2bab44 100644 --- a/internal/model/update_config.go +++ b/internal/model/config.go @@ -1,6 +1,6 @@ package model -type UpdateConfig struct { +type Config struct { Version string LatestVersion string TagUrl string diff --git a/main.go b/main.go index 94eb842..a1b7830 100644 --- a/main.go +++ b/main.go @@ -53,7 +53,7 @@ func main() { case "uninstall": status = cmd.Uninstall(gitPath) case "update": - config := &model.UpdateConfig{ + config := &model.Config{ Version: version, TagUrl: "https://api.github.com/repos/rockstaedt/commit-message-check/releases/latest", BinaryBaseUrl: "https://github.com/rockstaedt/commit-message-check/releases/latest/download/", From 0279aeeb0220e4b0a1d6177febaeae789cf43483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 18:22:17 +0200 Subject: [PATCH 04/45] Built handler upon config struct --- cmd/handler.go | 8 +++++--- cmd/handler_test.go | 4 +++- internal/model/config.go | 2 ++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index 816e410..d445029 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -1,11 +1,13 @@ package cmd +import "rockstaedt/commit-message-check/internal/model" + type Handler struct { - Command string + Config model.Config } -func NewHandler(command string) *Handler { - return &Handler{Command: command} +func NewHandler(config model.Config) *Handler { + return &Handler{Config: config} } func (h *Handler) Run() { diff --git a/cmd/handler_test.go b/cmd/handler_test.go index c58eea9..32a6d82 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -1,13 +1,15 @@ package cmd import ( + "rockstaedt/commit-message-check/internal/model" "testing" ) func TestHandler(t *testing.T) { t.Run("executes uninstall command", func(t *testing.T) { - myHandler := NewHandler("uninstall") + config := model.Config{Command: "uninstall", GitPath: "/:"} + myHandler := NewHandler(config) myHandler.Run() }) diff --git a/internal/model/config.go b/internal/model/config.go index b2bab44..e8910e4 100644 --- a/internal/model/config.go +++ b/internal/model/config.go @@ -6,4 +6,6 @@ type Config struct { TagUrl string BinaryBaseUrl string DownloadPath string + GitPath string + Command string } From 39054aa79ea396a5b124c983a015a4457f0905ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 18:24:17 +0200 Subject: [PATCH 05/45] Execute uninstall command --- cmd/handler.go | 2 +- cmd/handler_test.go | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/handler.go b/cmd/handler.go index d445029..29ad6fb 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -11,5 +11,5 @@ func NewHandler(config model.Config) *Handler { } func (h *Handler) Run() { - + Uninstall(h.Config.GitPath) } diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 32a6d82..4e2b9e9 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -1,6 +1,9 @@ package cmd import ( + "bytes" + "github.com/stretchr/testify/assert" + "log" "rockstaedt/commit-message-check/internal/model" "testing" ) @@ -8,10 +11,14 @@ import ( func TestHandler(t *testing.T) { t.Run("executes uninstall command", func(t *testing.T) { + buffer := &bytes.Buffer{} + log.SetOutput(buffer) config := model.Config{Command: "uninstall", GitPath: "/:"} myHandler := NewHandler(config) myHandler.Run() + + assert.Contains(t, buffer.String(), "Could not delete") }) t.Run("executes setup command", func(t *testing.T) { From dc53e47772236d641d308dd85018ecc01726c52b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 18:24:55 +0200 Subject: [PATCH 06/45] Fix fake git path --- cmd/handler_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 4e2b9e9..e2cbfe4 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -13,7 +13,7 @@ func TestHandler(t *testing.T) { t.Run("executes uninstall command", func(t *testing.T) { buffer := &bytes.Buffer{} log.SetOutput(buffer) - config := model.Config{Command: "uninstall", GitPath: "/:"} + config := model.Config{Command: "uninstall", GitPath: "/"} myHandler := NewHandler(config) myHandler.Run() From 18c3c40009d2a7abdf72c750735c2a6c13c9df90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 18:27:27 +0200 Subject: [PATCH 07/45] Exceute setup command --- cmd/handler.go | 5 +++++ cmd/handler_test.go | 13 ++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index 29ad6fb..bca9d11 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -11,5 +11,10 @@ func NewHandler(config model.Config) *Handler { } func (h *Handler) Run() { + + if h.Config.Command == "setup" { + Setup(h.Config.GitPath) + } + Uninstall(h.Config.GitPath) } diff --git a/cmd/handler_test.go b/cmd/handler_test.go index e2cbfe4..be43a9f 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -9,10 +9,11 @@ import ( ) func TestHandler(t *testing.T) { + buffer := &bytes.Buffer{} + log.SetOutput(buffer) t.Run("executes uninstall command", func(t *testing.T) { - buffer := &bytes.Buffer{} - log.SetOutput(buffer) + buffer.Reset() config := model.Config{Command: "uninstall", GitPath: "/"} myHandler := NewHandler(config) @@ -22,7 +23,13 @@ func TestHandler(t *testing.T) { }) t.Run("executes setup command", func(t *testing.T) { - t.Skip() + buffer.Reset() + config := model.Config{Command: "setup", GitPath: t.TempDir()} + myHandler := NewHandler(config) + + myHandler.Run() + + assert.Contains(t, buffer.String(), "successfully") }) t.Run("executes update command", func(t *testing.T) { From f28916bcf0dc6e992cef68f11bf722b9ba1eaa86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 18:30:59 +0200 Subject: [PATCH 08/45] Execute update command --- cmd/handler.go | 9 ++++++--- cmd/handler_test.go | 8 +++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index bca9d11..b193842 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -12,9 +12,12 @@ func NewHandler(config model.Config) *Handler { func (h *Handler) Run() { - if h.Config.Command == "setup" { + switch h.Config.Command { + case "setup": Setup(h.Config.GitPath) + case "uninstall": + Uninstall(h.Config.GitPath) + case "update": + Update(&h.Config) } - - Uninstall(h.Config.GitPath) } diff --git a/cmd/handler_test.go b/cmd/handler_test.go index be43a9f..b26e9b2 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -33,7 +33,13 @@ func TestHandler(t *testing.T) { }) t.Run("executes update command", func(t *testing.T) { - t.Skip() + buffer.Reset() + config := model.Config{Command: "update"} + myHandler := NewHandler(config) + + myHandler.Run() + + assert.Contains(t, buffer.String(), "Error at retrieving") }) t.Run("executes validate command", func(t *testing.T) { From 36e00e0dab3b4a1c3f3297309a840d703cb0b4bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 18:39:42 +0200 Subject: [PATCH 09/45] Execute validate command --- cmd/handler.go | 10 +++++++++- cmd/handler_test.go | 13 ++++++++++++- internal/model/config.go | 5 +++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index b193842..55997aa 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -1,6 +1,10 @@ package cmd -import "rockstaedt/commit-message-check/internal/model" +import ( + "github.com/rockstaedt/txtreader" + "os" + "rockstaedt/commit-message-check/internal/model" +) type Handler struct { Config model.Config @@ -19,5 +23,9 @@ func (h *Handler) Run() { Uninstall(h.Config.GitPath) case "update": Update(&h.Config) + case "validate": + commitLines, _ := txtreader.GetLinesFromTextFile(os.Args[2]) + + Validate(commitLines) } } diff --git a/cmd/handler_test.go b/cmd/handler_test.go index b26e9b2..0a3dfed 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -4,6 +4,7 @@ import ( "bytes" "github.com/stretchr/testify/assert" "log" + "os" "rockstaedt/commit-message-check/internal/model" "testing" ) @@ -43,7 +44,17 @@ func TestHandler(t *testing.T) { }) t.Run("executes validate command", func(t *testing.T) { - t.Skip() + buffer.Reset() + dir := t.TempDir() + testFile := dir + "/text.txt" + err := os.WriteFile(testFile, []byte("i am a commit msg"), 0666) + assert.Nil(t, err) + config := model.Config{Command: "validate", CommitMsg: testFile} + myHandler := NewHandler(config) + + myHandler.Run() + + assert.Contains(t, buffer.String(), "Valid commit message") }) t.Run("prints warning when any other command", func(t *testing.T) { diff --git a/internal/model/config.go b/internal/model/config.go index e8910e4..9581f1b 100644 --- a/internal/model/config.go +++ b/internal/model/config.go @@ -1,11 +1,12 @@ package model type Config struct { + Command string + CommitMsg string + GitPath string Version string LatestVersion string TagUrl string BinaryBaseUrl string DownloadPath string - GitPath string - Command string } From fbe84f4ced190e0604ef03a7f71601b33779e4b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 18:50:07 +0200 Subject: [PATCH 10/45] Handle error at reading commit message --- cmd/handler.go | 8 ++++++-- cmd/handler_test.go | 33 ++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index 55997aa..e1c978d 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -2,7 +2,7 @@ package cmd import ( "github.com/rockstaedt/txtreader" - "os" + "log" "rockstaedt/commit-message-check/internal/model" ) @@ -24,7 +24,11 @@ func (h *Handler) Run() { case "update": Update(&h.Config) case "validate": - commitLines, _ := txtreader.GetLinesFromTextFile(os.Args[2]) + commitLines, err := txtreader.GetLinesFromTextFile(h.Config.CommitMsg) + if err != nil { + log.Printf("Could not read commit message: %q", err.Error()) + break + } Validate(commitLines) } diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 0a3dfed..4709d2c 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -44,17 +44,32 @@ func TestHandler(t *testing.T) { }) t.Run("executes validate command", func(t *testing.T) { - buffer.Reset() - dir := t.TempDir() - testFile := dir + "/text.txt" - err := os.WriteFile(testFile, []byte("i am a commit msg"), 0666) - assert.Nil(t, err) - config := model.Config{Command: "validate", CommitMsg: testFile} - myHandler := NewHandler(config) - myHandler.Run() + t.Run("success", func(t *testing.T) { + buffer.Reset() + dir := t.TempDir() + testFile := dir + "/text.txt" + err := os.WriteFile(testFile, []byte("i am a commit msg"), 0666) + assert.Nil(t, err) + config := model.Config{Command: "validate", CommitMsg: testFile} + myHandler := NewHandler(config) + + myHandler.Run() + + assert.Contains(t, buffer.String(), "Valid commit message") + }) + + t.Run("error at reading file", func(t *testing.T) { + buffer.Reset() + config := model.Config{Command: "validate", CommitMsg: "/no_file"} + myHandler := NewHandler(config) + + myHandler.Run() - assert.Contains(t, buffer.String(), "Valid commit message") + want := `Could not read commit message: "file not found"` + assert.Contains(t, buffer.String(), want) + assert.NotContains(t, buffer.String(), "Valid") + }) }) t.Run("prints warning when any other command", func(t *testing.T) { From 61d65a0422bc436eef9878b6c542a499bb67a382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 18:52:27 +0200 Subject: [PATCH 11/45] Print warning when unknown subcommand --- cmd/handler.go | 2 ++ cmd/handler_test.go | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cmd/handler.go b/cmd/handler.go index e1c978d..1353172 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -31,5 +31,7 @@ func (h *Handler) Run() { } Validate(commitLines) + default: + log.Println("Unknown subcommand. Please check manual.") } } diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 4709d2c..24317db 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -73,6 +73,13 @@ func TestHandler(t *testing.T) { }) t.Run("prints warning when any other command", func(t *testing.T) { - t.Skip() + buffer.Reset() + config := model.Config{Command: "unknown"} + myHandler := NewHandler(config) + + myHandler.Run() + + want := "Unknown subcommand. Please check manual." + assert.Contains(t, buffer.String(), want) }) } From fd0e9c592ab78a9c465bc33c8792ba6ceccbda69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 19:02:11 +0200 Subject: [PATCH 12/45] Return OS status code --- cmd/handler.go | 14 +++++++++----- cmd/handler_test.go | 22 +++++++++++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index 1353172..4ee7160 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -14,24 +14,28 @@ func NewHandler(config model.Config) *Handler { return &Handler{Config: config} } -func (h *Handler) Run() { +func (h *Handler) Run() int { + var status int switch h.Config.Command { case "setup": - Setup(h.Config.GitPath) + status = Setup(h.Config.GitPath) case "uninstall": - Uninstall(h.Config.GitPath) + status = Uninstall(h.Config.GitPath) case "update": - Update(&h.Config) + status = Update(&h.Config) case "validate": commitLines, err := txtreader.GetLinesFromTextFile(h.Config.CommitMsg) if err != nil { log.Printf("Could not read commit message: %q", err.Error()) - break + return 3 } Validate(commitLines) default: log.Println("Unknown subcommand. Please check manual.") + return 4 } + + return status } diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 24317db..8011143 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -18,19 +18,24 @@ func TestHandler(t *testing.T) { config := model.Config{Command: "uninstall", GitPath: "/"} myHandler := NewHandler(config) - myHandler.Run() + status := myHandler.Run() assert.Contains(t, buffer.String(), "Could not delete") + assert.True(t, status > 0) }) t.Run("executes setup command", func(t *testing.T) { buffer.Reset() - config := model.Config{Command: "setup", GitPath: t.TempDir()} + protectedPath := t.TempDir() + "/fake" + err := os.Mkdir(protectedPath, 0000) + assert.Nil(t, err) + config := model.Config{Command: "setup", GitPath: protectedPath} myHandler := NewHandler(config) - myHandler.Run() + status := myHandler.Run() - assert.Contains(t, buffer.String(), "successfully") + assert.Contains(t, buffer.String(), "Could not create") + assert.True(t, status > 0) }) t.Run("executes update command", func(t *testing.T) { @@ -38,9 +43,10 @@ func TestHandler(t *testing.T) { config := model.Config{Command: "update"} myHandler := NewHandler(config) - myHandler.Run() + status := myHandler.Run() assert.Contains(t, buffer.String(), "Error at retrieving") + assert.True(t, status > 0) }) t.Run("executes validate command", func(t *testing.T) { @@ -64,11 +70,12 @@ func TestHandler(t *testing.T) { config := model.Config{Command: "validate", CommitMsg: "/no_file"} myHandler := NewHandler(config) - myHandler.Run() + status := myHandler.Run() want := `Could not read commit message: "file not found"` assert.Contains(t, buffer.String(), want) assert.NotContains(t, buffer.String(), "Valid") + assert.Equal(t, 3, status) }) }) @@ -77,9 +84,10 @@ func TestHandler(t *testing.T) { config := model.Config{Command: "unknown"} myHandler := NewHandler(config) - myHandler.Run() + status := myHandler.Run() want := "Unknown subcommand. Please check manual." assert.Contains(t, buffer.String(), want) + assert.Equal(t, 4, status) }) } From f449b857229ee59ffc90fcc08a8bd0535982b426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 19:04:53 +0200 Subject: [PATCH 13/45] Return result of validate command --- cmd/handler.go | 2 +- cmd/handler_test.go | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index 4ee7160..769749f 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -31,7 +31,7 @@ func (h *Handler) Run() int { return 3 } - Validate(commitLines) + status = Validate(commitLines) default: log.Println("Unknown subcommand. Please check manual.") return 4 diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 8011143..b21d7b4 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -53,8 +53,7 @@ func TestHandler(t *testing.T) { t.Run("success", func(t *testing.T) { buffer.Reset() - dir := t.TempDir() - testFile := dir + "/text.txt" + testFile := t.TempDir() + "/text.txt" err := os.WriteFile(testFile, []byte("i am a commit msg"), 0666) assert.Nil(t, err) config := model.Config{Command: "validate", CommitMsg: testFile} @@ -65,6 +64,21 @@ func TestHandler(t *testing.T) { assert.Contains(t, buffer.String(), "Valid commit message") }) + t.Run("commit msg too long aborts handler", func(t *testing.T) { + testFile := t.TempDir() + "/text.txt" + content := "waaaaaaaaaaaaaaaaaaaaaaaaaay tooooooooooooooooooo" + + "looooooooooooooooooooooong" + err := os.WriteFile(testFile, []byte(content), 0666) + assert.Nil(t, err) + config := model.Config{Command: "validate", CommitMsg: testFile} + myHandler := NewHandler(config) + + status := myHandler.Run() + + assert.Contains(t, buffer.String(), "Valid commit message") + assert.Equal(t, 1, status) + }) + t.Run("error at reading file", func(t *testing.T) { buffer.Reset() config := model.Config{Command: "validate", CommitMsg: "/no_file"} From cb62d02926d41b8308e0a59502656015a2c8bc9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 19:15:33 +0200 Subject: [PATCH 14/45] Integrate new handler into main --- main.go | 48 +++++++++++++++--------------------------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/main.go b/main.go index a1b7830..965aa65 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "github.com/rockstaedt/txtreader" "log" "os" "rockstaedt/commit-message-check/cmd" @@ -46,39 +45,22 @@ func main() { os.Exit(2) } - var status int - switch os.Args[1] { - case "setup": - status = cmd.Setup(gitPath) - case "uninstall": - status = cmd.Uninstall(gitPath) - case "update": - config := &model.Config{ - Version: version, - TagUrl: "https://api.github.com/repos/rockstaedt/commit-message-check/releases/latest", - BinaryBaseUrl: "https://github.com/rockstaedt/commit-message-check/releases/latest/download/", - DownloadPath: cwd, - } - - status = cmd.Update(config) - - if status > 0 { - log.Println("[ERROR]\t Could not update commit-message-check.") - break - } - log.Printf("[SUCCESS]\t Updated commit-message-check successfully to %s", config.LatestVersion) - case "validate": - commitLines, err := txtreader.GetLinesFromTextFile(os.Args[2]) - if err != nil { - log.Printf("[ERROR]\t Could not read commit message lines: %q", err.Error()) - status = 3 - } + var commitMsg string + if len(os.Args) == 3 { + commitMsg = os.Args[2] + } - status = cmd.Validate(commitLines) - default: - fmt.Printf("Unknown subcommand %q. Please check manual with -h flag.\n", os.Args[1]) - status = 4 + config := model.Config{ + Command: os.Args[1], + CommitMsg: commitMsg, + GitPath: gitPath, + Version: version, + TagUrl: "https://api.github.com/repos/rockstaedt/commit-message-check/releases/latest", + BinaryBaseUrl: "https://github.com/rockstaedt/commit-message-check/releases/latest/download/", + DownloadPath: cwd, } - os.Exit(status) + handler := cmd.NewHandler(config) + + os.Exit(handler.Run()) } From 78144a8e678335af21ab74a3c1ad15ffe172a5a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 19:26:12 +0200 Subject: [PATCH 15/45] Fix wanted message --- cmd/handler_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/handler_test.go b/cmd/handler_test.go index b21d7b4..611c68f 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -65,6 +65,7 @@ func TestHandler(t *testing.T) { }) t.Run("commit msg too long aborts handler", func(t *testing.T) { + buffer.Reset() testFile := t.TempDir() + "/text.txt" content := "waaaaaaaaaaaaaaaaaaaaaaaaaay tooooooooooooooooooo" + "looooooooooooooooooooooong" @@ -75,7 +76,7 @@ func TestHandler(t *testing.T) { status := myHandler.Run() - assert.Contains(t, buffer.String(), "Valid commit message") + assert.Contains(t, buffer.String(), "Abort commit") assert.Equal(t, 1, status) }) From 80bd1ca3993f43262c6a6e87e8c3ad32b09119dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 19:35:01 +0200 Subject: [PATCH 16/45] Print success message for update --- cmd/update.go | 5 +++++ cmd/update_test.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/cmd/update.go b/cmd/update.go index 1f1d4b1..d872f4f 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -66,6 +66,11 @@ func downloadScript(config *model.Config) int { _, _ = io.Copy(file, res.Body) + log.Printf( + "[SUCCESS]\t Updated commit-message-check successfully to %s", + config.LatestVersion, + ) + return 0 } diff --git a/cmd/update_test.go b/cmd/update_test.go index c9e9a3e..b2f12d3 100644 --- a/cmd/update_test.go +++ b/cmd/update_test.go @@ -119,6 +119,8 @@ func TestDownloadScript(t *testing.T) { } t.Run("returns 0 and writes downloaded binary content to file", func(t *testing.T) { + buffer := &bytes.Buffer{} + log.SetOutput(buffer) tempDir := t.TempDir() err := os.WriteFile(tempDir+"/dummy", []byte("i am a go binary"), os.ModePerm) assert.Nil(t, err) @@ -137,6 +139,9 @@ func TestDownloadScript(t *testing.T) { contentBytes, err := os.ReadFile(tempDir + "/commit-message-check") assert.Nil(t, err) assert.Contains(t, string(contentBytes), "i am a go binary") + wantedUpdateMsg := "[SUCCESS]\t Updated commit-message-check " + + "successfully to v1.1.1" + assert.Contains(t, buffer.String(), wantedUpdateMsg) }) t.Run("returns 1 when error at creating file", func(t *testing.T) { From 72f36df59f5812778d2b309dd730c86716d794a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sun, 2 Apr 2023 19:53:15 +0200 Subject: [PATCH 17/45] Rename test function --- cmd/handler_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 611c68f..d7b0c0d 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -9,7 +9,7 @@ import ( "testing" ) -func TestHandler(t *testing.T) { +func TestRun(t *testing.T) { buffer := &bytes.Buffer{} log.SetOutput(buffer) From fac47e597b2f7cb142a8a13e371e3e206c160bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sat, 8 Apr 2023 21:55:19 +0200 Subject: [PATCH 18/45] Transform setup method to receiver method --- cmd/handler.go | 2 +- cmd/setup.go | 4 ++-- cmd/setup_test.go | 19 +++++++++---------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index 769749f..cbaeb1a 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -19,7 +19,7 @@ func (h *Handler) Run() int { var status int switch h.Config.Command { case "setup": - status = Setup(h.Config.GitPath) + status = h.setup() case "uninstall": status = Uninstall(h.Config.GitPath) case "update": diff --git a/cmd/setup.go b/cmd/setup.go index efcfa54..fcc53c9 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -5,8 +5,8 @@ import ( "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.") return 1 diff --git a/cmd/setup_test.go b/cmd/setup_test.go index 075b591..0103dd9 100644 --- a/cmd/setup_test.go +++ b/cmd/setup_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/assert" "log" "os" + "rockstaedt/commit-message-check/internal/model" "testing" ) @@ -15,28 +16,25 @@ func TestSetup(t *testing.T) { 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 - } + return NewHandler(model.Config{GitPath: path}) + }() t.Run("creates commit-msg script in hook folder", func(t *testing.T) { - path := createDirs() - - status := Setup(path) + 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.") }) @@ -46,8 +44,9 @@ func TestSetup(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}) - status := Setup(errPath) + status := handler.setup() assert.Equal(t, 1, status) assert.Contains(t, buffer.String(), "[ERROR]\t Could not create commit-msg script.") From a0bc60ca1dac8358dc68e6c3905f0153b886dc54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sat, 8 Apr 2023 22:09:46 +0200 Subject: [PATCH 19/45] Transform uninstall method to receiver method --- cmd/handler.go | 2 +- cmd/uninstall.go | 4 ++-- cmd/uninstall_test.go | 17 ++++++++++------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index cbaeb1a..4a2802b 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -21,7 +21,7 @@ func (h *Handler) Run() int { case "setup": status = h.setup() case "uninstall": - status = Uninstall(h.Config.GitPath) + status = h.uninstall() case "update": status = Update(&h.Config) case "validate": diff --git a/cmd/uninstall.go b/cmd/uninstall.go index 90a2836..6a558af 100644 --- a/cmd/uninstall.go +++ b/cmd/uninstall.go @@ -5,8 +5,8 @@ import ( "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.") return 1 diff --git a/cmd/uninstall_test.go b/cmd/uninstall_test.go index e6d04b8..1000658 100644 --- a/cmd/uninstall_test.go +++ b/cmd/uninstall_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/assert" "log" "os" + "rockstaedt/commit-message-check/internal/model" "testing" ) @@ -13,7 +14,7 @@ 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) @@ -24,16 +25,17 @@ func TestUninstall(t *testing.T) { _, err = os.Create(fmt.Sprintf("%s/xyz/commit-msg", path)) assert.Nil(t, err) - return path + return NewHandler(model.Config{GitPath: path}) } 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)) @@ -41,9 +43,9 @@ func TestUninstall(t *testing.T) { 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.") }) @@ -54,8 +56,9 @@ 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}) - status := Uninstall(errPath) + status := handler.uninstall() assert.Equal(t, 1, status) assert.Contains(t, buffer.String(), "[ERROR]\t Could not delete commit-msg hook.") From 9e123dfb05e62795af132a399f89a982b76fb2e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sat, 8 Apr 2023 22:10:20 +0200 Subject: [PATCH 20/45] Transform update method to receiver method --- cmd/handler.go | 2 +- cmd/update.go | 10 +++++----- cmd/update_test.go | 12 ++++++------ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index 4a2802b..df74944 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -23,7 +23,7 @@ func (h *Handler) Run() int { case "uninstall": status = h.uninstall() case "update": - status = Update(&h.Config) + status = h.update() case "validate": commitLines, err := txtreader.GetLinesFromTextFile(h.Config.CommitMsg) if err != nil { diff --git a/cmd/update.go b/cmd/update.go index d872f4f..722535e 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -15,19 +15,19 @@ type responseData struct { TagName string `json:"tag_name"` } -func Update(config *model.Config) int { - config.LatestVersion = getLatestTag(config.TagUrl) - if config.LatestVersion == "" { +func (h *Handler) update() int { + h.Config.LatestVersion = getLatestTag(h.Config.TagUrl) + if h.Config.LatestVersion == "" { log.Println("Error at retrieving latest version.") return 1 } - if config.Version == config.LatestVersion { + if h.Config.Version == h.Config.LatestVersion { log.Println("Current version is latest version.") return 0 } - return downloadScript(config) + return downloadScript(&h.Config) } func getLatestTag(url string) string { diff --git a/cmd/update_test.go b/cmd/update_test.go index b2f12d3..6cd1358 100644 --- a/cmd/update_test.go +++ b/cmd/update_test.go @@ -23,9 +23,9 @@ func TestUpdate(t *testing.T) { buffer.Reset() ts := httptest.NewServer(getHandlerFor(`{"tag_name":"v1.0.0"}`)) defer ts.Close() - config := &model.Config{Version: "v1.0.0", TagUrl: ts.URL} + handler := NewHandler(model.Config{Version: "v1.0.0", TagUrl: ts.URL}) - status := Update(config) + status := handler.update() assert.Equal(t, 0, status) assert.Contains(t, buffer.String(), "Current version is latest version.") @@ -35,9 +35,9 @@ func TestUpdate(t *testing.T) { ts := httptest.NewServer(getHandlerFor(`{"tag_name":"v1.1.0"}`)) defer ts.Close() tempDir := t.TempDir() - config := &model.Config{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: tempDir} + handler := NewHandler(model.Config{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: tempDir}) - _ = Update(config) + _ = handler.update() assert.FileExists(t, tempDir+"/commit-message-check") }) @@ -47,9 +47,9 @@ func TestUpdate(t *testing.T) { buffer.Reset() ts := httptest.NewServer(getHandlerFor("", 500)) defer ts.Close() - config := &model.Config{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: ""} + handler := NewHandler(model.Config{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: ""}) - status := Update(config) + status := handler.update() assert.Equal(t, 1, status) assert.Contains(t, buffer.String(), "Error at retrieving latest version.") From 232b1de03993a86f1bcef7d1df7fa5123af4f332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sat, 8 Apr 2023 22:16:19 +0200 Subject: [PATCH 21/45] Pass command as method argument --- cmd/handler.go | 4 ++-- cmd/handler_test.go | 35 ++++++++++++++--------------------- internal/model/config.go | 1 - main.go | 3 +-- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index df74944..d096790 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -14,10 +14,10 @@ func NewHandler(config model.Config) *Handler { return &Handler{Config: config} } -func (h *Handler) Run() int { +func (h *Handler) Run(command string) int { var status int - switch h.Config.Command { + switch command { case "setup": status = h.setup() case "uninstall": diff --git a/cmd/handler_test.go b/cmd/handler_test.go index d7b0c0d..3ba506d 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -15,10 +15,9 @@ func TestRun(t *testing.T) { t.Run("executes uninstall command", func(t *testing.T) { buffer.Reset() - config := model.Config{Command: "uninstall", GitPath: "/"} - myHandler := NewHandler(config) + myHandler := NewHandler(model.Config{GitPath: "/"}) - status := myHandler.Run() + status := myHandler.Run("uninstall") assert.Contains(t, buffer.String(), "Could not delete") assert.True(t, status > 0) @@ -29,10 +28,9 @@ func TestRun(t *testing.T) { protectedPath := t.TempDir() + "/fake" err := os.Mkdir(protectedPath, 0000) assert.Nil(t, err) - config := model.Config{Command: "setup", GitPath: protectedPath} - myHandler := NewHandler(config) + myHandler := NewHandler(model.Config{GitPath: protectedPath}) - status := myHandler.Run() + status := myHandler.Run("setup") assert.Contains(t, buffer.String(), "Could not create") assert.True(t, status > 0) @@ -40,10 +38,9 @@ func TestRun(t *testing.T) { t.Run("executes update command", func(t *testing.T) { buffer.Reset() - config := model.Config{Command: "update"} - myHandler := NewHandler(config) + myHandler := NewHandler(model.Config{}) - status := myHandler.Run() + status := myHandler.Run("update") assert.Contains(t, buffer.String(), "Error at retrieving") assert.True(t, status > 0) @@ -56,10 +53,9 @@ func TestRun(t *testing.T) { testFile := t.TempDir() + "/text.txt" err := os.WriteFile(testFile, []byte("i am a commit msg"), 0666) assert.Nil(t, err) - config := model.Config{Command: "validate", CommitMsg: testFile} - myHandler := NewHandler(config) + myHandler := NewHandler(model.Config{CommitMsg: testFile}) - myHandler.Run() + myHandler.Run("validate") assert.Contains(t, buffer.String(), "Valid commit message") }) @@ -71,10 +67,9 @@ func TestRun(t *testing.T) { "looooooooooooooooooooooong" err := os.WriteFile(testFile, []byte(content), 0666) assert.Nil(t, err) - config := model.Config{Command: "validate", CommitMsg: testFile} - myHandler := NewHandler(config) + myHandler := NewHandler(model.Config{CommitMsg: testFile}) - status := myHandler.Run() + status := myHandler.Run("validate") assert.Contains(t, buffer.String(), "Abort commit") assert.Equal(t, 1, status) @@ -82,10 +77,9 @@ func TestRun(t *testing.T) { t.Run("error at reading file", func(t *testing.T) { buffer.Reset() - config := model.Config{Command: "validate", CommitMsg: "/no_file"} - myHandler := NewHandler(config) + myHandler := NewHandler(model.Config{CommitMsg: "/no_file"}) - status := myHandler.Run() + status := myHandler.Run("validate") want := `Could not read commit message: "file not found"` assert.Contains(t, buffer.String(), want) @@ -96,10 +90,9 @@ func TestRun(t *testing.T) { t.Run("prints warning when any other command", func(t *testing.T) { buffer.Reset() - config := model.Config{Command: "unknown"} - myHandler := NewHandler(config) + myHandler := NewHandler(model.Config{}) - status := myHandler.Run() + status := myHandler.Run("unknown") want := "Unknown subcommand. Please check manual." assert.Contains(t, buffer.String(), want) diff --git a/internal/model/config.go b/internal/model/config.go index 9581f1b..e78b797 100644 --- a/internal/model/config.go +++ b/internal/model/config.go @@ -1,7 +1,6 @@ package model type Config struct { - Command string CommitMsg string GitPath string Version string diff --git a/main.go b/main.go index 965aa65..287a4a5 100644 --- a/main.go +++ b/main.go @@ -51,7 +51,6 @@ func main() { } config := model.Config{ - Command: os.Args[1], CommitMsg: commitMsg, GitPath: gitPath, Version: version, @@ -62,5 +61,5 @@ func main() { handler := cmd.NewHandler(config) - os.Exit(handler.Run()) + os.Exit(handler.Run(os.Args[1])) } From 97735e8d73a4ca454991ae99db69c57da9386f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sat, 8 Apr 2023 22:18:52 +0200 Subject: [PATCH 22/45] Rename struct field --- cmd/handler.go | 2 +- cmd/handler_test.go | 6 +++--- internal/model/config.go | 2 +- main.go | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index d096790..f93601b 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -25,7 +25,7 @@ func (h *Handler) Run(command string) int { case "update": status = h.update() case "validate": - commitLines, err := txtreader.GetLinesFromTextFile(h.Config.CommitMsg) + commitLines, err := txtreader.GetLinesFromTextFile(h.Config.CommitMsgFile) if err != nil { log.Printf("Could not read commit message: %q", err.Error()) return 3 diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 3ba506d..a5f7a8a 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -53,7 +53,7 @@ func TestRun(t *testing.T) { testFile := t.TempDir() + "/text.txt" err := os.WriteFile(testFile, []byte("i am a commit msg"), 0666) assert.Nil(t, err) - myHandler := NewHandler(model.Config{CommitMsg: testFile}) + myHandler := NewHandler(model.Config{CommitMsgFile: testFile}) myHandler.Run("validate") @@ -67,7 +67,7 @@ func TestRun(t *testing.T) { "looooooooooooooooooooooong" err := os.WriteFile(testFile, []byte(content), 0666) assert.Nil(t, err) - myHandler := NewHandler(model.Config{CommitMsg: testFile}) + myHandler := NewHandler(model.Config{CommitMsgFile: testFile}) status := myHandler.Run("validate") @@ -77,7 +77,7 @@ func TestRun(t *testing.T) { t.Run("error at reading file", func(t *testing.T) { buffer.Reset() - myHandler := NewHandler(model.Config{CommitMsg: "/no_file"}) + myHandler := NewHandler(model.Config{CommitMsgFile: "/no_file"}) status := myHandler.Run("validate") diff --git a/internal/model/config.go b/internal/model/config.go index e78b797..384de28 100644 --- a/internal/model/config.go +++ b/internal/model/config.go @@ -1,7 +1,7 @@ package model type Config struct { - CommitMsg string + CommitMsgFile string GitPath string Version string LatestVersion string diff --git a/main.go b/main.go index 287a4a5..a340b81 100644 --- a/main.go +++ b/main.go @@ -45,13 +45,13 @@ func main() { os.Exit(2) } - var commitMsg string + var commitMsgFile string if len(os.Args) == 3 { - commitMsg = os.Args[2] + commitMsgFile = os.Args[2] } config := model.Config{ - CommitMsg: commitMsg, + CommitMsgFile: commitMsgFile, GitPath: gitPath, Version: version, TagUrl: "https://api.github.com/repos/rockstaedt/commit-message-check/releases/latest", From 8a74834b66531956de0f353b08fb964d19984689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sat, 8 Apr 2023 22:29:44 +0200 Subject: [PATCH 23/45] Transform validate method to receiver method --- cmd/handler.go | 9 +------- cmd/handler_test.go | 47 ++++++++-------------------------------- cmd/validate.go | 9 +++++++- cmd/validate_test.go | 51 ++++++++++++++++++++++++++++++++------------ 4 files changed, 55 insertions(+), 61 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index f93601b..2bfcfc2 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -1,7 +1,6 @@ package cmd import ( - "github.com/rockstaedt/txtreader" "log" "rockstaedt/commit-message-check/internal/model" ) @@ -25,13 +24,7 @@ func (h *Handler) Run(command string) int { case "update": status = h.update() case "validate": - commitLines, err := txtreader.GetLinesFromTextFile(h.Config.CommitMsgFile) - if err != nil { - log.Printf("Could not read commit message: %q", err.Error()) - return 3 - } - - status = Validate(commitLines) + status = h.validate() default: log.Println("Unknown subcommand. Please check manual.") return 4 diff --git a/cmd/handler_test.go b/cmd/handler_test.go index a5f7a8a..67a1c49 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -48,44 +48,15 @@ func TestRun(t *testing.T) { t.Run("executes validate command", func(t *testing.T) { - t.Run("success", 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.Contains(t, buffer.String(), "Valid commit message") - }) - - t.Run("commit msg too long aborts handler", func(t *testing.T) { - buffer.Reset() - testFile := t.TempDir() + "/text.txt" - content := "waaaaaaaaaaaaaaaaaaaaaaaaaay tooooooooooooooooooo" + - "looooooooooooooooooooooong" - err := os.WriteFile(testFile, []byte(content), 0666) - assert.Nil(t, err) - myHandler := NewHandler(model.Config{CommitMsgFile: testFile}) - - status := myHandler.Run("validate") - - assert.Contains(t, buffer.String(), "Abort commit") - assert.Equal(t, 1, status) - }) - - t.Run("error at reading file", func(t *testing.T) { - buffer.Reset() - myHandler := NewHandler(model.Config{CommitMsgFile: "/no_file"}) - - status := myHandler.Run("validate") - - want := `Could not read commit message: "file not found"` - assert.Contains(t, buffer.String(), want) - assert.NotContains(t, buffer.String(), "Valid") - assert.Equal(t, 3, status) - }) + 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.Contains(t, buffer.String(), "Valid commit message") }) t.Run("prints warning when any other command", func(t *testing.T) { diff --git a/cmd/validate.go b/cmd/validate.go index 071642c..647b86b 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/rockstaedt/txtreader" "log" "rockstaedt/commit-message-check/internal/model" ) @@ -10,9 +11,15 @@ const ( hardLimit = 72 ) -func Validate(commitLines []string) int { +func (h *Handler) validate() int { log.Println("[INFO]\t Validate commit message.") + commitLines, err := txtreader.GetLinesFromTextFile(h.Config.CommitMsgFile) + if err != nil { + log.Printf("Could not read commit message: %q", err.Error()) + return 1 + } + cm := model.CreateCommitMessageFrom(commitLines) numOfExceedingChars := cm.ValidateSubject() diff --git a/cmd/validate_test.go b/cmd/validate_test.go index a47461a..87815fe 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -4,6 +4,8 @@ import ( "bytes" "github.com/stretchr/testify/assert" "log" + "os" + "rockstaedt/commit-message-check/internal/model" "testing" ) @@ -11,34 +13,55 @@ func TestValidate(t *testing.T) { buffer := &bytes.Buffer{} log.SetOutput(buffer) - t.Run("returns 0 if valid and logs a success", func(t *testing.T) { + t.Run("returns 0 on success", func(t *testing.T) { buffer.Reset() - commitLines := []string{"i am shorter than 50 characters"} + testFile := t.TempDir() + "/text.txt" + err := os.WriteFile(testFile, []byte("i am a short commit msg"), 0666) + assert.Nil(t, err) + handler := NewHandler(model.Config{CommitMsgFile: testFile}) - status := Validate(commitLines) + handler.Run("validate") - assert.Equal(t, status, 0) - assert.Contains(t, buffer.String(), "[INFO]\t Validate commit message.") - assert.Contains(t, buffer.String(), "[SUCCESS]\t Valid commit message") + assert.Contains(t, buffer.String(), "Valid commit message") }) - t.Run("returns 0 if soft limit exceeds and logs a warning", func(t *testing.T) { + t.Run("returns 0 when soft limit exceeds and logs a warning", func(t *testing.T) { buffer.Reset() - commitLines := []string{"i am two characters more thaaaaaaaaaaaaaaaaaaaaan 50"} + testFile := t.TempDir() + "/text.txt" + err := os.WriteFile(testFile, []byte("i am two characters more thaaaaaaaaaaaaaaaaaaaaan 50"), 0666) + assert.Nil(t, err) + handler := NewHandler(model.Config{CommitMsgFile: testFile}) - status := Validate(commitLines) + status := handler.validate() assert.Equal(t, status, 0) assert.Contains(t, buffer.String(), "[WARN]\t Your subject exceeds the soft limit of 50 chars by 2 chars.") }) - t.Run("returns 1 if length > 70 and logs an error", func(t *testing.T) { + t.Run("returns 1 when commit message too long", func(t *testing.T) { + buffer.Reset() + testFile := t.TempDir() + "/text.txt" + content := "waaaaaaaaaaaaaaaaaaaaaaaaaay tooooooooooooooooooo" + + "looooooooooooooooooooooong" + err := os.WriteFile(testFile, []byte(content), 0666) + assert.Nil(t, err) + myHandler := NewHandler(model.Config{CommitMsgFile: testFile}) + + status := myHandler.Run("validate") + + assert.Contains(t, buffer.String(), "Abort commit") + assert.Equal(t, 1, status) + }) + + t.Run("returns 1 when error at reading file", func(t *testing.T) { buffer.Reset() - commitLines := []string{"i am way toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo long"} + myHandler := NewHandler(model.Config{CommitMsgFile: "/no_file"}) - status := Validate(commitLines) + status := myHandler.Run("validate") - assert.Equal(t, status, 1) - assert.Contains(t, buffer.String(), "[ERROR]\t Abort commit. Subject line too long. Please fix.") + want := `Could not read commit message: "file not found"` + assert.Contains(t, buffer.String(), want) + assert.NotContains(t, buffer.String(), "Valid commit") + assert.Equal(t, 1, status) }) } From d1863cc2a22da66ca79c5fc611a0390c951ffdd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sat, 8 Apr 2023 22:30:30 +0200 Subject: [PATCH 24/45] Reformat function --- cmd/handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/handler.go b/cmd/handler.go index 2bfcfc2..796c2d7 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -14,8 +14,8 @@ func NewHandler(config model.Config) *Handler { } func (h *Handler) Run(command string) int { - var status int + switch command { case "setup": status = h.setup() From 62cc1a842c1cbe3626a481f3e8288e0f6c3f8289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sat, 8 Apr 2023 22:31:23 +0200 Subject: [PATCH 25/45] Move to 1 as error status --- cmd/handler.go | 2 +- cmd/handler_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index 796c2d7..7f20b6b 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -27,7 +27,7 @@ func (h *Handler) Run(command string) int { status = h.validate() default: log.Println("Unknown subcommand. Please check manual.") - return 4 + return 1 } return status diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 67a1c49..c9d4915 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -67,6 +67,6 @@ func TestRun(t *testing.T) { want := "Unknown subcommand. Please check manual." assert.Contains(t, buffer.String(), want) - assert.Equal(t, 4, status) + assert.Equal(t, 1, status) }) } From d2b7a9b3595701d49e37a2d71cbe710516bac53d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Sat, 8 Apr 2023 22:39:19 +0200 Subject: [PATCH 26/45] Define tests for writing method --- cmd/handler_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cmd/handler_test.go b/cmd/handler_test.go index c9d4915..424b7d3 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -70,3 +70,25 @@ func TestRun(t *testing.T) { assert.Equal(t, 1, status) }) } + +func TestNotify(t *testing.T) { + + t.Run("writes message to write", func(t *testing.T) { + t.Skip() + }) + + t.Run("accepts a category", func(t *testing.T) { + + t.Run("success", func(t *testing.T) { + t.Skip() + }) + + t.Run("error", func(t *testing.T) { + t.Skip() + }) + }) + + t.Run("handles error at writing", func(t *testing.T) { + t.Skip() + }) +} From 50d602def46edd5ea573257e52a019ca85e662e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Fri, 21 Apr 2023 09:18:54 +0200 Subject: [PATCH 27/45] Transform fake writer to a mock --- go.mod | 3 ++- go.sum | 3 +++ testdata/mocks/writer.go | 18 ++++++++++++++---- util/hook_test.go | 6 ++++-- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 3077e45..ac70dfd 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,12 @@ go 1.19 require ( github.com/rockstaedt/txtreader v1.0.1 - github.com/stretchr/testify v1.8.1 + github.com/stretchr/testify v1.8.2 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.5.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index f59502a..d186fb2 100644 --- a/go.sum +++ b/go.sum @@ -7,11 +7,14 @@ github.com/rockstaedt/txtreader v1.0.1 h1:2FlMPrFEOGdw8CizWnuPe+1be3i7o+ErNhZzwe github.com/rockstaedt/txtreader v1.0.1/go.mod h1:5U0AithdsiR/v3kSTg1z0AbTUZaTy/AhdiDN8wJl9F4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/testdata/mocks/writer.go b/testdata/mocks/writer.go index 23f9eef..f973266 100644 --- a/testdata/mocks/writer.go +++ b/testdata/mocks/writer.go @@ -1,10 +1,20 @@ package mocks -import "errors" +import ( + "github.com/stretchr/testify/mock" +) -type FakeWriter struct { +type FakeWriterMock struct { + mock.Mock } -func (fw FakeWriter) Write(p []byte) (int, error) { - return 0, errors.New("error at writing") +func (fwm *FakeWriterMock) ResetCalls() { + fwm.Calls = []mock.Call{} + fwm.ExpectedCalls = []*mock.Call{} +} + +func (fwm *FakeWriterMock) Write(p []byte) (int, error) { + args := fwm.Called(p) + + return 0, args.Error(1) } diff --git a/util/hook_test.go b/util/hook_test.go index fda7c7a..df994dd 100644 --- a/util/hook_test.go +++ b/util/hook_test.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "log" "os" "rockstaedt/commit-message-check/testdata/mocks" @@ -155,9 +156,10 @@ func TestWriteContent(t *testing.T) { t.Run("logs any error", func(t *testing.T) { log.SetOutput(buffer) - errBuffer := mocks.FakeWriter{} + fwm := &mocks.FakeWriterMock{} + fwm.On("Write", mock.Anything).Return(0, errors.New("error at writing")) - writeContent(errBuffer, "usr/tmp") + writeContent(fwm, "usr/tmp") assert.Contains(t, buffer.String(), "[ERROR]\t Could not write commit-msg script: error at writing") }) From 051fcad5be7f5d8b839223593c9f8a46ac9f2cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Fri, 21 Apr 2023 09:19:07 +0200 Subject: [PATCH 28/45] Implement happy notify --- cmd/handler.go | 6 ++++++ cmd/handler_test.go | 13 +++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index 7f20b6b..9c0021f 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -1,12 +1,14 @@ package cmd import ( + "io" "log" "rockstaedt/commit-message-check/internal/model" ) type Handler struct { Config model.Config + Writer io.Writer } func NewHandler(config model.Config) *Handler { @@ -32,3 +34,7 @@ func (h *Handler) Run(command string) int { return status } + +func (h *Handler) notify() { + h.Writer.Write([]byte("hallo")) +} diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 424b7d3..f8cab3a 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -3,9 +3,11 @@ package cmd import ( "bytes" "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" ) @@ -73,8 +75,15 @@ func TestRun(t *testing.T) { func TestNotify(t *testing.T) { - t.Run("writes message to write", func(t *testing.T) { - t.Skip() + t.Run("writes a message to the writer", func(t *testing.T) { + handler := NewHandler(model.Config{}) + fwm := &mocks.FakeWriterMock{} + handler.Writer = fwm + fwm.On("Write", mock.Anything).Return(1, nil) + + handler.notify() + + fwm.AssertNumberOfCalls(t, "Write", 1) }) t.Run("accepts a category", func(t *testing.T) { From f185bb4b5b71fcf353c683e05237d67705eb2a4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Tue, 2 May 2023 16:04:35 +0200 Subject: [PATCH 29/45] Enable custom message --- cmd/handler.go | 4 ++-- cmd/handler_test.go | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index 9c0021f..50a5f6f 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -35,6 +35,6 @@ func (h *Handler) Run(command string) int { return status } -func (h *Handler) notify() { - h.Writer.Write([]byte("hallo")) +func (h *Handler) notify(message string) { + h.Writer.Write([]byte(message)) } diff --git a/cmd/handler_test.go b/cmd/handler_test.go index f8cab3a..13902d8 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -79,11 +79,12 @@ func TestNotify(t *testing.T) { handler := NewHandler(model.Config{}) fwm := &mocks.FakeWriterMock{} handler.Writer = fwm + fwm.ResetCalls() fwm.On("Write", mock.Anything).Return(1, nil) - handler.notify() + handler.notify("I am a message") - fwm.AssertNumberOfCalls(t, "Write", 1) + fwm.AssertCalled(t, "Write", []byte("I am a message")) }) t.Run("accepts a category", func(t *testing.T) { From 00e4b6476fbf418caa76c7cbb6e69f3f4e8b76a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Tue, 2 May 2023 16:08:43 +0200 Subject: [PATCH 30/45] Handle error at writing --- cmd/handler.go | 5 ++++- cmd/handler_test.go | 16 ++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index 50a5f6f..a8bbd12 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -36,5 +36,8 @@ func (h *Handler) Run(command string) int { } func (h *Handler) notify(message string) { - h.Writer.Write([]byte(message)) + _, err := h.Writer.Write([]byte(message)) + if err != nil { + log.Println("Error at writing!") + } } diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 13902d8..d3f46fc 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -2,6 +2,7 @@ package cmd import ( "bytes" + "errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "log" @@ -74,11 +75,11 @@ func TestRun(t *testing.T) { } 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) { - handler := NewHandler(model.Config{}) - fwm := &mocks.FakeWriterMock{} - handler.Writer = fwm fwm.ResetCalls() fwm.On("Write", mock.Anything).Return(1, nil) @@ -99,6 +100,13 @@ func TestNotify(t *testing.T) { }) t.Run("handles error at writing", func(t *testing.T) { - t.Skip() + 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!") }) } From b870db316e5d91f4f7a343c8b0f9da9409885f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Tue, 2 May 2023 16:20:44 +0200 Subject: [PATCH 31/45] Colorize text in green --- cmd/handler.go | 7 ++++++- cmd/handler_test.go | 13 +++++++++---- go.mod | 1 + go.sum | 2 ++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index a8bbd12..aa85dbc 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/TwiN/go-color" "io" "log" "rockstaedt/commit-message-check/internal/model" @@ -35,7 +36,11 @@ func (h *Handler) Run(command string) int { return status } -func (h *Handler) notify(message string) { +func (h *Handler) notify(message string, txtColor ...string) { + if len(txtColor) > 0 && txtColor[0] == "green" { + message = color.InGreen(message) + } + _, err := h.Writer.Write([]byte(message)) if err != nil { log.Println("Error at writing!") diff --git a/cmd/handler_test.go b/cmd/handler_test.go index d3f46fc..0ed2cda 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -88,13 +88,18 @@ func TestNotify(t *testing.T) { fwm.AssertCalled(t, "Write", []byte("I am a message")) }) - t.Run("accepts a category", func(t *testing.T) { + t.Run("colorize text in", func(t *testing.T) { - t.Run("success", func(t *testing.T) { - t.Skip() + 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("\033[32mI am a message\033[0m")) }) - t.Run("error", func(t *testing.T) { + t.Run("red", func(t *testing.T) { t.Skip() }) }) diff --git a/go.mod b/go.mod index ac70dfd..722c02a 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( ) require ( + github.com/TwiN/go-color v1.4.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/objx v0.5.0 // indirect diff --git a/go.sum b/go.sum index d186fb2..347d162 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/TwiN/go-color v1.4.0 h1:fNbOwOrvup5oj934UragnW0B1WKaAkkB85q19Y7h4ng= +github.com/TwiN/go-color v1.4.0/go.mod h1:0QTVEPlu+AoCyTrho7bXbVkrCkVpdQr7YF7PYWEtSxM= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= From 5619fb7f102bfdf67e03f65d77aa5047eb8a29eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Tue, 2 May 2023 16:22:38 +0200 Subject: [PATCH 32/45] Colorize text in red --- cmd/handler.go | 4 ++++ cmd/handler_test.go | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cmd/handler.go b/cmd/handler.go index aa85dbc..aaae9bd 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -41,6 +41,10 @@ func (h *Handler) notify(message string, txtColor ...string) { message = color.InGreen(message) } + if len(txtColor) > 0 && txtColor[0] == "red" { + message = color.InRed(message) + } + _, err := h.Writer.Write([]byte(message)) if err != nil { log.Println("Error at writing!") diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 0ed2cda..ab5b67c 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -100,7 +100,12 @@ func TestNotify(t *testing.T) { }) t.Run("red", func(t *testing.T) { - t.Skip() + fwm.ResetCalls() + fwm.On("Write", mock.Anything).Return(1, nil) + + handler.notify("I am a message", "red") + + fwm.AssertCalled(t, "Write", []byte("\033[31mI am a message\033[0m")) }) }) From 1a72f026fce054878af798cf6de8c8cbe7b12f07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Tue, 2 May 2023 16:36:33 +0200 Subject: [PATCH 33/45] Adds linebreak after message --- cmd/handler.go | 2 +- cmd/handler_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/handler.go b/cmd/handler.go index aaae9bd..5ed01e1 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -45,7 +45,7 @@ func (h *Handler) notify(message string, txtColor ...string) { message = color.InRed(message) } - _, err := h.Writer.Write([]byte(message)) + _, err := h.Writer.Write([]byte(message + "\n")) if err != nil { log.Println("Error at writing!") } diff --git a/cmd/handler_test.go b/cmd/handler_test.go index ab5b67c..ddfc8f2 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -85,7 +85,7 @@ func TestNotify(t *testing.T) { handler.notify("I am a message") - fwm.AssertCalled(t, "Write", []byte("I am a message")) + fwm.AssertCalled(t, "Write", []byte("I am a message\n")) }) t.Run("colorize text in", func(t *testing.T) { @@ -96,7 +96,7 @@ func TestNotify(t *testing.T) { handler.notify("I am a message", "green") - fwm.AssertCalled(t, "Write", []byte("\033[32mI am a message\033[0m")) + fwm.AssertCalled(t, "Write", []byte("\033[32mI am a message\033[0m\n")) }) t.Run("red", func(t *testing.T) { @@ -105,7 +105,7 @@ func TestNotify(t *testing.T) { handler.notify("I am a message", "red") - fwm.AssertCalled(t, "Write", []byte("\033[31mI am a message\033[0m")) + fwm.AssertCalled(t, "Write", []byte("\033[31mI am a message\033[0m\n")) }) }) From e8833103e3c9172346892304634176483b3c5978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Tue, 2 May 2023 16:41:35 +0200 Subject: [PATCH 34/45] Avoid logging and colorize error messages --- main.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index a340b81..1ef4a9f 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import ( "flag" "fmt" - "log" + "github.com/TwiN/go-color" "os" "rockstaedt/commit-message-check/cmd" "rockstaedt/commit-message-check/internal/model" @@ -28,21 +28,21 @@ func main() { } if len(os.Args) == 1 { - fmt.Println("No subcommands given. Please check manual.") + fmt.Println(color.InRed("No subcommands given. Please check manual.")) os.Exit(1) } cwd, err := os.Getwd() if err != nil { - log.Printf("[ERROR]\t Could not determine working directory: %q", err.Error()) + fmt.Println(color.InRed("Could not determine working directory: ") + err.Error()) os.Exit(1) } gitPath := fmt.Sprintf("%s/.git", cwd) _, err = os.Stat(gitPath) if err != nil { - log.Println("[ERROR]\t No git repository could be found.") - os.Exit(2) + fmt.Println(color.InRed("No git repository could be found.")) + os.Exit(1) } var commitMsgFile string From 366dd00a05898088bea9836dde80c694925d8962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Tue, 2 May 2023 16:56:09 +0200 Subject: [PATCH 35/45] Use notify in setup command --- cmd/handler_test.go | 7 +++++-- cmd/setup.go | 5 ++--- cmd/setup_test.go | 14 +++++++++----- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cmd/handler_test.go b/cmd/handler_test.go index ddfc8f2..088098c 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -3,6 +3,7 @@ package cmd import ( "bytes" "errors" + "github.com/TwiN/go-color" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "log" @@ -19,6 +20,7 @@ func TestRun(t *testing.T) { t.Run("executes uninstall command", func(t *testing.T) { buffer.Reset() myHandler := NewHandler(model.Config{GitPath: "/"}) + myHandler.Writer = buffer status := myHandler.Run("uninstall") @@ -32,6 +34,7 @@ func TestRun(t *testing.T) { err := os.Mkdir(protectedPath, 0000) assert.Nil(t, err) myHandler := NewHandler(model.Config{GitPath: protectedPath}) + myHandler.Writer = buffer status := myHandler.Run("setup") @@ -96,7 +99,7 @@ func TestNotify(t *testing.T) { handler.notify("I am a message", "green") - fwm.AssertCalled(t, "Write", []byte("\033[32mI am a message\033[0m\n")) + fwm.AssertCalled(t, "Write", []byte(color.Green+"I am a message"+color.Reset+"\n")) }) t.Run("red", func(t *testing.T) { @@ -105,7 +108,7 @@ func TestNotify(t *testing.T) { handler.notify("I am a message", "red") - fwm.AssertCalled(t, "Write", []byte("\033[31mI am a message\033[0m\n")) + fwm.AssertCalled(t, "Write", []byte(color.Red+"I am a message"+color.Reset+"\n")) }) }) diff --git a/cmd/setup.go b/cmd/setup.go index fcc53c9..90244d8 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -1,17 +1,16 @@ package cmd import ( - "log" "rockstaedt/commit-message-check/util" ) 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 } diff --git a/cmd/setup_test.go b/cmd/setup_test.go index 0103dd9..ed15756 100644 --- a/cmd/setup_test.go +++ b/cmd/setup_test.go @@ -3,8 +3,8 @@ package cmd import ( "bytes" "fmt" + "github.com/TwiN/go-color" "github.com/stretchr/testify/assert" - "log" "os" "rockstaedt/commit-message-check/internal/model" "testing" @@ -12,7 +12,6 @@ import ( func TestSetup(t *testing.T) { buffer := &bytes.Buffer{} - log.SetOutput(buffer) t.Run("returns 0 and", func(t *testing.T) { @@ -21,7 +20,10 @@ func TestSetup(t *testing.T) { err := os.Mkdir(fmt.Sprintf("%s/hooks", path), os.ModePerm) assert.Nil(t, err) - return NewHandler(model.Config{GitPath: path}) + handler := NewHandler(model.Config{GitPath: path}) + handler.Writer = buffer + + return handler }() t.Run("creates commit-msg script in hook folder", func(t *testing.T) { @@ -36,19 +38,21 @@ func TestSetup(t *testing.T) { _ = 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."+color.Reset) }) }) 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 := 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."+color.Reset) }) } From 63443528f58c4657b6a9d8d6bf5f270636343f41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Tue, 2 May 2023 16:58:59 +0200 Subject: [PATCH 36/45] Set output stream of binary to Stdout --- main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/main.go b/main.go index 1ef4a9f..dbc743d 100644 --- a/main.go +++ b/main.go @@ -60,6 +60,7 @@ func main() { } handler := cmd.NewHandler(config) + handler.Writer = os.Stdout os.Exit(handler.Run(os.Args[1])) } From c211f0db7761f0247b9a6c3e17918c828286dc2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Thu, 4 May 2023 09:31:05 +0200 Subject: [PATCH 37/45] Simplify test --- cmd/setup_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/setup_test.go b/cmd/setup_test.go index ed15756..dd30f94 100644 --- a/cmd/setup_test.go +++ b/cmd/setup_test.go @@ -38,7 +38,7 @@ func TestSetup(t *testing.T) { _ = fakeHandler.setup() - assert.Contains(t, buffer.String(), color.Green+"commit-message-check successfully installed."+color.Reset) + assert.Contains(t, buffer.String(), color.Green+"commit-message-check successfully installed.") }) }) @@ -53,6 +53,6 @@ func TestSetup(t *testing.T) { status := handler.setup() assert.Equal(t, 1, status) - assert.Contains(t, buffer.String(), color.Red+"Could not create commit-msg script."+color.Reset) + assert.Contains(t, buffer.String(), color.Red+"Could not create commit-msg script.") }) } From 4d4f03b3a39591845e433193ee1c170f761406a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Thu, 4 May 2023 09:34:32 +0200 Subject: [PATCH 38/45] Use notify in uninstall command --- cmd/uninstall.go | 5 ++--- cmd/uninstall_test.go | 13 ++++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cmd/uninstall.go b/cmd/uninstall.go index 6a558af..142b245 100644 --- a/cmd/uninstall.go +++ b/cmd/uninstall.go @@ -1,17 +1,16 @@ package cmd import ( - "log" "rockstaedt/commit-message-check/util" ) 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 } diff --git a/cmd/uninstall_test.go b/cmd/uninstall_test.go index 1000658..d206d11 100644 --- a/cmd/uninstall_test.go +++ b/cmd/uninstall_test.go @@ -3,8 +3,8 @@ package cmd import ( "bytes" "fmt" + "github.com/TwiN/go-color" "github.com/stretchr/testify/assert" - "log" "os" "rockstaedt/commit-message-check/internal/model" "testing" @@ -12,7 +12,6 @@ import ( func TestUninstall(t *testing.T) { buffer := &bytes.Buffer{} - log.SetOutput(buffer) createFakeHandlerWithDirs := func() *Handler { path := t.TempDir() @@ -25,7 +24,10 @@ func TestUninstall(t *testing.T) { _, err = os.Create(fmt.Sprintf("%s/xyz/commit-msg", path)) assert.Nil(t, err) - return NewHandler(model.Config{GitPath: path}) + handler := NewHandler(model.Config{GitPath: path}) + handler.Writer = buffer + + return handler } t.Run("returns 0 and", func(t *testing.T) { @@ -47,7 +49,7 @@ func TestUninstall(t *testing.T) { _ = 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.") }) }) @@ -57,10 +59,11 @@ func TestUninstall(t *testing.T) { err := os.Mkdir(fmt.Sprintf("%s/hooks", errPath), 0000) assert.Nil(t, err) handler := NewHandler(model.Config{GitPath: errPath}) + handler.Writer = buffer 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.") }) } From c686afbc54d7a330c72e63cb0220146471ff9db9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Thu, 4 May 2023 09:37:44 +0200 Subject: [PATCH 39/45] Use notify in update command --- cmd/handler_test.go | 1 + cmd/update.go | 4 ++-- cmd/update_test.go | 7 +++++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 088098c..ae236f6 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -45,6 +45,7 @@ func TestRun(t *testing.T) { t.Run("executes update command", func(t *testing.T) { buffer.Reset() myHandler := NewHandler(model.Config{}) + myHandler.Writer = buffer status := myHandler.Run("update") diff --git a/cmd/update.go b/cmd/update.go index 722535e..7893652 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -18,12 +18,12 @@ type responseData struct { func (h *Handler) update() int { h.Config.LatestVersion = getLatestTag(h.Config.TagUrl) if h.Config.LatestVersion == "" { - log.Println("Error at retrieving latest version.") + h.notify("Error at retrieving latest version.", "red") return 1 } if h.Config.Version == h.Config.LatestVersion { - log.Println("Current version is latest version.") + h.notify("Current version is latest version.") return 0 } diff --git a/cmd/update_test.go b/cmd/update_test.go index 6cd1358..0014736 100644 --- a/cmd/update_test.go +++ b/cmd/update_test.go @@ -3,6 +3,7 @@ package cmd import ( "bytes" "fmt" + "github.com/TwiN/go-color" "github.com/stretchr/testify/assert" "log" "net/http" @@ -15,7 +16,6 @@ import ( func TestUpdate(t *testing.T) { buffer := &bytes.Buffer{} - log.SetOutput(buffer) t.Run("returns 0 and", func(t *testing.T) { @@ -24,6 +24,7 @@ func TestUpdate(t *testing.T) { ts := httptest.NewServer(getHandlerFor(`{"tag_name":"v1.0.0"}`)) defer ts.Close() handler := NewHandler(model.Config{Version: "v1.0.0", TagUrl: ts.URL}) + handler.Writer = buffer status := handler.update() @@ -36,6 +37,7 @@ func TestUpdate(t *testing.T) { defer ts.Close() tempDir := t.TempDir() handler := NewHandler(model.Config{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: tempDir}) + handler.Writer = buffer _ = handler.update() @@ -48,11 +50,12 @@ func TestUpdate(t *testing.T) { ts := httptest.NewServer(getHandlerFor("", 500)) defer ts.Close() handler := NewHandler(model.Config{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: ""}) + handler.Writer = buffer status := handler.update() assert.Equal(t, 1, status) - assert.Contains(t, buffer.String(), "Error at retrieving latest version.") + assert.Contains(t, buffer.String(), color.Red+"Error at retrieving latest version.") }) } From a6e18b0e60a47f8186c7a35a11084ed41306f5ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Thu, 4 May 2023 09:56:08 +0200 Subject: [PATCH 40/45] Avoid usage of log --- cmd/update.go | 26 +++++++++++---------- cmd/update_test.go | 56 ++++++++++++++++++++++------------------------ 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/cmd/update.go b/cmd/update.go index 7893652..92c4154 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -3,8 +3,8 @@ package cmd import ( "encoding/json" "fmt" + "github.com/TwiN/go-color" "io" - "log" "net/http" "os" "rockstaedt/commit-message-check/internal/model" @@ -27,7 +27,14 @@ func (h *Handler) update() int { return 0 } - return downloadScript(&h.Config) + statusMsg := downloadScript(&h.Config) + if statusMsg == "" { + return 1 + } + + h.notify(color.Green + statusMsg) + + return 0 } func getLatestTag(url string) string { @@ -49,29 +56,24 @@ func getLatestTag(url string) string { return data.TagName } -func downloadScript(config *model.Config) int { +func downloadScript(config *model.Config) string { file, err := os.Create(config.DownloadPath + "/commit-message-check") if err != nil { - return 1 + return "" } res, err := http.Get(getBinaryUrl(config)) if err != nil { - return 2 + return "" } if res.StatusCode != 200 { - return 3 + return "" } _, _ = io.Copy(file, res.Body) - log.Printf( - "[SUCCESS]\t Updated commit-message-check successfully to %s", - config.LatestVersion, - ) - - return 0 + return "Updated commit-message-check successfully to " + config.LatestVersion } func getBinaryUrl(config *model.Config) string { diff --git a/cmd/update_test.go b/cmd/update_test.go index 0014736..1ef465a 100644 --- a/cmd/update_test.go +++ b/cmd/update_test.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/TwiN/go-color" "github.com/stretchr/testify/assert" - "log" "net/http" "net/http/httptest" "os" @@ -121,9 +120,7 @@ func TestDownloadScript(t *testing.T) { return protectedPath } - t.Run("returns 0 and writes downloaded binary content to file", func(t *testing.T) { - buffer := &bytes.Buffer{} - log.SetOutput(buffer) + t.Run("returns success message and writes downloaded binary content to file", func(t *testing.T) { tempDir := t.TempDir() err := os.WriteFile(tempDir+"/dummy", []byte("i am a go binary"), os.ModePerm) assert.Nil(t, err) @@ -136,44 +133,45 @@ func TestDownloadScript(t *testing.T) { defer ts.Close() config := &model.Config{LatestVersion: "v1.1.1", DownloadPath: tempDir, BinaryBaseUrl: ts.URL} - status := downloadScript(config) + msg := downloadScript(config) - assert.Equal(t, 0, status) contentBytes, err := os.ReadFile(tempDir + "/commit-message-check") assert.Nil(t, err) assert.Contains(t, string(contentBytes), "i am a go binary") - wantedUpdateMsg := "[SUCCESS]\t Updated commit-message-check " + - "successfully to v1.1.1" - assert.Contains(t, buffer.String(), wantedUpdateMsg) + wantedUpdateMsg := "Updated commit-message-check successfully to v1.1.1" + assert.Contains(t, msg, wantedUpdateMsg) }) - t.Run("returns 1 when error at creating file", func(t *testing.T) { - config := &model.Config{DownloadPath: getProtectedPath(t)} + t.Run("returns empty string when", func(t *testing.T) { - status := downloadScript(config) + t.Run("error at creating file", func(t *testing.T) { + config := &model.Config{DownloadPath: getProtectedPath(t)} - assert.Equal(t, 1, status) - }) + msg := downloadScript(config) - t.Run("return 2 when http protocol error", func(t *testing.T) { - tempDir := t.TempDir() - config := &model.Config{DownloadPath: tempDir, BinaryBaseUrl: "/xxx"} + assert.Equal(t, "", msg) + }) - status := downloadScript(config) + t.Run("http protocol error", func(t *testing.T) { + tempDir := t.TempDir() + config := &model.Config{DownloadPath: tempDir, BinaryBaseUrl: "/xxx"} - assert.Equal(t, 2, status) - }) + msg := downloadScript(config) - t.Run("return 3 when request not successfully", func(t *testing.T) { - tempDir := t.TempDir() - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - w.WriteHeader(500) - })) - defer ts.Close() - config := &model.Config{DownloadPath: tempDir, BinaryBaseUrl: ts.URL} + assert.Equal(t, "", msg) + }) - status := downloadScript(config) + t.Run("request not successfully", func(t *testing.T) { + tempDir := t.TempDir() + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(500) + })) + defer ts.Close() + config := &model.Config{DownloadPath: tempDir, BinaryBaseUrl: ts.URL} + + msg := downloadScript(config) - assert.Equal(t, 3, status) + assert.Equal(t, "", msg) + }) }) } From dcba66c4465e4ecd07dab1df17ccd422bd584897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Thu, 13 Jul 2023 17:16:21 +0200 Subject: [PATCH 41/45] Colorize text output in yellow --- cmd/handler.go | 4 ++++ cmd/handler_test.go | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/cmd/handler.go b/cmd/handler.go index 5ed01e1..baa7ca3 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -45,6 +45,10 @@ func (h *Handler) notify(message string, txtColor ...string) { 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!") diff --git a/cmd/handler_test.go b/cmd/handler_test.go index ae236f6..296b753 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -111,6 +111,15 @@ func TestNotify(t *testing.T) { 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) { From 107944b68700e620ea1966e6a034ad7fe69fecc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Thu, 13 Jul 2023 17:24:33 +0200 Subject: [PATCH 42/45] Use notify in validate command In this refactor, I also omitted the success message to align with unix standards. --- cmd/handler_test.go | 2 +- cmd/validate.go | 12 +++++------- cmd/validate_test.go | 21 ++++++++++++--------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/cmd/handler_test.go b/cmd/handler_test.go index 296b753..f82729d 100644 --- a/cmd/handler_test.go +++ b/cmd/handler_test.go @@ -63,7 +63,7 @@ func TestRun(t *testing.T) { myHandler.Run("validate") - assert.Contains(t, buffer.String(), "Valid commit message") + assert.Equal(t, 0, buffer.Len()) }) t.Run("prints warning when any other command", func(t *testing.T) { diff --git a/cmd/validate.go b/cmd/validate.go index 647b86b..0f92c62 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -1,8 +1,8 @@ package cmd import ( + "fmt" "github.com/rockstaedt/txtreader" - "log" "rockstaedt/commit-message-check/internal/model" ) @@ -12,11 +12,9 @@ const ( ) func (h *Handler) validate() int { - log.Println("[INFO]\t Validate commit message.") - commitLines, err := txtreader.GetLinesFromTextFile(h.Config.CommitMsgFile) if err != nil { - log.Printf("Could not read commit message: %q", err.Error()) + h.notify(fmt.Sprintf("Could not read commit message: %q", err.Error()), "red") return 1 } @@ -24,16 +22,16 @@ func (h *Handler) validate() int { numOfExceedingChars := cm.ValidateSubject() if numOfExceedingChars == 0 { - log.Println("[SUCCESS]\t Valid commit message.") return 0 } if numOfExceedingChars > (hardLimit - softLimit) { - log.Println("[ERROR]\t Abort commit. Subject line too long. Please fix.") + h.notify("Abort commit. Subject line too long. Please fix.", "red") return 1 } - log.Printf("[WARN]\t Your subject exceeds the soft limit of 50 chars by %d chars.", numOfExceedingChars) + message := fmt.Sprintf("Your subject exceeds the soft limit of 50 chars by %d chars.", numOfExceedingChars) + h.notify(message, "yellow") return 0 } diff --git a/cmd/validate_test.go b/cmd/validate_test.go index 87815fe..09b221c 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -2,8 +2,8 @@ package cmd import ( "bytes" + "github.com/TwiN/go-color" "github.com/stretchr/testify/assert" - "log" "os" "rockstaedt/commit-message-check/internal/model" "testing" @@ -11,7 +11,6 @@ import ( func TestValidate(t *testing.T) { buffer := &bytes.Buffer{} - log.SetOutput(buffer) t.Run("returns 0 on success", func(t *testing.T) { buffer.Reset() @@ -19,10 +18,11 @@ func TestValidate(t *testing.T) { err := os.WriteFile(testFile, []byte("i am a short commit msg"), 0666) assert.Nil(t, err) handler := NewHandler(model.Config{CommitMsgFile: testFile}) + handler.Writer = buffer handler.Run("validate") - assert.Contains(t, buffer.String(), "Valid commit message") + assert.Equal(t, 0, buffer.Len()) }) t.Run("returns 0 when soft limit exceeds and logs a warning", func(t *testing.T) { @@ -31,11 +31,12 @@ func TestValidate(t *testing.T) { err := os.WriteFile(testFile, []byte("i am two characters more thaaaaaaaaaaaaaaaaaaaaan 50"), 0666) assert.Nil(t, err) handler := NewHandler(model.Config{CommitMsgFile: testFile}) + handler.Writer = buffer status := handler.validate() assert.Equal(t, status, 0) - assert.Contains(t, buffer.String(), "[WARN]\t Your subject exceeds the soft limit of 50 chars by 2 chars.") + assert.Contains(t, buffer.String(), color.Yellow+"Your subject exceeds the soft limit of 50 chars by 2 chars.") }) t.Run("returns 1 when commit message too long", func(t *testing.T) { @@ -45,19 +46,21 @@ func TestValidate(t *testing.T) { "looooooooooooooooooooooong" err := os.WriteFile(testFile, []byte(content), 0666) assert.Nil(t, err) - myHandler := NewHandler(model.Config{CommitMsgFile: testFile}) + handler := NewHandler(model.Config{CommitMsgFile: testFile}) + handler.Writer = buffer - status := myHandler.Run("validate") + status := handler.Run("validate") - assert.Contains(t, buffer.String(), "Abort commit") + assert.Contains(t, buffer.String(), color.Red+"Abort commit") assert.Equal(t, 1, status) }) t.Run("returns 1 when error at reading file", func(t *testing.T) { buffer.Reset() - myHandler := NewHandler(model.Config{CommitMsgFile: "/no_file"}) + handler := NewHandler(model.Config{CommitMsgFile: "/no_file"}) + handler.Writer = buffer - status := myHandler.Run("validate") + status := handler.Run("validate") want := `Could not read commit message: "file not found"` assert.Contains(t, buffer.String(), want) From 30c1cad3308137d18e8aad2999656ac70b349ad4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Fri, 21 Jul 2023 17:11:08 +0200 Subject: [PATCH 43/45] Increase coverage to 100% --- cmd/update.go | 1 + cmd/update_test.go | 58 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/cmd/update.go b/cmd/update.go index 92c4154..e22e99e 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -29,6 +29,7 @@ func (h *Handler) update() int { statusMsg := downloadScript(&h.Config) if statusMsg == "" { + h.notify("Error while downloading binary.", "red") return 1 } diff --git a/cmd/update_test.go b/cmd/update_test.go index 1ef465a..0a691bd 100644 --- a/cmd/update_test.go +++ b/cmd/update_test.go @@ -32,29 +32,63 @@ func TestUpdate(t *testing.T) { }) t.Run("downloads install script if newer version available", func(t *testing.T) { - ts := httptest.NewServer(getHandlerFor(`{"tag_name":"v1.1.0"}`)) - defer ts.Close() + buffer.Reset() + tsTag := httptest.NewServer(getHandlerFor(`{"tag_name":"v1.1.0"}`)) + defer tsTag.Close() + tsBinary := httptest.NewServer(getHandlerFor("i am a binary")) + defer tsBinary.Close() tempDir := t.TempDir() - handler := NewHandler(model.Config{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: tempDir}) + handler := NewHandler(model.Config{ + Version: "v1.0.0", + BinaryBaseUrl: tsBinary.URL, + TagUrl: tsTag.URL, + DownloadPath: tempDir, + }) handler.Writer = buffer - _ = handler.update() + status := handler.update() assert.FileExists(t, tempDir+"/commit-message-check") + assert.Equal(t, 0, status) + assert.Contains(t, buffer.String(), color.Green+"Updated commit-message-check") }) }) t.Run("returns 1 and message when error at request", func(t *testing.T) { - buffer.Reset() - ts := httptest.NewServer(getHandlerFor("", 500)) - defer ts.Close() - handler := NewHandler(model.Config{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: ""}) - handler.Writer = buffer - status := handler.update() + t.Run("for tag", func(t *testing.T) { + buffer.Reset() + ts := httptest.NewServer(getHandlerFor("", 500)) + defer ts.Close() + handler := NewHandler(model.Config{Version: "v1.0.0", TagUrl: ts.URL, DownloadPath: ""}) + handler.Writer = buffer + + status := handler.update() + + assert.Equal(t, 1, status) + assert.Contains(t, buffer.String(), color.Red+"Error at retrieving latest version.") + }) + + t.Run("for binary", func(t *testing.T) { + buffer.Reset() + tsTag := httptest.NewServer(getHandlerFor(`{"tag_name":"v1.1.0"}`)) + defer tsTag.Close() + tsBinary := httptest.NewServer(getHandlerFor("", 500)) + defer tsBinary.Close() + tempDir := t.TempDir() + handler := NewHandler(model.Config{ + Version: "v1.0.0", + BinaryBaseUrl: tsBinary.URL, + TagUrl: tsTag.URL, + DownloadPath: tempDir, + }) + handler.Writer = buffer - assert.Equal(t, 1, status) - assert.Contains(t, buffer.String(), color.Red+"Error at retrieving latest version.") + status := handler.update() + + assert.Equal(t, 1, status) + assert.Contains(t, buffer.String(), color.Red+"Error while downloading binary.") + }) }) } From bafec90b00797a1b9a6c347010e525a4b572cac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Fri, 21 Jul 2023 17:16:23 +0200 Subject: [PATCH 44/45] Visualize characters after soft limit --- cmd/validate.go | 2 ++ cmd/validate_test.go | 1 + 2 files changed, 3 insertions(+) diff --git a/cmd/validate.go b/cmd/validate.go index 0f92c62..3690b2d 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/TwiN/go-color" "github.com/rockstaedt/txtreader" "rockstaedt/commit-message-check/internal/model" ) @@ -32,6 +33,7 @@ func (h *Handler) validate() int { message := fmt.Sprintf("Your subject exceeds the soft limit of 50 chars by %d chars.", numOfExceedingChars) h.notify(message, "yellow") + h.notify(cm.Subject[:softLimit] + color.InYellow(cm.Subject[softLimit:])) return 0 } diff --git a/cmd/validate_test.go b/cmd/validate_test.go index 09b221c..3b54a86 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -37,6 +37,7 @@ func TestValidate(t *testing.T) { assert.Equal(t, status, 0) assert.Contains(t, buffer.String(), color.Yellow+"Your subject exceeds the soft limit of 50 chars by 2 chars.") + assert.Contains(t, buffer.String(), "i am two characters more thaaaaaaaaaaaaaaaaaaaaan "+color.Yellow+"50") }) t.Run("returns 1 when commit message too long", func(t *testing.T) { From 0b2e33577c9994feeaa65f3e6362a022f0d064db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Rockst=C3=A4dt?= Date: Tue, 25 Jul 2023 12:13:56 +0200 Subject: [PATCH 45/45] Harmonize structure of messsages --- cmd/update.go | 2 +- cmd/update_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/update.go b/cmd/update.go index e22e99e..a20ec8b 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -74,7 +74,7 @@ func downloadScript(config *model.Config) string { _, _ = io.Copy(file, res.Body) - return "Updated commit-message-check successfully to " + config.LatestVersion + return "commit-message-check updated successfully to " + config.LatestVersion } func getBinaryUrl(config *model.Config) string { diff --git a/cmd/update_test.go b/cmd/update_test.go index 0a691bd..aab35e8 100644 --- a/cmd/update_test.go +++ b/cmd/update_test.go @@ -50,7 +50,7 @@ func TestUpdate(t *testing.T) { assert.FileExists(t, tempDir+"/commit-message-check") assert.Equal(t, 0, status) - assert.Contains(t, buffer.String(), color.Green+"Updated commit-message-check") + assert.Contains(t, buffer.String(), "updated successfully to") }) }) @@ -172,7 +172,7 @@ func TestDownloadScript(t *testing.T) { contentBytes, err := os.ReadFile(tempDir + "/commit-message-check") assert.Nil(t, err) assert.Contains(t, string(contentBytes), "i am a go binary") - wantedUpdateMsg := "Updated commit-message-check successfully to v1.1.1" + wantedUpdateMsg := "commit-message-check updated successfully to v1.1.1" assert.Contains(t, msg, wantedUpdateMsg) })