Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Integration functionality stop on terminal kill #50

Merged
merged 13 commits into from
Sep 27, 2024
20 changes: 12 additions & 8 deletions scripts/bash/keploy_record_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $!
28 changes: 26 additions & 2 deletions scripts/bash/keploy_test_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
# 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 $!
25 changes: 17 additions & 8 deletions scripts/zsh/keploy_record_script.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/zsh
#!/bin/zsh -i

log_file_path="$1"

Expand Down Expand Up @@ -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=$!
Expand All @@ -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"
wait $!
38 changes: 34 additions & 4 deletions scripts/zsh/keploy_test_script.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/zsh
#!/bin/zsh -i

log_file_path="$1"

Expand Down Expand Up @@ -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
(
trap 'kill_all' SIGINT SIGTERM EXIT
eval $keploycmd test > "$fifo" 2>&1
)
fi

# Clean up: Wait for keploy command to finish
wait $!
3 changes: 3 additions & 0 deletions sidebar/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
103 changes: 58 additions & 45 deletions src/Record.ts
Original file line number Diff line number Diff line change
@@ -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';

Check warning on line 3 in src/Record.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Import name `child_process` must match one of the following formats: camelCase, PascalCase

Check warning on line 3 in src/Record.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Import name `child_process` must match one of the following formats: camelCase, PascalCase

Check warning on line 3 in src/Record.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Import name `child_process` must match one of the following formats: camelCase, PascalCase
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<void> {
console.log('Displaying Recorded test cases');
let logData;
Expand All @@ -22,39 +22,40 @@
}
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',
Expand All @@ -67,29 +68,41 @@
}
}

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<void> {
export async function startRecording(wslscriptPath: string, wsllogfilePath: string, bashScriptPath: string, zshScriptPath: string, logfilePath: string, webview: any): Promise<void> {
try {
return new Promise<void>((resolve, reject) => {
try {
let terminalPath: string;
let currentShell ='';
let currentShell = '';

if (process.platform === 'win32') {
terminalPath = 'wsl.exe';
Expand Down
19 changes: 17 additions & 2 deletions src/Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,23 @@ export async function startTesting(wslscriptPath: string, wsllogfilePath: string

export async function stopTesting(): Promise<void> {
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) {
Expand Down
Loading