* Validate a snippet based on its language
(snippet)
| 489 | * Validate a snippet based on its language |
| 490 | */ |
| 491 | async function validateSnippet(snippet) { |
| 492 | totalTests++; |
| 493 | |
| 494 | const relativePath = path.relative(DOCS_BASE_DIR, snippet.file); |
| 495 | const testName = `${relativePath}:${snippet.line} (${snippet.language} block #${snippet.index})`; |
| 496 | |
| 497 | log(`\nTesting: ${testName}`, 'info'); |
| 498 | log(` Code preview: ${snippet.code.substring(0, 60)}...`, 'dim'); |
| 499 | |
| 500 | if (!snippet.shouldTest) { |
| 501 | skippedTests++; |
| 502 | log(` ⊘ SKIPPED (not marked for testing)`, 'warning'); |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | let result; |
| 507 | |
| 508 | try { |
| 509 | switch (snippet.language.toLowerCase()) { |
| 510 | case 'javascript': |
| 511 | case 'typescript': |
| 512 | case 'js': |
| 513 | case 'ts': |
| 514 | result = await testJavaScriptSnippet(snippet); |
| 515 | break; |
| 516 | |
| 517 | case 'bash': |
| 518 | case 'sh': |
| 519 | case 'shell': |
| 520 | // Check if it's a supported bash command (skip comments to find actual command) |
| 521 | const lines = snippet.code.split('\n'); |
| 522 | const firstCommand = lines.find(line => { |
| 523 | const trimmed = line.trim(); |
| 524 | return trimmed && !trimmed.startsWith('#'); |
| 525 | }); |
| 526 | |
| 527 | if (!firstCommand) { |
| 528 | result = { success: false, error: 'No executable command found in bash snippet' }; |
| 529 | } else { |
| 530 | // Extract the command name (first word) |
| 531 | const commandName = firstCommand.trim().split(/\s+/)[0]; |
| 532 | |
| 533 | // Skip informational commands (installation, navigation, etc.) |
| 534 | const SKIP_COMMANDS = ['npm', 'pip', 'pip3', 'cd', 'ls', 'mkdir', 'uv']; |
| 535 | if (SKIP_COMMANDS.includes(commandName)) { |
| 536 | skippedTests++; |
| 537 | log(` ⊘ SKIPPED (informational command: ${commandName})`, 'warning'); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | // Test supported executable commands |
| 542 | if (commandName === 'curl') { |
| 543 | result = await testCurlCommand(snippet); |
| 544 | } else if (commandName === 'npx' || commandName === 'uvx') { |
| 545 | result = await testBashCommand(snippet); |
| 546 | } else { |
| 547 | result = { success: false, error: `Bash command '${commandName}' not supported for testing (only curl, npx, uvx)` }; |
| 548 | } |
no test coverage detected