| 1 |
import * as assert from 'assert'; |
| 2 |
import * as path from 'path'; |
| 3 |
|
| 4 |
import * as glob from 'glob'; |
| 5 |
|
| 6 |
import * as fg from '../..'; |
| 7 |
import { Options } from '../../settings'; |
| 8 |
import { Pattern } from '../../types'; |
| 9 |
|
| 10 |
import Table = require('easy-table'); // eslint-disable-line @typescript-eslint/no-require-imports |
| 11 |
|
| 12 |
export type SmokeTest = { |
| 13 |
pattern: Pattern; |
| 14 |
ignore?: Pattern; |
| 15 |
cwd?: string; |
| 16 |
globOptions?: glob.IOptions; |
| 17 |
globFilter?: (entry: string, filepath: string) => boolean; |
| 18 |
globTransform?: (entry: string) => string; |
| 19 |
fgOptions?: Options; |
| 20 |
/** |
| 21 |
* Allow to run only one test case with debug information. |
| 22 |
*/ |
| 23 |
debug?: boolean; |
| 24 |
/** |
| 25 |
* Mark test case as broken. This is requires a issue to repair. |
| 26 |
*/ |
| 27 |
broken?: boolean; |
| 28 |
issue?: number | number[]; |
| 29 |
/** |
| 30 |
* Mark test case as correct. This is requires a reason why is true. |
| 31 |
*/ |
| 32 |
correct?: boolean; |
| 33 |
reason?: string; |
| 34 |
/** |
| 35 |
* The ability to conditionally run the test. |
| 36 |
*/ |
| 37 |
condition?: () => boolean; |
| 38 |
}; |
| 39 |
|
| 40 |
type MochaDefinition = Mocha.TestFunction | Mocha.ExclusiveTestFunction; |
| 41 |
type DebugCompareTestMarker = '+' | '-'; |
| 42 |
|
| 43 |
export function suite(name: string, tests: Array<SmokeTest | SmokeTest[]>): void { |
| 44 |
const testCases = getTestCases(tests); |
| 45 |
|
| 46 |
describe(name, () => { |
| 47 |
for (const test of testCases) { |
| 48 |
const title = getTestCaseTitle(test); |
| 49 |
const definition = getTestCaseMochaDefinition(test); |
| 50 |
|
| 51 |
definition(`${title} (sync)`, () => testCaseRunner(test, getFastGlobEntriesSync)); |
| 52 |
definition(`${title} (async)`, () => testCaseRunner(test, getFastGlobEntriesAsync)); |
| 53 |
definition(`${title} (stream)`, () => testCaseRunner(test, getFastGlobEntriesStream)); |
| 54 |
} |
| 55 |
}); |
| 56 |
} |
| 57 |
|
| 58 |
function getTestCases(tests: Array<SmokeTest | SmokeTest[]>): SmokeTest[] { |
| 59 |
return ([] as SmokeTest[]).concat(...tests); |
| 60 |
} |
| 61 |
|
| 62 |
function getTestCaseTitle(test: SmokeTest): string { |
| 63 |
let title = `pattern: '${test.pattern}'`; |
| 64 |
|
| 65 |
if (test.ignore !== undefined) { |
| 66 |
title += `, ignore: '${test.ignore}'`; |
| 67 |
} |
| 68 |
|
| 69 |
if (test.broken !== undefined) { |
| 70 |
title += ` (broken - ${test.issue})`; |
| 71 |
} |
| 72 |
|
| 73 |
if (test.correct !== undefined) { |
| 74 |
title += ' (correct)'; |
| 75 |
} |
| 76 |
|
| 77 |
return title; |
| 78 |
} |
| 79 |
|
| 80 |
function getTestCaseMochaDefinition(test: SmokeTest): MochaDefinition { |
| 81 |
if (test.debug === true) { |
| 82 |
return it.only; |
| 83 |
} |
| 84 |
|
| 85 |
if (test.condition?.() === false) { |
| 86 |
return it.skip; |
| 87 |
} |
| 88 |
|
| 89 |
return it; |
| 90 |
} |
| 91 |
|
| 92 |
async function testCaseRunner(test: SmokeTest, func: typeof getFastGlobEntriesSync | typeof getFastGlobEntriesAsync): Promise<void> { |
| 93 |
const expected = getNodeGlobEntries(test); |
| 94 |
const actual = await func(test.pattern, test.ignore, test.cwd, test.fgOptions); |
| 95 |
|
| 96 |
if (test.debug === true) { |
| 97 |
const report = generateDebugReport(expected, actual); |
| 98 |
|
| 99 |
console.log(report); |
| 100 |
} |
| 101 |
|
| 102 |
if (test.broken === true && test.issue === undefined) { |
| 103 |
assert.fail("This test is marked as «broken», but it doesn't have a issue key."); |
| 104 |
} |
| 105 |
|
| 106 |
if (test.correct === true && test.reason === undefined) { |
| 107 |
assert.fail("This test is marked as «correct», but it doesn't have a reason."); |
| 108 |
} |
| 109 |
|
| 110 |
const isInvertedTest = test.broken === true || test.correct === true; |
| 111 |
const assertAction: typeof assert.deepStrictEqual = isInvertedTest ? assert.notDeepStrictEqual : assert.deepStrictEqual; |
| 112 |
|
| 113 |
assertAction(actual, expected); |
| 114 |
} |
| 115 |
|
| 116 |
function generateDebugReport(expected: string[], actual: string[]): string | null { |
| 117 |
const table = new Table(); |
| 118 |
|
| 119 |
const items = actual.length > expected.length ? actual : expected; |
| 120 |
|
| 121 |
if (items.length === 0) { |
| 122 |
return null; |
| 123 |
} |
| 124 |
|
| 125 |
for (const item of items) { |
| 126 |
table.cell('FIXTURES', item); |
| 127 |
table.cell('NODE_GLOB', getTestMarker(expected, item)); |
| 128 |
table.cell('FAST_GLOB', getTestMarker(actual, item)); |
| 129 |
table.newRow(); |
| 130 |
} |
| 131 |
|
| 132 |
return table.toString(); |
| 133 |
} |
| 134 |
|
| 135 |
function getTestMarker(items: string[], item: string): DebugCompareTestMarker { |
| 136 |
return items.includes(item) ? '+' : '-'; |
| 137 |
} |
| 138 |
|
| 139 |
function getNodeGlobEntries(options: SmokeTest): string[] { |
| 140 |
const pattern = options.pattern; |
| 141 |
const cwd = options.cwd === undefined ? process.cwd() : options.cwd; |
| 142 |
const ignore = options.ignore === undefined ? [] : [options.ignore]; |
| 143 |
const globFilter = options.globFilter; |
| 144 |
const globTransform = options.globTransform; |
| 145 |
|
| 146 |
let entries = glob.sync(pattern, { cwd, ignore, ...options.globOptions }); |
| 147 |
|
| 148 |
if (globFilter !== undefined) { |
| 149 |
entries = entries.filter((entry) => { |
| 150 |
const filepath = path.join(cwd, entry); |
| 151 |
|
| 152 |
return globFilter(entry, filepath); |
| 153 |
}); |
| 154 |
} |
| 155 |
|
| 156 |
if (globTransform !== undefined) { |
| 157 |
entries = entries.map((entry) => globTransform(entry)); |
| 158 |
} |
| 159 |
|
| 160 |
entries.sort((a, b) => a.localeCompare(b)); |
| 161 |
|
| 162 |
return entries; |
| 163 |
} |
| 164 |
|
| 165 |
function getFastGlobEntriesSync(pattern: Pattern, ignore?: Pattern, cwd?: string, options?: Options): string[] { |
| 166 |
return fg.sync(pattern, getFastGlobOptions(ignore, cwd, options)).sort((a, b) => a.localeCompare(b)); |
| 167 |
} |
| 168 |
|
| 169 |
function getFastGlobEntriesAsync(pattern: Pattern, ignore?: Pattern, cwd?: string, options?: Options): Promise<string[]> { |
| 170 |
return fg(pattern, getFastGlobOptions(ignore, cwd, options)).then((entries) => { |
| 171 |
entries.sort((a, b) => a.localeCompare(b)); |
| 172 |
|
| 173 |
return entries; |
| 174 |
}); |
| 175 |
} |
| 176 |
|
| 177 |
function getFastGlobEntriesStream(pattern: Pattern, ignore?: Pattern, cwd?: string, options?: Options): Promise<string[]> { |
| 178 |
const entries: string[] = []; |
| 179 |
|
| 180 |
const stream = fg.stream(pattern, getFastGlobOptions(ignore, cwd, options)); |
| 181 |
|
| 182 |
return new Promise((resolve, reject) => { |
| 183 |
stream.on('data', (entry: string) => entries.push(entry)); |
| 184 |
stream.once('error', reject); |
| 185 |
stream.once('end', () => { |
| 186 |
entries.sort((a, b) => a.localeCompare(b)); |
| 187 |
|
| 188 |
resolve(entries); |
| 189 |
}); |
| 190 |
}); |
| 191 |
} |
| 192 |
|
| 193 |
function getFastGlobOptions(ignore?: Pattern, cwd?: string, options?: Options): Options { |
| 194 |
return { |
| 195 |
cwd: cwd === undefined ? process.cwd() : cwd, |
| 196 |
ignore: ignore === undefined ? [] : [ignore], |
| 197 |
onlyFiles: false, |
| 198 |
...options |
| 199 |
}; |
| 200 |
} |