Skip to content

Commit

Permalink
Issue-36 : Fix JSON parsing error (#57)
Browse files Browse the repository at this point in the history
* Issue-36 : Fix JSON parsing error

* Issue-36 : Ensure all tests pass

* Issue-36 : Update changelog

Co-authored-by: Robbie Ostermann <[email protected]>
  • Loading branch information
RobertOstermann and Robbie Ostermann authored Aug 30, 2022
1 parent 4c580b7 commit 99d81fc
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 63 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

## [Unreleased]

## [0.0.7] - 2022-08-30

- [#57](https://github.com/sqlfluff/vscode-sqlfluff/pull/57) Fix automated tests and JSON parsing errors.

## [0.0.6] - 2022-07-29

- [#50](https://github.com/sqlfluff/vscode-sqlfluff/pull/50) refactors the extension and adds several new settings.
Expand Down
16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vscode-sqlfluff",
"displayName": "sqlfluff",
"version": "0.0.6",
"version": "0.0.7",
"description": "A linter and auto-formatter for SQLfluff, a popular linting tool for SQL and dbt.",
"publisher": "dorzey",
"icon": "images/icon.png",
Expand Down Expand Up @@ -127,16 +127,18 @@
},
"scripts": {
"vscode:prepublish": "npm run -S esbuild-base -- --minify",
"compile": "rimraf out && tsc -p ./",
"compile": "rimraf out && tsc -p ./ && npm run postbuild",
"watch": "rimraf out && tsc -watch -p ./",
"pretest": "npm run compile",
"test": "node ./out/test/runTest.js",
"download-api": "vscode-dts dev",
"postdownload-api": "vscode-dts main",
"postinstall": "npm run download-api",
"lint": "eslint src --ext ts",
"lint:fix": "eslint src --ext ts --fix",
"esbuild-base": "rimraf out && esbuild ./src/extension.ts --bundle --outfile=out/src/extension.js --external:vscode --format=cjs --platform=node",
"esbuild": "npm run -S esbuild-base -- --sourcemap",
"esbuild-watch": "npm run -S esbuild-base -- --sourcemap --watch",
"postbuild": "copyfiles \"./test/suite/test_sql/**/*.sql\" \"./out\" && copyfiles \"./test/.sqlfluff\" \"./out\"",
"deploy": "vsce publish"
},
"devDependencies": {
Expand All @@ -146,7 +148,7 @@
"@types/vscode": "^1.59.0",
"@typescript-eslint/eslint-plugin": "^5.13.0",
"@typescript-eslint/parser": "^5.13.0",
"cpy-cli": "^4.0.0",
"copyfiles": "^2.4.1",
"esbuild": "^0.14.24",
"eslint": "^8.10.0",
"eslint-config-prettier": "^8.5.0",
Expand All @@ -158,5 +160,11 @@
"vsce": "^2.6.7",
"vscode-dts": "^0.3.3",
"vscode-test": "^1.3.0"
},
"__metadata": {
"id": "dd94e113-0773-42ed-9bc0-2f4b55bc7185",
"publisherDisplayName": "dorzey",
"publisherId": "db31b9db-3aa2-44a0-8541-71cab84423a9",
"isPreReleaseVersion": false
}
}
6 changes: 6 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export const activate = (context: vscode.ExtensionContext) => {
context.subscriptions.push(
vscode.languages.registerCodeActionsProvider("sql", new SQLFluffQuickFix(), {
providedCodeActionKinds: SQLFluffQuickFix.providedCodeActionKind
}),
vscode.languages.registerCodeActionsProvider("sql-bigquery", new SQLFluffQuickFix(), {
providedCodeActionKinds: SQLFluffQuickFix.providedCodeActionKind
}),
vscode.languages.registerCodeActionsProvider("jinja-sql", new SQLFluffQuickFix(), {
providedCodeActionKinds: SQLFluffQuickFix.providedCodeActionKind
})
);

Expand Down
4 changes: 2 additions & 2 deletions src/features/formatter/formattingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export class DocumentFormattingEditProvider {
async provideDocumentFormattingEdits(
document: vscode.TextDocument
): Promise<vscode.TextEdit[]> {
const filePath = document.fileName.replace(/\\/g, "/");
const rootPath = vscode.workspace.workspaceFolders[0].uri.fsPath.replace(/\\/g, "/");
const filePath = document.fileName.replace(/\\+/g, "/");
const rootPath = vscode.workspace.workspaceFolders[0].uri.fsPath.replace(/\\+/g, "/");
const workingDirectory = Configuration.workingDirectory() ? Configuration.workingDirectory() : rootPath;
const textEdits: vscode.TextEdit[] = [];

Expand Down
3 changes: 2 additions & 1 deletion src/features/sqlFluffLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ export class SQLFluffLinterProvider implements Linter {
const diagnostics: Diagnostic[] = [];
lines.forEach((line) => {
let filePaths: Array<FilePath>;
const normalizedLine = line.replace(/\\+/g, "/");
try {
filePaths = JSON.parse(line);
filePaths = JSON.parse(normalizedLine);
} catch (e) {
// JSON.parse may fail if sqlfluff compilation prints non-JSON formatted messages
console.warn(e);
Expand Down
4 changes: 2 additions & 2 deletions src/features/utils/lintingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ export class LintingProvider {
private doLint(document: vscode.TextDocument): Promise<void> {
return new Promise<void>((resolve) => {
const decoder = new LineDecoder();
const filePath = document.fileName.replace(/\\/g, "/");
const rootPath = vscode.workspace.workspaceFolders[0].uri.fsPath.replace(/\\/g, "/");
const filePath = document.fileName.replace(/\\+/g, "/");
const rootPath = vscode.workspace.workspaceFolders[0].uri.fsPath.replace(/\\+/g, "/");
const workingDirectory = Configuration.workingDirectory() ? Configuration.workingDirectory() : rootPath;

let args: string[];
Expand Down
50 changes: 20 additions & 30 deletions test/suite/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,52 @@
import * as assert from "assert";
import * as vscode from "vscode";

import { activate, format, getDocUri, toRange } from "./helper";
import * as Helper from "./helper";

const TIMEOUT = 1000000;

const TIMEOUT = 4000;
suite("Extension Test Suite", () => {
vscode.window.showInformationMessage("Start all tests.");

test("Good SQL should have zero diagnostics", async () => {

const docUri = getDocUri("/test_sql/good.sql");
await activate(docUri);


const actualDiagnostics = vscode.languages.getDiagnostics(docUri);
const documentUri = Helper.getDocumentUri("/test_sql/good.sql");
await Helper.activate(documentUri);

const actualDiagnostics = vscode.languages.getDiagnostics(documentUri);

assert.strictEqual(actualDiagnostics.length, 0);

}).timeout(TIMEOUT);

test("Bad SQL should have the correct diagnostics", async () => {
const documentUri = Helper.getDocumentUri("/test_sql/bad.sql");
await Helper.activate(documentUri);

const docUri = getDocUri("/test_sql/bad.sql");
await activate(docUri);


const actualDiagnostics = vscode.languages.getDiagnostics(docUri);

const actualDiagnostics = vscode.languages.getDiagnostics(documentUri);

assert.strictEqual(actualDiagnostics.length, 3);
assert.strictEqual(actualDiagnostics.length, 2);
[
{ range: toRange(0, 1, 0, 1), message: "Query produces an unknown number of result columns.", code: "L044" },
{ range: toRange(0, 10, 0, 10), message: "Inconsistent capitalisation of keywords.", code: "L010" },
{ range: toRange(0, 22, 0, 22), message: "Files must end with a trailing newline.", code: "L009" },
{ range: Helper.toRange(1, 10, 1, 10), message: "Keywords must be consistently upper case.", code: "L010" },
{ range: Helper.toRange(2, 1, 2, 1), message: "Files must end with a single trailing newline.", code: "L009" },
].forEach((expectedDiagnostic, i) => {
assertDiagnosticIsEqual(actualDiagnostics[i], expectedDiagnostic);
});

}).timeout(TIMEOUT);

test("Bad SQL has zero diagnostics after document format", async () => {
const documentUri = Helper.getDocumentUri("/test_sql/format.sql");
// const document = await Helper.activate(documentUri);
// const preFormatDiagnostics = vscode.languages.getDiagnostics(documentUri);
// assert.strictEqual(preFormatDiagnostics.length, 1, "Pre-format diagnostics not expected length");

const docUri = getDocUri("/test_sql/format.sql");
const document = await activate(docUri);
const preFormatDiagnostics = vscode.languages.getDiagnostics(docUri);
assert.strictEqual(preFormatDiagnostics.length, 1, "Pre-format diagnostics not expected length");


await format(document);
await activate(docUri);
await Helper.format(documentUri);
// await Helper.activate(documentUri);


const postFormatDiagnostics = vscode.languages.getDiagnostics(docUri);
const postFormatDiagnostics = vscode.languages.getDiagnostics(documentUri);
assert.strictEqual(postFormatDiagnostics.length, 0, "Post-format diagnostics not expected length");

}).timeout(9999999);
}).timeout(TIMEOUT);

const assertDiagnosticIsEqual = (actual: vscode.Diagnostic, expected: { range: any; message: any; code: any; }) => {
assert.deepStrictEqual(actual.range, expected.range);
Expand All @@ -64,5 +55,4 @@ suite("Extension Test Suite", () => {
assert.strictEqual(actual.code, expected.code);
assert.strictEqual(actual.source, "sqlfluff");
};

});
50 changes: 29 additions & 21 deletions test/suite/helper.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,54 @@
import * as vscode from "vscode";

export const activate = async (docUri: vscode.Uri): Promise<vscode.TextDocument | undefined> => {
export const SLEEP_TIME = 10000;

export const activate = async (documentUri: vscode.Uri): Promise<vscode.TextDocument | undefined> => {
// The extensionId is `publisher.name` from package.json
const ext = vscode.extensions.getExtension("vscode-sqlfluff");
await ext?.activate();
const extension = vscode.extensions.getExtension("vscode-sqlfluff");
await extension?.activate();
try {
const document = await vscode.workspace.openTextDocument(docUri);
const editor = await vscode.window.showTextDocument(document);
await vscode.commands.executeCommand("workbench.action.closeActiveEditor");
const document = await vscode.workspace.openTextDocument(documentUri);
await vscode.window.showTextDocument(document);
await document.save();

await sleep(2000); // Wait for server activation
await sleep(SLEEP_TIME); // Wait for server activation

return document;
} catch (e) {
console.error(e);
}
};

export const format = async (document: vscode.TextDocument | undefined) => {
if (document) {
try {
await vscode.commands.executeCommand("editor.action.formatDocument");
await sleep(2000);
await document.save();
await vscode.commands.executeCommand("workbench.action.closeActiveEditor");
} catch (e) {
console.error(e);
}
export const format = async (documentUri: vscode.Uri) => {
try {
await vscode.commands.executeCommand("workbench.action.closeActiveEditor");
const document = await vscode.workspace.openTextDocument(documentUri);
await vscode.window.showTextDocument(document);
await vscode.commands.executeCommand("editor.action.formatDocument");
await sleep(SLEEP_TIME);
await document.save();

await sleep(SLEEP_TIME); // Wait for server activation

return document;
} catch (e) {
console.error(e);
}
};

const sleep = async (ms: number): Promise<any> => {
export const sleep = async (ms: number): Promise<any> => {
return new Promise(resolve => {
return setTimeout(resolve, ms);
});
};

export const getDocUri = (p: string) => {
export const getDocumentUri = (p: string) => {
return vscode.Uri.file(__dirname + p);
};

export const toRange = (sLine: number, sChar: number, eLine: number, eChar: number) => {
const start = new vscode.Position(sLine, sChar);
const end = new vscode.Position(eLine, eChar);
export const toRange = (startLine: number, StartCharacter: number, endLine: number, endCharacter: number) => {
const start = new vscode.Position(startLine, StartCharacter);
const end = new vscode.Position(endLine, endCharacter);
return new vscode.Range(start, end);
};
3 changes: 2 additions & 1 deletion test/suite/test_sql/bad.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
-- 割引金額
select a from b;
SELECT a from b;

2 changes: 1 addition & 1 deletion test/suite/test_sql/format.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SELECT a FROM a_table;
SELECT a from a_table;
2 changes: 1 addition & 1 deletion test/suite/test_sql/good.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
select a from a_table;
SELECT a FROM a_table;

0 comments on commit 99d81fc

Please sign in to comment.