Skip to content

Commit 8ee5d97

Browse files
committed
Check for duplicate test names
1 parent 733fe52 commit 8ee5d97

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

build.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,16 @@ const usage = [
6262
' -e, --exclude Flag to exclude the specified tags instead of including them.',
6363
]
6464

65+
const exitCodeUsageError = 1;
66+
const exitCodeDuplicateNames = 2;
67+
6568
main()
6669

6770
function main() {
6871
const {values, positionals} = parseArgs({options, allowPositionals: true});
6972
if (positionals.length !== 1) {
7073
usage.forEach(line => console.error(line));
71-
process.exit(1);
74+
process.exit(exitCodeUsageError);
7275
}
7376
const [testsFolder] = positionals;
7477
buildTestSuite(testsFolder, values['tag'], values['require-all'], values['exclude']);
@@ -90,8 +93,10 @@ function buildTestSuite(testsFolder, tags, all, exclude) {
9093
const tagFilter = all
9194
? list => tags.every(t => list.includes(t))
9295
: list => tags.some(t => list.includes(t))
93-
const tests = readTestsFromDir(testsFolder)
94-
.filter(test => tags.length === 0 || (tagFilter(getTags(test)) !== exclude));
96+
let tests = readTestsFromDir(testsFolder);
97+
checkDuplicateNames(tests);
98+
99+
tests = tests.filter(test => tags.length === 0 || (tagFilter(getTags(test)) !== exclude));
95100
tests.forEach(test => {
96101
if ('tags' in test) test.tags.sort();
97102
});
@@ -194,3 +199,27 @@ function prependToTestName(prefix) {
194199
};
195200
}
196201

202+
/**
203+
* Verifies that no tests have duplicate names.
204+
* @param allTests - The list of all tests.
205+
*/
206+
function checkDuplicateNames(allTests) {
207+
const names = new Set();
208+
const duplicates = [];
209+
210+
for (const test of allTests) {
211+
const name = test.name;
212+
213+
if (names.has(name)) {
214+
duplicates.push(name);
215+
} else {
216+
names.add(name);
217+
}
218+
}
219+
220+
if (duplicates.length > 0) {
221+
duplicates.sort();
222+
console.error(['Error: Duplicate test names:', ...duplicates].join('\n '));
223+
process.exit(exitCodeDuplicateNames);
224+
}
225+
}

0 commit comments

Comments
 (0)