Skip to content

Commit

Permalink
fix: ACNA 2676 - confirm-new-workspace weirdness (#754)
Browse files Browse the repository at this point in the history
* fix confusing prompt/no-prompt logic

Co-authored-by: Shazron Abdullah <36107+shazron@users.noreply.github.com>
  • Loading branch information
purplecabbage and shazron committed Jan 12, 2024
1 parent 712e3f7 commit 8c5d879
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/commands/app/init.js
Expand Up @@ -468,8 +468,9 @@ InitCommand.flags = {
exclusive: ['import'] // also no-login
}),
'confirm-new-workspace': Flags.boolean({
description: 'Skip and confirm prompt for creating a new workspace',
default: false
description: 'Prompt to confirm before creating a new workspace',
default: true,
allowNo: true
}),
repo: Flags.string({
description: 'Init from gh quick-start repo. Expected to be of the form <owner>/<repo>/<path>',
Expand Down
41 changes: 29 additions & 12 deletions src/commands/app/use.js
Expand Up @@ -28,8 +28,6 @@ class Use extends BaseCommand {
async run () {
const { flags, args } = await this.parse(Use)

flags.workspace = flags.workspace || flags['workspace-name'] || ''

aioLogger.debug(`args: ${JSON.stringify(args, null, 2)}, flags: ${JSON.stringify(flags, null, 2)}`)

// some additional checks and updates of flags and args on top of what oclif provides
Expand Down Expand Up @@ -196,15 +194,23 @@ class Use extends BaseCommand {
let workspace
let workspaceData = { name: workspaceNameOrId }
// does not prompt if workspaceNameOrId is defined via the flag
workspace = await consoleCLI.promptForSelectWorkspace(workspaces, { workspaceId: workspaceNameOrId, workspaceName: workspaceNameOrId }, { allowCreate: true })
workspace = await consoleCLI.promptForSelectWorkspace(workspaces, {
workspaceId: workspaceNameOrId,
workspaceName: workspaceNameOrId
}, {
allowCreate: true
})

if (!workspace) {
aioLogger.debug(`--workspace=${workspaceNameOrId} was not found in the current Project ${project.name}`)
if (workspaceNameOrId) {
if (!flags['confirm-new-workspace']) {
if (flags['confirm-new-workspace']) {
const shouldNewWorkspace = await consoleCLI.prompt.promptConfirm(`Workspace '${workspaceNameOrId}' does not exist \n > Do you wish to create a new workspace?`)
if (!shouldNewWorkspace) {
this.error('Workspace creation aborted')
}
} else {
// this is not an error, if we end up here we just use `workspaceData` later
}
} else {
workspaceData = await consoleCLI.promptForCreateWorkspaceDetails()
Expand Down Expand Up @@ -369,15 +375,26 @@ Use.flags = {
exclusive: ['global', 'workspace-name']
}),
'confirm-new-workspace': Flags.boolean({
description: 'Skip and confirm prompt for creating a new workspace',
default: false
}),
'workspace-name': Flags.string({
description: '[DEPRECATED]: please use --workspace instead',
default: '',
char: 'w',
exclusive: ['global', 'workspace']
description: 'Prompt to confirm before creating a new workspace',
default: true,
allowNo: true,
relationships: [
// if prompt is false, then the workspace flag MUST be specified
{
type: 'all',
flags: [{
name: 'workspace',
when: async (flags) => flags['confirm-new-workspace'] === false
}]
}
]
}),
// 'workspace-name': Flags.string({
// description: '[DEPRECATED]: please use --workspace instead',
// default: '',
// char: 'w',
// exclusive: ['global', 'workspace']
// }),
'no-service-sync': Flags.boolean({
description: 'Skip the Service sync prompt and do not attach current Service subscriptions to the new Workspace',
default: false,
Expand Down
21 changes: 19 additions & 2 deletions test/commands/app/init.test.js
Expand Up @@ -225,7 +225,7 @@ describe('Command Prototype', () => {
expect(TheCommand.flags.workspace.exclusive).toEqual(['import'])

expect(TheCommand.flags['confirm-new-workspace'].type).toBe('boolean')
expect(TheCommand.flags['confirm-new-workspace'].default).toBe(false)
expect(TheCommand.flags['confirm-new-workspace'].default).toBe(true)
})

test('args', async () => {
Expand Down Expand Up @@ -556,10 +556,27 @@ describe('--login', () => {

command.selectTemplates.mockResolvedValue(['@adobe/my-extension'])

command.argv = ['-w', workspaceName]
command.argv = ['-w', workspaceName, '--no-confirm-new-workspace']
await expect(command.run()).rejects.toThrow(`Workspace '${workspaceName}' does not exist and creation aborted`)
})

test('select template, -w notexists, promptConfirm false, should throw coverage', async () => {
const workspaceName = 'notexists'
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
mockConsoleCLIInstance.prompt.promptConfirm.mockResolvedValue(true)
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
mockConsoleCLIInstance.promptForSelectProject.mockResolvedValue(fakeProject)
mockConsoleCLIInstance.getWorkspaces.mockResolvedValue(fakeWorkspaces)
mockConsoleCLIInstance.getServicePropertiesFromWorkspace.mockResolvedValue(fakeServicePropertiesNoAssetCompute)
mockConsoleCLIInstance.getEnabledServicesForOrg.mockResolvedValue(fakeSupportedOrgServices)
mockConsoleCLIInstance.getWorkspaceConfig.mockResolvedValue(fakeConfig)
mockConsoleCLIInstance.createWorkspace.mockResolvedValue(fakeWorkspaces[0])
command.selectTemplates.mockResolvedValue(['@adobe/my-extension'])

command.argv = ['-w', workspaceName, '--no-confirm-new-workspace']
await expect(command.run()).resolves.not.toThrow()
})

test('select template, -w notexists, --confirm-new-workspace', async () => {
const notexistsWorkspace = 'notexists'
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
Expand Down
27 changes: 23 additions & 4 deletions test/commands/app/use.test.js
Expand Up @@ -838,19 +838,38 @@ describe('switch to a workspace in the same org', () => {
.toHaveBeenCalledWith(fakeCurrentConfig.org.id, fakeCurrentConfig.id, { name: workspaceFlag })
})

test('-w newworkspace --confirm-new-workspace = false, error if canceled', async () => {
test('-w newworkspace --confirm-new-workspace=false, error if canceled', async () => {
mockConsoleImportConfig()
mockConsoleCLIInstance.promptForSelectWorkspace.mockReturnValueOnce(null)
mockConsoleCLIInstance.prompt.promptConfirm.mockReturnValueOnce(false)
TheCommand.argv = ['-w', 'new-workspace', '--confirm-new-workspace==false']
await expect(TheCommand.run()).rejects.toThrow()
expect(mockConsoleCLIInstance.prompt.promptConfirm).not.toHaveBeenCalled()
})

await expect(TheCommand.run(['-w new-workspace'])).rejects.toThrow()
test('-w newworkspace --no-confirm-new-workspace, error if cancelled', async () => {
mockConsoleImportConfig()
mockConsoleCLIInstance.promptForSelectWorkspace.mockReturnValueOnce(null)
mockConsoleCLIInstance.prompt.promptConfirm.mockReturnValueOnce(false)
TheCommand.argv = ['-w', 'new-workspace', '--no-confirm-new-workspace']
await expect(TheCommand.run()).rejects.toThrow()
expect(mockConsoleCLIInstance.prompt.promptConfirm).not.toHaveBeenCalled()
})
test('-w newworkspace --confirm-new-workspace = true, create workspace', async () => {

test('-w newworkspace --confirm-new-workspace, coverage', async () => {
mockConsoleImportConfig()
mockConsoleCLIInstance.promptForSelectWorkspace.mockReturnValueOnce(null)
mockConsoleCLIInstance.prompt.promptConfirm.mockReturnValueOnce(false)
await expect(TheCommand.run(['-w', 'new-workspace', '--confirm-new-workspace'])).rejects.toThrow('Workspace creation aborted')
expect(mockConsoleCLIInstance.prompt.promptConfirm).toHaveBeenCalled()
})

await TheCommand.run(['-w newWorkspace', '--confirm-new-workspace'])
test('-w newworkspace --no-confirm-new-workspace, coverage', async () => {
mockConsoleImportConfig()
mockConsoleCLIInstance.promptForSelectWorkspace.mockReturnValueOnce(null)
mockConsoleCLIInstance.prompt.promptConfirm.mockReturnValueOnce(false)
await expect(TheCommand.run(['-w', 'new-workspace', '--no-confirm-new-workspace'])).resolves.not.toThrow()
expect(mockConsoleCLIInstance.prompt.promptConfirm).not.toHaveBeenCalled()
expect(mockConsoleCLIInstance.createWorkspace).toHaveBeenCalled()
})
})

0 comments on commit 8c5d879

Please sign in to comment.