From e42cacea8d17852c7637a1bc285f49e7e5de2da3 Mon Sep 17 00:00:00 2001 From: Ayush Sharma <89914602+ayush3160@users.noreply.github.com> Date: Mon, 7 Oct 2024 06:38:35 +0530 Subject: [PATCH] fix: update keploy (#69) * fix: update keploy Signed-off-by: Ayush Sharma * fix: updating keploy without terminal Signed-off-by: Ayush Sharma * fix: updateKeploy test Signed-off-by: Ayush Sharma * feat: added coverage in pipeline Signed-off-by: Ayush Sharma * fix: coverage command for exit code Signed-off-by: Ayush Sharma --------- Signed-off-by: Ayush Sharma --- .github/workflows/test.yml | 4 +-- package.json | 2 +- src/test/suites/updateKeploy.test.ts | 45 ++++------------------------ src/updateKeploy.ts | 40 ++++++++----------------- 4 files changed, 21 insertions(+), 70 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1993de6..94b44fe 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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' diff --git a/package.json b/package.json index e55967a..9015fc1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/test/suites/updateKeploy.test.ts b/src/test/suites/updateKeploy.test.ts index 94226dd..2364424 100644 --- a/src/test/suites/updateKeploy.test.ts +++ b/src/test/suites/updateKeploy.test.ts @@ -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(() => { @@ -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; }); diff --git a/src/updateKeploy.ts b/src/updateKeploy.ts index a957dad..f4f6ebc 100644 --- a/src/updateKeploy.ts +++ b/src/updateKeploy.ts @@ -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 { try { @@ -95,39 +96,22 @@ export async function downloadAndInstallKeployBinary(): Promise { return new Promise((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 } }); } - -