Skip to content

Commit 02fb810

Browse files
committed
Increase ser/extension.ts unit tests code coverage
1 parent fb80fc8 commit 02fb810

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
"onCommand:openshift.component.delete",
4646
"onCommand:openshift.component.push",
4747
"onCommand:openshift.component.watch",
48-
"onCommand:openshift.catalog.list.components",
49-
"onCommand:openshift.catalog.list.services",
48+
"onCommand:openshift.catalog.listComponents",
49+
"onCommand:openshift.catalog.listServices",
5050
"onCommand:openshift.url.create",
5151
"onCommand:openshift.component.openUrl",
5252
"onCommand:openshift.storage.create",
@@ -175,12 +175,12 @@
175175
"category": "OpenShift"
176176
},
177177
{
178-
"command": "openshift.catalog.list.components",
178+
"command": "openshift.catalog.listComponents",
179179
"title": "List Catalog Components ",
180180
"category": "OpenShift"
181181
},
182182
{
183-
"command": "openshift.catalog.list.services",
183+
"command": "openshift.catalog.listCervices",
184184
"title": "List Catalog Services",
185185
"category": "OpenShift"
186186
},
@@ -352,12 +352,12 @@
352352
"group": "c1"
353353
},
354354
{
355-
"command": "openshift.catalog.list.components",
355+
"command": "openshift.catalog.listComponents",
356356
"when": "view == openshiftProjectExplorer && viewItem == cluster && isLoggedIn",
357357
"group": "c2@1"
358358
},
359359
{
360-
"command": "openshift.catalog.list.services",
360+
"command": "openshift.catalog.listServices",
361361
"when": "view == openshiftProjectExplorer && viewItem == cluster && isLoggedIn",
362362
"group": "c2@2"
363363
},

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export function activate(context: vscode.ExtensionContext) {
2525
vscode.commands.registerCommand('openshift.explorer.login', (context) => execute(Cluster.login, context)),
2626
vscode.commands.registerCommand('openshift.explorer.logout', (context) => execute(Cluster.logout, context)),
2727
vscode.commands.registerCommand('openshift.explorer.refresh', (context) => executeSync(Cluster.refresh, context)),
28-
vscode.commands.registerCommand('openshift.catalog.list.components', (context) => executeSync(Catalog.listComponents, context)),
29-
vscode.commands.registerCommand('openshift.catalog.list.services', (context) => executeSync(Catalog.listServices, context)),
28+
vscode.commands.registerCommand('openshift.catalog.listComponents', (context) => executeSync(Catalog.listComponents, context)),
29+
vscode.commands.registerCommand('openshift.catalog.listCervices', (context) => executeSync(Catalog.listServices, context)),
3030
vscode.commands.registerCommand('openshift.project.create', (context) => execute(Project.create, context)),
3131
vscode.commands.registerCommand('openshift.project.delete', (context) => execute(Project.del, context)),
3232
vscode.commands.registerCommand('openshift.project.delete.palette', (context) => execute(Project.del, context)),

test/extension.test.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,20 @@ import * as sinonChai from 'sinon-chai';
1515
import * as sinon from 'sinon';
1616
import { activate } from '../src/extension';
1717
import { Cluster } from '../src/openshift/cluster';
18+
import { Application } from '../src/openshift/application';
19+
import { Catalog } from '../src/openshift/catalog';
20+
import { Component} from '../src/openshift/component';
21+
import { Project} from '../src/openshift/project';
22+
import { Service} from '../src/openshift/service';
23+
import { Storage} from '../src/openshift/storage';
24+
import { Url} from '../src/openshift/url';
1825
import packagejson = require('../package.json');
26+
import { isContext } from 'vm';
1927

2028
const expect = chai.expect;
2129
chai.use(sinonChai);
2230

23-
suite('openshift connector Extension', () => {
31+
suite('openshift connector Extension', async () => {
2432

2533
let sandbox: sinon.SinonSandbox;
2634

@@ -57,10 +65,36 @@ suite('openshift connector Extension', () => {
5765
assert.ok(vscode.extensions.getExtension('redhat.vscode-openshift-connector'));
5866
});
5967

68+
async function getStaticMethosToStub(osc: string[]): Promise<string[]> {
69+
let mths: Set<string> = new Set();
70+
osc.forEach(name => {
71+
name.replace('.palette', '');
72+
let segs: string[] = name.split('.');
73+
let methName: string = segs[segs.length-1];
74+
methName = methName === 'delete'? 'del' : methName;
75+
!mths.has(methName) && mths.add(methName);
76+
77+
});
78+
return Array.from(mths);
79+
}
80+
6081
test('should activate extension', async () => {
6182
const registerTreeDataProviderStub = sandbox.stub(vscode.window, 'registerTreeDataProvider');
83+
sandbox.stub(vscode.window, 'showErrorMessage');
6284
await activate(context);
85+
let cmds:string[] = await vscode.commands.getCommands();
86+
let osc:string[] = cmds.filter((item) => item.includes('openshift.'));
6387
expect(registerTreeDataProviderStub).calledOnce;
88+
const mths: string[] = await getStaticMethosToStub(osc);
89+
(<any>[Application, Catalog, Cluster, Component, Project, Service, Storage, Url]).forEach(async (item) => {
90+
mths.forEach((name) => {
91+
if (item[name]) {
92+
sandbox.stub(item, name);
93+
}
94+
});
95+
})
96+
osc.forEach((item) => vscode.commands.executeCommand(item));
97+
expect(vscode.window.showErrorMessage).has.not.been.called;
6498
});
6599

66100
test('should register all server commands', async () => {
@@ -71,7 +105,7 @@ suite('openshift connector Extension', () => {
71105
serverCommands.push(value.command);
72106
});
73107
const foundServerCommands = commands.filter((value) => {
74-
return serverCommands.indexOf(value) >= 0 || value.startsWith('openshift.');
108+
return serverCommands.indexOf(value) >= 0;
75109
});
76110
assert.equal(foundServerCommands.length , serverCommands.length, 'Some openshift commands are not registered properly or a new command is not added to the test');
77111
});
@@ -107,6 +141,13 @@ suite('openshift connector Extension', () => {
107141
const simStub: sinon.SinonStub = sandbox.stub(vscode.window, 'showInformationMessage');
108142
await vscode.commands.executeCommand('openshift.about');
109143
expect(simStub).not.called;
110-
})
144+
});
111145

146+
test('sync command wrapper shows message returned from command', async () => {
147+
const error = new Error('Message');
148+
sandbox.stub(Cluster, 'refresh').throws(error);
149+
const semStub: sinon.SinonStub = sandbox.stub(vscode.window, 'showErrorMessage');
150+
await vscode.commands.executeCommand('openshift.explorer.refresh');
151+
expect(semStub).calledWith(error);
152+
});
112153
});

0 commit comments

Comments
 (0)