1
- import { Test } from '@nestjs/testing'
1
+ import { Test } from '@nestjs/testing'
2
2
import * as chalk from 'chalk'
3
- import { Command , createCommand } from 'commander'
4
- import { COMMANDER_PROGRAM , LOGGER } from '../constants'
5
- import { GeneratorService } from './generator.service'
6
- import { PassThroughService } from './pass-through.service'
7
- import { VersionManagerService } from './version-manager.service'
3
+ import { Command , createCommand } from 'commander'
4
+ import { COMMANDER_PROGRAM , LOGGER } from '../constants'
5
+ import { GeneratorService } from './generator.service'
6
+ import { PassThroughService } from './pass-through.service'
7
+ import { VersionManagerService } from './version-manager.service'
8
+ import { ConfigService } from "./config.service" ;
8
9
9
10
jest . mock ( 'child_process' )
10
11
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -19,6 +20,7 @@ describe('PassThroughService', () => {
19
20
const generate = jest . fn ( ) . mockResolvedValue ( true )
20
21
const getSelectedVersion = jest . fn ( ) . mockReturnValue ( '4.2.1' )
21
22
const filePath = jest . fn ( ) . mockReturnValue ( `/some/path/to/4.2.1.jar` )
23
+ const configServiceMock = { useDocker : false , get : jest . fn ( ) , cwd : '/foo/bar' } ;
22
24
23
25
const getCommand = ( name : string ) => program . commands . find ( c => c . name ( ) === name ) ;
24
26
@@ -29,17 +31,20 @@ describe('PassThroughService', () => {
29
31
const moduleRef = await Test . createTestingModule ( {
30
32
providers : [
31
33
PassThroughService ,
32
- { provide : VersionManagerService , useValue : { filePath, getSelectedVersion } } ,
33
- { provide : GeneratorService , useValue : { generate, enabled : true } } ,
34
- { provide : COMMANDER_PROGRAM , useValue : program } ,
35
- { provide : LOGGER , useValue : { log } } ,
34
+ { provide : VersionManagerService , useValue : { filePath, getSelectedVersion, getDockerImageName : ( v ) => `openapitools/openapi-generator-cli:v${ v || getSelectedVersion ( ) } ` } } ,
35
+ { provide : GeneratorService , useValue : { generate, enabled : true } } ,
36
+ { provide : ConfigService , useValue : configServiceMock } ,
37
+ { provide : COMMANDER_PROGRAM , useValue : program } ,
38
+ { provide : LOGGER , useValue : { log} } ,
36
39
] ,
37
40
} ) . compile ( )
38
41
39
42
fixture = moduleRef . get ( PassThroughService )
40
43
41
- childProcess . spawn . mockReset ( ) . mockReturnValue ( { on : jest . fn ( ) } )
42
-
44
+ childProcess . spawn . mockReset ( ) . mockReturnValue ( { on : jest . fn ( ) } )
45
+ configServiceMock . get . mockClear ( )
46
+ configServiceMock . get . mockReset ( )
47
+ configServiceMock . useDocker = false ;
43
48
} )
44
49
45
50
describe ( 'API' , ( ) => {
@@ -147,8 +152,28 @@ describe('PassThroughService', () => {
147
152
expect ( cmd [ '_allowUnknownOption' ] ) . toBeTruthy ( )
148
153
} )
149
154
155
+ describe ( 'useDocker is true' , ( ) => {
156
+
157
+ beforeEach ( ( ) => {
158
+ configServiceMock . useDocker = true ;
159
+ } ) ;
160
+
161
+ it ( 'delegates to docker' , async ( ) => {
162
+ await program . parseAsync ( [ name , ...argv ] , { from : 'user' } )
163
+ expect ( childProcess . spawn ) . toHaveBeenNthCalledWith (
164
+ 1 ,
165
+ 'docker run --rm -v "/foo/bar:/local" openapitools/openapi-generator-cli:v4.2.1' ,
166
+ [ name , ...argv ] ,
167
+ {
168
+ stdio : 'inherit' ,
169
+ shell : true
170
+ }
171
+ )
172
+ } )
173
+ } )
174
+
150
175
it ( 'can delegate' , async ( ) => {
151
- await program . parseAsync ( [ name , ...argv ] , { from : 'user' } )
176
+ await program . parseAsync ( [ name , ...argv ] , { from : 'user' } )
152
177
expect ( childProcess . spawn ) . toHaveBeenNthCalledWith (
153
178
1 ,
154
179
'java -jar "/some/path/to/4.2.1.jar"' ,
@@ -162,7 +187,7 @@ describe('PassThroughService', () => {
162
187
163
188
it ( 'can delegate with JAVA_OPTS' , async ( ) => {
164
189
process . env [ 'JAVA_OPTS' ] = 'java-opt-1=1'
165
- await program . parseAsync ( [ name , ...argv ] , { from : 'user' } )
190
+ await program . parseAsync ( [ name , ...argv ] , { from : 'user' } )
166
191
expect ( childProcess . spawn ) . toHaveBeenNthCalledWith (
167
192
1 ,
168
193
'java java-opt-1=1 -jar "/some/path/to/4.2.1.jar"' ,
@@ -175,7 +200,7 @@ describe('PassThroughService', () => {
175
200
} )
176
201
177
202
it ( 'can delegate with custom jar' , async ( ) => {
178
- await program . parseAsync ( [ name , ...argv , '--custom-generator=../some/custom.jar' ] , { from : 'user' } )
203
+ await program . parseAsync ( [ name , ...argv , '--custom-generator=../some/custom.jar' ] , { from : 'user' } )
179
204
const cpDelimiter = process . platform === 'win32' ? ';' : ':'
180
205
181
206
expect ( childProcess . spawn ) . toHaveBeenNthCalledWith (
@@ -191,8 +216,8 @@ describe('PassThroughService', () => {
191
216
192
217
if ( name === 'generate' ) {
193
218
it ( 'can delegate with custom jar to generate command' , async ( ) => {
194
- await program . parseAsync ( [ name , ...argv , '--generator-key=genKey' , '--custom-generator=../some/custom.jar' ] , { from : 'user' } )
195
-
219
+ await program . parseAsync ( [ name , ...argv , '--generator-key=genKey' , '--custom-generator=../some/custom.jar' ] , { from : 'user' } )
220
+
196
221
expect ( generate ) . toHaveBeenNthCalledWith (
197
222
1 ,
198
223
'../some/custom.jar' ,
@@ -201,29 +226,6 @@ describe('PassThroughService', () => {
201
226
} )
202
227
}
203
228
204
- // if (name === 'help') {
205
- // it('prints the help info and does not delegate, if args length = 0', async () => {
206
- // childProcess.spawn.mockReset()
207
- // cmd.args = []
208
- // const logSpy = jest.spyOn(console, 'log').mockImplementation(noop)
209
- // await program.parseAsync([name], { from: 'user' })
210
- // expect(childProcess.spawn).toBeCalledTimes(0)
211
- // expect(program.helpInformation).toBeCalledTimes(1)
212
- // // expect(logSpy).toHaveBeenCalledTimes(2)
213
- // expect(logSpy).toHaveBeenNthCalledWith(1, 'some help text')
214
- // expect(logSpy).toHaveBeenNthCalledWith(2, 'has custom generator')
215
- // })
216
- // }
217
- //
218
- // if (name === 'generate') {
219
- // it('generates by using the generator config', async () => {
220
- // childProcess.spawn.mockReset()
221
- // await program.parseAsync([name], { from: 'user' })
222
- // expect(childProcess.spawn).toBeCalledTimes(0)
223
- // expect(generate).toHaveBeenNthCalledWith(1)
224
- // })
225
- // }
226
-
227
229
} )
228
230
229
231
describe ( 'command behavior' , ( ) => {
@@ -239,13 +241,13 @@ describe('PassThroughService', () => {
239
241
${ 'help generate' } | ${ commandHelp ( 'generate' ) } | ${ 'a' }
240
242
${ 'help author' } | ${ commandHelp ( 'author' ) } | ${ 'b' }
241
243
${ 'help hidden' } | ${ undefined } | ${ 'c' }
242
- ` ( '$cmd' , ( { cmd, helpText, spawn } ) => {
244
+ ` ( '$cmd' , ( { cmd, helpText, spawn} ) => {
243
245
244
246
let spy : jest . SpyInstance ;
245
247
246
248
beforeEach ( async ( ) => {
247
249
spy = jest . spyOn ( console , 'log' ) . mockClear ( ) . mockImplementation ( ) ;
248
- await program . parseAsync ( cmd . split ( ' ' ) , { from : 'user' } )
250
+ await program . parseAsync ( cmd . split ( ' ' ) , { from : 'user' } )
249
251
} )
250
252
251
253
describe ( 'help text' , ( ) => {
0 commit comments