UNPKG

2.81 kBPlain TextView Raw
1import { Logger } from '@stryker-mutator/api/logging';
2import { FileDescriptions, StrykerOptions } from '@stryker-mutator/api/core';
3import { I, normalizeWhitespaces, propertyPath } from '@stryker-mutator/util';
4import { MutationTestResult } from 'mutation-testing-report-schema';
5
6import { FileSystem } from './file-system.js';
7import { ProjectFile } from './project-file.js';
8
9/**
10 * Represents the project that is under test by Stryker users.
11 * This represents the files in the current working directory.
12 * Each file can be read into memory when needed (via `readContent`)
13 */
14export class Project {
15 public readonly files = new Map<string, ProjectFile>();
16 public readonly filesToMutate = new Map<string, ProjectFile>();
17
18 constructor(fs: I<FileSystem>, public readonly fileDescriptions: FileDescriptions, public readonly incrementalReport?: MutationTestResult) {
19 Object.entries(fileDescriptions).forEach(([name, desc]) => {
20 const file = new ProjectFile(fs, name, desc.mutate);
21 this.files.set(name, file);
22 if (desc.mutate) {
23 this.filesToMutate.set(name, file);
24 }
25 });
26 }
27
28 public get isEmpty(): boolean {
29 return this.files.size === 0;
30 }
31
32 public logFiles(log: Logger, ignoreRules: readonly string[], force: boolean): void {
33 if (this.isEmpty) {
34 log.warn(
35 normalizeWhitespaces(`No files found in directory ${process.cwd()} using ignore rules: ${JSON.stringify(ignoreRules)}.
36 Make sure you run Stryker from the root directory of your project with the correct "${propertyPath<StrykerOptions>()('ignorePatterns')}".`)
37 );
38 } else {
39 if (this.filesToMutate.size) {
40 const incrementalInfo = this.incrementalReport
41 ? ` using incremental report with ${Object.values(this.incrementalReport.files).reduce(
42 (total, { mutants }) => total + mutants.length,
43 0
44 )} mutant(s), and ${Object.values(this.incrementalReport.testFiles ?? {}).reduce((total, { tests }) => total + tests.length, 0)} test(s)${
45 force ? '. Force mode is activated, all mutants will be retested' : ''
46 }`
47 : '';
48
49 log.info(`Found ${this.filesToMutate.size} of ${this.files.size} file(s) to be mutated${incrementalInfo}.`);
50 } else {
51 log.warn(
52 normalizeWhitespaces(`No files marked to be mutated, Stryker will perform a dry-run without actually mutating anything.
53 You can configure the \`mutate\` property in your config file (or use \`--mutate\` via command line).`)
54 );
55 }
56 if (log.isDebugEnabled()) {
57 log.debug(`All input files: ${JSON.stringify([...this.files.keys()], null, 2)}`);
58 log.debug(`Files to mutate: ${JSON.stringify([...this.filesToMutate.keys()], null, 2)}`);
59 }
60 }
61 }
62}