Skip to content

Commit

Permalink
Try sorting and deduplicating events before comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Apr 18, 2024
1 parent 17e420d commit 0c3cafd
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/testRunner/unittests/sys/symlinkWatching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ describe("unittests:: sys:: symlinkWatching::", () => {
}

interface EventAndFileName {
event: string;
event: "rename" | "change";
// eslint-disable-next-line no-restricted-syntax
fileName: string | null | undefined;
}
interface ExpectedEventAndFileName {
event: string | readonly string[]; // Its expected event name or any of the event names
event: "rename" | "change" | readonly ["rename", "change"]; // Its expected event name or any of the event names
// eslint-disable-next-line no-restricted-syntax
fileName: string | null | undefined;
}
Expand Down Expand Up @@ -126,23 +126,38 @@ describe("unittests:: sys:: symlinkWatching::", () => {
return deferred.promise;
}

function compareEventFileName(a: EventAndFileName["fileName"], b: EventAndFileName["fileName"]) {
return a === b ? ts.Comparison.EqualTo :
// eslint-disable-next-line no-restricted-syntax
a === undefined || a === null ? ts.Comparison.LessThan :
// eslint-disable-next-line no-restricted-syntax
b === undefined || b === null ? ts.Comparison.GreaterThan :
a < b ? ts.Comparison.LessThan :
ts.Comparison.GreaterThan;
}

function compareEventAndFileName(a: EventAndFileName, b: EventAndFileName): ts.Comparison {
return compareEventFileName(b.fileName, a.fileName) || // Also longer string to be before shorter string
ts.compareStringsCaseSensitive(b.event, a.event); // We want rename to be before change
}

function verifyEventAndFileNames(
prefix: string,
actual: readonly EventAndFileName[],
expected: readonly ExpectedEventAndFileName[] | undefined,
) {
assert(actual.length >= (expected?.length ?? 0), `${prefix}:: Expected ${JSON.stringify(expected)} events, got ${JSON.stringify(actual)}`);
const sortedActual = ts.sortAndDeduplicate(actual, compareEventAndFileName);

let expectedIndex = 0;
for (const a of actual) {
for (const a of sortedActual) {
if (isExpectedEventAndFileName(a, expected![expectedIndex])) {
expectedIndex++;
continue;
}
// Previous event repeated?
if (isExpectedEventAndFileName(a, expected![expectedIndex - 1])) continue;
ts.Debug.fail(`${prefix}:: Expected ${JSON.stringify(expected)} events, got ${JSON.stringify(actual)}`);
ts.Debug.fail(`${prefix}:: Expected ${JSON.stringify(expected)} events, got ${JSON.stringify(actual)} Sorted: ${JSON.stringify(sortedActual)}`);
}
assert(expectedIndex >= (expected?.length ?? 0), `${prefix}:: Should get all events: Expected ${JSON.stringify(expected)} events, got ${JSON.stringify(actual)}`);
assert(expectedIndex >= (expected?.length ?? 0), `${prefix}:: Should get all events: Expected ${JSON.stringify(expected)} events, got ${JSON.stringify(actual)} Sorted: ${JSON.stringify(sortedActual)}`);
}

function isExpectedEventAndFileName(actual: EventAndFileName, expected: ExpectedEventAndFileName | undefined) {
Expand Down

0 comments on commit 0c3cafd

Please sign in to comment.