Files
code-utils/launch_detect_test.go
2026-08-15 17:18:00 +08:00

67 lines
1.8 KiB
Go

package main
import (
"os"
"path/filepath"
"testing"
)
func TestDetectLaunchProfileNode(t *testing.T) {
dir := t.TempDir()
raw := `{"name":"@acme/demo-app","scripts":{"dev":"vite","build":"vite build","start":"node server.js"}}`
if e := os.WriteFile(filepath.Join(dir, "package.json"), []byte(raw), 0644); e != nil {
t.Fatal(e)
}
_ = os.WriteFile(filepath.Join(dir, "vite.config.ts"), []byte("export default {}"), 0644)
p := detectLaunchProfile(dir)
if p.Kind != "node" || p.Name != "demo-app" {
t.Fatalf("got kind=%q name=%q", p.Kind, p.Name)
}
if p.Port != 5173 {
t.Fatalf("vite port want 5173 got %d", p.Port)
}
if p.StartCmd != "npm run dev" {
t.Fatalf("startCmd=%q", p.StartCmd)
}
if len(p.Start) < 2 {
t.Fatalf("expected multiple suggestions: %v", p.Start)
}
}
func TestDetectLaunchProfileGo(t *testing.T) {
dir := t.TempDir()
if e := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module github.com/x/myapi\n\ngo 1.22\n"), 0644); e != nil {
t.Fatal(e)
}
_ = os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\nfunc main(){}\n"), 0644)
p := detectLaunchProfile(dir)
if p.Kind != "go" || p.Name != "myapi" || p.StartCmd == "" {
t.Fatalf("%+v", p)
}
}
func TestFindLaunchExes(t *testing.T) {
dir := t.TempDir()
_ = os.WriteFile(filepath.Join(dir, "app.exe"), []byte("MZ"), 0644)
_ = os.MkdirAll(filepath.Join(dir, "bin"), 0755)
_ = os.WriteFile(filepath.Join(dir, "bin", "tool.exe"), []byte("MZ"), 0644)
_ = os.WriteFile(filepath.Join(dir, "uninstall.exe"), []byte("MZ"), 0644)
got := findLaunchExes(dir)
if len(got) < 2 {
t.Fatalf("want >=2 exes, got %v", got)
}
p := detectLaunchProfile(dir)
if p.Kind != "exe" || p.StartCmd == "" {
t.Fatalf("exe profile: %+v", p)
}
}
func containsStr(ss []string, want string) bool {
for _, s := range ss {
if s == want {
return true
}
}
return false
}