From fb36db8d6e9eca1d8969eeadb3030fe18460ec97 Mon Sep 17 00:00:00 2001 From: Nanddeep Nachan Date: Tue, 2 Jan 2024 13:33:50 +0000 Subject: [PATCH] Enhances 'pa app get' to throw error when app not found. Closes #5728 --- src/m365/pa/commands/app/app-get.spec.ts | 18 ++++++++++++------ src/m365/pa/commands/app/app-get.ts | 14 +++++--------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/m365/pa/commands/app/app-get.spec.ts b/src/m365/pa/commands/app/app-get.spec.ts index a92737ab023..e3f46b52e4d 100644 --- a/src/m365/pa/commands/app/app-get.spec.ts +++ b/src/m365/pa/commands/app/app-get.spec.ts @@ -20,7 +20,6 @@ describe(commands.APP_GET, () => { let logger: Logger; let loggerLogSpy: sinon.SinonSpy; let commandInfo: CommandInfo; - let loggerLogToStderrSpy: sinon.SinonSpy; const apps = [ { @@ -517,7 +516,6 @@ describe(commands.APP_GET, () => { } }; loggerLogSpy = sinon.spy(logger, 'log'); - loggerLogToStderrSpy = sinon.spy(logger, 'logToStderr'); }); afterEach(() => { @@ -714,8 +712,12 @@ describe(commands.APP_GET, () => { throw 'Invalid request'; }); - await command.action(logger, { options: { debug: true, displayName: 'NoAppFound' } }); - assert(loggerLogToStderrSpy.calledWith(`No app found with displayName 'NoAppFound'`)); + await assert.rejects(command.action(logger, { + options: { + verbose: true, + displayName: 'NoAppFound' + } + } as any), new CommandError(`No app found with displayName 'NoAppFound'.`)); }); it('correctly handles no apps found using displayName (debug)', async () => { @@ -726,8 +728,12 @@ describe(commands.APP_GET, () => { throw 'Invalid request'; }); - await command.action(logger, { options: { debug: true, displayName: 'Playwright' } }); - assert(loggerLogToStderrSpy.calledWith(`No apps found`)); + await assert.rejects(command.action(logger, { + options: { + verbose: true, + displayName: 'Playwright' + } + } as any), new CommandError('No apps found.')); }); it('correctly handles API OData error', async () => { diff --git a/src/m365/pa/commands/app/app-get.ts b/src/m365/pa/commands/app/app-get.ts index cecab31a0ef..5228827446d 100644 --- a/src/m365/pa/commands/app/app-get.ts +++ b/src/m365/pa/commands/app/app-get.ts @@ -124,25 +124,21 @@ class PaAppGetCommand extends PowerAppsCommand { } const getAppsOutput = await this.getApps(args, logger); - - const allApps: any = JSON.parse(getAppsOutput.stdout); - if (allApps.length > 0) { + if (getAppsOutput.stdout && JSON.parse(getAppsOutput.stdout).length > 0) { + const allApps: any[] = JSON.parse(getAppsOutput.stdout); const app = allApps.find((a: any) => { return a.properties.displayName.toLowerCase() === `${args.options.displayName}`.toLowerCase(); }); + if (!!app) { await logger.log(this.setProperties(app)); } else { - if (this.verbose) { - await logger.logToStderr(`No app found with displayName '${args.options.displayName}'`); - } + throw `No app found with displayName '${args.options.displayName}'.`; } } else { - if (this.verbose) { - await logger.logToStderr('No apps found'); - } + throw 'No apps found.'; } } }