From 94b95ebbc637081591a2335d74dd7f06d5a5faf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nacho=20Cord=C3=B3n?= Date: Tue, 6 Feb 2024 15:52:29 +0000 Subject: [PATCH 1/2] Adds test for a bug with an empty query in VSCode --- .../fixtures/syntax-validation.cypher | 1 + .../e2e_tests/tests/syntax-validation.spec.ts | 92 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 packages/vscode-extension/e2e_tests/fixtures/syntax-validation.cypher create mode 100644 packages/vscode-extension/e2e_tests/tests/syntax-validation.spec.ts diff --git a/packages/vscode-extension/e2e_tests/fixtures/syntax-validation.cypher b/packages/vscode-extension/e2e_tests/fixtures/syntax-validation.cypher new file mode 100644 index 000000000..03bb1f860 --- /dev/null +++ b/packages/vscode-extension/e2e_tests/fixtures/syntax-validation.cypher @@ -0,0 +1 @@ +MATCH (n) RETURN m \ No newline at end of file diff --git a/packages/vscode-extension/e2e_tests/tests/syntax-validation.spec.ts b/packages/vscode-extension/e2e_tests/tests/syntax-validation.spec.ts new file mode 100644 index 000000000..e1dfae2ab --- /dev/null +++ b/packages/vscode-extension/e2e_tests/tests/syntax-validation.spec.ts @@ -0,0 +1,92 @@ +import * as assert from 'assert'; +import * as vscode from 'vscode'; +import { eventually, getDocumentUri, openDocument } from '../helpers'; + +type InclusionTestArgs = { + textFile: string; + expected: vscode.Diagnostic[]; +}; + +export async function testSyntaxValidation({ + textFile, + expected, +}: InclusionTestArgs) { + await eventually( + () => + new Promise((resolve, reject) => { + const docUri = getDocumentUri(textFile); + const diagnostics: vscode.Diagnostic[] = + vscode.languages.getDiagnostics(docUri); + + try { + // We need to test diagnostics one by one + // because the ones returned by VSCode contain + // more information we don't care about in the tests + assert.equal(diagnostics.length, expected.length); + diagnostics.forEach((diagnostic, i) => { + const expectedDiagnostic = expected[i]; + assert.equal(diagnostic.message, expectedDiagnostic.message); + assert.deepEqual(diagnostic.range, expectedDiagnostic.range); + assert.equal(diagnostic.severity, expectedDiagnostic.severity); + }); + resolve(); + } catch (e) { + reject(); + } + }), + ); +} + +suite('Syntax validation spec', () => { + test('Correctly validates empty cypher statement', async () => { + const textFile = 'syntax-validation.cypher'; + const docUri = getDocumentUri(textFile); + + await openDocument(docUri); + + const editor = vscode.window.activeTextEditor; + + await editor.edit((editBuilder) => + editBuilder.replace( + // Select the whole file + new vscode.Range( + new vscode.Position(0, 0), + new vscode.Position(100, 0), + ), + 'MATCH (n)', + ), + ); + + // We need to wait here because diagnostics are eventually + // consistent i.e. they don't show up immediately + await testSyntaxValidation({ + textFile: 'syntax-validation.cypher', + expected: [ + new vscode.Diagnostic( + new vscode.Range( + new vscode.Position(0, 0), + new vscode.Position(0, 9), + ), + 'Query cannot conclude with MATCH (must be a RETURN clause, an update clause, a unit subquery call, or a procedure call with no YIELD)', + vscode.DiagnosticSeverity.Error, + ), + ], + }); + + await editor.edit((editBuilder) => + editBuilder.replace( + // Select the whole file + new vscode.Range( + new vscode.Position(0, 0), + new vscode.Position(100, 0), + ), + '', + ), + ); + + await testSyntaxValidation({ + textFile: 'syntax-validation.cypher', + expected: [], + }); + }); +}); From 6612ceb2853ffb6a3b75a0ff26a30fc56e2f0b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nacho=20Cord=C3=B3n?= Date: Tue, 6 Feb 2024 15:55:47 +0000 Subject: [PATCH 2/2] Fixes bug --- packages/language-server/src/linting.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/language-server/src/linting.ts b/packages/language-server/src/linting.ts index 88982411f..ebb1c1db1 100644 --- a/packages/language-server/src/linting.ts +++ b/packages/language-server/src/linting.ts @@ -25,6 +25,7 @@ async function rawLintDocument( const query = document.getText(); if (query.length === 0) { + sendDiagnostics([]); return; }