diff --git a/cmd/world/root/create.go b/cmd/world/root/create.go index 0e01512..282fa06 100644 --- a/cmd/world/root/create.go +++ b/cmd/world/root/create.go @@ -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" @@ -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 // ///////////////////////// @@ -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 @@ -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 +} diff --git a/cmd/world/root/root_test.go b/cmd/world/root/root_test.go index 49e49d3..ae4caea 100644 --- a/cmd/world/root/root_test.go +++ b/cmd/world/root/root_test.go @@ -7,6 +7,7 @@ import ( "fmt" "net" "os" + "os/exec" "strings" "testing" "time" @@ -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("Success 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@email.email") + 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 +} diff --git a/common/teacmd/git.go b/common/teacmd/git.go index 53d2243..4626c0d 100644 --- a/common/teacmd/git.go +++ b/common/teacmd/git.go @@ -94,7 +94,7 @@ func GitCloneCmd(url string, targetDir string, initMsg string) error { return err } - _, err = git("commit", "-m", initMsg) + _, err = git("commit", "--author=\"World CLI \"", "-m", initMsg) if err != nil { return err }