Skip to content

Commit

Permalink
check git configuration before exec world create
Browse files Browse the repository at this point in the history
  • Loading branch information
zulkhair committed Aug 27, 2024
1 parent ef6982b commit e00d350
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cmd/world/root/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package root

import (
"io"
"os/exec"
"strings"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/rotisserie/eris"
"github.com/spf13/cobra"

"pkg.world.dev/world-cli/common/teacmd"
Expand All @@ -15,6 +17,9 @@ import (

const TemplateGitURL = "https://github.com/Argus-Labs/starter-game-template.git"

// execCommand is a variable to allow mocking exec.Command in tests
var execCommand = exec.Command

/////////////////////////
// Bubble Tea Commands //
/////////////////////////
Expand Down Expand Up @@ -204,6 +209,11 @@ Otherwise, it will prompt you to enter a directory name.`,
GroupID: "starter",
Args: cobra.MaximumNArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
err := checkGitConfig()
if err != nil {
return err
}

p := tea.NewProgram(NewWorldCreateModel(args), tea.WithOutput(writer))
if _, err := p.Run(); err != nil {
return err
Expand All @@ -214,3 +224,21 @@ Otherwise, it will prompt you to enter a directory name.`,

return createCmd
}

func checkGitConfig() error {
// Check Git user name
cmdUserName := execCommand("git", "config", "--get", "user.name")
userName, err := cmdUserName.Output()
if err != nil || (userName != nil && strings.TrimSpace(string(userName)) == "") {
return eris.New("retrieving Git user name, please set it using `git config --global user.name 'Your Name'`")
}

// Check Git user email
cmdUserEmail := execCommand("git", "config", "--get", "user.email")
userEmail, err := cmdUserEmail.Output()
if err != nil || (userEmail != nil && strings.TrimSpace(string(userEmail)) == "") {
return eris.New("retrieving Git user email, please set it using `git config --global user.email 'Your Email'`")
}

return nil
}
59 changes: 59 additions & 0 deletions cmd/world/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net"
"os"
"os/exec"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -303,3 +304,61 @@ func TestEVMStart(t *testing.T) {
// Check and wait until evm is down
assert.Assert(t, evmIsDown(t), "EVM is not successfully shutdown")
}

func TestCheckGitConfig(t *testing.T) {
t.Run("Real Scenario", func(t *testing.T) {
// Test when both user name and email with real values
err := checkGitConfig()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
})

t.Run("Error Username", func(t *testing.T) {
// Test when user name is not set
execCommand = fakeExecCommandGetGitUsername
err := checkGitConfig()
assert.Error(t, err, "retrieving Git user name, please set it using `git config --global user.name 'Your Name'`")
})

t.Run("Error Email", func(t *testing.T) {
// Test when user name is not set
execCommand = fakeExecCommandGetGitEmail
err := checkGitConfig()
assert.Error(t, err, "retrieving Git user email, please set it using `git config --global user.email 'Your Email'`")
})
}

// fakeExecCommand is a fake version of exec.Command that simulates git config output
func fakeExecCommandGetGitUsername(command string, args ...string) *exec.Cmd {
commandStr := fmt.Sprintf("%s %s", command, strings.Join(args, " "))

var cmd *exec.Cmd
switch commandStr {
case "git config --get user.name":
cmd = exec.Command("echo")
case "git config --get user.email":
cmd = exec.Command("echo", "[email protected]")
default:
cmd = exec.Command("echo")
}

return cmd
}

// fakeExecCommand is a fake version of exec.Command that simulates git config output
func fakeExecCommandGetGitEmail(command string, args ...string) *exec.Cmd {
commandStr := fmt.Sprintf("%s %s", command, strings.Join(args, " "))

var cmd *exec.Cmd
switch commandStr {
case "git config --get user.name":
cmd = exec.Command("echo", "Test User")
case "git config --get user.email":
cmd = exec.Command("echo")
default:
cmd = exec.Command("echo")
}

return cmd
}

0 comments on commit e00d350

Please sign in to comment.