Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix(vscode-plugin): make test reliable with waitForDiagnosticChange (#1322)

Dongsung "Donny" Kime41c6763750b
diff --git a/packages/vscode-plugin/src/tests/suite/helper.ts b/packages/vscode-plugin/src/tests/suite/helper.ts
index 9b7aede13..5af33f11c 100644
--- a/packages/vscode-plugin/src/tests/suite/helper.ts
+++ b/packages/vscode-plugin/src/tests/suite/helper.ts
@@ -27,10 +27,12 @@ export async function activateHarper(): Promise<Extension<void>> {
return harper;
}
-export async function openFile(...pathSegments: string[]): Promise<Uri> {
- const uri = Uri.joinPath(Uri.file(workspace.workspaceFolders![0].uri.path), ...pathSegments);
+export function getUri(...pathSegments: string[]): Uri {
+ return Uri.joinPath(Uri.file(workspace.workspaceFolders![0].uri.path), ...pathSegments);
+}
+
+export async function openUri(uri: Uri): Promise<void> {
await window.showTextDocument(await workspace.openTextDocument(uri));
- return uri;
}
export async function openUntitled(text: string): Promise<Uri> {
@@ -45,10 +47,6 @@ export async function setTextDocumentLanguage(uri: Uri, languageId: string): Pro
languages.setTextDocumentLanguage(document, languageId);
}
-export function getActualDiagnostics(resource: Uri): Diagnostic[] {
- return languages.getDiagnostics(resource).filter((d) => d.source === 'Harper');
-}
-
export function createExpectedDiagnostics(
...data: { message: string; range: Range }[]
): Diagnostic[] {
@@ -80,19 +78,51 @@ export function createRange(
return new Range(new Position(startRow, startColumn), new Position(endRow, endColumn));
}
-// The numbers used in these functions are what works when running tests in GitHub CI.
-export async function waitForHarperToActivate() {
- await sleep(500);
-}
-export async function waitForUpdatesFromOpenedFile() {
- await sleep(75);
-}
-export async function waitForUpdatesFromConfigChange() {
- await sleep(300);
-}
-export async function waitForUpdatesFromDeletedFile() {
- await sleep(450);
+function getActualDiagnostics(resource: Uri): Diagnostic[] {
+ return languages.getDiagnostics(resource).filter((d) => d.source === 'Harper');
}
-function sleep(duration: number): Promise<void> {
- return new Promise((resolve) => setTimeout(resolve, duration));
+
+/** Note that this function times out if there is no change detected. */
+export function waitForDiagnosticsChange(
+ uri: Uri,
+ func?: () => Promise<void>,
+): Promise<Diagnostic[]> {
+ return new Promise((resolve, reject) => {
+ const before = func ? getActualDiagnostics(uri) : [];
+
+ (func || (async () => {}))().then(() => {
+ const delay = 10;
+ const limit = 10;
+ let counter = 0;
+
+ const tryCompare = () => {
+ const after = getActualDiagnostics(uri);
+ try {
+ compareActualVsExpectedDiagnostics(before, after);
+
+ // after didn't change, try again
+ counter = 0;
+ tryAgain();
+ } catch (e) {
+ // after did change, try until stabilized
+ counter++;
+ if (counter < limit) {
+ tryAgain();
+ } else {
+ clearTimeout(rejectTimer);
+ resolve(after);
+ }
+ }
+ };
+ let tryTimer = setTimeout(tryCompare);
+ const tryAgain = () => {
+ tryTimer = setTimeout(tryCompare, delay);
+ };
+
+ const rejectTimer = setTimeout(() => {
+ clearTimeout(tryTimer);
+ reject('No change of diagnostics detected.');
+ }, 4000);
+ });
+ });
}
diff --git a/packages/vscode-plugin/src/tests/suite/integration.test.ts b/packages/vscode-plugin/src/tests/suite/integration.test.ts
index a72351496..49e3fce88 100644
--- a/packages/vscode-plugin/src/tests/suite/integration.test.ts
+++ b/packages/vscode-plugin/src/tests/suite/integration.test.ts
@@ -8,14 +8,11 @@ import {
compareActualVsExpectedDiagnostics,
createExpectedDiagnostics,
createRange,
- getActualDiagnostics,
- openFile,
+ getUri,
openUntitled,
+ openUri,
setTextDocumentLanguage,
- waitForHarperToActivate,
- waitForUpdatesFromConfigChange,
- waitForUpdatesFromDeletedFile,
- waitForUpdatesFromOpenedFile,
+ waitForDiagnosticsChange,
} from './helper';
describe('Integration >', () => {
@@ -25,17 +22,17 @@ describe('Integration >', () => {
beforeAll(async () => {
await closeAll();
harper = await activateHarper();
- markdownUri = await openFile('integration.md');
- await waitForHarperToActivate();
+ markdownUri = getUri('integration.md');
+ await openUri(markdownUri);
});
it('runs', () => {
expect(harper.isActive).toBe(true);
});
- it('gives correct diagnostics for files', () => {
+ it('gives correct diagnostics for files', async () => {
compareActualVsExpectedDiagnostics(
- getActualDiagnostics(markdownUri),
+ await waitForDiagnosticsChange(markdownUri),
createExpectedDiagnostics(
{
message: 'Did you mean to repeat this word?',
@@ -55,10 +52,9 @@ describe('Integration >', () => {
it('gives correct diagnostics for untitled', async () => {
const untitledUri = await openUntitled('Errorz');
- await waitForHarperToActivate(); // requires a longer time
compareActualVsExpectedDiagnostics(
- getActualDiagnostics(untitledUri),
+ await waitForDiagnosticsChange(untitledUri),
createExpectedDiagnostics({
message: 'Did you mean to spell `Errorz` this way?',
range: createRange(0, 0, 0, 6),
@@ -68,11 +64,12 @@ describe('Integration >', () => {
it('gives correct diagnostics when language is changed', async () => {
const untitledUri = await openUntitled('Errorz # Errorz');
- await setTextDocumentLanguage(untitledUri, 'plaintext');
- await waitForHarperToActivate(); // requires a longer time
compareActualVsExpectedDiagnostics(
- getActualDiagnostics(untitledUri),
+ await waitForDiagnosticsChange(
+ untitledUri,
+ async () => await setTextDocumentLanguage(untitledUri, 'plaintext'),
+ ),
createExpectedDiagnostics(
{
message: 'Did you mean to spell `Errorz` this way?',
@@ -85,13 +82,11 @@ describe('Integration >', () => {
),
);
- await setTextDocumentLanguage(untitledUri, 'shellscript');
-
- // Wait for `harper-ls` to send diagnostics
- await waitForUpdatesFromConfigChange();
-
compareActualVsExpectedDiagnostics(
- getActualDiagnostics(untitledUri),
+ await waitForDiagnosticsChange(
+ untitledUri,
+ async () => await setTextDocumentLanguage(untitledUri, 'shellscript'),
+ ),
createExpectedDiagnostics({
message: 'Did you mean to spell `Errorz` this way?',
range: createRange(0, 9, 0, 15),
@@ -101,11 +96,12 @@ describe('Integration >', () => {
it('updates diagnostics on configuration change', async () => {
const config = workspace.getConfiguration('harper.linters');
- await config.update('RepeatedWords', false, ConfigurationTarget.Workspace);
- await waitForUpdatesFromConfigChange();
compareActualVsExpectedDiagnostics(
- getActualDiagnostics(markdownUri),
+ await waitForDiagnosticsChange(
+ markdownUri,
+ async () => await config.update('RepeatedWords', false, ConfigurationTarget.Workspace),
+ ),
createExpectedDiagnostics(
{
message: 'Did you mean to spell `errorz` this way?',
@@ -119,16 +115,20 @@ describe('Integration >', () => {
);
// Set config back to default value
- await config.update('RepeatedWords', true, ConfigurationTarget.Workspace);
+ await waitForDiagnosticsChange(
+ markdownUri,
+ async () => await config.update('RepeatedWords', true, ConfigurationTarget.Workspace),
+ );
});
it('accepts British spellings when dialect is set to British', async () => {
const config = workspace.getConfiguration('harper');
- await config.update('dialect', 'British', ConfigurationTarget.Workspace);
- await waitForUpdatesFromConfigChange();
compareActualVsExpectedDiagnostics(
- getActualDiagnostics(markdownUri),
+ await waitForDiagnosticsChange(
+ markdownUri,
+ async () => await config.update('dialect', 'British', ConfigurationTarget.Workspace),
+ ),
createExpectedDiagnostics(
{
message: 'Did you mean to repeat this word?',
@@ -142,7 +142,10 @@ describe('Integration >', () => {
);
// Set config back to default value
- await config.update('dialect', 'American', ConfigurationTarget.Workspace);
+ await waitForDiagnosticsChange(
+ markdownUri,
+ async () => await config.update('dialect', 'American', ConfigurationTarget.Workspace),
+ );
});
it('updates diagnostics when files are deleted', async () => {
@@ -150,30 +153,29 @@ describe('Integration >', () => {
// Delete file through VS Code
await commands.executeCommand('workbench.files.action.showActiveFileInExplorer');
- await commands.executeCommand('deleteFile');
- await waitForUpdatesFromDeletedFile();
compareActualVsExpectedDiagnostics(
- getActualDiagnostics(markdownUri),
+ await waitForDiagnosticsChange(
+ markdownUri,
+ async () => await commands.executeCommand('deleteFile'),
+ ),
createExpectedDiagnostics(),
);
// Restore and reopen deleted file
await workspace.fs.writeFile(markdownUri, markdownContent);
- await openFile('integration.md');
- await waitForUpdatesFromOpenedFile();
+ await waitForDiagnosticsChange(markdownUri, async () => await openUri(markdownUri));
// Delete file directly
- await workspace.fs.delete(markdownUri);
- await waitForUpdatesFromDeletedFile();
-
compareActualVsExpectedDiagnostics(
- getActualDiagnostics(markdownUri),
+ await waitForDiagnosticsChange(
+ markdownUri,
+ async () => await workspace.fs.delete(markdownUri),
+ ),
createExpectedDiagnostics(),
);
// Restore and reopen deleted file
await workspace.fs.writeFile(markdownUri, markdownContent);
- await openFile('integration.md');
});
});
diff --git a/packages/vscode-plugin/src/tests/suite/languages.test.ts b/packages/vscode-plugin/src/tests/suite/languages.test.ts
index 20a3f5a79..78eded2de 100644
--- a/packages/vscode-plugin/src/tests/suite/languages.test.ts
+++ b/packages/vscode-plugin/src/tests/suite/languages.test.ts
@@ -2,9 +2,9 @@ import {
compareActualVsExpectedDiagnostics,
createExpectedDiagnostics,
createRange,
- getActualDiagnostics,
- openFile,
- waitForUpdatesFromOpenedFile,
+ getUri,
+ openUri,
+ waitForDiagnosticsChange,
} from './helper';
describe('Languages >', () => {
@@ -49,11 +49,10 @@ describe('Languages >', () => {
{ type: 'TypeScript JSX', file: 'typescriptreact.tsx', row: 3, column: 7 },
].forEach((testCase) => {
it(`gives correct diagnostics for ${testCase.type} files`, async () => {
- const uri = await openFile('languages', testCase.file);
- await waitForUpdatesFromOpenedFile();
+ const uri = getUri('languages', testCase.file);
compareActualVsExpectedDiagnostics(
- getActualDiagnostics(uri),
+ await waitForDiagnosticsChange(uri, async () => await openUri(uri)),
createExpectedDiagnostics({
message: 'Did you mean to spell `Errorz` this way?',
range: createRange(testCase.row, testCase.column, testCase.row, testCase.column + 6),