Skip to content

Commit

Permalink
fix: pack throws getting api-mesh (#774)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelGoberling authored Feb 22, 2024
1 parent 73a01b2 commit cb59ac5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
13 changes: 8 additions & 5 deletions src/commands/app/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -24,4 +21,4 @@ Successfully retrieved mesh {
"displayName": "John Doe"
},
"meshStatus": "success"
}
}
40 changes: 30 additions & 10 deletions test/commands/app/pack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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 }
})

Expand Down Expand Up @@ -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'
}
})
Expand All @@ -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()
Expand All @@ -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 () => {
Expand Down

0 comments on commit cb59ac5

Please sign in to comment.