|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestCreateCommand(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + args []string |
| 16 | + wantErr bool |
| 17 | + errContains string |
| 18 | + validate func(t *testing.T, appPath string) |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "create typescript sample-app", |
| 22 | + args: []string{"--name", "test-app", "--language", "typescript", "--template", "sample-app"}, |
| 23 | + validate: func(t *testing.T, appPath string) { |
| 24 | + // Verify files were created |
| 25 | + assert.FileExists(t, filepath.Join(appPath, "index.ts")) |
| 26 | + assert.FileExists(t, filepath.Join(appPath, "package.json")) |
| 27 | + assert.FileExists(t, filepath.Join(appPath, ".gitignore")) |
| 28 | + assert.NoFileExists(t, filepath.Join(appPath, "_gitignore")) |
| 29 | + }, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "fail with python sample-app (template not found)", |
| 33 | + args: []string{"--name", "test-app", "--language", "python", "--template", "sample-app"}, |
| 34 | + wantErr: true, |
| 35 | + errContains: "template not found: python/sample-app", |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + for _, tt := range tests { |
| 40 | + t.Run(tt.name, func(t *testing.T) { |
| 41 | + tmpDir := t.TempDir() |
| 42 | + |
| 43 | + orgDir, err := os.Getwd() |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + err = os.Chdir(tmpDir) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + t.Cleanup(func() { |
| 50 | + os.Chdir(orgDir) |
| 51 | + }) |
| 52 | + |
| 53 | + createCmd.SetArgs(tt.args) |
| 54 | + err = createCmd.Execute() |
| 55 | + |
| 56 | + // Check if error is expected |
| 57 | + if tt.wantErr { |
| 58 | + require.Error(t, err, "expected command to fail but it succeeded") |
| 59 | + if tt.errContains != "" { |
| 60 | + assert.Contains(t, err.Error(), tt.errContains, "error message should contain expected text") |
| 61 | + } |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + require.NoError(t, err, "failed to execute create command") |
| 66 | + |
| 67 | + // Validate the created app |
| 68 | + appPath := filepath.Join(tmpDir, "test-app") |
| 69 | + assert.DirExists(t, appPath, "app directory should be created") |
| 70 | + |
| 71 | + if tt.validate != nil { |
| 72 | + tt.validate(t, appPath) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments