diff --git a/scripts/bash/keploy_record_script.sh b/scripts/bash/keploy_record_script.sh index 39914ab..39aab23 100755 --- a/scripts/bash/keploy_record_script.sh +++ b/scripts/bash/keploy_record_script.sh @@ -64,13 +64,17 @@ cat_pid=$! (while true; do sleep 1; done) & dummy_pid=$! -# Execute the keploy command, redirecting output to the named pipe -sudo -E $keploycmd record > "$fifo" 2>&1 +# Function to Terminate the dummy process and the logging process +kill_all() { + kill $dummy_pid + rm -f "$fifo" +} + +# Execute the keploy command with the trap, redirecting output to the named pipe +( + trap 'kill_all' SIGINT SIGTERM EXIT + $keploycmd record > "$fifo" 2>&1 +) # Clean up: Wait for keploy command to finish -wait $! - -# Terminate the dummy process and the logging process -kill $dummy_pid -wait $cat_pid -rm "$fifo" +wait $! \ No newline at end of file diff --git a/scripts/bash/keploy_test_script.sh b/scripts/bash/keploy_test_script.sh index da809d2..9e46da7 100755 --- a/scripts/bash/keploy_test_script.sh +++ b/scripts/bash/keploy_test_script.sh @@ -41,5 +41,29 @@ else keploycmd="sudo -E env PATH=\"$PATH\" keploy" fi -# Execute the keploy command and append the output to the log file -sudo $keploycmd test | tee -a "$log_file_path" \ No newline at end of file +# Create a named pipe +fifo=$(mktemp -u) +mkfifo "$fifo" + +# Background process to read from the named pipe and write to the log file +cat "$fifo" | tee -a "$log_file_path" & +cat_pid=$! + +# Dummy background process to keep the parent script running +(while true; do sleep 1; done) & +dummy_pid=$! + +# Function to Terminate the dummy process and the logging process +kill_all() { + kill $dummy_pid + rm -f "$fifo" +} + +# Execute the keploy command with the trap, redirecting output to the named pipe +( + trap 'kill_all' SIGINT SIGTERM EXIT + $keploycmd test > "$fifo" 2>&1 +) + +# Clean up: Wait for keploy command to finish +wait $! diff --git a/scripts/zsh/keploy_record_script.sh b/scripts/zsh/keploy_record_script.sh index 01b3284..231f214 100755 --- a/scripts/zsh/keploy_record_script.sh +++ b/scripts/zsh/keploy_record_script.sh @@ -1,4 +1,4 @@ -#!/bin/zsh +#!/bin/zsh -i log_file_path="$1" @@ -49,6 +49,9 @@ fi fifo=$(mktemp -u) mkfifo "$fifo" +# Disable job control messages +set +m + # Background process to read from the named pipe and write to the log file cat "$fifo" | tee -a "$log_file_path" & cat_pid=$! @@ -57,18 +60,24 @@ cat_pid=$! (while true; do sleep 1; done) & dummy_pid=$! +kill_all() { + kill $dummy_pid 2>/dev/null + rm -f "$fifo" +} + # Check if running on WSL if grep -qEi "(Microsoft|WSL)" /proc/version &> /dev/null ; then + ( + trap 'kill_all' SIGINT SIGTERM EXIT sudo -E env "PATH=$PATH" keploy record > "$fifo" 2>&1 + ) else keploycmd="sudo -E env PATH=\"$PATH\" keploy" - eval $keploycmd record > "$fifo" 2>&1 + ( + trap 'kill_all' SIGINT SIGTERM EXIT + eval $keploycmd record > "$fifo" 2>&1 + ) fi # Clean up: Wait for keploy command to finish -wait $! - -# Terminate the dummy process and the logging process -kill $dummy_pid -wait $cat_pid -rm "$fifo" \ No newline at end of file +wait $! \ No newline at end of file diff --git a/scripts/zsh/keploy_test_script.sh b/scripts/zsh/keploy_test_script.sh index 117cf18..07e21fa 100755 --- a/scripts/zsh/keploy_test_script.sh +++ b/scripts/zsh/keploy_test_script.sh @@ -1,4 +1,4 @@ -#!/bin/zsh +#!/bin/zsh -i log_file_path="$1" @@ -34,10 +34,40 @@ elif [[ "$command" =~ .*"java".* ]] || [[ "$command" =~ .*"mvn".* ]]; then fi +# Create a named pipe +fifo=$(mktemp -u) +mkfifo "$fifo" + +# Disable job control messages +set +m + +# Background process to read from the named pipe and write to the log file +cat "$fifo" | tee -a "$log_file_path" & +cat_pid=$! + +# Dummy background process to keep the parent script running +(while true; do sleep 1; done) & +dummy_pid=$! + +# Function to Terminate the dummy process and the logging process +kill_all() { + kill $dummy_pid 2>/dev/null + rm -f "$fifo" +} + # Check if running on WSL if grep -qEi "(Microsoft|WSL)" /proc/version &> /dev/null ; then - sudo -E env "PATH=$PATH" keploy test | tee -a "$log_file_path" + ( + trap 'kill_all' SIGINT SIGTERM EXIT + sudo -E env "PATH=$PATH" keploy test > "$fifo" 2>&1 + ) else keploycmd="sudo -E env PATH=\"$PATH\" keploy" - eval $keploycmd test | tee -a "$log_file_path" -fi \ No newline at end of file + ( + trap 'kill_all' SIGINT SIGTERM EXIT + eval $keploycmd test > "$fifo" 2>&1 + ) +fi + +# Clean up: Wait for keploy command to finish +wait $! \ No newline at end of file diff --git a/sidebar/sidebar.js b/sidebar/sidebar.js index 26ef5db..8bb8cdd 100644 --- a/sidebar/sidebar.js +++ b/sidebar/sidebar.js @@ -442,6 +442,9 @@ window.addEventListener('message', event => { } else if (message.type === 'testcaserecorded') { console.log("message.textContent", message.textContent); + if(stopRecordingButton){ + stopRecordingButton.click(); + } recordStatus.style.display = "block"; recordedTestCasesDiv.style.display = "grid"; diff --git a/src/Record.ts b/src/Record.ts index 03a488e..843d742 100644 --- a/src/Record.ts +++ b/src/Record.ts @@ -1,18 +1,18 @@ import * as vscode from 'vscode'; -import { readFileSync , appendFile} from 'fs'; +import { readFileSync, appendFile } from 'fs'; import * as child_process from 'child_process'; import * as os from 'os'; function extractTestSetName(logContent: string) { // Define the regular expression pattern to find the test set name const regex = /Keploy has captured test cases for the user's application\.\s*{"path": ".*\/(test-set-\d+)\/tests"/; - + // Execute the regular expression on the log content const match = regex.exec(logContent); - + // Check if a match was found and return the test set name, otherwise return null return match ? match[1] : null; - } +} export async function displayRecordedTestCases(logfilePath: string, webview: any): Promise { console.log('Displaying Recorded test cases'); let logData; @@ -22,39 +22,40 @@ export async function displayRecordedTestCases(logfilePath: string, webview: any } catch (error) { appendFile(logfilePath, "", function (err) { - if (err) { console.log("err here" + err); } + if (err) { console.log("err here" + err); } }); logData = readFileSync(logfilePath, 'utf8'); } const testSetName = extractTestSetName(logData); - // Split the log data into lines - const logLines = logData.split('\n'); - // Filter out the lines containing the desired information - const capturedTestLines = logLines.filter(line => line.includes('🟠 Keploy has captured test cases')); - // Display the captured test cases in your frontend - if (capturedTestLines.length === 0) { - webview.postMessage({ - type: 'testcaserecorded', - value: 'Test Case has been recorded', - textContent: "No test cases captured. Please try again.", - noTestCases: true + // Split the log data into lines + const logLines = logData.split('\n'); + // Filter out the lines containing the desired information + const capturedTestLines = logLines.filter(line => line.includes('🟠 Keploy has captured test cases')); + // Display the captured test cases in your frontend + if (capturedTestLines.length === 0) { + webview.postMessage({ + type: 'testcaserecorded', + value: 'Test Case has been recorded', + textContent: "No test cases captured. Please try again.", + noTestCases: true + }); + return; + } + capturedTestLines.forEach(testLine => { + const testCaseInfo = JSON.parse(testLine.substring(testLine.indexOf('{'))); + const textContent = `"${testCaseInfo['testcase name']}"`; + const path = testCaseInfo.path + '/' + testCaseInfo['testcase name'] + '.yaml'; + webview.postMessage({ + type: 'testcaserecorded', + value: 'Test Case has been recorded', + textContent: textContent, + path: path, + testSetName: testSetName + }); }); - return; } - capturedTestLines.forEach(testLine => { - const testCaseInfo = JSON.parse(testLine.substring(testLine.indexOf('{'))); - const textContent = `"${testCaseInfo['testcase name']}"`; - const path = testCaseInfo.path + '/' + testCaseInfo['testcase name'] + '.yaml'; - webview.postMessage({ - type: 'testcaserecorded', - value: 'Test Case has been recorded', - textContent: textContent, - path: path, - testSetName: testSetName - }); - });} - catch(error){ + catch (error) { console.log(error); webview.postMessage({ type: 'testcaserecorded', @@ -67,29 +68,41 @@ export async function displayRecordedTestCases(logfilePath: string, webview: any } } -export async function stopRecording(){ - try{ - vscode.window.activeTerminal?.sendText('\x03', true); - //set timeout for 5 seconds - setTimeout(() => { - vscode.window.activeTerminal?.dispose(); - }, 5000); +export async function stopRecording() { - return; - } - catch(error){ - console.log(error); - throw error; - } + try { + let pid: number | undefined; + await Promise.all(vscode.window.terminals.map(async (terminal) => { + if (terminal.name === 'Keploy Terminal') { + pid = await terminal.processId; + terminal.sendText('\x03', true); + } + })); + setTimeout(async () => { + await Promise.all(vscode.window.terminals.map(async (terminal) => { + if (terminal.name === 'Keploy Terminal') { + const currentPid = await terminal.processId; + if (pid && currentPid && pid === currentPid) { + vscode.window.activeTerminal?.dispose(); + } + } + })); + }, 5000); + return; + } + catch (error) { + console.log(error); + throw error; + } } -export async function startRecording( wslscriptPath: string, wsllogfilePath: string, bashScriptPath: string, zshScriptPath : string , logfilePath: string, webview: any): Promise { +export async function startRecording(wslscriptPath: string, wsllogfilePath: string, bashScriptPath: string, zshScriptPath: string, logfilePath: string, webview: any): Promise { try { return new Promise((resolve, reject) => { try { let terminalPath: string; - let currentShell =''; + let currentShell = ''; if (process.platform === 'win32') { terminalPath = 'wsl.exe'; diff --git a/src/Test.ts b/src/Test.ts index 236fb98..fd569fb 100644 --- a/src/Test.ts +++ b/src/Test.ts @@ -374,8 +374,23 @@ export async function startTesting(wslscriptPath: string, wsllogfilePath: string export async function stopTesting(): Promise { try { - vscode.window.activeTerminal?.sendText('\x03', true); - vscode.window.activeTerminal?.dispose(); + let pid: number | undefined; + await Promise.all(vscode.window.terminals.map(async (terminal) => { + if (terminal.name === 'Keploy Terminal') { + pid = await terminal.processId; + terminal.sendText('\x03', true); + } + })); + setTimeout(async () => { + await Promise.all(vscode.window.terminals.map(async (terminal) => { + if (terminal.name === 'Keploy Terminal') { + const currentPid = await terminal.processId; + if (pid && currentPid && pid === currentPid) { + vscode.window.activeTerminal?.dispose(); + } + } + })); + }, 5000); return ; } catch (error) {