Skip to content

Commit

Permalink
Fixes killing teams process for 'teams cache remove'. Closes pnp#6515
Browse files Browse the repository at this point in the history
  • Loading branch information
milanholemans authored and martinlingstuyl committed Dec 13, 2024
1 parent 2f9c2c6 commit 0c3fdce
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
17 changes: 8 additions & 9 deletions src/m365/teams/commands/cache/cache-remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import command from './cache-remove.js';
import os, { homedir } from 'os';

describe(commands.CACHE_REMOVE, () => {
const processOutput = `ProcessId
6456
14196
11352`;
const processOutput = `"Image Name","PID","Session Name","Session#","Mem Usage"
"ms-teams.exe","32544","Console","17","99,892 K"
"ms-teams.exe","47784","Console","17","127,152 K"`;
let log: string[];
let logger: Logger;
let loggerLogSpy: sinon.SinonSpy;
Expand Down Expand Up @@ -133,7 +132,7 @@ describe(commands.CACHE_REMOVE, () => {
sinon.stub(fs, 'existsSync').returns(true);
const error = new Error('random error');
sinon.stub(command, 'exec' as any).callsFake(async (opts) => {
if (opts === 'wmic process where caption="Teams.exe" get ProcessId') {
if (opts === 'tasklist /FI "IMAGENAME eq Teams.exe" /FO csv') {
throw error;
}
throw 'Invalid request';
Expand All @@ -148,7 +147,7 @@ describe(commands.CACHE_REMOVE, () => {
sinon.stub(fs, 'existsSync').returns(true);
const error = new Error('random error');
sinon.stub(command, 'exec' as any).callsFake(async (opts) => {
if (opts === 'wmic process where caption="Teams.exe" get ProcessId') {
if (opts === 'tasklist /FI "IMAGENAME eq Teams.exe" /FO csv') {
return { stdout: processOutput };
}
if (opts === 'rmdir /s /q "C:\\Users\\Administrator\\AppData\\Roaming\\Microsoft\\Teams"') {
Expand Down Expand Up @@ -212,7 +211,7 @@ describe(commands.CACHE_REMOVE, () => {
sinon.stub(process, 'env').value({ 'CLIMICROSOFT365_ENV': '', APPDATA: 'C:\\Users\\Administrator\\AppData\\Roaming' });
sinon.stub(process, 'kill' as any).returns(null);
sinon.stub(command, 'exec' as any).callsFake(async (opts) => {
if (opts === 'wmic process where caption="Teams.exe" get ProcessId') {
if (opts === 'tasklist /FI "IMAGENAME eq Teams.exe" /FO csv') {
return { stdout: 'No Instance(s) Available.' };
}
if (opts === 'rmdir /s /q "C:\\Users\\Administrator\\AppData\\Roaming\\Microsoft\\Teams"') {
Expand All @@ -237,7 +236,7 @@ describe(commands.CACHE_REMOVE, () => {
sinon.stub(process, 'env').value({ 'CLIMICROSOFT365_ENV': '', APPDATA: 'C:\\Users\\Administrator\\AppData\\Roaming' });
sinon.stub(process, 'kill' as any).returns(null);
sinon.stub(command, 'exec' as any).callsFake(async (opts) => {
if (opts === 'wmic process where caption="Teams.exe" get ProcessId') {
if (opts === 'tasklist /FI "IMAGENAME eq Teams.exe" /FO csv') {
return { stdout: processOutput };
}
if (opts === 'rmdir /s /q "C:\\Users\\Administrator\\AppData\\Roaming\\Microsoft\\Teams"') {
Expand All @@ -261,7 +260,7 @@ describe(commands.CACHE_REMOVE, () => {
sinon.stub(process, 'env').value({ 'CLIMICROSOFT365_ENV': '', APPDATA: 'C:\\Users\\Administrator\\AppData\\Roaming', LOCALAPPDATA: 'C:\\Users\\Administrator\\AppData\\Local' });
sinon.stub(process, 'kill' as any).returns(null);
sinon.stub(command, 'exec' as any).callsFake(async (opts) => {
if (opts === 'wmic process where caption="ms-teams.exe" get ProcessId') {
if (opts === 'tasklist /FI "IMAGENAME eq ms-teams.exe" /FO csv') {
return { stdout: processOutput };
}
if (opts === 'rmdir /s /q "C:\\Users\\Administrator\\AppData\\Local\\Packages\\MSTeams_8wekyb3d8bbwe\\LocalCache\\Microsoft\\MSTeams"') {
Expand Down
8 changes: 4 additions & 4 deletions src/m365/teams/commands/cache/cache-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface Options extends GlobalOptions {
}

interface Win32Process {
ProcessId: number;
PID: number;
}

class TeamsCacheRemoveCommand extends AnonymousCommand {
Expand Down Expand Up @@ -175,10 +175,10 @@ class TeamsCacheRemoveCommand extends AnonymousCommand {
switch (platform) {
case 'win32':
if (client === 'classic') {
cmd = 'wmic process where caption="Teams.exe" get ProcessId';
cmd = 'tasklist /FI "IMAGENAME eq Teams.exe" /FO csv';
}
else {
cmd = 'wmic process where caption="ms-teams.exe" get ProcessId';
cmd = 'tasklist /FI "IMAGENAME eq ms-teams.exe" /FO csv';
}
break;
case 'darwin':
Expand All @@ -204,7 +204,7 @@ class TeamsCacheRemoveCommand extends AnonymousCommand {
else if (platform === 'win32') {
const processJson: Win32Process[] = formatting.parseCsvToJson(cmdOutput.stdout);
for (const proc of processJson) {
process.kill(proc.ProcessId);
process.kill(proc.PID);
}
}
if (this.verbose) {
Expand Down

0 comments on commit 0c3fdce

Please sign in to comment.