From 6d8c0a4e4472f434f4785e9879d65d5e32af4b3c Mon Sep 17 00:00:00 2001 From: Mickael Gaillard Date: Tue, 21 Jan 2025 18:54:05 +0100 Subject: [PATCH] Re-enabled test --- .github/workflows/ci.yml | 5 +- .vscode/launch.json | 6 +- .vscode/settings.json | 20 ++++- package-lock.json | 55 ++++++++++++- package.json | 6 +- src/command/global/register.ts | 2 +- src/pshstore.ts | 17 +++- .../fake-upsun/.upsun/local/project.yaml | 2 + src/test/runTest.ts | 4 +- .../command/environment/register.test.ts | 38 ++++----- .../suite/command/environment/ssh.test.ts | 2 +- .../suite/command/environment/url.test.ts | 2 +- src/test/suite/extension.test.ts | 10 ++- src/test/suite/index.ts | 81 +++++++------------ src/test/suite/project.test.ts | 6 +- src/test/suite/pshcli.test.ts | 37 ++++++--- src/test/suite/pshconfig.test.ts | 5 +- src/test/suite/pshstore.test.ts | 18 ++++- src/test/suite/utils/platform.test.ts | 74 +++++++++++++++++ src/test/suite/utils/webbrowser.test.ts | 2 +- src/utils/platform.ts | 51 ++++++------ tsconfig.json | 7 +- 22 files changed, 316 insertions(+), 134 deletions(-) create mode 100644 src/test/fixtures/fake-upsun/.upsun/local/project.yaml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f37f7df..0b2ca1f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,7 +31,6 @@ jobs: run: npm run test:unit -- --coverage test-integration: - if: false continue-on-error: true strategy: fail-fast: true @@ -48,7 +47,7 @@ jobs: node_version: ${{ env.NODE_VERSION}} - name: Create Folder - run: mkdir -p out/coverage/.nyc_output + run: mkdir -p ${{ github.workspace }}/out/coverage/.nyc_output - name: Run Test Linux run: xvfb-run -a npm test @@ -81,7 +80,7 @@ jobs: build-vsix: name: Create VSIX runs-on: ubuntu-latest - needs: [lint] # test-unit, test-integration, + needs: [lint, test-integration] # test-unit, test-integration, steps: - name: Checkout uses: actions/checkout@v4 diff --git a/.vscode/launch.json b/.vscode/launch.json index 4a2009e..0f30af3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -44,14 +44,16 @@ "request": "launch", "runtimeExecutable": "${execPath}", "args": [ - "${workspaceFolder}/src/test/fixtures/fake-psh", + "${workspaceFolder}/src/test/fixtures/fake-upsun", "--extensionDevelopmentPath=${workspaceFolder}", "--extensionTestsPath=${workspaceFolder}/out/test/suite/index", "--disable-extensions", "--disable-workspace-trust", ], + // "sourceMaps": true, + // "trace": true, "outFiles": [ - "${workspaceFolder}/out/test/**/*.js" + "${workspaceFolder}/out/**/*.js" ], "preLaunchTask": "${defaultBuildTask}" } diff --git a/.vscode/settings.json b/.vscode/settings.json index 30bf8c2..3152909 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,5 +7,21 @@ "out": true // set this to false to include "out" folder in search results }, // Turn off tsc task auto detection since we have the necessary tasks as npm scripts - "typescript.tsc.autoDetect": "off" -} \ No newline at end of file + "typescript.tsc.autoDetect": "on", + + "mochaExplorer.files": "src/test/**/*.test.ts", + "mochaExplorer.require": "ts-node/register", + //"mochaExplorer.require": "source-map-support/register", + //"mochaExplorer.files": "out/test/**/*.js", + "mochaExplorer.watch": "src/**/*.ts", + "mochaExplorer.ui": "tdd", + "mochaExplorer.launcherScript": "node_modules/mocha-explorer-launcher-scripts/vscode-test", + "mochaExplorer.autoload": false, + "mochaExplorer.ipcRole": "server", + "mochaExplorer.ipcTimeout": 30000, + "mochaExplorer.env": { + "VSCODE_VERSION": "1.96.3", //"insiders", + "VSCODE_LAUNCH_ARGS": "[\"./src/test/fixtures/fake-upsun\"]", + "ELECTRON_RUN_AS_NODE": null + } +} diff --git a/package-lock.json b/package-lock.json index c55f7ba..079aeff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,13 +23,14 @@ "@typescript-eslint/eslint-plugin": "^5.16.0", "@typescript-eslint/parser": "^5.16.0", "@vscode/l10n-dev": "^0.0.35", - "@vscode/test-electron": "^2.3.8", + "@vscode/test-electron": "^2.4.1", "@vscode/vsce": "^3.2.1", "chai": "^4.3.6", "eslint": "^8.11.0", "glob": "^7.2.0", "gulp": "^5.0.0", "mocha": "^9.2.2", + "mocha-explorer-launcher-scripts": "^0.4.0", "nyc": "^15.1.0", "prettier": "^3.4.2", "source-map-support": "^0.5.21", @@ -5739,6 +5740,16 @@ "url": "https://opencollective.com/mochajs" } }, + "node_modules/mocha-explorer-launcher-scripts": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/mocha-explorer-launcher-scripts/-/mocha-explorer-launcher-scripts-0.4.0.tgz", + "integrity": "sha512-cik/K4r+7WlhpzRmaecA5MWBPOgFRqCdZ95Tvbc5HBohj1I8vgRvBSfAIKdLVJes0PooFlrOzn7Alh4lEELSjg==", + "dev": true, + "license": "MIT", + "dependencies": { + "vscode-test-adapter-remoting-util": "^0.13.0" + } + }, "node_modules/mocha/node_modules/debug": { "version": "4.3.3", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.3.tgz", @@ -7650,6 +7661,19 @@ "node": ">=8" } }, + "node_modules/split": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", + "integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==", + "dev": true, + "license": "MIT", + "dependencies": { + "through": "2" + }, + "engines": { + "node": "*" + } + }, "node_modules/sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", @@ -8029,6 +8053,13 @@ "dev": true, "license": "MIT" }, + "node_modules/through": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", + "integrity": "sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==", + "dev": true, + "license": "MIT" + }, "node_modules/tmp": { "version": "0.2.3", "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.2.3.tgz", @@ -8496,6 +8527,28 @@ "dev": true, "license": "MIT" }, + "node_modules/vscode-test-adapter-api": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/vscode-test-adapter-api/-/vscode-test-adapter-api-1.9.0.tgz", + "integrity": "sha512-lltjehUP0J9H3R/HBctjlqeUCwn2t9Lbhj2Y500ib+j5Y4H3hw+hVTzuSsfw16LtxY37knlU39QIlasa7svzOQ==", + "dev": true, + "license": "MIT", + "engines": { + "vscode": "^1.23.0" + } + }, + "node_modules/vscode-test-adapter-remoting-util": { + "version": "0.13.0", + "resolved": "https://registry.npmjs.org/vscode-test-adapter-remoting-util/-/vscode-test-adapter-remoting-util-0.13.0.tgz", + "integrity": "sha512-7yI+A+v4K24j+X/pJLgIlAGCIY1tOs9B/lBpPXMvukfPSJibMGts5t2BNb2Kh1wLe2tJBOADs4pu5oWnXKPvzQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "split": "^1.0.1", + "tslib": "^2.0.0", + "vscode-test-adapter-api": "^1.9.0" + } + }, "node_modules/web-tree-sitter": { "version": "0.20.8", "resolved": "https://registry.npmjs.org/web-tree-sitter/-/web-tree-sitter-0.20.8.tgz", diff --git a/package.json b/package.json index b2c251c..86c1f87 100644 --- a/package.json +++ b/package.json @@ -329,7 +329,8 @@ "format-check": "prettier --check 'src/**/*.ts' '.github/**/*.yml'", "format-fix": "prettier --write 'src/**/*.ts' '.github/**/*.yml' gulpfile.js", "checkDependencies": "gulp checkDependencies", - "test": "node ./out/test/runTest.js" + "test": "node ./out/test/runTest.js", + "test-new": "vscode-test" }, "devDependencies": { "@istanbuljs/nyc-config-typescript": "^1.0.2", @@ -342,13 +343,14 @@ "@typescript-eslint/eslint-plugin": "^5.16.0", "@typescript-eslint/parser": "^5.16.0", "@vscode/l10n-dev": "^0.0.35", - "@vscode/test-electron": "^2.3.8", + "@vscode/test-electron": "^2.4.1", "@vscode/vsce": "^3.2.1", "chai": "^4.3.6", "eslint": "^8.11.0", "glob": "^7.2.0", "gulp": "^5.0.0", "mocha": "^9.2.2", + "mocha-explorer-launcher-scripts": "^0.4.0", "nyc": "^15.1.0", "prettier": "^3.4.2", "source-map-support": "^0.5.21", diff --git a/src/command/global/register.ts b/src/command/global/register.ts index 1a13395..c4b8fc9 100644 --- a/src/command/global/register.ts +++ b/src/command/global/register.ts @@ -51,7 +51,7 @@ async function registerSettingToken(context: vscode.ExtensionContext) { URI_EXTENSION_SETTING_TOKEN, async () => { const store = new PshStorage(context); - await store.setToken(); + await store.setTokenInteractive(); }, ), ); diff --git a/src/pshstore.ts b/src/pshstore.ts index bbcb2b6..59e5c44 100644 --- a/src/pshstore.ts +++ b/src/pshstore.ts @@ -32,13 +32,22 @@ export class PshStorage { * * Open InputBox to enter the token value. */ - async setToken() { - const token: string = - (await vscode.window.showInputBox({ + //TODO move on UI code (not tested) + setTokenInteractive() { + vscode.window + .showInputBox({ password: true, title: 'Upsun Token', - })) ?? ''; + }) + .then((token) => { + this.setToken(token ?? ''); + }); + } + /** + * Define Token into settings(boolean) and SecretStorage(value). + */ + setToken(token: string) { if (token !== '') { this.secretStorage.store(SECRET_TOKEN, token); vscode.workspace.getConfiguration().update(SECRET_TOKEN, true); diff --git a/src/test/fixtures/fake-upsun/.upsun/local/project.yaml b/src/test/fixtures/fake-upsun/.upsun/local/project.yaml new file mode 100644 index 0000000..91ca124 --- /dev/null +++ b/src/test/fixtures/fake-upsun/.upsun/local/project.yaml @@ -0,0 +1,2 @@ +id: erwlgagf6enrq +host: api.upsun.com diff --git a/src/test/runTest.ts b/src/test/runTest.ts index 43d2392..f81b171 100644 --- a/src/test/runTest.ts +++ b/src/test/runTest.ts @@ -1,7 +1,9 @@ import * as path from 'path'; import { runTests } from '@vscode/test-electron'; +import { Console } from 'console'; +// Use from CLI not on VScode laucnher !! async function main() { try { // The folder containing the Extension Manifest package.json @@ -13,7 +15,7 @@ async function main() { const extensionTestsPath = path.resolve(__dirname, './suite/index'); const launchArgs = [ - path.resolve(__dirname, '../../src/test/fixtures/fake-project1/'), + path.resolve(__dirname, '../../src/test/fixtures/fake-upsun'), ]; // Download VS Code, unzip it and run the integration test diff --git a/src/test/suite/command/environment/register.test.ts b/src/test/suite/command/environment/register.test.ts index f64e4c5..cf226e8 100644 --- a/src/test/suite/command/environment/register.test.ts +++ b/src/test/suite/command/environment/register.test.ts @@ -1,24 +1,24 @@ import * as path from 'path'; import { assert } from 'chai'; import * as vscode from 'vscode'; -import { registerEnvironment } from '../../../../command/environment/register'; +// import { registerEnvironment } from '../../../../command/environment/register'; -export const rootPath = path.resolve(__dirname, '../'); -const packageJSON: any = require(path.resolve(rootPath, 'package.json')); -export const extensionId = `${packageJSON.publisher}.${packageJSON.name}`; +// export const rootPath = path.resolve(__dirname, '../../../'); +// const packageJSON: any = require(path.resolve(rootPath, 'package.json')); +// export const extensionId = `${packageJSON.publisher}.${packageJSON.name}`; -suite('Env Test Suite', () => { - test('Environement.register', async () => { - const ext = vscode.extensions.getExtension(extensionId); - if (ext) { - //const extCtx = await ext.activate(); - //const extCtx = await vscode.commands.executeCommand("getContext") as vscode.ExtensionContext; - //registerEnvironment(extCtx); - // assert.isDefined(extCtx.subscriptions.push() ) - // assert.strictEqual(ctx.projectId, prj); - // assert.strictEqual(ctx.toString(), ` -p ${prj}`); - } else { - assert.fail(); - } - }); -}); +// suite('Env Test Suite', () => { +// test('Environement.register', async () => { +// const ext = vscode.extensions.getExtension(extensionId); +// if (ext) { +// //const extCtx = await ext.activate(); +// //const extCtx = await vscode.commands.executeCommand("getContext") as vscode.ExtensionContext; +// //registerEnvironment(extCtx); +// // assert.isDefined(extCtx.subscriptions.push() ) +// // assert.strictEqual(ctx.projectId, prj); +// // assert.strictEqual(ctx.toString(), ` -p ${prj}`); +// } else { +// assert.fail(); +// } +// }); +// }); diff --git a/src/test/suite/command/environment/ssh.test.ts b/src/test/suite/command/environment/ssh.test.ts index 01aa870..dfbe605 100644 --- a/src/test/suite/command/environment/ssh.test.ts +++ b/src/test/suite/command/environment/ssh.test.ts @@ -28,5 +28,5 @@ suite('Env/SSH Test Suite', () => { } catch (e) { assert.fail(); } - }); + }).skip(); }); diff --git a/src/test/suite/command/environment/url.test.ts b/src/test/suite/command/environment/url.test.ts index b6eff9b..04f8131 100644 --- a/src/test/suite/command/environment/url.test.ts +++ b/src/test/suite/command/environment/url.test.ts @@ -35,5 +35,5 @@ suite('Env/URL Test Suite', () => { } catch (e) { assert.fail(); } - }); + }).skip(); }); diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts index c573b43..20ceb8d 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/suite/extension.test.ts @@ -3,7 +3,7 @@ import * as vscode from 'vscode'; import { activate } from '../../extension'; import * as path from 'path'; -export const rootPath = path.resolve(__dirname, '../'); +export const rootPath = path.resolve(__dirname, '../../../'); const packageJSON: any = require(path.resolve(rootPath, 'package.json')); export const extensionId = `${packageJSON.publisher}.${packageJSON.name}`; @@ -13,15 +13,19 @@ suite('Extension Test Suite', () => { let gitExtension: any | undefined; if (ext) { + // Extension Found! if (!ext.isActive) { + // Not active, but activate them! ext.activate().then(() => { new Promise((f) => setTimeout(f, 1000)); assert.isTrue(ext.isActive); }); + } else { + // And is active. + assert.isTrue(ext.isActive); } - - assert.isTrue(ext.isActive); } else { + // Not extension found! assert.fail(); } }); diff --git a/src/test/suite/index.ts b/src/test/suite/index.ts index 753b06e..1a0b370 100644 --- a/src/test/suite/index.ts +++ b/src/test/suite/index.ts @@ -2,32 +2,6 @@ import * as path from 'path'; import * as Mocha from 'mocha'; import * as glob from 'glob'; -function setupCoverage() { - const NYC = require('nyc'); - const nyc = new NYC({ - // set the project root - cwd: path.join(__dirname, '..', '..'), - extension: ['.ts', '.js'], - include: ['**/*.js', '**/*.ts'], - exclude: ['**/test/**', '.vscode-test/**'], - sourceMap: true, - reporter: ['html', 'text', 'text-summary'], - tempDir: path.join(__dirname, '..', '..', 'coverage', '.nyc_output'), - all: true, - cache: false, - instrument: true, - hookRequire: true, - hookRunInContext: true, - hookRunInThisContext: true, - }); - - nyc.reset(); - nyc.wrap(); - - // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return nyc; -} - export async function run(): Promise { // Create the mocha test const mocha = new Mocha({ @@ -36,13 +10,17 @@ export async function run(): Promise { timeout: 10 * 1000, }); + // Code coverage. let nyc; if (shouldGenerateCoverage()) { nyc = setupCoverage(); } + + // Tests runner const testsRoot = path.resolve(__dirname, '..'); const options = { cwd: testsRoot }; const files = glob.sync('**/**.test.js', options); + // Add files to the test suite files.forEach((file) => mocha.addFile(path.resolve(testsRoot, file))); @@ -62,31 +40,6 @@ export async function run(): Promise { await nyc.report(); } } - - // return new Promise((c, e) => { - // glob('**/**.test.js', { cwd: testsRoot }, (err, files) => { - // if (err) { - // return e(err); - // } - - // // Add files to the test suite - // files.forEach(f => mocha.addFile(path.resolve(testsRoot, f))); - - // try { - // // Run the mocha test - // mocha.run(failures => { - // if (failures > 0) { - // e(new Error(`${failures} tests failed.`)); - // } else { - // c(); - // } - // }); - // } catch (err) { - // console.error(err); - // e(err); - // } - // }); - // }); } function shouldGenerateCoverage(): boolean { @@ -96,3 +49,29 @@ function shouldGenerateCoverage(): boolean { 'true' ); } + +function setupCoverage() { + const NYC = require('nyc'); + const nyc = new NYC({ + // set the project root + cwd: path.join(__dirname, '..', '..'), + extension: ['.ts', '.js'], + include: ['**/*.js', '**/*.ts'], + exclude: ['**/test/**', '.vscode-test/**'], + sourceMap: true, + reporter: ['html', 'text', 'text-summary'], + tempDir: path.join(__dirname, '..', '..', 'coverage', '.nyc_output'), + all: true, + cache: false, + instrument: true, + hookRequire: true, + hookRunInContext: true, + hookRunInThisContext: true, + }); + + nyc.reset(); + nyc.wrap(); + + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return nyc; +} diff --git a/src/test/suite/project.test.ts b/src/test/suite/project.test.ts index 741031a..7925711 100644 --- a/src/test/suite/project.test.ts +++ b/src/test/suite/project.test.ts @@ -7,7 +7,8 @@ suite('Tools Test Suite', () => { assert.isDefined(ctx); assert.isDefined(ctx.projectId); - assert.isDefined(ctx.environment); + //TODO manage usage of external extension (fixture has not git at root) + //assert.isDefined(ctx.environment); }); test('Tools.makeCliContext', () => { @@ -16,7 +17,8 @@ suite('Tools Test Suite', () => { assert.isDefined(pshCli); assert.isDefined(ctx); assert.isDefined(ctx.projectId); - assert.isDefined(ctx.environment); + //TODO manage usage of external extension (fixture has not git at root) + //assert.isDefined(ctx.environment); pshCli.dispose(); }); diff --git a/src/test/suite/pshcli.test.ts b/src/test/suite/pshcli.test.ts index 598c993..1d7071d 100644 --- a/src/test/suite/pshcli.test.ts +++ b/src/test/suite/pshcli.test.ts @@ -1,25 +1,41 @@ import { assert } from 'chai'; +import * as vscode from 'vscode'; import { PshContext, PshContextCommand } from '../../command/base'; import { PshCli } from '../../pshcli'; +import { ExtensionContext, extensions } from 'vscode'; + +//TODO refactor +import * as path from 'path'; +export const rootPath = path.resolve(__dirname, '../../../'); +const packageJSON: any = require(path.resolve(rootPath, 'package.json')); +export const extensionId = `${packageJSON.publisher}.${packageJSON.name}`; class MockCommand extends PshContextCommand { - constructor() { - super(new PshContext('', '', null)); + constructor(ext: vscode.ExtensionContext) { + super(new PshContext('', '', ext)); } displayMessage(): string { - throw new Error('Method not implemented.'); + return 'display mock'; } prepare(): string { - return ' > /dev/null ; echo -n success'; + return ' &> /dev/null ; echo -n success'; } - process(param: any): void {} + process(param: any): any { + return param; + } } suite('PshCli Test Suite', () => { let cli: PshCli; + let extensionContext: ExtensionContext; + + suiteSetup('PshStore suite setup', async () => { + await extensions.getExtension(extensionId)?.activate(); + extensionContext = (global as any).testExtensionContext; + }); setup('PshCli setup', () => { cli = new PshCli(); @@ -33,9 +49,10 @@ suite('PshCli Test Suite', () => { assert.isDefined(cli); }); - test('PshCli.dispose', async () => { - cli.dispose(); - }); + // Close by Teardown cannot be test. + // test('PshCli.dispose', async () => { + // cli.dispose(); + // }); test('PshCli.executeStr.ok', async () => { const rawResult = await cli.executeStr( @@ -51,7 +68,9 @@ suite('PshCli Test Suite', () => { // }); test('PshCli.executeObj.ok', async () => { - const rawResult = await cli.executeObj(new MockCommand()); + const rawResult = await cli.executeObj( + new MockCommand(extensionContext), + ); assert.strictEqual(rawResult, 'success'); }); }); diff --git a/src/test/suite/pshconfig.test.ts b/src/test/suite/pshconfig.test.ts index a130cee..a791dba 100644 --- a/src/test/suite/pshconfig.test.ts +++ b/src/test/suite/pshconfig.test.ts @@ -4,8 +4,9 @@ import { ConfigBase, ConfigFactory } from '../../pshconfig'; const testPathPsh = path.resolve( __dirname, - '../../../src/test/fixtures/fake-project1/', + '../../../src/test/fixtures/fake-upsun/', ); +const projectId = 'erwlgagf6enrq'; suite('PshConfig Test Suite', () => { let config: ConfigBase; @@ -15,6 +16,6 @@ suite('PshConfig Test Suite', () => { }); test('PshConfig.pshProjectId', () => { - assert.equal(config.projectId, '6q5ffgcj3ieeo'); + assert.equal(config.projectId, projectId); }); }); diff --git a/src/test/suite/pshstore.test.ts b/src/test/suite/pshstore.test.ts index e70aba6..419a4f4 100644 --- a/src/test/suite/pshstore.test.ts +++ b/src/test/suite/pshstore.test.ts @@ -2,8 +2,9 @@ import { assert } from 'chai'; import { ExtensionContext, extensions } from 'vscode'; import { PshStorage } from '../../pshstore'; +//TODO refactor import * as path from 'path'; -export const rootPath = path.resolve(__dirname, '../'); +export const rootPath = path.resolve(__dirname, '../../../'); const packageJSON: any = require(path.resolve(rootPath, 'package.json')); export const extensionId = `${packageJSON.publisher}.${packageJSON.name}`; @@ -16,15 +17,26 @@ suite('PshStore Test Suite', () => { extensionContext = (global as any).testExtensionContext; }); - test('PshStore.reset', async () => { + test('PshStore.resetDefaultApp', async () => { const store = new PshStorage(extensionContext); store.resetDefaultApp(); assert.isNull(store.getDefaultApp()); }); - test('PshStore.setget', async () => { + test('PshStore.setgetDefaultApp', async () => { const store = new PshStorage(extensionContext); store.setDefaultApp(valueDefaultApp); assert.equal(store.getDefaultApp(), valueDefaultApp); }); + + test('PshStore.setgetToken', async () => { + const store = new PshStorage(extensionContext); + const value = 'SECRET_VALUE'; + + store.setToken(value); + assert.equal(await store.getToken(), value); + + store.setToken(''); + assert.equal(await store.getToken(), value); + }); }); diff --git a/src/test/suite/utils/platform.test.ts b/src/test/suite/utils/platform.test.ts index c32dcd0..8413e3d 100644 --- a/src/test/suite/utils/platform.test.ts +++ b/src/test/suite/utils/platform.test.ts @@ -2,6 +2,8 @@ import { assert } from 'chai'; import { Architecture, getArchitecture, + getBrowserCommand, + getGithubFileTag, getOSType, OSType, } from '../../../utils/platform'; @@ -31,6 +33,14 @@ suite('Platform Test Suite', () => { assert.strictEqual(getArchitecture('x64'), Architecture.x64); }); + test('Platform.getArchitecture.arm', () => { + assert.strictEqual(getArchitecture('arm'), Architecture.arm); + }); + + test('Platform.getArchitecture.arm64', () => { + assert.strictEqual(getArchitecture('arm64'), Architecture.arm64); + }); + test('Platform.getArchitecture.empty', () => { assert.strictEqual(getArchitecture(''), Architecture.unknown); }); @@ -39,4 +49,68 @@ suite('Platform Test Suite', () => { // Base on current curring machine assert.strictEqual(getArchitecture('undefined'), Architecture.x64); }); + + test('Platform.getBrowserCommand.Linux', () => { + assert.strictEqual(getBrowserCommand('linux'), 'xdg-open'); + }); + + test('Platform.getBrowserCommand.OSX', () => { + assert.strictEqual(getBrowserCommand('darwin'), 'open'); + }); + + test('Platform.getBrowserCommand.Win', () => { + assert.strictEqual(getBrowserCommand('windows'), 'start'); + }); + + test('Platform.getGithubFileTag.Linux_amd64', () => { + const [cmd, item, dest] = getGithubFileTag('linux', 'x64'); + + assert.strictEqual(cmd, 'tar -xvzf'); + assert.strictEqual(item, '-linux-amd64.tar.gz'); + assert.strictEqual(dest, '~/.upsun-cli/addons'); + }); + + test('Platform.getGithubFileTag.Linux_arm64', () => { + const [cmd, item, dest] = getGithubFileTag('linux', 'arm64'); + + assert.strictEqual(cmd, 'tar -xvzf'); + assert.strictEqual(item, '-linux-arm64.tar.gz'); + assert.strictEqual(dest, '~/.upsun-cli/addons'); + }); + + test('Platform.getGithubFileTag.OSX_arm64', () => { + const [cmd, item, dest] = getGithubFileTag('darwin', 'arm64'); + + assert.strictEqual(cmd, 'tar -xvzf'); + assert.strictEqual(item, '-darwin-arm64.tar.gz'); + assert.strictEqual(dest, '~/.upsun-cli/addons'); + }); + + test('Platform.getGithubFileTag.Win_amd64', () => { + const [cmd, item, dest] = getGithubFileTag('win', 'x64'); + + assert.strictEqual(cmd, 'powershell Expand-Archive -Path'); + assert.strictEqual(item, '-windows-amd64.zip'); + assert.strictEqual(dest, '%USER%/.upsun-cli/addons'); + }); + + test('Platform.getGithubFileTag.Win_x86', () => { + try { + getGithubFileTag('win', 'x86'); + + assert.fail(); + } catch (e) { + assert.strictEqual(1, 1); + } + }); + + test('Platform.getGithubFileTag.Win_arm', () => { + try { + getGithubFileTag('win', 'arm'); + + assert.fail(); + } catch (e) { + assert.strictEqual(1, 1); + } + }); }); diff --git a/src/test/suite/utils/webbrowser.test.ts b/src/test/suite/utils/webbrowser.test.ts index 841f3d7..dc501fb 100644 --- a/src/test/suite/utils/webbrowser.test.ts +++ b/src/test/suite/utils/webbrowser.test.ts @@ -6,7 +6,7 @@ suite('WebBrowser Test Suite', () => { try { WebBrowser.open('Test : You can close !'); - assert.equal(1, 1); + assert.equal(1, 1); // Only test if not fail! } catch (e) { assert.fail(); } diff --git a/src/utils/platform.ts b/src/utils/platform.ts index f5466ed..7c14879 100644 --- a/src/utils/platform.ts +++ b/src/utils/platform.ts @@ -41,21 +41,21 @@ const architectures: Record = { /** * Identify the host's native architecture/bitness. */ -export function getArchitecture(archIdx: string = process.arch): Architecture { - const fromProc = architectures[archIdx]; +export function getArchitecture(arch: string = process.arch): Architecture { + const fromProc = architectures[arch]; if (fromProc !== undefined) { return fromProc; } // Fallback - const arch = require('arch'); - return architectures[arch()] || Architecture.unknown; + const archModule = require('arch'); + return architectures[archModule()] || Architecture.unknown; } -export function getBrowserCommand(): string { +export function getBrowserCommand(platform: string = process.platform): string { let cmd: string; - switch (getOSType()) { + switch (getOSType(platform)) { case OSType.windows: cmd = 'start'; break; @@ -71,35 +71,38 @@ export function getBrowserCommand(): string { return cmd; } -export function getGithubFileTag(): [string, string, string] { - let os: string; - let arch: string; - let ext: string = 'tar.gz'; - let cmd: string = 'tar -xvzf'; - let dest: string = '~/.upsun-cli/addons'; +export function getGithubFileTag( + platform: string = process.platform, + arch: string = process.arch, +): [string, string, string] { + let _os: string; + let _arch: string; + let _ext: string = 'tar.gz'; + let _cmd: string = 'tar -xvzf'; + let _dest: string = '~/.upsun-cli/addons'; - switch (getOSType()) { + switch (getOSType(platform)) { case OSType.windows: - os = OSType.windows.toLowerCase(); - cmd = 'powershell Expand-Archive -Path '; //TODO need to test - ext = 'zip'; - dest = '%USERQ%/.upsun-cli/addons'; + _os = OSType.windows.toLowerCase(); + _cmd = 'powershell Expand-Archive -Path'; //TODO need to test + _ext = 'zip'; + _dest = '%USER%/.upsun-cli/addons'; break; case OSType.osx: - os = 'darwin'; + _os = 'darwin'; break; case OSType.linux: default: - os = OSType.linux.toLowerCase(); + _os = OSType.linux.toLowerCase(); break; } - switch (getArchitecture()) { + switch (getArchitecture(arch)) { case Architecture.x64: - arch = 'amd64'; + _arch = 'amd64'; break; case Architecture.arm64: - arch = 'arm64'; + _arch = 'arm64'; break; case Architecture.arm: case Architecture.x86: @@ -107,6 +110,6 @@ export function getGithubFileTag(): [string, string, string] { throw new Error('Not supported'); } - const item: string = `-${os}-${arch}.${ext}`; - return [cmd, item, dest]; + const _item: string = `-${_os}-${_arch}.${_ext}`; + return [_cmd, _item, _dest]; } diff --git a/tsconfig.json b/tsconfig.json index 15853aa..43cae68 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,11 +8,14 @@ ], "sourceMap": true, "rootDir": "src", - "strict": true /* enable all strict type-checking options */ + "strict": true, /* enable all strict type-checking options */ + // "esModuleInterop": true, + // "forceConsistentCasingInFileNames": true, // "useUnknownInCatchVariables": 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. */ // "noUnusedParameters": true, /* Report errors on unused parameters. */ - } + }, + "exclude": ["node_modules", ".vscode-test"] }