Skip to content

Commit

Permalink
fix: Integration functionality stop on terminal kill (#50)
Browse files Browse the repository at this point in the history
* fix: fixed recording

Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>

* fix: fixed record command for zsh shell

Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>

* fix: fixed recording for all cases

Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>

* fix: fixed record script

Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>

* minor change

Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>

* fix: test script

Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>

* fix: terminal disposing

Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>

* added trap condition in zshScript

Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>

* fixed recording and testing

Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>

* added exit in trap

Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>

---------

Signed-off-by: Ayush Sharma <kshitij3160@gmail.com>
Co-authored-by: Yash Khare <khareyash05@gmail.com>
  • Loading branch information
2 people authored and shivamsouravjha committed Oct 7, 2024
1 parent b91d3b7 commit 8ee95f7
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 69 deletions.
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';
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 @@ 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',
Expand All @@ -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<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

0 comments on commit 8ee95f7

Please sign in to comment.