@@ -3,12 +3,13 @@ import { resolve } from 'path';
33import { fancy } from 'fancy-test' ;
44import { expect } from 'chai' ;
55import cloneDeep from 'lodash/cloneDeep' ;
6- import { ux } from '@contentstack/cli-utilities' ;
6+ import { ux , cliux } from '@contentstack/cli-utilities' ;
77import sinon from 'sinon' ;
88
99import config from '../../../src/config' ;
1010import { Workflows } from '../../../src/modules' ;
11- import { $t , auditMsg } from '../../../src/messages' ;
11+ import { $t , auditMsg , commonMsg } from '../../../src/messages' ;
12+ import { Workflow } from '../../../src/types' ;
1213import { values } from 'lodash' ;
1314import { mockLogger } from '../mock-logger' ;
1415
@@ -147,4 +148,69 @@ describe('Workflows', () => {
147148 ] ) ;
148149 } ) ;
149150 } ) ;
151+
152+ describe ( 'fixWorkflowSchema confirmation before removing a workflow' , ( ) => {
153+ class WorkflowsWithCapturedFix extends Workflows {
154+ public fixedWorkflows : Record < string , Workflow > = { } ;
155+
156+ async writeFixContent ( newWorkflowSchema : Record < string , Workflow > ) {
157+ this . fixedWorkflows = newWorkflowSchema ;
158+ }
159+ }
160+
161+ const createWorkflows = ( flags : Record < string , unknown > ) =>
162+ new WorkflowsWithCapturedFix ( {
163+ moduleName : 'workflows' ,
164+ // Without ct2, every content type on wf3 is missing, so the fix removes wf3.
165+ ctSchema : cloneDeep ( require ( './../mock/contents/workflows/ctSchema.json' ) ) . filter (
166+ ( ct : { uid : string } ) => ct . uid !== 'ct2' ,
167+ ) ,
168+ config : Object . assign ( cloneDeep ( config ) , {
169+ basePath : resolve ( `./test/unit/mock/contents/` ) ,
170+ branch : undefined ,
171+ flags,
172+ } ) ,
173+ fix : true ,
174+ } ) ;
175+
176+ it ( 'should remove the workflow without prompting when external-config skipConfirm is set' , async ( ) => {
177+ const confirm = sinon . stub ( cliux , 'confirm' ) . resolves ( false ) ;
178+ const wf = createWorkflows ( { 'external-config' : { skipConfirm : true } } ) ;
179+
180+ await wf . run ( ) ;
181+
182+ expect ( confirm . notCalled ) . to . be . true ;
183+ expect ( wf . fixedWorkflows ) . to . not . have . property ( 'wf3' ) ;
184+ } ) ;
185+
186+ it ( 'should remove the workflow without prompting when copy-dir is set' , async ( ) => {
187+ const confirm = sinon . stub ( cliux , 'confirm' ) . resolves ( false ) ;
188+ const wf = createWorkflows ( { 'copy-dir' : true } ) ;
189+
190+ await wf . run ( ) ;
191+
192+ expect ( confirm . notCalled ) . to . be . true ;
193+ expect ( wf . fixedWorkflows ) . to . not . have . property ( 'wf3' ) ;
194+ } ) ;
195+
196+ it ( 'should remove the workflow without prompting when yes is set' , async ( ) => {
197+ const confirm = sinon . stub ( cliux , 'confirm' ) . resolves ( false ) ;
198+ const wf = createWorkflows ( { yes : true } ) ;
199+
200+ await wf . run ( ) ;
201+
202+ expect ( confirm . notCalled ) . to . be . true ;
203+ expect ( wf . fixedWorkflows ) . to . not . have . property ( 'wf3' ) ;
204+ } ) ;
205+
206+ it ( 'should ask once per workflow to remove and keep it when the user declines' , async ( ) => {
207+ const confirm = sinon . stub ( cliux , 'confirm' ) . resolves ( false ) ;
208+ const wf = createWorkflows ( { } ) ;
209+
210+ await wf . run ( ) ;
211+
212+ expect ( confirm . calledOnceWithExactly ( commonMsg . WORKFLOW_FIX_CONFIRMATION ) ) . to . be . true ;
213+ expect ( wf . fixedWorkflows ) . to . have . property ( 'wf3' ) ;
214+ } ) ;
215+ } ) ;
150216} ) ;
0 commit comments