Skip to content

Commit

Permalink
fix 🐛: fix mod
Browse files Browse the repository at this point in the history
Signed-off-by: moualhi zine el abidine <[email protected]>
  • Loading branch information
muandane committed Apr 15, 2024
1 parent 8289b2f commit 60feb58
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .goji.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@
"name": "package"
}
]
}
}
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<img align="center" src="public/logo.svg" alt="IT Challenge" width="200"/>

<!-- adding pre-commit hook doc for goji check -->
10 changes: 9 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down
96 changes: 96 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions makefile

This file was deleted.

0 comments on commit 60feb58

Please sign in to comment.