From 60feb58ea0387635aee932e80448812a8038acc8 Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Mon, 15 Apr 2024 23:27:50 +0100 Subject: [PATCH 01/15] =?UTF-8?q?fix=20=F0=9F=90=9B:=20fix=20mod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: moualhi zine el abidine --- .goji.json | 2 +- README.md | 2 - cmd/root.go | 10 ++++- cmd/root_test.go | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ makefile | 4 -- 6 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 cmd/root_test.go delete mode 100644 makefile diff --git a/.goji.json b/.goji.json index 096056b..a07276a 100644 --- a/.goji.json +++ b/.goji.json @@ -76,4 +76,4 @@ "name": "package" } ] -} +} \ No newline at end of file diff --git a/README.md b/README.md index 552025f..2dfe12b 100644 --- a/README.md +++ b/README.md @@ -159,5 +159,3 @@ Apache 2.0 license [Zine El Abidine Moualhi](https://www.linkedin.com/in/zinemou Thanks to [@Simplifi-ED](https://www.simplified.fr) & @IT Challenge in letting me work on this open source side project and to my mentor [@EtienneDeneuve](https://github.com/EtienneDeneuve) for the help with learning Go lang. IT Challenge - - diff --git a/cmd/root.go b/cmd/root.go index 0dc9fab..285c588 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -109,7 +109,6 @@ var rootCmd = &cobra.Command{ func init() { rootCmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Display version information") - // rootCmd.Flags().BoolVarP(&signFlag, "sign-off", "S", false, "add a Signed-off-by trailer") rootCmd.Flags().StringVarP(&typeFlag, "type", "t", "", "Specify the type from the config file") rootCmd.Flags().StringVarP(&scopeFlag, "scope", "s", "", "Specify a custom scope") rootCmd.Flags().StringVarP(&messageFlag, "message", "m", "", "Specify a commit message") @@ -122,6 +121,15 @@ func Execute() { } } +// commit executes a git commit with the given message and body. +// +// Parameters: +// - message: the commit message. +// - body: the commit body. +// - sign: a boolean indicating whether to add a Signed-off-by trailer. +// +// Returns: +// - error: an error if the git commit execution fails. func commit(message, body string, sign bool) error { args := []string{"commit", "-m", message, "-m", body} if sign { diff --git a/cmd/root_test.go b/cmd/root_test.go new file mode 100644 index 0000000..19a2a25 --- /dev/null +++ b/cmd/root_test.go @@ -0,0 +1,96 @@ +package cmd + +import ( + "bytes" + "fmt" + "io" + "os" + "os/exec" + "testing" + + "github.com/alecthomas/assert/v2" + "github.com/fatih/color" + "github.com/spf13/cobra" +) + +func TestCommit(t *testing.T) { + t.Run("success", func(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + + tempDir, err := os.MkdirTemp("", "git-commit-test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tempDir) + + if err := os.Chdir(tempDir); err != nil { + t.Fatalf("error changing to temp dir: %v", err) + } + + if err := exec.Command("git", "init").Run(); err != nil { + t.Fatalf("error initializing git repo: %v", err) + } + + if err := os.WriteFile("testfile", []byte("test content"), 0644); err != nil { + t.Fatalf("error writing testfile: %v", err) + } + + if err := exec.Command("git", "add", "testfile").Run(); err != nil { + t.Fatalf("error adding testfile to index: %v", err) + } + + if err := commit("test commit", "test commit body", false); err != nil { + t.Fatalf("error committing: %v", err) + } + + if err := exec.Command("git", "log", "-1", "--pretty=%s").Run(); err != nil { + t.Fatalf("error checking commit: %v", err) + } + }) +} + +func TestRootCmd_VersionFlag(t *testing.T) { + + var versionFlag bool + // Save the original os.Stdout + originalStdout := os.Stdout + // Create a buffer to capture the output + r, w, _ := os.Pipe() + os.Stdout = w + + // Create a new command + cmd := &cobra.Command{ + Use: "goji", + Short: "Goji CLI", + Long: `Goji is a cli tool to generate conventional commits with emojis`, + Run: func(cmd *cobra.Command, args []string) { + if versionFlag { + color.Set(color.FgGreen) + fmt.Printf("goji version: v%s\n", version) + color.Unset() + return + } + }, + } + + // Set the version flag + cmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Display version information") + cmd.SetArgs([]string{"--version"}) + + // Execute the command + cmd.Execute() + + // Close the writer and restore os.Stdout + w.Close() + os.Stdout = originalStdout + + // Read the captured output + var buf bytes.Buffer + io.Copy(&buf, r) + output := buf.String() + + // Assert that the output contains the expected version string + assert.Contains(t, output, "goji version: v") +} diff --git a/go.mod b/go.mod index c16165b..cdef0bc 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/muandane/goji go 1.22.2 require ( + github.com/alecthomas/assert/v2 v2.2.1 github.com/charmbracelet/glamour v0.7.0 github.com/charmbracelet/huh v0.3.0 github.com/charmbracelet/huh/spinner v0.0.0-20240404200615-66118a2cb3cf @@ -18,6 +19,7 @@ require ( require ( github.com/alecthomas/chroma/v2 v2.8.0 // indirect + github.com/alecthomas/repr v0.2.0 // indirect github.com/atotto/clipboard v0.1.4 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/aymerick/douceur v0.2.0 // indirect @@ -31,6 +33,7 @@ require ( github.com/google/go-cmp v0.6.0 // indirect github.com/gorilla/css v1.0.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect + github.com/hexops/gotextdiff v1.0.3 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/magiconair/properties v1.8.7 // indirect diff --git a/makefile b/makefile deleted file mode 100644 index ac9cee4..0000000 --- a/makefile +++ /dev/null @@ -1,4 +0,0 @@ - -update-pkg-cache: - GOPROXY=https://proxy.golang.org GO111MODULE=on \ - go get github.com/$(USER)/$(PACKAGE)@v$(VERSION) \ No newline at end of file From 9f154688d73025eec32bf48e920439e10c031348 Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 08:50:01 +0100 Subject: [PATCH 02/15] =?UTF-8?q?test=20=F0=9F=A7=AA:=20noemojis=20=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: moualhi zine el abidine --- cmd/root_test.go | 188 +++++++++++++++++++++++------------------------ 1 file changed, 94 insertions(+), 94 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index 19a2a25..9b26430 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -1,96 +1,96 @@ package cmd -import ( - "bytes" - "fmt" - "io" - "os" - "os/exec" - "testing" - - "github.com/alecthomas/assert/v2" - "github.com/fatih/color" - "github.com/spf13/cobra" -) - -func TestCommit(t *testing.T) { - t.Run("success", func(t *testing.T) { - if testing.Short() { - t.Skip("skipping test in short mode.") - } - - tempDir, err := os.MkdirTemp("", "git-commit-test") - if err != nil { - t.Fatalf("error creating temp dir: %v", err) - } - defer os.RemoveAll(tempDir) - - if err := os.Chdir(tempDir); err != nil { - t.Fatalf("error changing to temp dir: %v", err) - } - - if err := exec.Command("git", "init").Run(); err != nil { - t.Fatalf("error initializing git repo: %v", err) - } - - if err := os.WriteFile("testfile", []byte("test content"), 0644); err != nil { - t.Fatalf("error writing testfile: %v", err) - } - - if err := exec.Command("git", "add", "testfile").Run(); err != nil { - t.Fatalf("error adding testfile to index: %v", err) - } - - if err := commit("test commit", "test commit body", false); err != nil { - t.Fatalf("error committing: %v", err) - } - - if err := exec.Command("git", "log", "-1", "--pretty=%s").Run(); err != nil { - t.Fatalf("error checking commit: %v", err) - } - }) -} - -func TestRootCmd_VersionFlag(t *testing.T) { - - var versionFlag bool - // Save the original os.Stdout - originalStdout := os.Stdout - // Create a buffer to capture the output - r, w, _ := os.Pipe() - os.Stdout = w - - // Create a new command - cmd := &cobra.Command{ - Use: "goji", - Short: "Goji CLI", - Long: `Goji is a cli tool to generate conventional commits with emojis`, - Run: func(cmd *cobra.Command, args []string) { - if versionFlag { - color.Set(color.FgGreen) - fmt.Printf("goji version: v%s\n", version) - color.Unset() - return - } - }, - } - - // Set the version flag - cmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Display version information") - cmd.SetArgs([]string{"--version"}) - - // Execute the command - cmd.Execute() - - // Close the writer and restore os.Stdout - w.Close() - os.Stdout = originalStdout - - // Read the captured output - var buf bytes.Buffer - io.Copy(&buf, r) - output := buf.String() - - // Assert that the output contains the expected version string - assert.Contains(t, output, "goji version: v") -} +// import ( +// "bytes" +// "fmt" +// "io" +// "os" +// "os/exec" +// "testing" + +// "github.com/alecthomas/assert/v2" +// "github.com/fatih/color" +// "github.com/spf13/cobra" +// ) + +// func TestCommit(t *testing.T) { +// t.Run("success", func(t *testing.T) { +// if testing.Short() { +// t.Skip("skipping test in short mode.") +// } + +// tempDir, err := os.MkdirTemp("", "git-commit-test") +// if err != nil { +// t.Fatalf("error creating temp dir: %v", err) +// } +// defer os.RemoveAll(tempDir) + +// if err := os.Chdir(tempDir); err != nil { +// t.Fatalf("error changing to temp dir: %v", err) +// } + +// if err := exec.Command("git", "init").Run(); err != nil { +// t.Fatalf("error initializing git repo: %v", err) +// } + +// if err := os.WriteFile("testfile", []byte("test content"), 0644); err != nil { +// t.Fatalf("error writing testfile: %v", err) +// } + +// if err := exec.Command("git", "add", "testfile").Run(); err != nil { +// t.Fatalf("error adding testfile to index: %v", err) +// } + +// if err := commit("test commit", "test commit body", false); err != nil { +// t.Fatalf("error committing: %v", err) +// } + +// if err := exec.Command("git", "log", "-1", "--pretty=%s").Run(); err != nil { +// t.Fatalf("error checking commit: %v", err) +// } +// }) +// } + +// func TestRootCmd_VersionFlag(t *testing.T) { + +// var versionFlag bool +// // Save the original os.Stdout +// originalStdout := os.Stdout +// // Create a buffer to capture the output +// r, w, _ := os.Pipe() +// os.Stdout = w + +// // Create a new command +// cmd := &cobra.Command{ +// Use: "goji", +// Short: "Goji CLI", +// Long: `Goji is a cli tool to generate conventional commits with emojis`, +// Run: func(cmd *cobra.Command, args []string) { +// if versionFlag { +// color.Set(color.FgGreen) +// fmt.Printf("goji version: v%s\n", version) +// color.Unset() +// return +// } +// }, +// } + +// // Set the version flag +// cmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Display version information") +// cmd.SetArgs([]string{"--version"}) + +// // Execute the command +// cmd.Execute() + +// // Close the writer and restore os.Stdout +// w.Close() +// os.Stdout = originalStdout + +// // Read the captured output +// var buf bytes.Buffer +// io.Copy(&buf, r) +// output := buf.String() + +// // Assert that the output contains the expected version string +// assert.Contains(t, output, "goji version: v") +// } From c8d654b95dd03a3c4030981f329ec1e9add4aa2c Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 08:50:27 +0100 Subject: [PATCH 03/15] =?UTF-8?q?test=20=F0=9F=A7=AA:=20noemojis=20=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: moualhi zine el abidine --- .goji.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.goji.json b/.goji.json index a07276a..e928373 100644 --- a/.goji.json +++ b/.goji.json @@ -1,5 +1,5 @@ { - "noemojis": false, + "noemojis": true, "scopes": [ "home", "accounts", @@ -76,4 +76,4 @@ "name": "package" } ] -} \ No newline at end of file +} From 357b7a81dbf0d6d6d9580b8793c26e09cdf4a2f8 Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 08:52:14 +0100 Subject: [PATCH 04/15] =?UTF-8?q?test=20=F0=9F=A7=AA:=20noemojis=20=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: moualhi zine el abidine --- cmd/root.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 285c588..f45683a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -48,13 +48,13 @@ var rootCmd = &cobra.Command{ var commitBody string if typeFlag != "" && messageFlag != "" { // If all flags are provided, construct the commit message from them - var typeMatch string + typeMatch := "" for _, t := range config.Types { if typeFlag == t.Name { if !config.NoEmoji { typeMatch = fmt.Sprintf("%s %s", t.Name, t.Emoji) } else { - typeMatch = fmt.Sprintf(t.Name) + typeMatch = t.Name } break } From 584d2d39dd1fb936e4cc90e75738015d29062bf0 Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 08:52:50 +0100 Subject: [PATCH 05/15] =?UTF-8?q?test=20=F0=9F=A7=AA:=20noemojis=20=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: moualhi zine el abidine --- cmd/root.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index f45683a..7ec0352 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -60,10 +60,10 @@ var rootCmd = &cobra.Command{ } } - // If no match was found, use the type flag as is - if typeMatch == "" { - typeMatch = typeFlag - } + // // If no match was found, use the type flag as is + // if typeMatch == "" { + // typeMatch = typeFlag + // } // Construct the commit message from the flags commitMessage = messageFlag From 292749056be8c427fe3f0ddf948b3ab2d8e022aa Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 08:57:10 +0100 Subject: [PATCH 06/15] =?UTF-8?q?test=20=F0=9F=A7=AA:=20noemojis=20=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: moualhi zine el abidine --- cmd/root.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 7ec0352..f45683a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -60,10 +60,10 @@ var rootCmd = &cobra.Command{ } } - // // If no match was found, use the type flag as is - // if typeMatch == "" { - // typeMatch = typeFlag - // } + // If no match was found, use the type flag as is + if typeMatch == "" { + typeMatch = typeFlag + } // Construct the commit message from the flags commitMessage = messageFlag From fa6dd2e152a9ff8e09dbcf416054f79144bdd71e Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 08:59:11 +0100 Subject: [PATCH 07/15] =?UTF-8?q?test=20=F0=9F=A7=AA:=20noemojis=20=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: moualhi zine el abidine --- pkg/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index ddacc67..35c6a3c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -19,7 +19,7 @@ func ViperConfig() (*Config, error) { homeDir, _ := os.UserHomeDir() if _, err = os.Stat(filepath.Join(gitDir, ".goji.json")); err == nil { viper.AddConfigPath(gitDir) - // fmt.Printf("using repo conf") // used for debug + fmt.Printf("using repo conf") // used for debug } else { viper.AddConfigPath(homeDir) // fmt.Printf("using home conf") // used for debug From f883ab56d5ce5c1b7d750c072b93573480b7c889 Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 09:01:11 +0100 Subject: [PATCH 08/15] =?UTF-8?q?test=20=F0=9F=A7=AA:=20noemojis=20=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: moualhi zine el abidine --- pkg/config/config.go | 1 - pkg/config/configType.go | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 35c6a3c..ecbf352 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -19,7 +19,6 @@ func ViperConfig() (*Config, error) { homeDir, _ := os.UserHomeDir() if _, err = os.Stat(filepath.Join(gitDir, ".goji.json")); err == nil { viper.AddConfigPath(gitDir) - fmt.Printf("using repo conf") // used for debug } else { viper.AddConfigPath(homeDir) // fmt.Printf("using home conf") // used for debug diff --git a/pkg/config/configType.go b/pkg/config/configType.go index e4b0b40..49811c4 100644 --- a/pkg/config/configType.go +++ b/pkg/config/configType.go @@ -23,10 +23,10 @@ type Gitmoji struct { Name string `json:"name"` } type initConfig struct { - Types []Gitmoji `json:"Types"` - Scopes []string `json:"Scopes"` - SkipQuestions []string `json:"SkipQuestions"` - SubjectMaxLength int `json:"SubjectMaxLength"` - SignOff bool `json:"SignOff"` - NoEmoji bool `json:"NoEmoji"` + Types []Gitmoji `json:"types"` + Scopes []string `json:"scopes"` + SkipQuestions []string `json:"skipQuestions"` + SubjectMaxLength int `json:"subjectMaxLength"` + SignOff bool `json:"signOff"` + NoEmoji bool `json:"noEmoji"` } From 4f77befe0fe446923a2601909ef7d03aafdabd9f Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 09:01:33 +0100 Subject: [PATCH 09/15] =?UTF-8?q?test=20=F0=9F=A7=AA:=20noemojis=20=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: moualhi zine el abidine --- .goji.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.goji.json b/.goji.json index e928373..096056b 100644 --- a/.goji.json +++ b/.goji.json @@ -1,5 +1,5 @@ { - "noemojis": true, + "noemojis": false, "scopes": [ "home", "accounts", From e199de7c8a1b8d24748301fffbe36ef23a0501cf Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 09:02:06 +0100 Subject: [PATCH 10/15] =?UTF-8?q?test=20=F0=9F=A7=AA:=20noemojis=20=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: moualhi zine el abidine --- .goji.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.goji.json b/.goji.json index 096056b..e928373 100644 --- a/.goji.json +++ b/.goji.json @@ -1,5 +1,5 @@ { - "noemojis": false, + "noemojis": true, "scopes": [ "home", "accounts", From 93d766e450d15ed70ccbedd292714bb420c27e36 Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 09:05:05 +0100 Subject: [PATCH 11/15] test: noemojis ? Signed-off-by: moualhi zine el abidine --- .goji.json | 2 +- pkg/config/configInit.go | 2 +- pkg/config/configType.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.goji.json b/.goji.json index e928373..a3e40cb 100644 --- a/.goji.json +++ b/.goji.json @@ -1,5 +1,5 @@ { - "noemojis": true, + "noemoji": true, "scopes": [ "home", "accounts", diff --git a/pkg/config/configInit.go b/pkg/config/configInit.go index 3b29355..f5a14dd 100644 --- a/pkg/config/configInit.go +++ b/pkg/config/configInit.go @@ -46,7 +46,7 @@ func SaveConfigToFile(config initConfig, file, dir string) error { viper.Set("skipQuestions", config.SkipQuestions) viper.Set("subjectMaxLength", config.SubjectMaxLength) viper.Set("signOff", config.SignOff) - viper.Set("noEmojis", config.NoEmoji) + viper.Set("noemoji", config.NoEmoji) viper.SetConfigName(file) viper.SetConfigType("json") diff --git a/pkg/config/configType.go b/pkg/config/configType.go index 49811c4..ce0c142 100644 --- a/pkg/config/configType.go +++ b/pkg/config/configType.go @@ -28,5 +28,5 @@ type initConfig struct { SkipQuestions []string `json:"skipQuestions"` SubjectMaxLength int `json:"subjectMaxLength"` SignOff bool `json:"signOff"` - NoEmoji bool `json:"noEmoji"` + NoEmoji bool `json:"noemoji"` } From 2c47a9b28e0cff10b56eece3a2dad93755b1dc9f Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 09:07:15 +0100 Subject: [PATCH 12/15] =?UTF-8?q?fix=20=F0=9F=90=9B:=20noemojis=20=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: moualhi zine el abidine --- .goji.json | 2 +- cmd/root_test.go | 193 ++++++++++++++++++++++++----------------------- 2 files changed, 100 insertions(+), 95 deletions(-) diff --git a/.goji.json b/.goji.json index a3e40cb..2f91f76 100644 --- a/.goji.json +++ b/.goji.json @@ -1,5 +1,5 @@ { - "noemoji": true, + "noemoji": false, "scopes": [ "home", "accounts", diff --git a/cmd/root_test.go b/cmd/root_test.go index 9b26430..6d18366 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -1,96 +1,101 @@ package cmd -// import ( -// "bytes" -// "fmt" -// "io" -// "os" -// "os/exec" -// "testing" - -// "github.com/alecthomas/assert/v2" -// "github.com/fatih/color" -// "github.com/spf13/cobra" -// ) - -// func TestCommit(t *testing.T) { -// t.Run("success", func(t *testing.T) { -// if testing.Short() { -// t.Skip("skipping test in short mode.") -// } - -// tempDir, err := os.MkdirTemp("", "git-commit-test") -// if err != nil { -// t.Fatalf("error creating temp dir: %v", err) -// } -// defer os.RemoveAll(tempDir) - -// if err := os.Chdir(tempDir); err != nil { -// t.Fatalf("error changing to temp dir: %v", err) -// } - -// if err := exec.Command("git", "init").Run(); err != nil { -// t.Fatalf("error initializing git repo: %v", err) -// } - -// if err := os.WriteFile("testfile", []byte("test content"), 0644); err != nil { -// t.Fatalf("error writing testfile: %v", err) -// } - -// if err := exec.Command("git", "add", "testfile").Run(); err != nil { -// t.Fatalf("error adding testfile to index: %v", err) -// } - -// if err := commit("test commit", "test commit body", false); err != nil { -// t.Fatalf("error committing: %v", err) -// } - -// if err := exec.Command("git", "log", "-1", "--pretty=%s").Run(); err != nil { -// t.Fatalf("error checking commit: %v", err) -// } -// }) -// } - -// func TestRootCmd_VersionFlag(t *testing.T) { - -// var versionFlag bool -// // Save the original os.Stdout -// originalStdout := os.Stdout -// // Create a buffer to capture the output -// r, w, _ := os.Pipe() -// os.Stdout = w - -// // Create a new command -// cmd := &cobra.Command{ -// Use: "goji", -// Short: "Goji CLI", -// Long: `Goji is a cli tool to generate conventional commits with emojis`, -// Run: func(cmd *cobra.Command, args []string) { -// if versionFlag { -// color.Set(color.FgGreen) -// fmt.Printf("goji version: v%s\n", version) -// color.Unset() -// return -// } -// }, -// } - -// // Set the version flag -// cmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Display version information") -// cmd.SetArgs([]string{"--version"}) - -// // Execute the command -// cmd.Execute() - -// // Close the writer and restore os.Stdout -// w.Close() -// os.Stdout = originalStdout - -// // Read the captured output -// var buf bytes.Buffer -// io.Copy(&buf, r) -// output := buf.String() - -// // Assert that the output contains the expected version string -// assert.Contains(t, output, "goji version: v") -// } +import ( + "bytes" + "fmt" + "io" + "os" + "os/exec" + "testing" + + "github.com/alecthomas/assert/v2" + "github.com/fatih/color" + "github.com/spf13/cobra" +) + +func TestCommit(t *testing.T) { + t.Run("success", func(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + + tempDir, err := os.MkdirTemp("", "git-commit-test") + if err != nil { + t.Fatalf("error creating temp dir: %v", err) + } + defer os.RemoveAll(tempDir) + + if err := os.Chdir(tempDir); err != nil { + t.Fatalf("error changing to temp dir: %v", err) + } + + if err := exec.Command("git", "init").Run(); err != nil { + t.Fatalf("error initializing git repo: %v", err) + } + + if err := os.WriteFile("testfile", []byte("test content"), 0644); err != nil { + t.Fatalf("error writing testfile: %v", err) + } + + if err := exec.Command("git", "add", "testfile").Run(); err != nil { + t.Fatalf("error adding testfile to index: %v", err) + } + + if err := commit("test commit", "test commit body", false); err != nil { + t.Fatalf("error committing: %v", err) + } + + if err := exec.Command("git", "log", "-1", "--pretty=%s").Run(); err != nil { + t.Fatalf("error checking commit: %v", err) + } + }) +} + +func TestRootCmd_VersionFlag(t *testing.T) { + + var versionFlag bool + // Save the original os.Stdout + originalStdout := os.Stdout + // Create a buffer to capture the output + r, w, _ := os.Pipe() + os.Stdout = w + + // Create a new command + cmd := &cobra.Command{ + Use: "goji", + Short: "Goji CLI", + Long: `Goji is a cli tool to generate conventional commits with emojis`, + Run: func(cmd *cobra.Command, args []string) { + if versionFlag { + color.Set(color.FgGreen) + fmt.Printf("goji version: v%s\n", version) + color.Unset() + return + } + }, + } + + // Set the version flag + cmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Display version information") + cmd.SetArgs([]string{"--version"}) + + // Execute the command + if err := cmd.Execute(); err != nil { + t.Fatalf("Error executing command: %v", err) + } + + // Close the writer and restore os.Stdout + w.Close() + os.Stdout = originalStdout + + // Read the captured output + var buf bytes.Buffer + _, err := io.Copy(&buf, r) + if err != nil { + t.Fatalf("Error reading captured output: %v", err) + } + output := buf.String() + + // Assert that the output contains the expected version string + assert.Contains(t, output, "goji version: v") +} From 81f4f31852b9c407a8e40df4dc3cb88a838452a3 Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 09:11:37 +0100 Subject: [PATCH 13/15] =?UTF-8?q?fix=20=F0=9F=90=9B:=20updated=20branch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .goji.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.goji.json b/.goji.json index 2f91f76..39cfd4a 100644 --- a/.goji.json +++ b/.goji.json @@ -5,7 +5,7 @@ "accounts", "ci" ], - "signoff": true, + "signoff": false, "skipquestions": null, "subjectmaxlength": 100, "types": [ From cb490928d0dcc574d4f74e14853f85d1308bfb97 Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 09:13:58 +0100 Subject: [PATCH 14/15] =?UTF-8?q?fix=20=F0=9F=90=9B:=20issue=20with=20gh?= =?UTF-8?q?=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/coverage.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 71a310c..a0379ae 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -10,6 +10,10 @@ jobs: go-version: '1.22.1' - name: Check out code uses: actions/checkout@v4.1.1 + - name: Set up Git + run: | + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" - name: Test and generate coverage report run: go test -coverprofile=coverage.out ./... - name: Upload coverage reports to Codecov From 79dd62b727f72122bb5521f840474d36890b875e Mon Sep 17 00:00:00 2001 From: moualhi zine el abidine Date: Sat, 1 Jun 2024 09:16:22 +0100 Subject: [PATCH 15/15] =?UTF-8?q?fix=20=F0=9F=90=9B:=20issue=20with=20gh?= =?UTF-8?q?=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/coverage.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 9ccd542..d41a1fd 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -12,8 +12,8 @@ jobs: uses: actions/checkout@v4.1.6 - name: Set up Git run: | - git config --local user.email "action@github.com" - git config --local user.name "GitHub Action" + git config --global user.email "action@github.com" + git config --global user.name "GitHub Action" - name: Test and generate coverage report run: go test -coverprofile=coverage.out ./... - name: Upload coverage reports to Codecov