-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
98 lines (91 loc) · 3.69 KB
/
index.ts
File metadata and controls
98 lines (91 loc) · 3.69 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
import * as chalk from 'chalk';
import * as ora from 'ora';
import { defaultOptions } from './src/constants/default-options';
import { getFilesByPattern } from './src/get-files-by-pattern';
import { NoSkippedTestsAnalyzerError } from './src/interfaces/no-skipped-tests-analyzer-error.interface';
import { NoSkippedTestsAnalyzerResult } from './src/interfaces/no-skipped-tests-analyzer-result.interface';
import { NoSkippedTestsOptions } from './src/interfaces/options.interface';
import { NoSkippedTestsAnalyzer } from './src/no-skipped-tests-analyzer';
/**
* Analyze files for skipped tests
*
* @param [customOptions={}] - Options, default options as fallback
* @returns - Promise, resolves when done with the analysis results for all files matching the given pattern
*/
export async function analyzeFilesForSkippedTests(customOptions: NoSkippedTestsOptions = {}): Promise<Array<NoSkippedTestsAnalyzerResult>> {
// Get options
const options: NoSkippedTestsOptions = { ...defaultOptions, ...customOptions };
// Get list of files (relative file paths, to be specific) matching the given pattern, but exit early of no files are found
let logger: any;
if (options.log) {
logger = ora('Searching files').start();
}
let filePaths: Array<string>;
try {
filePaths = await getFilesByPattern(options.pattern);
} catch (error) {
if (options.log) {
logger.stop();
console.error(chalk.red('AN UNEXPECTED ERROR OCCURED:'), chalk.white(error.toString()));
}
throw new Error(error.message);
}
const numberOfFiles: number = filePaths.length;
if (numberOfFiles === 0) {
if (options.log) {
logger.stop();
console.warn(chalk.white.bgYellow(' WARNING '), `No files found using the given pattern ("${options.pattern}").`);
}
return [];
}
// Analyze all files (asynchronously, #perfmatters)
if (options.log) {
logger.text = 'Analyzing files for skipped tests [0%]';
}
let numberOfAnalyzedFiles = 0;
let results: Array<NoSkippedTestsAnalyzerResult>;
try {
results = await Promise.all(
filePaths.map(
async (filePath: string): Promise<NoSkippedTestsAnalyzerResult> => {
if (options.log) {
numberOfAnalyzedFiles++;
logger.text = `Analyzing files for skipped tests [${Math.round((numberOfAnalyzedFiles / numberOfFiles) * 100)}%]`;
}
return new NoSkippedTestsAnalyzer(filePath).analyze();
},
),
);
} catch (error) {
if (options.log) {
logger.stop();
console.error(chalk.red('AN UNEXPECTED ERROR OCCURED:'), chalk.white(error.toString()));
}
throw new Error(error.message);
}
// Log results (success or error)
if (options.log) {
logger.stop();
const numberOfErrors: number = results.reduce((accumulatedValue: number, currentValue: NoSkippedTestsAnalyzerResult): number => {
return accumulatedValue + currentValue.errors.length;
}, 0);
if (numberOfErrors === 0) {
console.log(chalk.white.bgGreen(' OK '), 'Everything is fine, all tests are active.');
} else {
console.log(chalk.white.bgRed(' ERROR '), `Seems that not all tests are active (${numberOfErrors} issues found).`);
console.log('');
// Show errors for the same file beneath each other
results.forEach((fileResult: NoSkippedTestsAnalyzerResult): void => {
if (fileResult.errors.length !== 0) {
fileResult.errors.forEach((error: NoSkippedTestsAnalyzerError): void => {
console.log(
chalk.red(` ${fileResult.filePath} (${error.line}:${error.char}):`),
chalk.white(`Found "${error.identifier}"`),
);
});
}
});
}
}
return results;
}