Skip to content

Commit

Permalink
feat(StrykerInitializer): Add the option to select mutators and trans…
Browse files Browse the repository at this point in the history
…pilers (#403)
  • Loading branch information
simondel authored Oct 3, 2017
1 parent ff6962a commit c61786f
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 48 deletions.
10 changes: 10 additions & 0 deletions packages/stryker/src/initializer/NpmClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ export default class NpmClient {
.then(mapSearchResultToPromptOption);
}

getMutatorOptions(): Promise<PromptOption[]> {
return this.search('/v2/search?q=keywords:stryker-mutator')
.then(mapSearchResultToPromptOption);
}

getTranspilerOptions(): Promise<PromptOption[]> {
return this.search('/v2/search?q=keywords:stryker-transpiler')
.then(mapSearchResultToPromptOption);
}

getTestReporterOptions(): Promise<PromptOption[]> {
return this.search(`/v2/search?q=keywords:stryker-reporter`)
.then(mapSearchResultToPromptOption);
Expand Down
4 changes: 4 additions & 0 deletions packages/stryker/src/initializer/StrykerConfigWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export default class StrykerConfigWriter {
private out: (output: string) => void,
selectedTestRunner: null | PromptOption,
private selectedTestFramework: null | PromptOption,
selectedMutator: null | PromptOption,
selectedTranspilers: null | PromptOption[],
selectedReporters: PromptOption[],
private additionalPiecesOfConfig: Partial<StrykerOptions>[]
) {
Expand All @@ -25,6 +27,8 @@ export default class StrykerConfigWriter {
'test/**/*.js'
],
testRunner: selectedTestRunner ? selectedTestRunner.name : '',
mutator: selectedMutator ? selectedMutator.name : '',
transpilers: selectedTranspilers ? selectedTranspilers.map(t => t.name) : [],
reporter: selectedReporters.map(rep => rep.name)
};
}
Expand Down
37 changes: 36 additions & 1 deletion packages/stryker/src/initializer/StrykerInitializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ export default class StrykerInitializer {
this.patchProxies();
const selectedTestRunner = await this.selectTestRunner();
const selectedTestFramework = selectedTestRunner ? await this.selectTestFramework(selectedTestRunner) : null;
const selectedMutator = await this.selectMutator();
const selectedTranspilers = await this.selectTranspilers();
const selectedReporters = await this.selectReporters();
const npmDependencies = this.getSelectedNpmDependencies([selectedTestRunner, selectedTestFramework].concat(selectedReporters));
const npmDependencies = this.getSelectedNpmDependencies(
[selectedTestRunner, selectedTestFramework, selectedMutator]
.concat(selectedTranspilers)
.concat(selectedReporters)
);
this.installNpmDependencies(npmDependencies);
await new StrykerConfigWriter(this.out,
selectedTestRunner,
selectedTestFramework,
selectedMutator,
selectedTranspilers,
selectedReporters,
await this.fetchAdditionalConfig(npmDependencies)).write();
this.out('Done configuring stryker. Please review `stryker.conf.js`, you might need to configure your files and test runner correctly.');
Expand Down Expand Up @@ -94,6 +102,33 @@ export default class StrykerInitializer {
return selectedTestFramework;
}

private async selectMutator(): Promise<PromptOption | null> {
const mutatorOptions = await this.client.getMutatorOptions();
if (mutatorOptions.length) {
log.debug(`Found mutators: ${JSON.stringify(mutatorOptions)}`);
const es5 = {
name: 'es5',
npm: null
};
mutatorOptions.push(es5);
return await this.inquirer.promptMutator(mutatorOptions);
} else {
this.out('Unable to select a mutator. You will need to configure it manually.');
return null;
}
}

private async selectTranspilers(): Promise<PromptOption[] | null> {
const options = await this.client.getTranspilerOptions();
if (options.length) {
log.debug(`Found transpilers: ${JSON.stringify(options)}`);
return await this.inquirer.promptTranspilers(options);
} else {
this.out('Unable to select transpilers. You will need to configure it manually, if you want to use any.');
return null;
}
}

private getSelectedNpmDependencies(selectedOptions: (PromptOption | null)[]) {
return filterEmpty(filterEmpty(selectedOptions)
.map(option => option.npm));
Expand Down
20 changes: 20 additions & 0 deletions packages/stryker/src/initializer/StrykerInquirer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ export class StrykerInquirer {
return options.filter(_ => _.name === answers['testFramework'])[0];
}

public async promptMutator(options: PromptOption[]): Promise<PromptOption> {
const answers = await inquirer.prompt({
type: 'list',
name: 'mutator',
message: 'What kind of code do you want to mutate?',
choices: options.map(_ => _.name)
});
return options.filter(_ => _.name === answers['mutator'])[0];
}

public async promptTranspilers(options: PromptOption[]): Promise<PromptOption[]> {
const answers = await inquirer.prompt({
type: 'checkbox',
name: 'transpilers',
message: '[optional] What kind transformations should be applied to your code?',
choices: options.map(_ => _.name)
});
return options.filter(option => (answers['transpilers'] as string[]).some(transpilerName => option.name === transpilerName));
}

public async promptReporters(options: PromptOption[]): Promise<PromptOption[]> {
const answers = await inquirer.prompt({
type: 'checkbox',
Expand Down
Loading

0 comments on commit c61786f

Please sign in to comment.