Skip to content

Commit 05f4dba

Browse files
Try sorting and deduplicating events before comparing. (#58242)
Co-authored-by: Ron Buckton <[email protected]>
1 parent 5b3060d commit 05f4dba

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/testRunner/unittests/sys/symlinkWatching.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ describe("unittests:: sys:: symlinkWatching::", () => {
7171
}
7272

7373
interface EventAndFileName {
74-
event: string;
74+
event: "rename" | "change";
7575
// eslint-disable-next-line no-restricted-syntax
7676
fileName: string | null | undefined;
7777
}
7878
interface ExpectedEventAndFileName {
79-
event: string | readonly string[]; // Its expected event name or any of the event names
79+
event: "rename" | "change" | readonly ["rename", "change"]; // Its expected event name or any of the event names
8080
// eslint-disable-next-line no-restricted-syntax
8181
fileName: string | null | undefined;
8282
}
@@ -126,23 +126,32 @@ describe("unittests:: sys:: symlinkWatching::", () => {
126126
return deferred.promise;
127127
}
128128

129+
function compareEventFileName(a: EventAndFileName["fileName"], b: EventAndFileName["fileName"]) {
130+
return ts.compareStringsCaseSensitive(a ?? undefined, b ?? undefined);
131+
}
132+
133+
function compareEventAndFileName(a: EventAndFileName, b: EventAndFileName): ts.Comparison {
134+
return compareEventFileName(b.fileName, a.fileName) || // Also longer string to be before shorter string
135+
ts.compareStringsCaseSensitive(b.event, a.event); // We want rename to be before change
136+
}
137+
129138
function verifyEventAndFileNames(
130139
prefix: string,
131140
actual: readonly EventAndFileName[],
132141
expected: readonly ExpectedEventAndFileName[] | undefined,
133142
) {
134143
assert(actual.length >= (expected?.length ?? 0), `${prefix}:: Expected ${JSON.stringify(expected)} events, got ${JSON.stringify(actual)}`);
144+
const sortedActual = ts.sortAndDeduplicate(actual, compareEventAndFileName);
145+
135146
let expectedIndex = 0;
136-
for (const a of actual) {
147+
for (const a of sortedActual) {
137148
if (isExpectedEventAndFileName(a, expected![expectedIndex])) {
138149
expectedIndex++;
139150
continue;
140151
}
141-
// Previous event repeated?
142-
if (isExpectedEventAndFileName(a, expected![expectedIndex - 1])) continue;
143-
ts.Debug.fail(`${prefix}:: Expected ${JSON.stringify(expected)} events, got ${JSON.stringify(actual)}`);
152+
ts.Debug.fail(`${prefix}:: Expected ${JSON.stringify(expected)} events, got ${JSON.stringify(actual)} Sorted: ${JSON.stringify(sortedActual)}`);
144153
}
145-
assert(expectedIndex >= (expected?.length ?? 0), `${prefix}:: Should get all events: Expected ${JSON.stringify(expected)} events, got ${JSON.stringify(actual)}`);
154+
assert(expectedIndex >= (expected?.length ?? 0), `${prefix}:: Should get all events: Expected ${JSON.stringify(expected)} events, got ${JSON.stringify(actual)} Sorted: ${JSON.stringify(sortedActual)}`);
146155
}
147156

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

0 commit comments

Comments
 (0)