-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathconfig.service.spec.ts
More file actions
223 lines (195 loc) · 6.5 KB
/
config.service.spec.ts
File metadata and controls
223 lines (195 loc) · 6.5 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
import { Test } from '@nestjs/testing';
import { Command, createCommand } from 'commander';
import { ConfigService } from './config.service';
import { LOGGER, COMMANDER_PROGRAM } from '../constants';
jest.mock('fs-extra');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = jest.mocked(require('fs-extra'));
describe('ConfigService', () => {
let fixture: ConfigService;
let program: Command;
const log = jest.fn();
beforeEach(async () => {
program = createCommand();
jest.spyOn(program, 'helpInformation');
const moduleRef = await Test.createTestingModule({
providers: [
ConfigService,
{ provide: LOGGER, useValue: { log } },
{ provide: COMMANDER_PROGRAM, useValue: program },
],
}).compile();
fixture = moduleRef.get(ConfigService);
fs.writeJSONSync.mockReset();
fs.readJSONSync.mockReset();
fs.ensureFileSync.mockReset();
});
describe('API', () => {
describe('get()', () => {
describe.each([
['the config is undefined', undefined],
['the config empty', {}],
])('%s', (_, config) => {
beforeEach(() => {
fs.readJSONSync.mockReturnValue(config);
});
it.each([
[
'$schema',
'./node_modules/@openapitools/openapi-generator-cli/config.schema.json',
],
['spaces', 2],
['generator-cli', { version: undefined }],
])('the key "%s" returns %s as default', (key, expectedValue) => {
expect(fixture.get(key)).toEqual(expectedValue);
});
it('can return a default value', () => {
expect(fixture.get('unknown', 'foo')).toEqual('foo');
});
});
describe('the config has values', () => {
beforeEach(() => {
fs.readJSONSync.mockReturnValue({
$schema: 'foo.json',
spaces: 4,
'generator-cli': {
version: '1.2.3',
},
});
});
it('ensures the config file', () => {
fixture.get('some-path');
expect(fs.ensureFileSync).toHaveBeenNthCalledWith(
1,
fixture.configFile
);
});
it.each([
['$schema', 'foo.json'],
['spaces', 4],
['generator-cli', { version: '1.2.3' }],
['generator-cli.version', '1.2.3'],
])('"%s" returns %s as default', (key, expectedValue) => {
expect(fixture.get(key)).toEqual(expectedValue);
expect(fs.readJSONSync).toHaveBeenNthCalledWith(
1,
fixture.configFile,
{ throws: false, encoding: 'utf8' }
);
});
});
describe('the config has values having placeholders', () => {
beforeEach(() => {
fs.readJSONSync.mockReturnValue({
$schema: 'foo.json',
spaces: 4,
'generator-cli': {
version: '1.2.3',
repository: {
queryUrl: 'https://${env.__unit_test_username}:${env.__unit_test_password}@server/api',
downloadUrl: 'https://${env.__unit_test_non_matching}@server/api'
}
},
});
process.env['__unit_test_username'] = 'myusername';
process.env['__unit_test_password'] = 'mypassword';
});
afterEach(() => {
delete process.env['__unit_test_username'];
delete process.env['__unit_test_password'];
})
it('verify placeholder replaced with env vars', () => {
const value = fixture.get('generator-cli.repository.queryUrl');
expect(value).toEqual('https://myusername:mypassword@server/api');
});
it('verify placeholders not matching env vars are not replaced', () => {
const value = fixture.get('generator-cli.repository.downloadUrl');
expect(value).toEqual('https://${env.__unit_test_non_matching}@server/api');
});
});
});
describe('has()', () => {
beforeEach(() => {
fs.readJSONSync.mockReturnValue({
propFalsy: false,
propUndefined: undefined,
propNull: null,
});
});
it('returns true, if the prop is set', () => {
expect(fixture.has('propFalsy')).toBeTruthy();
expect(fixture.has('propUndefined')).toBeTruthy();
expect(fixture.has('propNull')).toBeTruthy();
});
it('returns false, if the value is set', () => {
expect(fixture.has('foo')).toBeFalsy();
});
});
describe('set()', () => {
beforeEach(() => {
fs.readJSONSync.mockReturnValue({
$schema: 'foo.json',
spaces: 4,
'generator-cli': {
version: '1.2.3',
},
});
fixture.set('generator-cli.version', '4.5.6');
});
it('ensures the config file', () => {
expect(fs.ensureFileSync).toHaveBeenNthCalledWith(
1,
fixture.configFile
);
});
it('saves the correct value', () => {
expect(fs.writeJSONSync).toHaveBeenNthCalledWith(
1,
fixture.configFile,
{
$schema: 'foo.json',
spaces: 4,
'generator-cli': {
version: '4.5.6',
},
},
{
encoding: 'utf8',
spaces: 4,
}
);
});
});
describe('configFileOrDefault()', () => {
describe('--openapitools set', () => {
beforeEach(async () => {
program = createCommand();
program.opts().openapitools = '/tmp/myopenapitools.json';
const moduleRef = await Test.createTestingModule({
providers: [
ConfigService,
{ provide: LOGGER, useValue: { log } },
{ provide: COMMANDER_PROGRAM, useValue: program },
],
}).compile();
fixture = moduleRef.get(ConfigService);
fs.writeJSONSync.mockReset();
fs.readJSONSync.mockReset();
fs.ensureFileSync.mockReset();
});
it('returns path set at cli, if openapitools argument provided', () => {
expect(fixture.configFile).toEqual('/tmp/myopenapitools.json');
});
});
describe('--openapitools not set', () => {
it('returns default path, if openapitools argument not provided', () => {
expect(
fixture.configFile.endsWith(
'openapi-generator-cli/openapitools.json'
)
).toBeTruthy();
});
});
});
});
});