From 4f036afbe48d58b5b5e18645609b79e18a2198de Mon Sep 17 00:00:00 2001 From: Michael Goberling Date: Tue, 20 Feb 2024 18:18:29 -0500 Subject: [PATCH] fix: pack throws getting api-mesh --- src/commands/app/pack.js | 13 +++--- ...3.api-mesh.get.txt => 3.api-mesh.get.json} | 7 +--- test/commands/app/pack.test.js | 40 ++++++++++++++----- 3 files changed, 40 insertions(+), 20 deletions(-) rename test/__fixtures__/pack/{3.api-mesh.get.txt => 3.api-mesh.get.json} (81%) diff --git a/src/commands/app/pack.js b/src/commands/app/pack.js index 5bf75d69..c407d506 100644 --- a/src/commands/app/pack.js +++ b/src/commands/app/pack.js @@ -166,15 +166,18 @@ class Pack extends BaseCommand { if (command) { try { this.spinner.start('Getting api-mesh config...') - const { stdout } = await execa('aio', ['api-mesh', 'get'], { cwd: process.cwd() }) - // until we get the --json flag, we parse the output - const idx = stdout.indexOf('{') - meshConfig = JSON.parse(stdout.substring(idx)).meshConfig + const { stdout, stderr } = await execa('aio', ['api-mesh', 'get', '--json'], { cwd: process.cwd() }) + + if (stderr) { + throw new Error(stderr) + } + + meshConfig = JSON.parse(stdout).meshConfig aioLogger.debug(`api-mesh:get - ${JSON.stringify(meshConfig, null, 2)}`) this.spinner.succeed('Got api-mesh config') } catch (err) { // Ignore error if no mesh found, otherwise throw - if (err?.stderr.includes('Error: Unable to get mesh config. No mesh found for Org')) { + if (err?.message.includes('Error: Unable to get mesh config. No mesh found for Org')) { aioLogger.debug('No api-mesh config found') } else { console.error(err) diff --git a/test/__fixtures__/pack/3.api-mesh.get.txt b/test/__fixtures__/pack/3.api-mesh.get.json similarity index 81% rename from test/__fixtures__/pack/3.api-mesh.get.txt rename to test/__fixtures__/pack/3.api-mesh.get.json index fa3b3f79..225c8423 100644 --- a/test/__fixtures__/pack/3.api-mesh.get.txt +++ b/test/__fixtures__/pack/3.api-mesh.get.json @@ -1,7 +1,4 @@ -Selected organization: My Org -Selected project: My Project -Select workspace: Stage -Successfully retrieved mesh { +{ "lastUpdated": "2023-03-02T09:58:23.896Z", "meshConfig": { "sources": [ @@ -24,4 +21,4 @@ Successfully retrieved mesh { "displayName": "John Doe" }, "meshStatus": "success" -} \ No newline at end of file +} diff --git a/test/commands/app/pack.test.js b/test/commands/app/pack.test.js index f3ce9676..799943bc 100644 --- a/test/commands/app/pack.test.js +++ b/test/commands/app/pack.test.js @@ -130,7 +130,7 @@ test('copyPackageFiles', async () => { test('createDeployYamlFile (1 extension)', async () => { const extConfig = fixtureJson('pack/2.all.config.json') - const meshOutput = fixtureFile('pack/3.api-mesh.get.txt') + const meshOutput = fixtureFile('pack/3.api-mesh.get.json') const command = new TheCommand() command.argv = [] @@ -142,7 +142,7 @@ test('createDeployYamlFile (1 extension)', async () => { execa.mockImplementationOnce((cmd, args) => { expect(cmd).toEqual('aio') - expect(args).toEqual(['api-mesh', 'get']) + expect(args).toEqual(['api-mesh', 'get', '--json']) return { stdout: meshOutput } }) @@ -180,9 +180,8 @@ test('createDeployYamlFile (1 extension), no api-mesh', async () => { execa.mockImplementationOnce((cmd, args) => { expect(cmd).toEqual('aio') - expect(args).toEqual(['api-mesh', 'get']) - // eslint-disable-next-line no-throw-literal - throw { + expect(args).toEqual(['api-mesh', 'get', '--json']) + return { stderr: 'Error: Unable to get mesh config. No mesh found for Org' } }) @@ -194,7 +193,7 @@ test('createDeployYamlFile (1 extension), no api-mesh', async () => { await expect(importHelper.writeFile.mock.calls[0][2]).toMatchObject({ overwrite: true }) }) -test('createDeployYamlFile (1 extension), api-mesh get call throws non 404 error', async () => { +test('createDeployYamlFile (1 extension), no api-mesh, plugin throws error', async () => { const extConfig = fixtureJson('pack/2.all.config.json') const command = new TheCommand() @@ -207,16 +206,37 @@ test('createDeployYamlFile (1 extension), api-mesh get call throws non 404 error execa.mockImplementationOnce((cmd, args) => { expect(cmd).toEqual('aio') - expect(args).toEqual(['api-mesh', 'get']) + expect(args).toEqual(['api-mesh', 'get', '--json']) // eslint-disable-next-line no-throw-literal throw { + stderr: 'Error: Unable to get mesh config. No mesh found for Org' + } + }) + + await expect(command.createDeployYamlFile(extConfig)).rejects.toEqual(TypeError('Cannot read properties of undefined (reading \'includes\')')) +}) + +test('createDeployYamlFile (1 extension), api-mesh get call throws non 404 error', async () => { + const extConfig = fixtureJson('pack/2.all.config.json') + + const command = new TheCommand() + command.argv = [] + command.config = { + findCommand: jest.fn().mockReturnValue({}), + runCommand: jest.fn(), + runHook: jest.fn() + } + + execa.mockImplementationOnce((cmd, args) => { + expect(cmd).toEqual('aio') + expect(args).toEqual(['api-mesh', 'get', '--json']) + // eslint-disable-next-line no-throw-literal + return { stderr: 'Error: api-mesh service is unavailable' } }) - await expect(command.createDeployYamlFile(extConfig)).rejects.toEqual(expect.objectContaining({ - stderr: expect.stringContaining('Error: api-mesh service is unavailable') - })) + await expect(command.createDeployYamlFile(extConfig)).rejects.toEqual(Error('Error: api-mesh service is unavailable')) }) test('createDeployYamlFile (coverage: standalone app, no services)', async () => {