-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcreate_test.go
More file actions
381 lines (319 loc) · 11 KB
/
Copy pathcreate_test.go
File metadata and controls
381 lines (319 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package cmd
import (
"bytes"
"context"
"io"
"os"
"path/filepath"
"testing"
"github.com/onkernel/cli/pkg/create"
"github.com/pterm/pterm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCreateCommand(t *testing.T) {
tests := []struct {
name string
input CreateInput
wantErr bool
errContains string
validate func(t *testing.T, appPath string)
}{
{
name: "create typescript sample-app",
input: CreateInput{
Name: "test-app",
Language: "typescript",
Template: "sample-app",
},
validate: func(t *testing.T, appPath string) {
// Verify files were created
assert.FileExists(t, filepath.Join(appPath, "index.ts"))
assert.FileExists(t, filepath.Join(appPath, "package.json"))
assert.FileExists(t, filepath.Join(appPath, ".gitignore"))
assert.NoFileExists(t, filepath.Join(appPath, "_gitignore"))
},
},
{
name: "fail with invalid template",
input: CreateInput{
Name: "test-app",
Language: "typescript",
Template: "nonexistent",
},
wantErr: true,
errContains: "template not found: typescript/nonexistent",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
orgDir, err := os.Getwd()
require.NoError(t, err)
err = os.Chdir(tmpDir)
require.NoError(t, err)
t.Cleanup(func() {
os.Chdir(orgDir)
})
c := CreateCmd{}
err = c.Create(context.Background(), tt.input)
// Check if error is expected
if tt.wantErr {
require.Error(t, err, "expected command to fail but it succeeded")
if tt.errContains != "" {
assert.Contains(t, err.Error(), tt.errContains, "error message should contain expected text")
}
return
}
require.NoError(t, err, "failed to execute create command")
// Validate the created app
appPath := filepath.Join(tmpDir, tt.input.Name)
assert.DirExists(t, appPath, "app directory should be created")
if tt.validate != nil {
tt.validate(t, appPath)
}
})
}
}
// TestAllTemplatesWithDependencies tests all available templates and verifies dependencies are installed
func TestAllTemplatesWithDependencies(t *testing.T) {
if testing.Short() {
t.Skip("Skipping dependency installation tests in short mode")
}
tests := getTemplateInfo()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
appName := "test-app"
orgDir, err := os.Getwd()
require.NoError(t, err)
err = os.Chdir(tmpDir)
require.NoError(t, err)
t.Cleanup(func() {
os.Chdir(orgDir)
})
// Create the app
c := CreateCmd{}
err = c.Create(context.Background(), CreateInput{
Name: appName,
Language: tt.language,
Template: tt.template,
})
require.NoError(t, err, "failed to create app")
appPath := filepath.Join(tmpDir, appName)
// Verify app directory exists
assert.DirExists(t, appPath, "app directory should exist")
// Language-specific validations
switch tt.language {
case create.LanguageTypeScript:
validateTypeScriptTemplate(t, appPath, true)
case create.LanguagePython:
validatePythonTemplate(t, appPath, true)
}
})
}
}
// TestAllTemplatesCreation tests that all templates can be created without installing dependencies
func TestAllTemplatesCreation(t *testing.T) {
tests := getTemplateInfo()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
appName := "test-app"
appPath := filepath.Join(tmpDir, appName)
// Create app directory
err := os.MkdirAll(appPath, 0755)
require.NoError(t, err, "failed to create app directory")
// Copy template files without installing dependencies
err = create.CopyTemplateFiles(appPath, tt.language, tt.template)
require.NoError(t, err, "failed to copy template files")
// Verify app directory exists
assert.DirExists(t, appPath, "app directory should exist")
// Language-specific validations (without dependency checks)
switch tt.language {
case create.LanguageTypeScript:
validateTypeScriptTemplate(t, appPath, false)
case create.LanguagePython:
validatePythonTemplate(t, appPath, false)
}
})
}
}
// validateTypeScriptTemplate verifies TypeScript template structure and optionally dependencies
func validateTypeScriptTemplate(t *testing.T, appPath string, checkDependencies bool) {
t.Helper()
// Verify essential files exist
assert.FileExists(t, filepath.Join(appPath, "package.json"), "package.json should exist")
assert.FileExists(t, filepath.Join(appPath, "tsconfig.json"), "tsconfig.json should exist")
assert.FileExists(t, filepath.Join(appPath, "index.ts"), "index.ts should exist")
assert.FileExists(t, filepath.Join(appPath, ".gitignore"), ".gitignore should exist")
// Verify _gitignore was renamed
assert.NoFileExists(t, filepath.Join(appPath, "_gitignore"), "_gitignore should not exist")
if checkDependencies {
// Verify node_modules exists (dependencies were installed)
nodeModulesPath := filepath.Join(appPath, "node_modules")
if _, err := os.Stat(nodeModulesPath); err == nil {
// Only check contents if node_modules exists
entries, err := os.ReadDir(nodeModulesPath)
require.NoError(t, err, "should be able to read node_modules directory")
assert.NotEmpty(t, entries, "node_modules should contain installed packages")
} else {
t.Logf("Warning: node_modules not found at %s (npm install may have failed)", nodeModulesPath)
}
}
}
// validatePythonTemplate verifies Python template structure and optionally dependencies
func validatePythonTemplate(t *testing.T, appPath string, checkDependencies bool) {
t.Helper()
// Verify essential files exist
assert.FileExists(t, filepath.Join(appPath, "pyproject.toml"), "pyproject.toml should exist")
assert.FileExists(t, filepath.Join(appPath, "main.py"), "main.py should exist")
assert.FileExists(t, filepath.Join(appPath, ".gitignore"), ".gitignore should exist")
// Verify _gitignore was renamed
assert.NoFileExists(t, filepath.Join(appPath, "_gitignore"), "_gitignore should not exist")
if checkDependencies {
// Verify .venv exists (virtual environment was created)
venvPath := filepath.Join(appPath, ".venv")
if _, err := os.Stat(venvPath); err == nil {
// Only check contents if .venv exists
binPath := filepath.Join(venvPath, "bin")
assert.DirExists(t, binPath, ".venv/bin directory should exist")
pythonPath := filepath.Join(binPath, "python")
assert.FileExists(t, pythonPath, ".venv/bin/python should exist")
} else {
t.Logf("Warning: .venv not found at %s (uv venv may have failed)", venvPath)
}
}
}
// TestCreateCommand_DependencyInstallationFails tests that the app is still created
// even when dependency installation fails, with appropriate warning message
func TestCreateCommand_DependencyInstallationFails(t *testing.T) {
tmpDir := t.TempDir()
appName := "test-app"
orgDir, err := os.Getwd()
require.NoError(t, err)
err = os.Chdir(tmpDir)
require.NoError(t, err)
t.Cleanup(func() {
os.Chdir(orgDir)
})
var outputBuf bytes.Buffer
multiWriter := io.MultiWriter(&outputBuf, os.Stdout)
pterm.SetDefaultOutput(multiWriter)
t.Cleanup(func() {
pterm.SetDefaultOutput(os.Stdout)
})
// Override the install command to use a command that will fail
originalInstallCommands := create.InstallCommands
create.InstallCommands = map[string]string{
create.LanguageTypeScript: "exit 1", // Command that always fails
}
// Restore original install commands after test
t.Cleanup(func() {
create.InstallCommands = originalInstallCommands
})
// Create the app - should succeed even though dependency installation fails
c := CreateCmd{}
err = c.Create(context.Background(), CreateInput{
Name: appName,
Language: create.LanguageTypeScript,
Template: "sample-app",
})
output := outputBuf.String()
assert.Contains(t, output, "cd test-app", "should print cd command")
assert.Contains(t, output, "pnpm install", "should print pnpm install command")
}
// TestCreateCommand_RequiredToolMissing tests that the app is created
func TestCreateCommand_RequiredToolMissing(t *testing.T) {
tests := []struct {
name string
language string
template string
}{
{
name: "typescript with missing pnpm",
language: create.LanguageTypeScript,
template: "sample-app",
},
{
name: "python with missing uv",
language: create.LanguagePython,
template: "sample-app",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
appName := "test-app"
orgDir, err := os.Getwd()
require.NoError(t, err)
err = os.Chdir(tmpDir)
require.NoError(t, err)
t.Cleanup(func() {
os.Chdir(orgDir)
})
// Override the required tool to point to a non-existent command
originalRequiredTools := create.RequiredTools
create.RequiredTools = map[string]string{
create.LanguageTypeScript: "nonexistent-pnpm-tool",
create.LanguagePython: "nonexistent-uv-tool",
}
// Restore original required tools after test
t.Cleanup(func() {
create.RequiredTools = originalRequiredTools
})
// Create the app - should succeed even though required tool is missing
c := CreateCmd{}
err = c.Create(context.Background(), CreateInput{
Name: appName,
Language: tt.language,
Template: tt.template,
})
// Should not return an error - the command should complete successfully
// but skip dependency installation
require.NoError(t, err, "app creation should succeed even when required tool is missing")
// Verify the app directory and files were created
appPath := filepath.Join(tmpDir, appName)
assert.DirExists(t, appPath, "app directory should exist")
// Language-specific file checks
switch tt.language {
case create.LanguageTypeScript:
assert.FileExists(t, filepath.Join(appPath, "package.json"), "package.json should exist")
assert.FileExists(t, filepath.Join(appPath, "index.ts"), "index.ts should exist")
assert.FileExists(t, filepath.Join(appPath, "tsconfig.json"), "tsconfig.json should exist")
// node_modules should NOT exist since pnpm was not available
assert.NoDirExists(t, filepath.Join(appPath, "node_modules"), "node_modules should not exist when pnpm is missing")
case create.LanguagePython:
assert.FileExists(t, filepath.Join(appPath, "pyproject.toml"), "pyproject.toml should exist")
assert.FileExists(t, filepath.Join(appPath, "main.py"), "main.py should exist")
// .venv should NOT exist since uv was not available
assert.NoDirExists(t, filepath.Join(appPath, ".venv"), ".venv should not exist when uv is missing")
}
})
}
}
func getTemplateInfo() []struct {
name string
language string
template string
} {
tests := make([]struct {
name string
language string
template string
}, 0)
for templateKey, templateInfo := range create.Templates {
for _, lang := range templateInfo.Languages {
tests = append(tests, struct {
name string
language string
template string
}{
name: lang + "/" + templateKey,
language: lang,
template: templateKey,
})
}
}
return tests
}