"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.suite = void 0; const assert = require("assert"); const glob = require("glob"); const fg = require("../.."); const Table = require("easy-table"); // eslint-disable-line @typescript-eslint/no-require-imports function suite(name, tests) { const testCases = getTestCases(tests); describe(name, () => { for (const test of testCases) { const title = getTestCaseTitle(test); const definition = getTestCaseMochaDefinition(test); definition(`${title} (sync)`, () => testCaseRunner(test, getFastGlobEntriesSync)); definition(`${title} (async)`, () => testCaseRunner(test, getFastGlobEntriesAsync)); definition(`${title} (stream)`, () => testCaseRunner(test, getFastGlobEntriesStream)); } }); } exports.suite = suite; function getTestCases(tests) { return [].concat(...tests); } function getTestCaseTitle(test) { let title = `pattern: '${test.pattern}'`; if (test.ignore !== undefined) { title += `, ignore: '${test.ignore}'`; } if (test.broken !== undefined) { title += ` (broken - ${test.issue})`; } if (test.correct !== undefined) { title += ' (correct)'; } return title; } function getTestCaseMochaDefinition(test) { var _a; if (test.debug === true) { return it.only; } if (((_a = test.condition) === null || _a === void 0 ? void 0 : _a.call(test)) === false) { return it.skip; } return it; } async function testCaseRunner(test, func) { const expected = getNodeGlobEntries(test.pattern, test.ignore, test.cwd, test.globOptions); const actual = await func(test.pattern, test.ignore, test.cwd, test.fgOptions); if (test.debug === true) { const report = generateDebugReport(expected, actual); console.log(report); } if (test.broken === true && test.issue === undefined) { assert.fail("This test is marked as «broken», but it doesn't have a issue key."); } if (test.correct === true && test.reason === undefined) { assert.fail("This test is marked as «correct», but it doesn't have a reason."); } const isInvertedTest = test.broken === true || test.correct === true; const assertAction = isInvertedTest ? assert.notDeepStrictEqual : assert.deepStrictEqual; assertAction(actual, expected); } function generateDebugReport(expected, actual) { const table = new Table(); const items = actual.length > expected.length ? actual : expected; if (items.length === 0) { return null; } for (const item of items) { table.cell('FIXTURES', item); table.cell('NODE_GLOB', getTestMarker(expected, item)); table.cell('FAST_GLOB', getTestMarker(actual, item)); table.newRow(); } return table.toString(); } function getTestMarker(items, item) { return items.includes(item) ? '+' : '-'; } function getNodeGlobEntries(pattern, ignore, cwd, options) { const entries = glob.sync(pattern, Object.assign({ cwd: cwd === undefined ? process.cwd() : cwd, ignore: ignore === undefined ? [] : [ignore] }, options)); entries.sort((a, b) => a.localeCompare(b)); return entries; } function getFastGlobEntriesSync(pattern, ignore, cwd, options) { return fg.sync(pattern, getFastGlobOptions(ignore, cwd, options)).sort((a, b) => a.localeCompare(b)); } function getFastGlobEntriesAsync(pattern, ignore, cwd, options) { return fg(pattern, getFastGlobOptions(ignore, cwd, options)).then((entries) => { entries.sort((a, b) => a.localeCompare(b)); return entries; }); } function getFastGlobEntriesStream(pattern, ignore, cwd, options) { const entries = []; const stream = fg.stream(pattern, getFastGlobOptions(ignore, cwd, options)); return new Promise((resolve, reject) => { stream.on('data', (entry) => entries.push(entry)); stream.once('error', reject); stream.once('end', () => { entries.sort((a, b) => a.localeCompare(b)); resolve(entries); }); }); } function getFastGlobOptions(ignore, cwd, options) { return Object.assign({ cwd: cwd === undefined ? process.cwd() : cwd, ignore: ignore === undefined ? [] : [ignore], onlyFiles: false }, options); }