smoke.ps1 5.7 KB

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