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: update keploy #69

Merged
merged 6 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -174,7 +174,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
}
});
}


Loading