Skip to content

Commit 332c2df

Browse files
committed
Add unit tests when Get-CimInstance fails
1 parent bd0bc2d commit 332c2df

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

src/extension/debugger/attachQuickPick/powerShellProcessParser.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@
88
import { IAttachItem, ProcessListCommand } from './types';
99

1010
export namespace PowerShellProcessParser {
11-
// Perf numbers on Win10:
12-
// | # of processes | Time (ms) |
13-
// |----------------+-----------|
14-
// | 309 | 413 |
15-
// | 407 | 463 |
16-
// | 887 | 746 |
17-
// | 1308 | 1132 |
1811
export const powerShellCommand: ProcessListCommand = {
1912
command: 'powershell',
2013
args: [
@@ -24,6 +17,16 @@ export namespace PowerShellProcessParser {
2417
], // Get-WmiObject For the legacy compatibility
2518
};
2619

20+
//for unit test with Get-WmiObject
21+
export const powerShellWithoutCimCommand: ProcessListCommand = {
22+
command: 'powershell',
23+
args: [
24+
'-Command',
25+
'$processes = if (Get-Command NotExistCommand-That-Will-Never-Exist -ErrorAction SilentlyContinue) { Get-CimInstance Win32_Process } else { Get-WmiObject Win32_Process }; \
26+
$processes | % { @{ name = $_.Name; commandLine = $_.CommandLine; processId = $_.ProcessId } } | ConvertTo-Json',
27+
],
28+
};
29+
2730
export function parseProcesses(processes: string): IAttachItem[] {
2831
const processesArray = JSON.parse(processes);
2932
const processEntries: IAttachItem[] = [];

src/extension/debugger/attachQuickPick/provider.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ export class AttachProcessProvider implements IAttachProcessProvider {
9494
}
9595
const output = await plainExec(processCmd.command, processCmd.args, { throwOnStdErr: true }, customEnvVars);
9696
logProcess(processCmd.command, processCmd.args, { throwOnStdErr: true });
97-
97+
console.log(processCmd);
9898
if (processCmd === WmicProcessParser.wmicCommand) {
9999
return WmicProcessParser.parseProcesses(output.stdout);
100-
} else if (processCmd === PowerShellProcessParser.powerShellCommand) {
100+
} else if (processCmd === PowerShellProcessParser.powerShellCommand ||
101+
processCmd === PowerShellProcessParser.powerShellWithoutCimCommand) {
101102
return PowerShellProcessParser.parseProcesses(output.stdout);
102103
}
103104
return PsProcessParser.parseProcesses(output.stdout);

src/test/unittest/attachQuickPick/provider.unit.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,5 +752,63 @@ ProcessId=8026\r
752752
);
753753
assert.deepEqual(output, expectedOutput);
754754
});
755+
756+
test('The Windows powershell Get-WmiObject process list command should be called when Get-CimInstance fails', async () => {
757+
const windowsProcesses = [
758+
{
759+
processId: 4,
760+
commandLine: null,
761+
name: 'System',
762+
},
763+
{
764+
processId: 5372,
765+
commandLine: null,
766+
name: 'svchost.exe',
767+
},
768+
];
769+
const expectedOutput: IAttachItem[] = [
770+
{
771+
label: 'svchost.exe',
772+
description: '5372',
773+
detail: '',
774+
id: '5372',
775+
processName: 'svchost.exe',
776+
commandLine: '',
777+
},
778+
{
779+
label: 'System',
780+
description: '4',
781+
detail: '',
782+
id: '4',
783+
processName: 'System',
784+
commandLine: '',
785+
},
786+
];
787+
const foundPowerShellOutput = 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\r\n';
788+
789+
plainExecStub
790+
.withArgs('where', ['powershell'], sinon.match.any, sinon.match.any)
791+
.resolves({ stderr: '', stdout: foundPowerShellOutput });
792+
793+
const windowsOutput = JSON.stringify(windowsProcesses, null, 4);
794+
plainExecStub
795+
.withArgs(
796+
PowerShellProcessParser.powerShellWithoutCimCommand.command,
797+
sinon.match.any,
798+
sinon.match.any,
799+
sinon.match.any,
800+
)
801+
.resolves({ stdout: windowsOutput });
802+
803+
const output = await provider.getAttachItems(PowerShellProcessParser.powerShellWithoutCimCommand);
804+
sinon.assert.calledWithExactly(
805+
plainExecStub.firstCall,
806+
PowerShellProcessParser.powerShellWithoutCimCommand.command,
807+
PowerShellProcessParser.powerShellWithoutCimCommand.args,
808+
sinon.match.any,
809+
sinon.match.any,
810+
);
811+
assert.deepEqual(output, expectedOutput);
812+
});
755813
});
756814
});

0 commit comments

Comments
 (0)