Skip to content

Commit

Permalink
Feedback from Waldek
Browse files Browse the repository at this point in the history
  • Loading branch information
MathijsVerbeeck committed Oct 30, 2024
1 parent b10c8dd commit bfc6ec2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
35 changes: 34 additions & 1 deletion src/m365/teams/commands/cache/cache-remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { session } from '../../../../utils/session.js';
import { sinonUtil } from '../../../../utils/sinonUtil.js';
import commands from '../../commands.js';
import command from './cache-remove.js';
import os from 'os';
import os, { homedir } from 'os';

describe(commands.CACHE_REMOVE, () => {
const processOutput = `ProcessId
Expand All @@ -21,6 +21,7 @@ describe(commands.CACHE_REMOVE, () => {
11352`;
let log: string[];
let logger: Logger;
let loggerLogSpy: sinon.SinonSpy;
let commandInfo: CommandInfo;

before(() => {
Expand All @@ -46,6 +47,7 @@ describe(commands.CACHE_REMOVE, () => {
log.push(msg);
}
};
loggerLogSpy = sinon.spy(logger, 'log');

sinon.stub(cli, 'promptForConfirmation').resolves(true);
});
Expand Down Expand Up @@ -157,6 +159,37 @@ describe(commands.CACHE_REMOVE, () => {
await assert.rejects(command.action(logger, { options: { client: 'classic', force: true } } as any), new CommandError('random error'));
});

it('shows error message when exec fails when removing the teams cache folder on mac os', async () => {
const deleteError = {
code: 1,
killed: false,
signal: null,
cmd: 'rm -r "/Users/John/Library/Group Containers/UBF8T346G9.com.microsoft.teams"',
stdout: '',
stderr: 'rm: /Users/John/Library/Group Containers/UBF8T346G9.com.microsoft.teams: Operation not permitted\\n'
};

sinon.stub(process, 'platform').value('darwin');
sinon.stub(process, 'env').value({ 'CLIMICROSOFT365_ENV': '' });
sinon.stub(process, 'kill' as any).returns(null);
sinon.stub(fs, 'existsSync').returns(true);

sinon.stub(command, 'exec' as any).callsFake(async (opts) => {
if (opts === `ps ax | grep MacOS/MSTeams -m 1 | grep -v grep | awk '{ print $1 }'`) {
return {};
}
if (opts === `rm -r "${homedir}/Library/Group Containers/UBF8T346G9.com.microsoft.teams"`) {
return;
}
if (opts === `rm -r "${homedir}/Library/Containers/com.microsoft.teams2"`) {
throw deleteError;
}
throw 'Invalid request';
});
await command.action(logger, { options: { force: true } } as any);
assert(loggerLogSpy.calledWith('Deleting the folder failed. Please have a look at the following URL to delete the folders manually: https://answers.microsoft.com/en-us/msteams/forum/all/clearing-cache-on-microsoft-teams/35876f6b-eb1a-4b77-bed1-02ce3277091f'));
});

it('removes Teams cache from macOs platform without prompting using classic client', async () => {
sinon.stub(process, 'platform').value('darwin');
sinon.stub(process, 'env').value({ 'CLIMICROSOFT365_ENV': '' });
Expand Down
18 changes: 14 additions & 4 deletions src/m365/teams/commands/cache/cache-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TeamsCacheRemoveCommand extends AnonymousCommand {
#initTelemetry(): void {
this.telemetry.push((args: CommandArgs) => {
Object.assign(this.telemetryProperties, {
client: typeof args.options.client !== 'undefined',
client: args.options.client,
force: !!args.options.force
});
});
Expand Down Expand Up @@ -105,7 +105,7 @@ class TeamsCacheRemoveCommand extends AnonymousCommand {
}

private async clearTeamsCache(client: string, logger: Logger): Promise<void> {
const filePaths = await this.getTeamsCacheFolderPath(client, logger);
const filePaths = await this.getTeamsCacheFolderPaths(client, logger);

let folderExists = true;
for (const filePath of filePaths) {
Expand All @@ -126,7 +126,7 @@ class TeamsCacheRemoveCommand extends AnonymousCommand {

}

private async getTeamsCacheFolderPath(client: string, logger: Logger): Promise<string[]> {
private async getTeamsCacheFolderPaths(client: string, logger: Logger): Promise<string[]> {
const platform = process.platform;

if (this.verbose) {
Expand Down Expand Up @@ -227,7 +227,17 @@ class TeamsCacheRemoveCommand extends AnonymousCommand {
await logger.logToStderr(cmd);
}

await this.exec(cmd);
try {
await this.exec(cmd);
}
catch (err: any) {
if (err?.stderr?.includes('Operation not permitted')) {
await logger.log('Deleting the folder failed. Please have a look at the following URL to delete the folders manually: https://answers.microsoft.com/en-us/msteams/forum/all/clearing-cache-on-microsoft-teams/35876f6b-eb1a-4b77-bed1-02ce3277091f');
}
else {
throw err;
}
}
}
}

Expand Down

0 comments on commit bfc6ec2

Please sign in to comment.