move testID logic to onTestsChanged

This commit is contained in:
Simon Knott 2024-08-26 10:08:28 +02:00
parent e53c5c6e6a
commit 7d191d3a39
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC

View file

@ -91,8 +91,8 @@ export async function runWatchModeLoop(configLocation: ConfigLocation, initialOp
const teleSuiteUpdater = new TeleSuiteUpdater({ pathSeparator: path.sep, onUpdate() { } }); const teleSuiteUpdater = new TeleSuiteUpdater({ pathSeparator: path.sep, onUpdate() { } });
const dirtyTestFiles = new Set<string>(); const dirtyTestIds = new Set<string>();
const onDirtyTestFiles: { resolve?(): void } = {}; const onDirtyTests: { resolve?(): void } = {};
let queue = Promise.resolve(); let queue = Promise.resolve();
@ -104,19 +104,16 @@ export async function runWatchModeLoop(configLocation: ConfigLocation, initialOp
const { report } = await testServerConnection.listTests({ locations: options.files, projects: options.projects, grep: options.grep }); const { report } = await testServerConnection.listTests({ locations: options.files, projects: options.projects, grep: options.grep });
teleSuiteUpdater.processListReport(report); teleSuiteUpdater.processListReport(report);
for (const project of teleSuiteUpdater.rootSuite!.suites) { for (const test of teleSuiteUpdater.rootSuite!.allTests()) {
for (const suite of project.suites) { if (changedFiles.includes(test.location.file))
if (suite.location?.file && changedFiles.includes(suite.location.file)) dirtyTestIds.add(test.id);
dirtyTestFiles.add(suite.location.file);
}
} }
// if listTests takes longer than the debouncing interval of onTestFilesChanged, // listTests can take longer than the debouncing interval of onTestFilesChanged,
// then the queue might contain another listTests call. // so the queue might contain another set of changed files to be processed before executing tests.
// appending `resolve` to the queue ensures that all pending listTests calls are executed first.
queue = queue.then(() => { queue = queue.then(() => {
if (dirtyTestFiles.size > 0) if (dirtyTestIds.size > 0)
onDirtyTestFiles.resolve?.(); onDirtyTests.resolve?.();
}); });
}); });
}); });
@ -138,7 +135,7 @@ export async function runWatchModeLoop(configLocation: ConfigLocation, initialOp
printPrompt(); printPrompt();
const readCommandPromise = readCommand(); const readCommandPromise = readCommand();
await Promise.race([ await Promise.race([
new Promise<void>(resolve => { onDirtyTestFiles.resolve = resolve; }), new Promise<void>(resolve => { onDirtyTests.resolve = resolve; }),
readCommandPromise, readCommandPromise,
]); ]);
if (!readCommandPromise.isDone()) if (!readCommandPromise.isDone())
@ -147,8 +144,8 @@ export async function runWatchModeLoop(configLocation: ConfigLocation, initialOp
const command = await readCommandPromise; const command = await readCommandPromise;
if (command === 'changed') { if (command === 'changed') {
const testIds = teleSuiteUpdater.rootSuite!.allTests().filter(t => dirtyTestFiles.has(t.location.file)).map(t => t.id); const testIds = [...dirtyTestIds];
dirtyTestFiles.clear(); dirtyTestIds.clear();
await runTests(options, testServerConnection, { testIds, title: 'files changed' }); await runTests(options, testServerConnection, { testIds, title: 'files changed' });
lastRun = { type: 'changed', dirtyTestIds: testIds }; lastRun = { type: 'changed', dirtyTestIds: testIds };
continue; continue;