fix: add unit tests for commandExtractor and improve multiline comment handling
This commit is contained in:
parent
359a59fe26
commit
0d940ec6d1
3 changed files with 131 additions and 36 deletions
|
|
@ -3,6 +3,7 @@ import { useContext } from "react";
|
|||
import { lightGray, vscForeground } from "../..";
|
||||
import { IdeMessengerContext } from "../../../context/IdeMessenger";
|
||||
import { isJetBrains } from "../../../util";
|
||||
import { extractCommand } from "../utils/commandExtractor";
|
||||
|
||||
interface RunInTerminalButtonProps {
|
||||
command: string;
|
||||
|
|
@ -16,42 +17,6 @@ export function RunInTerminalButton({ command }: RunInTerminalButtonProps) {
|
|||
return null;
|
||||
}
|
||||
|
||||
// Extract just the command line (the line after $ or the first line)
|
||||
function extractCommand(cmd: string): string {
|
||||
// First handle the $ prompt case, extract the line after it
|
||||
if (cmd.includes("$")) {
|
||||
const match = cmd.match(/\$\s*([^\n]+)/);
|
||||
if (match) {
|
||||
return match[1].trim();
|
||||
}
|
||||
}
|
||||
|
||||
// Process all lines, filtering out comments and empty lines
|
||||
const lines = cmd.split("\n")
|
||||
.map(line => line.trim())
|
||||
.filter(line =>
|
||||
line &&
|
||||
!line.startsWith("#") &&
|
||||
!line.startsWith("//") &&
|
||||
!line.startsWith("/*")
|
||||
);
|
||||
|
||||
// Handle multi-line commands
|
||||
let result = "";
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
result += line;
|
||||
|
||||
// Add space for command continuation
|
||||
if (line.endsWith("&&") || line.endsWith("|") || line.endsWith("\\")) {
|
||||
result += " ";
|
||||
} else if (i < lines.length - 1) {
|
||||
result += " ";
|
||||
}
|
||||
}
|
||||
return result.trim();
|
||||
}
|
||||
|
||||
function runInTerminal() {
|
||||
// Extract just the command line
|
||||
const extractedCommand = extractCommand(command);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,80 @@
|
|||
import { extractCommand } from './commandExtractor';
|
||||
|
||||
describe('extractCommand', () => {
|
||||
// Test basic command extraction
|
||||
it('should extract command from $ prompt', () => {
|
||||
expect(extractCommand('$ echo hello')).toBe('echo hello');
|
||||
expect(extractCommand('$ echo hello')).toBe('echo hello');
|
||||
expect(extractCommand('$echo hello')).toBe('echo hello');
|
||||
});
|
||||
|
||||
// Test comment removal
|
||||
it('should remove comments at beginning of lines', () => {
|
||||
expect(extractCommand('# shell comment\necho hello')).toBe('echo hello');
|
||||
expect(extractCommand('// js comment\necho hello')).toBe('echo hello');
|
||||
expect(extractCommand('/* multi-line\ncomment */\necho hello')).toBe('echo hello');
|
||||
});
|
||||
|
||||
// Test multiline comment removal
|
||||
it('should remove multiline comments anywhere in the text', () => {
|
||||
expect(extractCommand('echo /* comment */ hello')).toBe('echo hello');
|
||||
expect(extractCommand('echo hello /* comment */')).toBe('echo hello');
|
||||
expect(extractCommand('echo /* multiple\nline\ncomment */ hello')).toBe('echo hello');
|
||||
});
|
||||
|
||||
// Test line continuation handling
|
||||
it('should handle line continuations correctly', () => {
|
||||
expect(extractCommand('echo hello && \\\necho world')).toBe('echo hello && echo world');
|
||||
expect(extractCommand('echo hello | \\\ngrep h')).toBe('echo hello | grep h');
|
||||
expect(extractCommand('echo hello \\\nworld')).toBe('echo hello world');
|
||||
});
|
||||
|
||||
// Test complex command combinations
|
||||
it('should handle complex command combinations', () => {
|
||||
expect(extractCommand(`
|
||||
# shell comment
|
||||
echo hello /* inline comment */ && \\
|
||||
// js comment
|
||||
echo world
|
||||
`)).toBe('echo hello && echo world');
|
||||
});
|
||||
|
||||
// Test empty and whitespace handling
|
||||
it('should handle empty and whitespace inputs', () => {
|
||||
expect(extractCommand('')).toBe('');
|
||||
expect(extractCommand(' \n \t ')).toBe('');
|
||||
});
|
||||
|
||||
// Test comment-only inputs
|
||||
it('should handle comment-only inputs', () => {
|
||||
expect(extractCommand('/* comment */')).toBe('');
|
||||
expect(extractCommand('# comment')).toBe('');
|
||||
expect(extractCommand('// comment')).toBe('');
|
||||
expect(extractCommand('/* multi\nline\ncomment */')).toBe('');
|
||||
});
|
||||
|
||||
// Test inline comments
|
||||
it('should handle inline comments correctly according to implementation', () => {
|
||||
// But removes multiline comments even when inline
|
||||
expect(extractCommand('/* inline */ echo hello world')).toBe('echo hello world');
|
||||
});
|
||||
|
||||
// Test multiple line continuations
|
||||
it('should handle multiple line continuations', () => {
|
||||
expect(extractCommand(`
|
||||
echo hello && \\
|
||||
echo world && \\
|
||||
echo test
|
||||
`)).toBe('echo hello && echo world && echo test');
|
||||
});
|
||||
|
||||
// Test nested line continuations
|
||||
it('should handle nested line continuations', () => {
|
||||
expect(extractCommand(`
|
||||
echo hello && \\
|
||||
(echo nested && \\
|
||||
echo more) && \\
|
||||
echo final
|
||||
`)).toBe('echo hello && (echo nested && echo more) && echo final');
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
/**
|
||||
* Extracts and processes command strings, handling comments and line continuations.
|
||||
* @param cmd The command string to process
|
||||
* @returns The processed command string
|
||||
*/
|
||||
export function extractCommand(cmd: string): string {
|
||||
if (!cmd || cmd.trim() === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Remove multi-line comments (/* ... */) and replace with a single space
|
||||
cmd = cmd.replace(/\/\*[\s\S]*?\*\//g, " ");
|
||||
|
||||
// Handle the $ prompt case first
|
||||
if (cmd.includes("$")) {
|
||||
const match = cmd.match(/\$\s*([^\n]+)/);
|
||||
if (match) {
|
||||
// Return just the command after $ without further processing
|
||||
return match[1].trim();
|
||||
}
|
||||
}
|
||||
|
||||
// Process lines - remove single-line comments only at beginning of lines
|
||||
const lines = cmd.split('\n')
|
||||
.map(line => line.replace(/^\s*#.*$/, '').replace(/^\s*\/\/.*$/, ''))
|
||||
.map(line => line.trim())
|
||||
.filter(line => line);
|
||||
|
||||
// Join lines with proper handling of continuations
|
||||
let result = '';
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
let line = lines[i];
|
||||
|
||||
// Remove trailing continuation characters and join with next line
|
||||
while (
|
||||
(line.endsWith('&&') || line.endsWith('|') || line.endsWith('\\')) &&
|
||||
i < lines.length - 1
|
||||
) {
|
||||
// Remove the continuation character(s) and any trailing spaces
|
||||
line = line.replace(/(&&|\||\\)\s*$/, '').trim();
|
||||
// Join with the next line
|
||||
i++;
|
||||
line += ' ' + lines[i];
|
||||
}
|
||||
|
||||
result += line + ' ';
|
||||
}
|
||||
|
||||
return result.trim().replace(/\s+/g, ' ');
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue