-
-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathpass-through.service.spec.ts
More file actions
229 lines (192 loc) · 8.08 KB
/
pass-through.service.spec.ts
File metadata and controls
229 lines (192 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import {Test} from '@nestjs/testing';
import {PassThroughService} from './pass-through.service';
import {mocked} from 'ts-jest/utils';
import {COMMANDER_PROGRAM, LOGGER} from '../constants';
import {VersionManagerService} from './version-manager.service';
import {noop} from 'rxjs';
import {CommandMock} from '../mocks/command.mock';
import {PassthroughCommandMock} from '../mocks/passthrough-command.mock';
import {GeneratorService} from './generator.service';
jest.mock('child_process');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const childProcess = mocked(require('child_process'), true)
describe('PassThroughService', () => {
let fixture: PassThroughService;
let commandMock: CommandMock;
const log = jest.fn()
const generate = jest.fn().mockResolvedValue(true)
const getSelectedVersion = jest.fn().mockReturnValue('4.2.1');
const filePath = jest.fn().mockReturnValue(`/some/path/to/4.2.1.jar`);
beforeEach(async () => {
commandMock = new CommandMock()
const moduleRef = await Test.createTestingModule({
providers: [
PassThroughService,
{provide: VersionManagerService, useValue: {filePath, getSelectedVersion}},
{provide: GeneratorService, useValue: {generate, enabled: true}},
{provide: COMMANDER_PROGRAM, useValue: commandMock},
{provide: LOGGER, useValue: {log}},
],
}).compile();
fixture = moduleRef.get(PassThroughService);
});
describe('API', () => {
describe('init', () => {
describe('the help command failed', () => {
let error: Error
beforeEach(async () => {
childProcess.exec.mockImplementation((cmd: string, cb) => cb(true, undefined, 'Some error'))
try {
await fixture.init()
} catch (e) {
error = e;
}
})
it('throw the error', () => {
expect(error.message).toEqual('Some error')
})
it('adds no commands', () => {
expect(commandMock.action).toBeCalledTimes(0)
expect(commandMock.command).toBeCalledTimes(0)
expect(commandMock.description).toBeCalledTimes(0)
})
});
describe('the help command works', () => {
const helpText = [
'usage: openapi-generator-cli <command> [<args>]',
'',
'The most commonly used openapi-generator-cli commands are:',
' author Utilities for authoring generators or customizing templates.',
' config-help Config help for chosen lang',
' generate Generate code with the specified generator.',
' help Display help information about openapi-generator',
' list Lists the available generators',
' meta MetaGenerator. Generator for creating a new template set and configuration for Codegen. The output will be based on the language you specify, and includes default templates to include.',
' validate Validate specification',
' version Show version information used in tooling',
'',
`See 'openapi-generator-cli help <command>' for more information on a specific`,
'command.'
].join('\n')
const completionText = [
' list',
' generate',
' meta',
' help',
' config-help',
' validate',
' version',
' completion',
' batch',
' --version',
' --help',
].join('\n')
beforeEach(async () => {
childProcess.exec.mockImplementation((cmd: string, cb) => {
if(cmd.endsWith('"/some/path/to/4.2.1.jar" help')) {
cb(undefined, helpText)
}
if(cmd.endsWith('"/some/path/to/4.2.1.jar" completion')) {
cb(undefined, completionText)
}
})
await fixture.init()
})
it('adds 19 commands', () => {
expect(commandMock.action).toBeCalledTimes(10)
expect(commandMock.command).toBeCalledTimes(10)
expect(commandMock.description).toBeCalledTimes(10)
})
describe.each([
['author', 'Utilities for authoring generators or customizing templates.'],
['config-help', 'Config help for chosen lang'],
['generate', 'Generate code with the specified generator.'],
['help', 'Display help information about openapi-generator'],
['list', 'Lists the available generators'],
['meta', 'MetaGenerator. Generator for creating a new template set and configuration for Codegen. The output will be based on the language you specify, and includes default templates to include.'],
['validate', 'Validate specification'],
['version', 'Show version information used in tooling'],
['batch', ''],
['completion', ''],
])('%s', (cmd, desc) => {
let cmdMock: PassthroughCommandMock;
beforeEach(() => {
cmdMock = new PassthroughCommandMock(cmd, ['foo', 'baz']);
const on = jest.fn();
childProcess.spawn.mockReset().mockReturnValue({on})
})
it('adds the correct description', () => {
expect(commandMock.commands[cmd].description).toEqual(desc)
})
it('allows unknown options', () => {
expect(commandMock.commands[cmd].allowUnknownOption).toBeTruthy()
})
it('can delegate with JAVA_OPTS', () => {
process.env['JAVA_OPTS'] = 'java-opt-1=1'
commandMock.commands[cmd].action(cmdMock)
expect(childProcess.spawn).toHaveBeenNthCalledWith(
1,
'java java-opt-1=1 -jar "/some/path/to/4.2.1.jar"',
[cmd, ...cmdMock.args],
{
stdio: 'inherit',
shell: true
}
)
})
it('can delegate without JAVA_OPTS', () => {
delete process.env['JAVA_OPTS']
commandMock.commands[cmd].action(cmdMock)
expect(childProcess.spawn).toHaveBeenNthCalledWith(
1,
'java -jar "/some/path/to/4.2.1.jar"',
[cmd, ...cmdMock.args],
{
stdio: 'inherit',
shell: true
}
)
})
it('can delegate with custom jar', () => {
delete process.env['JAVA_OPTS'];
const args = [...cmdMock.args];
cmdMock.args.push('--custom-generator=../some/custom.jar');
commandMock.commands[cmd].action(cmdMock)
const cpDelimiter = process.platform === "win32" ? ';' : ':';
expect(childProcess.spawn).toHaveBeenNthCalledWith(
1,
`java -cp "${['/some/path/to/4.2.1.jar', '../some/custom.jar'].join(cpDelimiter)}" org.openapitools.codegen.OpenAPIGenerator`,
[cmd, ...args],
{
stdio: 'inherit',
shell: true
}
)
})
if (cmd === 'help') {
it('prints the help info and does not delegate, if args length = 0', () => {
childProcess.spawn.mockReset()
cmdMock.args = []
const logSpy = jest.spyOn(console, 'log').mockImplementation(noop)
commandMock.commands[cmd].action(cmdMock)
expect(childProcess.spawn).toBeCalledTimes(0)
expect(commandMock.helpInformation).toBeCalledTimes(1)
expect(logSpy).toHaveBeenCalledTimes(2);
expect(logSpy).toHaveBeenNthCalledWith(1, 'some help text');
expect(logSpy).toHaveBeenNthCalledWith(2, 'has custom generator');
})
}
if (cmd === 'generate') {
it('generates by using the generator config', () => {
childProcess.spawn.mockReset()
cmdMock.args = []
commandMock.commands[cmd].action(cmdMock)
expect(childProcess.spawn).toBeCalledTimes(0)
expect(generate).toBeCalledTimes(1);
})
}
})
})
})
})
})