Skip to content

Commit

Permalink
Merge pull request #84 from senkenn/fix/empty_sql_format_bug
Browse files Browse the repository at this point in the history
Fix SQL formatting function in the case of empty sql content and test suite
  • Loading branch information
senkenn authored Jun 22, 2024
2 parents eef29ca + 2015583 commit 7012afd
Show file tree
Hide file tree
Showing 12 changed files with 697 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"rust-analyzer.linkedProjects": [
"./sql-extraction/rs/Cargo.toml",
"./vsce/test-workspace-rs/Cargo.toml"
"./vsce-test/test-workspace-rs/Cargo.toml"
],
"github-actions.workflows.pinned.workflows": [
".github/workflows/e2e-test.yaml",
Expand Down
8 changes: 5 additions & 3 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@
"rules": {
"recommended": true,
"suspicious": {
"noExplicitAny": "warn"
"noExplicitAny": "warn",
"noConsoleLog": "error"
},
"style": {
"noParameterAssign": "warn",
"noNonNullAssertion": "warn"
}
}
},
"ignore": ["test-workspace*"]
},
"json": {
"parser": {
"allowTrailingCommas": false,
"allowTrailingCommas": true,
"allowComments": true
}
}
Expand Down
4 changes: 2 additions & 2 deletions lefthook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ pre-commit:
biome:
glob: "*.{js,ts,jsx,tsx,json,css,scss}"
run: |
bunx biome check --apply {staged_files} && git add {staged_files}
pnpm biome check --apply {staged_files} && git add {staged_files}
cspell:
glob: "*"
run: |
# check file name
git diff --name-only --cached | bunx cspell --no-progress --show-context stdin --cache
git diff --name-only --cached | pnpm cspell --no-progress --show-context stdin --cache
5 changes: 4 additions & 1 deletion vsce-test/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "@senken/vsce-test",
"scripts": {
"test:e2e": "tsc -p . && node ./out/runTest.js",
"test:e2e": "tsc && node ./out/runTest.js",
"test:e2e:bg": "xvfb-run -a pnpm test:e2e"
},
"devDependencies": {
"@types/vscode": "^1.90.0",
"@vscode/test-electron": "^2.4.0"
},
"jest": {
"snapshotResolver": "<rootDir>/test/snapshotResolver.js"
}
}
5 changes: 3 additions & 2 deletions vsce-test/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export async function resetTestWorkspace(
const settingsJsonPath = path.resolve(wsPath, ".vscode", "settings.json");
const testFilePathJoinedWithSpace = testFilePaths.join(" ");

// restore saved files
execSync(`git restore ${testFilePathJoinedWithSpace} ${settingsJsonPath}`);

// close active editor
await vscode.commands.executeCommand("workbench.action.closeAllGroups");
// revert unsaved changes
await vscode.commands.executeCommand("workbench.action.files.revert");
}
26 changes: 26 additions & 0 deletions vsce-test/test/snapshotResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
/**
* @param {string} testPath
* @param {string} snapshotExtension
*/
resolveSnapshotPath: (testPath, snapshotExtension) =>
testPath.replace(/\/out\//, "/test/").replace(/\.js/, ".ts") +
snapshotExtension,

/**
* @param {string} snapshotFilePath
* @param {string} snapshotExtension
*/
resolveTestPath: (snapshotFilePath, snapshotExtension) =>
snapshotFilePath.slice(0, -snapshotExtension.length),

// Example test path, used for preflight consistency check of the implementation above
testPathForConsistencyCheck: "some/__tests__/example.test.ts",
};

// Results in:
// .
// ├── test
// │ ├── suite-ts
// │ │ ├── extension.test.ts
// │ │ ├── extension.test.ts.snap
35 changes: 35 additions & 0 deletions vsce-test/test/suite-rs/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,39 @@ describe("Formatting Test", () => {
const formattedText = doc.getText();
expect(formattedText).toMatchSnapshot();
});

it.each`
desc | content
${"empty"} | ${""}
${"one space"} | ${" "}
${"one new line"} | ${"\n"}
${"spaces and new lines"} | ${"\n \n \n"}
`(
"Should NOT be formatted with empty content: $desc",
async ({ content }) => {
const filePath = path.resolve(wsPath, "src", "main.rs");
const docUri = vscode.Uri.file(filePath);
const doc = await vscode.workspace.openTextDocument(docUri);
const editor = await vscode.window.showTextDocument(doc);

await sleep(1000);

// change config
const replaceRange = new vscode.Range(
new vscode.Position(52, 9),
new vscode.Position(52, 69),
);
await editor.edit((editBuilder) => {
editBuilder.replace(replaceRange, content);
});

// execute command
await vscode.commands.executeCommand("sqlsurge.formatSql");
await sleep(1000);

// execute command
const formattedText = doc.getText();
expect(formattedText).toMatchSnapshot();
},
);
});
Loading

0 comments on commit 7012afd

Please sign in to comment.