MCPcopy Create free account
hub / github.com/adcontextprotocol/adcp / testBashCommand

Function testBashCommand

tests/snippet-validation.test.cjs:350–410  ·  view source on GitHub ↗

* Test a bash command (npx, uvx, etc)

(snippet)

Source from the content-addressed store, hash-verified

348 * Test a bash command (npx, uvx, etc)
349 */
350async function testBashCommand(snippet) {
351 try {
352 // Execute the bash command - find the first non-comment, non-empty line
353 const lines = snippet.code.split('\n');
354 const firstCommand = lines.find(line => {
355 const trimmed = line.trim();
356 return trimmed && !trimmed.startsWith('#');
357 });
358
359 if (!firstCommand) {
360 return { success: false, error: 'No executable command found in bash snippet' };
361 }
362
363 // For multi-line commands with continuation, collect all continued lines
364 const commandParts = [];
365 const startIndex = lines.indexOf(firstCommand);
366
367 for (let i = startIndex; i < lines.length; i++) {
368 const line = lines[i].trim();
369
370 // Skip empty lines and comments unless we're in a multi-line command
371 if (!line || line.startsWith('#')) {
372 if (commandParts.length === 0 || !commandParts[commandParts.length - 1].endsWith('\\')) {
373 if (commandParts.length > 0) break; // End of command
374 continue; // Skip to find start of command
375 }
376 }
377
378 // Remove trailing backslash and add the line content
379 if (line.endsWith('\\')) {
380 commandParts.push(line.slice(0, -1).trim());
381 } else {
382 commandParts.push(line);
383 break; // End of command
384 }
385 }
386
387 const fullCommand = commandParts.join(' ');
388
389 const { stdout, stderr } = await execAsync(fullCommand, {
390 timeout: 60000, // 60 second timeout for CLI commands
391 shell: '/bin/bash',
392 cwd: path.join(__dirname, '..') // Run from project root
393 });
394
395 return {
396 success: true,
397 output: stdout,
398 error: stderr
399 };
400 } catch (error) {
401 return {
402 success: false,
403 error: error.message,
404 stdout: error.stdout,
405 stderr: error.stderr,
406 code: error.code,
407 signal: error.signal

Callers 1

validateSnippetFunction · 0.85

Calls 1

trimMethod · 0.65

Tested by

no test coverage detected