|
1 | 1 | import { Test } from '@nestjs/testing';
|
| 2 | +import { Command, createCommand } from 'commander'; |
2 | 3 | import { ConfigService } from './config.service';
|
3 |
| -import { LOGGER } from '../constants'; |
| 4 | +import { LOGGER, COMMANDER_PROGRAM } from '../constants'; |
4 | 5 |
|
5 | 6 | jest.mock('fs-extra');
|
6 | 7 | // eslint-disable-next-line @typescript-eslint/no-var-requires
|
7 | 8 | const fs = jest.mocked(require('fs-extra'));
|
8 | 9 |
|
9 | 10 | describe('ConfigService', () => {
|
10 | 11 | let fixture: ConfigService;
|
| 12 | + let program: Command; |
11 | 13 |
|
12 | 14 | const log = jest.fn();
|
13 | 15 |
|
14 | 16 | beforeEach(async () => {
|
| 17 | + program = createCommand(); |
| 18 | + jest.spyOn(program, 'helpInformation'); |
| 19 | + |
15 | 20 | const moduleRef = await Test.createTestingModule({
|
16 |
| - providers: [ConfigService, { provide: LOGGER, useValue: { log } }], |
| 21 | + providers: [ |
| 22 | + ConfigService, |
| 23 | + { provide: LOGGER, useValue: { log } }, |
| 24 | + { provide: COMMANDER_PROGRAM, useValue: program }, |
| 25 | + ], |
17 | 26 | }).compile();
|
18 | 27 |
|
19 | 28 | fixture = moduleRef.get(ConfigService);
|
@@ -141,5 +150,39 @@ describe('ConfigService', () => {
|
141 | 150 | );
|
142 | 151 | });
|
143 | 152 | });
|
| 153 | + |
| 154 | + describe('configFileOrDefault()', () => { |
| 155 | + describe('--openapitools set', () => { |
| 156 | + beforeEach(async () => { |
| 157 | + program = createCommand(); |
| 158 | + program.opts().openapitools = '/tmp/myopenapitools.json'; |
| 159 | + |
| 160 | + const moduleRef = await Test.createTestingModule({ |
| 161 | + providers: [ |
| 162 | + ConfigService, |
| 163 | + { provide: LOGGER, useValue: { log } }, |
| 164 | + { provide: COMMANDER_PROGRAM, useValue: program }, |
| 165 | + ], |
| 166 | + }).compile(); |
| 167 | + |
| 168 | + fixture = moduleRef.get(ConfigService); |
| 169 | + fs.writeJSONSync.mockReset(); |
| 170 | + fs.readJSONSync.mockReset(); |
| 171 | + fs.ensureFileSync.mockReset(); |
| 172 | + }); |
| 173 | + it('returns path set at cli, if openapitools argument provided', () => { |
| 174 | + expect(fixture.configFile).toEqual('/tmp/myopenapitools.json'); |
| 175 | + }); |
| 176 | + }); |
| 177 | + describe('--openapitools not set', () => { |
| 178 | + it('returns default path, if openapitools argument not provided', () => { |
| 179 | + expect( |
| 180 | + fixture.configFile.endsWith( |
| 181 | + 'openapi-generator-cli/openapitools.json' |
| 182 | + ) |
| 183 | + ).toBeTruthy(); |
| 184 | + }); |
| 185 | + }); |
| 186 | + }); |
144 | 187 | });
|
145 | 188 | });
|
0 commit comments