Skip to content

Commit

Permalink
fix: incorrect logic for flag --confirm-new-workspace in command aio …
Browse files Browse the repository at this point in the history
…app init (#793)
  • Loading branch information
shazron committed Apr 22, 2024
1 parent 6566c29 commit d1741cf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/commands/app/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class InitCommand extends TemplatesCommand {
const workspaces = await consoleCLI.getWorkspaces(org.id, project.id)
let workspace = workspaces.find(w => w.name.toLowerCase() === workspaceName.toLowerCase())
if (!workspace) {
if (!flags['confirm-new-workspace']) {
if (flags['confirm-new-workspace']) {
const shouldNewWorkspace = await consoleCLI.prompt.promptConfirm(`Workspace '${workspaceName}' does not exist \n > Do you wish to create a new workspace?`)
if (!shouldNewWorkspace) {
this.error(`Workspace '${workspaceName}' does not exist and creation aborted`)
Expand Down
29 changes: 17 additions & 12 deletions test/commands/app/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ describe('--login', () => {
expect(mockConsoleCLIInstance.createWorkspace).toHaveBeenCalledWith(expect.anything(), expect.anything(), expect.objectContaining({ name: 'notexists', title: '' }))
})

test('select template, -w notexists, promptConfirm false, should throw', async () => {
test('select template, -w notexists, --no-confirm-new-workspace (no confirm, create workspace)', async () => {
const workspaceName = 'notexists'
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
mockConsoleCLIInstance.prompt.promptConfirm.mockResolvedValue(false)
Expand All @@ -601,28 +601,33 @@ describe('--login', () => {
command.selectTemplates.mockResolvedValue(['@adobe/my-extension'])

command.argv = ['-w', workspaceName, '--no-confirm-new-workspace']
await expect(command.run()).rejects.toThrow(`Workspace '${workspaceName}' does not exist and creation aborted`)
await expect(command.run()).resolves.not.toThrow()
expect(mockConsoleCLIInstance.prompt.promptConfirm).not.toHaveBeenCalled()
expect(mockConsoleCLIInstance.createWorkspace).toHaveBeenCalledWith(expect.anything(), expect.anything(), expect.objectContaining({ name: workspaceName, title: '' }))
})

test('select template, -w notexists, promptConfirm false, should throw coverage', async () => {
test('select template, -w notexists, --confirm-new-workspace (confirm result: false, throws error)', async () => {
const workspaceName = 'notexists'
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
mockConsoleCLIInstance.prompt.promptConfirm.mockResolvedValue(true)
mockConsoleCLIInstance.prompt.promptConfirm.mockResolvedValue(false)
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()
command.argv = ['-w', workspaceName, '--confirm-new-workspace']
await expect(command.run()).rejects.toThrow(`Workspace '${workspaceName}' does not exist and creation aborted`)
expect(mockConsoleCLIInstance.prompt.promptConfirm).toHaveBeenCalled()
expect(mockConsoleCLIInstance.createWorkspace).not.toHaveBeenCalled()
})

test('select template, -w notexists, --confirm-new-workspace', async () => {
const notexistsWorkspace = 'notexists'
test('select template, -w notexists, --confirm-new-workspace (confirm result: true, create workspace)', async () => {
const workspaceName = 'notexists'
mockConsoleCLIInstance.checkDevTermsForOrg.mockResolvedValue(true)
mockConsoleCLIInstance.prompt.promptConfirm.mockResolvedValue(true)
mockConsoleCLIInstance.promptForSelectOrganization.mockResolvedValue(fakeOrg)
Expand All @@ -635,10 +640,10 @@ describe('--login', () => {

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

command.argv = ['-w', notexistsWorkspace, '--confirm-new-workspace']
await command.run()
expect(mockConsoleCLIInstance.prompt.promptConfirm).not.toHaveBeenCalled()
expect(mockConsoleCLIInstance.createWorkspace).toHaveBeenCalledWith(expect.anything(), expect.anything(), expect.objectContaining({ name: 'notexists', title: '' }))
command.argv = ['-w', workspaceName, '--confirm-new-workspace']
await expect(command.run()).resolves.not.toThrow()
expect(mockConsoleCLIInstance.prompt.promptConfirm).toHaveBeenCalled()
expect(mockConsoleCLIInstance.createWorkspace).toHaveBeenCalledWith(expect.anything(), expect.anything(), expect.objectContaining({ name: workspaceName, title: '' }))
})

test('--extension dx/excshell/1 --extension dx/something/1 (found)', async () => {
Expand Down

0 comments on commit d1741cf

Please sign in to comment.