Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d3d84a8
feat: initial setup for "create" cmd in cli
archandatta Dec 2, 2025
cb1dde5
feat: add ts sample-app template
archandatta Dec 3, 2025
966ea89
feat: add cli prompts
archandatta Dec 3, 2025
f93d77c
feat: add file copying function into new directory
archandatta Dec 3, 2025
ee4ac92
feat: add types
archandatta Dec 4, 2025
078e7e9
feat: add types_test.go
archandatta Dec 4, 2025
fd452dd
fix: remove old files
archandatta Dec 4, 2025
628b530
feat: add testing for create_test.go
archandatta Dec 5, 2025
39f13c7
hide `create` command
archandatta Dec 5, 2025
89aa20b
self review
archandatta Dec 5, 2025
d1376a1
review: refactor prompting to use pterm
archandatta Dec 8, 2025
4db2ecd
review: add app name validation with flag
archandatta Dec 8, 2025
f54d0d4
review: update copy text
archandatta Dec 8, 2025
261ead0
refactor: fix test and refactor structure
archandatta Dec 8, 2025
3a20a69
feat: add python and ts templates
archandatta Dec 5, 2025
b5091a1
feat: add fetch templates logic
archandatta Dec 5, 2025
bd95ae9
feat: add template validation
archandatta Dec 5, 2025
9a78824
enable py templates
archandatta Dec 5, 2025
5570832
remove test
archandatta Dec 5, 2025
35220b4
fix: update ts template readme
archandatta Dec 8, 2025
ed1d016
fix: update ts package.json to new pkg versions
archandatta Dec 8, 2025
7dc6f47
fix: update py package.json to new pkg versions
archandatta Dec 8, 2025
b8dced4
fix: adding sorting for UX consistency && tests
archandatta Dec 8, 2025
412069a
feat: add dependecy pkg for installations
archandatta Dec 8, 2025
f9d23ca
feat: add dependency installation tests
archandatta Dec 9, 2025
31a7eb2
review: add installation step && update error message
archandatta Dec 9, 2025
ea70fee
review: update installation step
archandatta Dec 9, 2025
4337c65
refactor: update dependencies
archandatta Dec 10, 2025
2b33946
Merge branch 'main' into archand/install-dependencies
archandatta Dec 10, 2025
2cc2438
Merge branch 'main' into archand/install-dependencies
archandatta Dec 10, 2025
66d4e7b
review
archandatta Dec 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,19 @@ func (c CreateCmd) Create(ctx context.Context, ci CreateInput) error {
return fmt.Errorf("failed to create directory: %w", err)
}

pterm.Println(fmt.Sprintf("\nCreating a new %s %s\n", ci.Language, ci.Template))
pterm.Printfln("\nCreating a new %s %s", ci.Language, ci.Template)

spinner, _ := pterm.DefaultSpinner.Start("Copying template files...")

if err := create.CopyTemplateFiles(appPath, ci.Language, ci.Template); err != nil {
spinner.Fail("Failed to copy template files")
return fmt.Errorf("failed to copy template files: %w", err)
}
spinner.Success(fmt.Sprintf("✔ %s environment set up successfully", ci.Language))

nextSteps := fmt.Sprintf(`Next steps:
brew install onkernel/tap/kernel
cd %s
kernel login # or: export KERNEL_API_KEY=<YOUR_API_KEY>
kernel deploy index.ts
kernel invoke ts-basic get-page-title --payload '{"url": "https://www.google.com"}'
`, ci.Name)

nextSteps, err := create.InstallDependencies(ci.Name, appPath, ci.Language)
if err != nil {
return fmt.Errorf("failed to install dependencies: %w", err)
}
pterm.Success.Println("🎉 Kernel app created successfully!")
pterm.Println()
pterm.FgYellow.Println(nextSteps)
Expand Down
296 changes: 296 additions & 0 deletions cmd/create_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
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"
)
Expand Down Expand Up @@ -83,3 +87,295 @@ func TestCreateCommand(t *testing.T) {
})
}
}

// 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
}
Loading