Skip to content

Commit 6459c0f

Browse files
NEW: @W-18173799@: Update config command to not quit if engine valida… (#1773)
1 parent c1f36b8 commit 6459c0f

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

src/lib/models/ConfigModel.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,13 @@ abstract class YamlFormatter {
193193

194194
private toYamlEngineOverridesForEngine(engineName: string): string {
195195
const engineConfigDescriptor: ConfigDescription = this.codeAnalyzer.getEngineConfigDescription(engineName);
196-
const userEngineConfig: EngineConfig = this.codeAnalyzer.getEngineConfig(engineName);
196+
197+
// We get the normalized (validated) engine config if it is available. But if an engine failed validation then
198+
// getEngineConfig will just error... so instead we fall back to the raw config that the user may have supplied
199+
// to keep things untouched. This is ok because we are already throwing an error log event displaying the issue.
200+
// So now users can still see create a config file and make changes as needed.
201+
const userEngineConfig: EngineConfig = this.codeAnalyzer.getEngineNames().includes(engineName) ?
202+
this.codeAnalyzer.getEngineConfig(engineName) : this.codeAnalyzer.getConfig().getEngineOverridesFor(engineName);
197203

198204
let yamlCode: string = '\n' +
199205
this.toYamlSectionHeadingComment(engineConfigDescriptor.overview) + '\n' +

test/lib/actions/ConfigAction.test.ts

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {ConfigActionSummaryViewer} from '../../../src/lib/viewers/ActionSummaryV
1313

1414
import {SpyConfigWriter} from '../../stubs/SpyConfigWriter';
1515
import {SpyConfigViewer} from '../../stubs/SpyConfigViewer';
16-
import {DisplayEventType, SpyDisplay} from '../../stubs/SpyDisplay';
16+
import {DisplayEvent, DisplayEventType, SpyDisplay} from '../../stubs/SpyDisplay';
17+
import { LogEventDisplayer } from '../../../src/lib/listeners/LogEventListener';
1718

1819
const PATH_TO_FIXTURES = path.join(__dirname, '..', '..', 'fixtures');
1920

@@ -32,7 +33,7 @@ describe('ConfigAction tests', () => {
3233
beforeEach(() => {
3334
spyDisplay = new SpyDisplay();
3435
dependencies = {
35-
logEventListeners: [],
36+
logEventListeners: [new LogEventDisplayer(spyDisplay)],
3637
progressEventListeners: [],
3738
viewer: new ConfigStyledYamlViewer(spyDisplay),
3839
configFactory: new StubCodeAnalyzerConfigFactory(),
@@ -158,7 +159,7 @@ describe('ConfigAction tests', () => {
158159
stubConfigFactory = new AlternativeStubCodeAnalyzerConfigFactory();
159160
spyDisplay = new SpyDisplay();
160161
dependencies = {
161-
logEventListeners: [],
162+
logEventListeners: [new LogEventDisplayer(spyDisplay)],
162163
progressEventListeners: [],
163164
viewer: new ConfigStyledYamlViewer(spyDisplay),
164165
configFactory: stubConfigFactory,
@@ -332,25 +333,44 @@ describe('ConfigAction tests', () => {
332333
expect(output).toContain('disable_engine: true # Modified from: false');
333334
});
334335

335-
it('Edge Case: When plugin throws error when attempting to create engine config and engine is enabled, then error', async () => {
336+
it('Edge Case: When plugin throws error when attempting to create engine config but engine is disabled, then do not error', async () => {
336337
dependencies.pluginsFactory = new FactoryForThrowingEnginePlugin();
337-
dependencies.configFactory = new StubCodeAnalyzerConfigFactory();
338+
dependencies.configFactory = new StubCodeAnalyzerConfigFactory(CodeAnalyzerConfig.fromObject({
339+
engines: {
340+
uncreatableEngine: {
341+
disable_engine: true,
342+
someField: 'some non default value'
343+
}
344+
}
345+
}));
338346

339-
await expect(runActionAndGetDisplayedConfig(dependencies, ['NoRuleHasThisTag'])).rejects.toThrowError();
347+
const output: string = await runActionAndGetDisplayedConfig(dependencies, ['NoRuleHasThisTag']);
348+
349+
expect(spyDisplay.getDisplayEvents().filter(e => e.type == DisplayEventType.ERROR)).toHaveLength(0);
350+
351+
expect(output).toContain('disable_engine: true # Modified from: false');
352+
expect(output).toContain('someField: some non default value # Modified from: "someDefault"');
340353
});
341354

342-
it('Edge Case: When plugin thrown error when attempting to create engine config but engine is disabled, then do not error', async () => {
355+
it('Edge Case: When plugin throws error when attempting to create engine config, but engine is enabled, then issue error log but continue with whatever is in the users config for that engine', async () => {
343356
dependencies.pluginsFactory = new FactoryForThrowingEnginePlugin();
344357
dependencies.configFactory = new StubCodeAnalyzerConfigFactory(CodeAnalyzerConfig.fromObject({
345358
engines: {
346359
uncreatableEngine: {
347-
disable_engine: true
360+
disable_engine: false,
361+
someField: 'some non default value'
348362
}
349363
}
350364
}));
351365

352-
const output: string = await runActionAndGetDisplayedConfig(dependencies, ['NoRuleHasThisTag']);
353-
expect(output).toContain('disable_engine: true # Modified from: false');
366+
const output: string = await runActionAndGetDisplayedConfig(dependencies, ['uncreatableEngine']);
367+
368+
const errorEvents: DisplayEvent[] = spyDisplay.getDisplayEvents().filter(e => e.type == DisplayEventType.ERROR);
369+
expect(errorEvents).toHaveLength(1);
370+
expect(errorEvents[0].data).toContain('Error thrown by createEngineConfig');
371+
372+
expect(output).toContain('disable_engine: false');
373+
expect(output).toContain('someField: some non default value # Modified from: "someDefault"');
354374
});
355375

356376
it.each([
@@ -515,8 +535,13 @@ describe('ConfigAction tests', () => {
515535

516536
// ==== OUTPUT PROCESSING ====
517537
const displayEvents = spyDisplay.getDisplayEvents();
518-
expect(displayEvents[4].type).toEqual(DisplayEventType.LOG);
519-
return ansis.strip(displayEvents[4].data);
538+
if (displayEvents[4].type === DisplayEventType.LOG) {
539+
return ansis.strip(displayEvents[4].data);
540+
} else if (displayEvents[5].type === DisplayEventType.LOG) {
541+
return ansis.strip(displayEvents[5].data);
542+
} else {
543+
return 'Could Not Get Specific Output';
544+
}
520545
}
521546
});
522547

@@ -806,6 +831,19 @@ class ThrowingEnginePlugin extends EngineApi.EnginePluginV1 {
806831
return ['uncreatableEngine'];
807832
}
808833

834+
describeEngineConfig(): EngineApi.ConfigDescription {
835+
return {
836+
overview: 'Some Overview',
837+
fieldDescriptions: {
838+
someField: {
839+
descriptionText: 'some description',
840+
valueType: 'string',
841+
defaultValue: 'someDefault'
842+
}
843+
}
844+
}
845+
}
846+
809847
createEngineConfig(_engineName: string, _configValueExtractor: EngineApi.ConfigValueExtractor): Promise<EngineApi.ConfigObject> {
810848
throw new Error('Error thrown by createEngineConfig');
811849
}

0 commit comments

Comments
 (0)