tests/smoke.ps1
tests/smoke.ps1Browse 14 files
1,726 tokens
5,701 bytes
Token encoding: o200k_base
Snapshot 24fd22b
← Back to SKILL.md
1#Requires -Version 5.12# Smoke test for the ast-grep skill on Windows (PowerShell 5.1+).3$ErrorActionPreference = 'Stop'4 5$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path6$SkillDir = Split-Path -Parent $ScriptDir7$Helper = Join-Path $SkillDir 'scripts/ast_grep_helper.py'8$Python = if (Get-Command py -ErrorAction SilentlyContinue) { 'py' } else { 'python' }9 10$Output = Join-Path $env:TEMP ("ast-grep-skill-smoke-" + [guid]::NewGuid().ToString('N').Substring(0,8))11New-Item -ItemType Directory -Path $Output -Force | Out-Null12 13function Pass([string]$msg) { Write-Host "PASS: $msg" }14function Fail([string]$msg) { Write-Host "FAIL: $msg" -ForegroundColor Red; Remove-Item -Recurse -Force $Output -ErrorAction SilentlyContinue; exit 1 }15 16function Run([string[]]$Args) {17 $stdoutFile = Join-Path $Output ("out-" + [guid]::NewGuid().ToString('N').Substring(0,8) + ".txt")18 $proc = Start-Process -FilePath $Python -ArgumentList (@($Helper) + $Args) -NoNewWindow -PassThru -Wait -RedirectStandardOutput $stdoutFile -RedirectStandardError "$stdoutFile.err"19 $stdout = if (Test-Path $stdoutFile) { Get-Content $stdoutFile -Raw } else { '' }20 $stderr = if (Test-Path "$stdoutFile.err") { Get-Content "$stdoutFile.err" -Raw } else { '' }21 return [pscustomobject]@{22 ExitCode = $proc.ExitCode23 Stdout = $stdout24 Stderr = $stderr25 Combined = "$stdout`n$stderr"26 }27}28 29try {30 # 1. --version31 $r = Run @('--version')32 if ($r.Combined -notmatch 'ast-grep-helper') { Fail '--version output missing' }33 Pass '--version'34 35 # 2. langs (must list >=25)36 $r = Run @('langs')37 $langCount = ($r.Stdout -split "`n" | Where-Object { $_ -match '^ [a-z]' }).Count38 if ($langCount -lt 25) { Fail "langs listed only $langCount (expected >=25)" }39 Pass 'langs lists at least 25 languages'40 41 # 3. regex misuse: \w+42 $r = Run @('validate', '\w+', '--lang', 'ts')43 if ($r.ExitCode -ne 2) { Fail "validate '\w+' should exit 2, got $($r.ExitCode)" }44 if ($r.Combined -notmatch 'regex') { Fail "validate '\w+' should mention regex" }45 Pass 'validate detects \w regex misuse'46 47 # 4. valid pattern48 $r = Run @('validate', 'console.log($MSG)', '--lang', 'ts')49 if ($r.ExitCode -ne 0) { Fail "validate 'console.log(`$MSG)' should exit 0, got $($r.ExitCode)" }50 Pass 'validate accepts plausible pattern'51 52 # 5. Python trailing colon53 $r = Run @('validate', 'def $F($$$):', '--lang', 'py')54 if ($r.ExitCode -ne 2) { Fail "validate 'def `$F(`$`$`$):' should exit 2, got $($r.ExitCode)" }55 if ($r.Combined -notmatch 'colon|trailing') { Fail 'validate should mention trailing colon' }56 Pass 'validate detects Python trailing colon'57 58 # 6. Incomplete TS function59 $r = Run @('validate', 'function $N', '--lang', 'ts')60 if ($r.ExitCode -ne 2) { Fail "validate 'function `$N' should exit 2, got $($r.ExitCode)" }61 if ($r.Combined -notmatch 'incomplete|params|body') { Fail 'validate should hint about params/body' }62 Pass 'validate detects incomplete TS function'63 64 # 7. Alternation pipe65 $r = Run @('validate', 'foo|bar', '--lang', 'ts')66 if ($r.ExitCode -ne 2) { Fail "validate 'foo|bar' should exit 2, got $($r.ExitCode)" }67 if ($r.Combined -notmatch 'alternation|regex') { Fail 'validate should mention alternation' }68 Pass 'validate detects literal | alternation'69 70 # 8. doctor71 $r = Run @('doctor')72 if ($r.Combined -notmatch 'ast-grep-helper') { Fail 'doctor missing helper version line' }73 Pass 'doctor produces output'74 75 # 9. search w/o binary76 $r = Run @('-q', 'search', 'foo()', '--lang', 'ts', 'C:/nonexistent-path-xyzzy')77 switch ($r.ExitCode) {78 { $_ -in 0,1,4 } { Pass "search runs (rc=$($r.ExitCode), ast-grep available)" }79 3 {80 if ($r.Combined -notmatch 'install') { Fail 'search rc=3 should print install hint' }81 Pass 'search without binary prints install hint'82 }83 default { Fail "search returned unexpected rc=$($r.ExitCode): $($r.Combined)" }84 }85 86 # 10. install.ps1 syntax (parse-check via PowerShell tokenizer)87 $tokens = $null88 $errors = $null89 [System.Management.Automation.Language.Parser]::ParseFile(90 (Join-Path $SkillDir 'install.ps1'), [ref]$tokens, [ref]$errors) | Out-Null91 if ($errors -and $errors.Count -gt 0) { Fail "install.ps1 has parse errors: $($errors -join '; ')" }92 Pass 'install.ps1 parses cleanly'93 94 # 11. SKILL.md frontmatter95 $skill = Get-Content (Join-Path $SkillDir 'SKILL.md') -Raw96 if (-not $skill.StartsWith("---`n") -and -not $skill.StartsWith("---`r`n")) {97 Fail 'SKILL.md must start with YAML frontmatter'98 }99 $endIdx = $skill.IndexOf("`n---`n", 4)100 if ($endIdx -lt 0) { $endIdx = $skill.IndexOf("`r`n---`r`n", 4) }101 if ($endIdx -lt 0) { Fail 'SKILL.md missing closing ---' }102 $fm = $skill.Substring(4, $endIdx - 4)103 if ($fm -notmatch '(?m)^name:\s*ast-grep\s*$') { Fail 'frontmatter missing name: ast-grep' }104 if ($fm -notmatch '(?m)^description:') { Fail 'frontmatter missing description' }105 Pass 'SKILL.md frontmatter shape'106 107 # 12. All required reference files exist108 $required = @(109 'references/install.md', 'references/patterns.md', 'references/pitfalls.md',110 'references/recipes.md', 'references/cli.md', 'references/yaml-rules.md',111 'references/sgconfig.md'112 )113 foreach ($f in $required) {114 if (-not (Test-Path (Join-Path $SkillDir $f))) { Fail "missing $f" }115 }116 Pass 'all references present'117 118 Write-Host ''119 Write-Host 'all smoke tests passed'120}121finally {122 Remove-Item -Recurse -Force $Output -ErrorAction SilentlyContinue123}124