Skip to content

Commit

Permalink
check dir exist before create/clone
Browse files Browse the repository at this point in the history
  • Loading branch information
zulkhair committed Oct 25, 2024
1 parent 84f5fb5 commit 9e8086f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
14 changes: 13 additions & 1 deletion cmd/world/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,17 @@ func TestCreateStartStopRestartPurge(t *testing.T) {
// set tea ouput to variable
teaOut := &bytes.Buffer{}
createCmd := getCreateCmd(teaOut)
createCmd.SetArgs([]string{gameDir})

// checkout the repo
sgtDir := gameDir + "/sgt"
createCmd.SetArgs([]string{sgtDir})
err = createCmd.Execute()
assert.NilError(t, err)

// Change dir
err = os.Chdir(sgtDir)
assert.NilError(t, err)

// Start cardinal
rootCmd.SetArgs([]string{"cardinal", "start", "--detach", "--editor=false"})
err = rootCmd.Execute()
Expand Down Expand Up @@ -173,6 +179,9 @@ func TestDev(t *testing.T) {
createCmd := getCreateCmd(teaOut)
createCmd.SetArgs([]string{gameDir})

// checkout the repo
sgtDir := gameDir + "/sgt"
createCmd.SetArgs([]string{sgtDir})
err = createCmd.Execute()
assert.NilError(t, err)

Expand Down Expand Up @@ -280,6 +289,9 @@ func TestEVMStart(t *testing.T) {
createCmd := getCreateCmd(teaOut)
createCmd.SetArgs([]string{gameDir})

// checkout the repo
sgtDir := gameDir + "/sgt"
createCmd.SetArgs([]string{sgtDir})
err = createCmd.Execute()
assert.NilError(t, err)

Expand Down
6 changes: 4 additions & 2 deletions common/docker/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,19 @@ func TestBuild(t *testing.T) {
err = os.Chdir(dir)
assert.NilError(t, err)

sgtDir := dir + "/sgt"

// Pull the repository
templateGitURL := "https://github.com/Argus-Labs/starter-game-template.git"
err = teacmd.GitCloneCmd(templateGitURL, dir, "Initial commit from World CLI")
err = teacmd.GitCloneCmd(templateGitURL, sgtDir, "Initial commit from World CLI")
assert.NilError(t, err)

// Preparation
cfg := &config.Config{
DockerEnv: map[string]string{
"CARDINAL_NAMESPACE": cardinalNamespace,
},
RootDir: dir,
RootDir: sgtDir,
}
cardinalService := service.Cardinal(cfg)
ctx := context.Background()
Expand Down
6 changes: 6 additions & 0 deletions common/teacmd/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func git(args ...string) (string, error) {
}

func GitCloneCmd(url string, targetDir string, initMsg string) error {
// check targetDir exists
if _, err := os.Stat(targetDir); err == nil {
return eris.Errorf("Game shard named '%s' already exists in this directory, "+
"please change the directory or use another name", targetDir)
}

Check warning on line 54 in common/teacmd/git.go

View check run for this annotation

Codecov / codecov/patch

common/teacmd/git.go#L52-L54

Added lines #L52 - L54 were not covered by tests

_, err := git("clone", url, targetDir)
if err != nil {
return err
Expand Down

0 comments on commit 9e8086f

Please sign in to comment.