@@ -20,7 +20,7 @@ import { before, describe, esmocha, expect, it } from 'esmocha';
2020import { basename } from 'node:path' ;
2121
2222import EnvironmentBuilder from '../../cli/environment-builder.ts' ;
23- import { defaultHelpers as helpers } from '../../lib/testing/index.ts' ;
23+ import { defaultHelpers as helpers , result } from '../../lib/testing/index.ts' ;
2424import { getCommandHelpOutput , shouldSupportFeatures } from '../../test/support/tests.ts' ;
2525
2626import BaseGenerator from './index.ts' ;
@@ -89,4 +89,118 @@ describe(`generator - ${generator}`, () => {
8989 expect ( postWriting ) . not . toHaveBeenCalled ( ) ;
9090 } ) ;
9191 } ) ;
92+
93+ describe ( 'control' , ( ) => {
94+ class CustomGenerator extends BaseGenerator {
95+ beforeQueue ( ) {
96+ this . customLifecycle = true ;
97+ }
98+ }
99+
100+ describe ( '.jhipsterOldVersion' , ( ) => {
101+ let jhipsterOldVersion : string | undefined ;
102+
103+ before ( async ( ) => {
104+ await helpers
105+ . run ( CustomGenerator )
106+ . withJHipsterConfig ( { jhipsterVersion : '1.0.0' } )
107+ . commitFiles ( )
108+ . withJHipsterGenerators ( { useDefaultMocks : true } )
109+ . withTask ( 'postWriting' , async function ( this , { control } ) {
110+ jhipsterOldVersion = control . jhipsterOldVersion ;
111+ } ) ;
112+ } ) ;
113+
114+ it ( 'have jhipsterOldVersion set correctly' , async ( ) => {
115+ expect ( jhipsterOldVersion ) . toBe ( '1.0.0' ) ;
116+ } ) ;
117+ } ) ;
118+
119+ describe ( '.cleanupFiles' , ( ) => {
120+ let removeFiles : ReturnType < typeof esmocha . fn > ;
121+
122+ before ( async ( ) => {
123+ removeFiles = esmocha . fn ( ) ;
124+
125+ await helpers
126+ . run ( CustomGenerator )
127+ . withJHipsterConfig ( { jhipsterVersion : '1.0.0' } )
128+ . commitFiles ( )
129+ . withJHipsterGenerators ( { useDefaultMocks : true } )
130+ . withTask ( 'postWriting' , async function ( this , { control } ) {
131+ control . removeFiles = removeFiles ;
132+ await control . cleanupFiles ( {
133+ '1.0.1' : [
134+ '1.0.1-file.txt' ,
135+ [ true , '1.0.1-conditional-truthy-file1.txt' , '1.0.1-conditional-truthy-file2.txt' ] ,
136+ [ false , '1.0.1-conditional-falsy-file1.txt' ] ,
137+ ] ,
138+ } ) ;
139+ } ) ;
140+ } ) ;
141+
142+ it ( 'should call removeFiles' , async ( ) => {
143+ expect ( removeFiles . mock . lastCall ) . toMatchInlineSnapshot ( `
144+ [
145+ {
146+ "oldVersion": "1.0.0",
147+ "removedInVersion": "1.0.1",
148+ },
149+ "1.0.1-file.txt",
150+ "1.0.1-conditional-truthy-file1.txt",
151+ "1.0.1-conditional-truthy-file2.txt",
152+ ]
153+ ` ) ;
154+ } ) ;
155+ } ) ;
156+
157+ describe ( '.removeFiles' , ( ) => {
158+ before ( async ( ) => {
159+ await helpers
160+ . run ( CustomGenerator )
161+ . withJHipsterConfig ( { jhipsterVersion : '1.0.0' } )
162+ . withFiles ( { 'diskFileToBeRemoved1.txt' : 'foo' , 'diskFileToBeRemoved2.txt' : 'foo' , 'diskFileNotToBeRemoved1.txt' : 'foo' } )
163+ . commitFiles ( )
164+ . withFiles ( { 'memFsFileToBeRemoved1.txt' : 'foo' } )
165+ . withJHipsterGenerators ( { useDefaultMocks : true } )
166+ . withTask ( 'postWriting' , async function ( this , { control } ) {
167+ await control . removeFiles ( { removedInVersion : '1.0.0' } , 'diskFileNotToBeRemoved1.txt' ) ;
168+ await control . removeFiles ( { removedInVersion : '1.0.1' } , 'diskFileToBeRemoved1.txt' , 'memFsFileToBeRemoved1.txt' ) ;
169+ await control . removeFiles ( { oldVersion : '0.1.0' , removedInVersion : '1.0.0' } , 'diskFileToBeRemoved2.txt' ) ;
170+ } ) ;
171+ } ) ;
172+
173+ it ( 'should remove files' , ( ) => {
174+ result . assertNoFile ( 'diskFileToBeRemoved1.txt' ) ;
175+ result . assertNoFile ( 'diskFileToBeRemoved2.txt' ) ;
176+ result . assertNoFile ( 'memFsFileToBeRemoved1.txt' ) ;
177+ } ) ;
178+ it ( 'should not remove files' , ( ) => {
179+ result . assertFile ( 'diskFileNotToBeRemoved1.txt' ) ;
180+ } ) ;
181+ it ( 'should match state snapshot' , async ( ) => {
182+ expect ( result . getStateSnapshot ( ) ) . toMatchInlineSnapshot ( `
183+ {
184+ ".yo-rc.json": {
185+ "stateCleared": "modified",
186+ },
187+ "diskFileNotToBeRemoved1.txt": {
188+ "stateCleared": "modified",
189+ },
190+ "diskFileToBeRemoved1.txt": {
191+ "state": "deleted",
192+ "stateCleared": "modified",
193+ },
194+ "diskFileToBeRemoved2.txt": {
195+ "state": "deleted",
196+ "stateCleared": "modified",
197+ },
198+ "memFsFileToBeRemoved1.txt": {
199+ "state": "deleted",
200+ },
201+ }
202+ ` ) ;
203+ } ) ;
204+ } ) ;
205+ } ) ;
92206} ) ;
0 commit comments