diff --git a/src/m365/teams/commands/cache/cache-remove.spec.ts b/src/m365/teams/commands/cache/cache-remove.spec.ts index 106c4c2ff5..bb4726db0a 100644 --- a/src/m365/teams/commands/cache/cache-remove.spec.ts +++ b/src/m365/teams/commands/cache/cache-remove.spec.ts @@ -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 @@ -21,6 +21,7 @@ describe(commands.CACHE_REMOVE, () => { 11352`; let log: string[]; let logger: Logger; + let loggerLogSpy: sinon.SinonSpy; let commandInfo: CommandInfo; before(() => { @@ -46,6 +47,7 @@ describe(commands.CACHE_REMOVE, () => { log.push(msg); } }; + loggerLogSpy = sinon.spy(logger, 'log'); sinon.stub(cli, 'promptForConfirmation').resolves(true); }); @@ -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': '' }); diff --git a/src/m365/teams/commands/cache/cache-remove.ts b/src/m365/teams/commands/cache/cache-remove.ts index e3804660c8..d96a002edc 100644 --- a/src/m365/teams/commands/cache/cache-remove.ts +++ b/src/m365/teams/commands/cache/cache-remove.ts @@ -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 }); }); @@ -105,7 +105,7 @@ class TeamsCacheRemoveCommand extends AnonymousCommand { } private async clearTeamsCache(client: string, logger: Logger): Promise { - const filePaths = await this.getTeamsCacheFolderPath(client, logger); + const filePaths = await this.getTeamsCacheFolderPaths(client, logger); let folderExists = true; for (const filePath of filePaths) { @@ -126,7 +126,7 @@ class TeamsCacheRemoveCommand extends AnonymousCommand { } - private async getTeamsCacheFolderPath(client: string, logger: Logger): Promise { + private async getTeamsCacheFolderPaths(client: string, logger: Logger): Promise { const platform = process.platform; if (this.verbose) { @@ -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; + } + } } }