Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure that -h works for all commands. fixes #506 #507

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/hooks/prerun/help-override.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright 2021 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

const { isThisPlugin } = require('../../cloudmanager-hook-helpers')

module.exports = function (hookOptions) {
if (!isThisPlugin(hookOptions)) {
return
}
if (hookOptions && hookOptions.argv && hookOptions.argv.includes('-h')) {
new hookOptions.Command(hookOptions.argv, hookOptions.config)._help()
}
}
4 changes: 4 additions & 0 deletions src/hooks/prerun/prerun-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ const { handleError } = require('../../cloudmanager-helpers')

module.exports = function (hookOptions) {
try {
require('./help-override').apply(this, [hookOptions])
require('./permission-info').apply(this, [hookOptions])
require('./environment-id-from-config').apply(this, [hookOptions])
require('./check-ims-context-config').apply(this, [hookOptions])
} catch (err) {
if (err.code && err.code === 'EEXIT') { // code from @oclif/errors - ExitError
throw err
}
handleError(err, this.error)
}
}
85 changes: 85 additions & 0 deletions test/hooks/prerun/help-override.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2021 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

const hook = require('../../../src/hooks/prerun/help-override')

let mockHelp
let mockConstructor

beforeEach(() => {
mockHelp = jest.fn()
mockConstructor = jest.fn()
})

const invoke = (options) => {
return () => hook.apply({}, [{
Command: Fixture,
config: {},
...options,
}])
}

test('hook -- command from other plugin', async () => {
expect(invoke({
Command: FixtureFromOtherPlugin,
argv: [],
})).not.toThrowError()
})

test("hook -- no argv doesn't call help", async () => {
invoke({
argv: [],
})()
expect(mockHelp.mock.calls.length).toBe(0)
})

test("hook -- some other argv doesn't call help", async () => {
invoke({
argv: ['--environmentId=3'],
})()
expect(mockHelp.mock.calls.length).toBe(0)
})

test('hook -- -h calls help', async () => {
invoke({
argv: ['-h'],
config: {
MOCKCONFIG: true,
},
})()
expect(mockHelp.mock.calls.length).toBe(1)
expect(mockConstructor.mock.calls.length).toBe(1)
expect(mockConstructor.mock.calls[0][0]).toEqual(['-h'])
expect(mockConstructor.mock.calls[0][1]).toEqual({
MOCKCONFIG: true,
})
})

class Fixture {
constructor (argv, config) {
mockConstructor(argv, config)
}

_help () {
mockHelp()
}
}
Fixture.id = 'somecommand'
Fixture.plugin = {
name: '@adobe/aio-cli-plugin-cloudmanager',
}

class FixtureFromOtherPlugin {
}
FixtureFromOtherPlugin.plugin = {
name: 'something-else',
}
30 changes: 26 additions & 4 deletions test/hooks/prerun/prerun-all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ const { RequiredArgsError } = require('@oclif/parser/lib/errors')
const hook = require('../../../src/hooks/prerun/prerun-all')

let parse
let error

beforeEach(() => {
setStore({})
parse = jest.fn()
error = jest.fn()
})

const invoke = (options) => {
return () => hook.apply({ error: (msg) => { throw new Error(msg) } }, [{
return () => hook.apply({ error }, [{
...options,
}])
}
Expand Down Expand Up @@ -65,12 +67,23 @@ test('hook -- bad configuration', async () => {

parse = jest.fn().mockImplementationOnce(() => true)

expect.assertions(1)
expect.assertions(2)

expect(invoke({
invoke({
Command: FixtureWithEnvironmentIdArg,
argv: [],
})).toThrowError('One or more of the required fields in ims.contexts.aio-cli-plugin-cloudmanager were not set. Missing keys were private_key.')
})()
expect(error.mock.calls.length).toBe(1)
expect(error.mock.calls[0][0]).toBe('[CloudManagerCLI:IMS_CONTEXT_MISSING_FIELDS] One or more of the required fields in ims.contexts.aio-cli-plugin-cloudmanager were not set. Missing keys were private_key.')
})

test('hook -- exit error is not handled', async () => {
expect.assertions(1)

expect(invoke({
Command: HelpFixture,
argv: ['-h'],
})).toThrowError('')
})

const thisPlugin = {
Expand All @@ -87,3 +100,12 @@ FixtureWithEnvironmentIdArg.args = [
{ name: 'environmentId' },
]
FixtureWithEnvironmentIdArg.plugin = thisPlugin

class HelpFixture {
_help () {
const err = new Error()
err.code = 'EEXIT'
throw err
}
}
HelpFixture.plugin = thisPlugin