From 30bea9ffe7962fd5b7e0a7452120bcc514c8025a Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Fri, 29 Nov 2024 22:26:38 +0530 Subject: [PATCH 1/4] feat: added keploy version mapping Signed-off-by: Ayush Sharma --- keploy-version.json | 4 ++++ package.json | 7 +++---- src/OneClickInstall.ts | 19 +++++++++++++++++-- src/updateKeploy.ts | 6 ++++-- src/version.ts | 22 ++++++++++++---------- tsconfig.json | 3 ++- 6 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 keploy-version.json diff --git a/keploy-version.json b/keploy-version.json new file mode 100644 index 0000000..c09eedf --- /dev/null +++ b/keploy-version.json @@ -0,0 +1,4 @@ +{ + "1.0.20": "v2.3.0-beta41", + "1.0.25": "v2.3.0-beta41" +} \ No newline at end of file diff --git a/package.json b/package.json index dd2e2d7..ca37715 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "keployio", "displayName": "Keploy", "description": "Streamline testing with the power of Keploy, directly in your favorite IDE.", - "version": "1.0.20", + "version": "1.0.25", "publisher": "Keploy", "icon": "media/logo.png", "pricing": "Free", @@ -57,7 +57,7 @@ }, "submenus": [ { - "icon": "$(account)", + "icon": "$(account)", "label": "Sign In Options", "id": "sign_in_submenu" } @@ -107,7 +107,6 @@ { "command": "keploy.SignInWithMicrosoft", "title": "Sign In with Microsoft" - } ] }, @@ -216,4 +215,4 @@ "walk": "^2.3.15", "yaml": "^2.4.2" } -} +} \ No newline at end of file diff --git a/src/OneClickInstall.ts b/src/OneClickInstall.ts index c6b47d8..7f2baa6 100644 --- a/src/OneClickInstall.ts +++ b/src/OneClickInstall.ts @@ -1,11 +1,26 @@ import { exec } from 'child_process'; +import { getKeployVersion , getCurrentKeployVersion } from './version'; -export default function executeKeployOneClickCommand(): void { +export default async function executeKeployOneClickCommand(): Promise { // Check if Keploy is installed by trying to run the `keploy` command const checkKeployExistsCommand = `keploy`; + const keployVersion = await getKeployVersion(); + const currentKeployVersion = await getCurrentKeployVersion(); + + // Check the if keploy is installed and have the same version or not + if(currentKeployVersion !== "" && keployVersion !== currentKeployVersion){ + const removeKeployCommand = `rm -rf ~/.keploy`; + exec(removeKeployCommand, (error, stdout, stderr) => { + if (error) { + console.error(`Error during removal: ${error.message}`); + return; + } + }); + } // The command to download and install Keploy - const installationCommand = `curl --silent -L https://keploy.io/install.sh -o /tmp/install.sh && chmod +x /tmp/install.sh && /tmp/install.sh -noRoot`; + const installationCommand = `curl --silent -L https://keploy.io/install.sh -o /tmp/install.sh && chmod +x /tmp/install.sh && /tmp/install.sh -v ${keployVersion} -noRoot`; + exec(checkKeployExistsCommand, (error, stdout, stderr) => { if (error) { // Execute the installation command diff --git a/src/updateKeploy.ts b/src/updateKeploy.ts index f4f6ebc..061297d 100644 --- a/src/updateKeploy.ts +++ b/src/updateKeploy.ts @@ -93,10 +93,12 @@ export async function downloadAndUpdateDocker(): Promise { export async function downloadAndInstallKeployBinary(): Promise { console.log('Downloading and installing Keploy binary...'); - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { try { - const curlCmd = `curl --silent -L https://keploy.io/install.sh -o /tmp/install.sh && chmod +x /tmp/install.sh && /tmp/install.sh -noRoot`; + const keployVersion = await getKeployVersion(); + + const curlCmd = `curl --silent -L https://keploy.io/install.sh -o /tmp/install.sh && chmod +x /tmp/install.sh && /tmp/install.sh -v ${keployVersion} -noRoot`; child_process.exec(curlCmd, (error, stdout, stderr) => { if (error) { diff --git a/src/version.ts b/src/version.ts index a70701b..ade240d 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1,17 +1,19 @@ import { execShell } from './execShell'; +import * as path from 'path'; +import * as fs from 'fs'; export async function getKeployVersion() { - // GitHub repository details - const repoOwner = "keploy"; - const repoName = "keploy"; + const packagePath = path.resolve(__dirname, '../package.json'); + const packageContent = fs.readFileSync(packagePath, 'utf-8'); + const packageData = JSON.parse(packageContent); - const apiURL = `https://api.github.com/repos/${repoOwner}/${repoName}/releases/latest`; + const keployVersionJsonPath = path.resolve(__dirname, '../keploy-version.json'); + const keployVersionJsonContent = fs.readFileSync(keployVersionJsonPath, 'utf-8'); + const keployVersionJson = JSON.parse(keployVersionJsonContent); - // Get the latest release - const response = await fetch(apiURL); - const data: any = await response.json(); - const latestVersion = data.tag_name; - return latestVersion; + const keployVersion = keployVersionJson[`${packageData.version}`]; + + return keployVersion; } export async function getCurrentKeployVersion() { @@ -24,7 +26,7 @@ export async function getCurrentKeployVersion() { output = await execShell('/usr/local/bin/keploybin --version'); }catch(error){ console.log("Error Fetching version With Absolute path " + error); - throw error; + return ''; } } console.log('output:', output); diff --git a/tsconfig.json b/tsconfig.json index d3a78fb..76abcf1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,8 @@ "rootDir": "src", "esModuleInterop": true, "skipLibCheck": true, - "strict": true /* enable all strict type-checking options */ + "strict": true, /* enable all strict type-checking options */ + "resolveJsonModule": true /* Additional Checks */ // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ From e74e9425aabc2cc41a5a9f415f40850c59b9a0a8 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Sun, 1 Dec 2024 11:52:24 +0530 Subject: [PATCH 2/4] removed update keploy & latest version buttons Signed-off-by: Ayush Sharma --- package.json | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/package.json b/package.json index ca37715..ae448fd 100644 --- a/package.json +++ b/package.json @@ -132,21 +132,11 @@ "command": "keploy.utg", "title": "Keploy: Unit Test Generation" }, - { - "command": "keploy.updateKeploy", - "title": "Update your Keploy", - "when": "view == Keploy-Sidebar" - }, { "command": "keploy.KeployVersion", "title": "View Your Keploy Version", "when": "view == Keploy-Sidebar" }, - { - "command": "keploy.getLatestVersion", - "title": "Get Latest Version", - "when": "view == Keploy-Sidebar" - }, { "command": "keploy.viewChangeLog", "title": "View Change Log", From 164cc0f42a168b1c37134f35eb7fb71acd5244d5 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Sun, 1 Dec 2024 11:59:33 +0530 Subject: [PATCH 3/4] removed from menu items Signed-off-by: Ayush Sharma --- package.json | 8 -------- 1 file changed, 8 deletions(-) diff --git a/package.json b/package.json index ae448fd..16eee74 100644 --- a/package.json +++ b/package.json @@ -69,14 +69,6 @@ "when": "view == Keploy-Sidebar && keploy.signedIn != true", "group": "navigation" }, - { - "command": "keploy.updateKeploy", - "when": "view == Keploy-Sidebar" - }, - { - "command": "keploy.getLatestVersion", - "when": "view == Keploy-Sidebar" - }, { "command": "keploy.KeployVersion", "when": "view == Keploy-Sidebar" From 7a51736e73619ea085247a94bf3bb02136972e8f Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Sun, 1 Dec 2024 13:14:00 +0530 Subject: [PATCH 4/4] fix: tests Signed-off-by: Ayush Sharma --- src/test/suites/oneclickinstall.test.ts | 15 ++++++++++----- src/test/suites/updateKeploy.test.ts | 11 +++++++---- src/test/suites/version.test.ts | 16 ++++++++-------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/test/suites/oneclickinstall.test.ts b/src/test/suites/oneclickinstall.test.ts index 130df7c..0455263 100644 --- a/src/test/suites/oneclickinstall.test.ts +++ b/src/test/suites/oneclickinstall.test.ts @@ -3,6 +3,7 @@ import * as child_process from 'child_process'; import assert from 'assert'; import executeKeployOneClickCommand from '../../OneClickInstall'; import { suite, test, setup, teardown } from 'mocha'; +import * as version from "../../version" suite('executeKeployOneClickCommand', () => { let execStub: sinon.SinonStub; @@ -15,24 +16,28 @@ suite('executeKeployOneClickCommand', () => { sinon.restore(); }); - test('should install Keploy if not already installed', () => { + test('should install Keploy if not already installed', async () => { const checkKeployExistsCommand = `keploy`; - const installationCommand = `curl --silent -L https://keploy.io/install.sh -o /tmp/install.sh && chmod +x /tmp/install.sh && /tmp/install.sh -noRoot`; + const installationCommand = `curl --silent -L https://keploy.io/install.sh -o /tmp/install.sh && chmod +x /tmp/install.sh && /tmp/install.sh -v v2.3.0-beta25 -noRoot`; + sinon.stub(version, 'getKeployVersion').resolves('v2.3.0-beta25'); + sinon.stub(version, 'getCurrentKeployVersion').resolves(''); // Simulate Keploy not being installed execStub.withArgs(checkKeployExistsCommand).callsArgWith(1, new Error('command not found'), '', ''); - executeKeployOneClickCommand(); + await executeKeployOneClickCommand(); assert(execStub.calledWith(installationCommand)); }); - test('should not install Keploy if already installed', () => { + test('should not install Keploy if already installed', async () => { const checkKeployExistsCommand = `keploy`; + sinon.stub(version, 'getKeployVersion').resolves('v2.3.0-beta25'); + sinon.stub(version, 'getCurrentKeployVersion').resolves(''); execStub.withArgs(checkKeployExistsCommand).callsArgWith(1, null, 'Keploy version 1.0.0', ''); - executeKeployOneClickCommand(); + await executeKeployOneClickCommand(); assert(execStub.calledWith(checkKeployExistsCommand)); assert(execStub.calledOnce); diff --git a/src/test/suites/updateKeploy.test.ts b/src/test/suites/updateKeploy.test.ts index 2364424..2e95843 100644 --- a/src/test/suites/updateKeploy.test.ts +++ b/src/test/suites/updateKeploy.test.ts @@ -32,16 +32,19 @@ suite("updateKeploy Test", function () { execStub.restore(); }); + afterEach(() => { + sinon.restore(); + }); + test('downloading and updating binary should execute correct command with exec', async () => { sinon.stub(process, 'platform').value('linux'); + sinon.stub(version, 'getKeployVersion').resolves('v2.3.0-beta25'); - 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 -L https://keploy.io/install.sh -o /tmp/install.sh && chmod +x /tmp/install.sh && /tmp/install.sh -v v2.3.0-beta25 -noRoot`; - const promise = updateKeploy.downloadAndInstallKeployBinary(); + await updateKeploy.downloadAndInstallKeployBinary(); assert.strictEqual(execStub.calledOnceWith(curlCmd), true); - - await promise; }); test('Download from docker should create a terminal and run the curl command on non-Windows platform', async () => { diff --git a/src/test/suites/version.test.ts b/src/test/suites/version.test.ts index 0655403..acd95aa 100644 --- a/src/test/suites/version.test.ts +++ b/src/test/suites/version.test.ts @@ -16,16 +16,16 @@ suite("Version tests", () => { sinon.restore() }) - test("getKeployVersion should be calling correct api", async () => { - let api = "https://api.github.com/repos/keploy/keploy/releases/latest" - const mockResponse = {tag_name : "v2.3.0-beta25"}; - fetchStub.resolves(new Response(JSON.stringify(mockResponse), { status: 200 })) + // test("getKeployVersion should be calling correct api", async () => { + // let api = "https://api.github.com/repos/keploy/keploy/releases/latest" + // const mockResponse = {tag_name : "v2.3.0-beta25"}; + // fetchStub.resolves(new Response(JSON.stringify(mockResponse), { status: 200 })) - await getKeployVersion(); + // await getKeployVersion(); - assert(fetchStub.calledOnce) - assert(fetchStub.calledWith(api)) - }) + // assert(fetchStub.calledOnce) + // assert(fetchStub.calledWith(api)) + // }) test("getCurrentKeployVersion should return version by executing keploy command", async () => {