Skip to content

Commit

Permalink
fix: update keploy (#69)
Browse files Browse the repository at this point in the history
* fix: update keploy

Signed-off-by: Ayush Sharma <[email protected]>

* fix: updating keploy without terminal

Signed-off-by: Ayush Sharma <[email protected]>

* fix: updateKeploy test

Signed-off-by: Ayush Sharma <[email protected]>

* feat: added coverage in pipeline

Signed-off-by: Ayush Sharma <[email protected]>

* fix: coverage command for exit code

Signed-off-by: Ayush Sharma <[email protected]>

---------

Signed-off-by: Ayush Sharma <[email protected]>
  • Loading branch information
ayush3160 authored Oct 7, 2024
1 parent 27edc34 commit e42cace
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 70 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ jobs:

- run: npm install

- run: xvfb-run -a npm test
- run: xvfb-run -a npm run coverage
if: runner.os == 'Linux'

- run: npm test
- run: npm run coverage
if: runner.os != 'Linux'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
"pretest": "npm run compile && npm run lint",
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js",
"coverage": "c8 --check-coverage npm run test"
"coverage": "c8 --check-coverage=false npm run test"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.7",
Expand Down
45 changes: 6 additions & 39 deletions src/test/suites/updateKeploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ suite("updateKeploy Test", function () {
let createTerminalStub: sinon.SinonStub;
let showInformationMessageStub: sinon.SinonStub;
let onDidCloseTerminalStub: sinon.SinonStub;
let execStub: sinon.SinonStub;
let terminalMock: any;

setup(() => {
Expand All @@ -21,59 +22,25 @@ suite("updateKeploy Test", function () {
createTerminalStub = sinon.stub(vscode.window, 'createTerminal').returns(terminalMock);
showInformationMessageStub = sinon.stub(vscode.window, 'showInformationMessage');
onDidCloseTerminalStub = sinon.stub(vscode.window, 'onDidCloseTerminal');
execStub = sinon.stub(require("child_process"), 'exec').yields(undefined, '', '');
});

teardown(() => {
createTerminalStub.restore();
showInformationMessageStub.restore();
onDidCloseTerminalStub.restore();
execStub.restore();
});

test('should create a terminal and run the curl command on non-Windows platform', async () => {
test('downloading and updating binary should execute correct command with exec', async () => {
sinon.stub(process, 'platform').value('linux');

let terminalCloseListener: Function | undefined;
onDidCloseTerminalStub.callsFake((listener) => {
terminalCloseListener = listener;
return { dispose: sinon.spy() };
});
const curlCmd = `curl --silent -L https://keploy.io/install.sh -o /tmp/install.sh && chmod +x /tmp/install.sh && /tmp/install.sh -noRoot`;

const promise = updateKeploy.downloadAndInstallKeployBinary();

assert.strictEqual(createTerminalStub.calledOnce, true);
assert.strictEqual(createTerminalStub.firstCall.args[0].shellPath, '/bin/bash');
assert.strictEqual(terminalMock.sendText.calledOnceWith(" curl --silent -O -L https://keploy.io/install.sh && source install.sh;exit 0"), true);
assert.strictEqual(terminalMock.show.calledOnce, true);
assert.strictEqual(showInformationMessageStub.calledOnceWith('Downloading and updating Keploy binary...'), true);

if (terminalCloseListener) {
terminalCloseListener(terminalMock);
}

await promise;
});

test('should create a terminal with WSL on Windows platform', async () => {
sinon.stub(process, 'platform').value('win32');
assert.strictEqual(execStub.calledOnceWith(curlCmd), true);

let terminalCloseListener: Function | undefined;
onDidCloseTerminalStub.callsFake((listener) => {
terminalCloseListener = listener;
return { dispose: sinon.spy() };
});

const promise = updateKeploy.downloadAndInstallKeployBinary();

assert.strictEqual(createTerminalStub.calledOnce, true);
assert.strictEqual(createTerminalStub.firstCall.args[0].shellPath, 'wsl.exe');
assert.strictEqual(createTerminalStub.firstCall.args[0].name, 'Keploy Terminal');
assert.strictEqual(terminalMock.sendText.calledOnceWith(" curl --silent -O -L https://keploy.io/install.sh && source install.sh;exit 0"), true);
assert.strictEqual(terminalMock.show.calledOnce, true);
assert.strictEqual(showInformationMessageStub.calledOnceWith('Downloading and updating Keploy binary...'), true);

if (terminalCloseListener) {
terminalCloseListener(terminalMock);
}
await promise;
});

Expand Down
40 changes: 12 additions & 28 deletions src/updateKeploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import {getKeployVersion , getCurrentKeployVersion} from './version';
import * as child_process from 'child_process';

export async function downloadAndUpdate(): Promise<void> {
try {
Expand Down Expand Up @@ -95,39 +96,22 @@ export async function downloadAndInstallKeployBinary(): Promise<void> {
return new Promise<void>((resolve, reject) => {

try {
let bashPath: string;
if (process.platform === 'win32') {
// If on Windows, use the correct path to WSL's Bash shell
bashPath = 'wsl.exe';
} else {
// Otherwise, assume Bash is available at the standard location
bashPath = '/bin/bash';
}
// Create a new terminal instance with the Bash shell
const terminal = vscode.window.createTerminal({
name: 'Keploy Terminal',
shellPath: bashPath
});

// Show the terminal
terminal.show();
const curlCmd = `curl --silent -L https://keploy.io/install.sh -o /tmp/install.sh && chmod +x /tmp/install.sh && /tmp/install.sh -noRoot`;

const curlCmd = " curl --silent -O -L https://keploy.io/install.sh && source install.sh;exit 0";
terminal.sendText(curlCmd);

vscode.window.showInformationMessage('Downloading and updating Keploy binary...');
// Listen for terminal close event
const disposable = vscode.window.onDidCloseTerminal(eventTerminal => {
console.log('Terminal closed');
if (eventTerminal === terminal) {
disposable.dispose(); // Dispose the listener
resolve();
child_process.exec(curlCmd, (error, stdout, stderr) => {
if (error) {
console.error(`Error during installation: ${error.message}`);
reject(error);
vscode.window.showErrorMessage('Failed to update Keploy binary: ' + error);
return;
}
resolve();
vscode.window.showInformationMessage('Updated Keploy binary successfully!');
});

vscode.window.showInformationMessage('Downloading and updating Keploy binary...');
} catch (error) {
reject(error); // Reject the promise if an error occurs during execution
}
});
}


0 comments on commit e42cace

Please sign in to comment.