Skip to content

Commit

Permalink
Combine diagnostics and formattings tests in 1 suite (#524)
Browse files Browse the repository at this point in the history
## Summary

This PR combines the diagnostics and formatting tests into a single
suite. It also updates the tests in the following ways to reduce the
overall test time:
* No need to wait for the formatting tests
* Use polling to wait for diagnostics instead of a hardcoded sleep value

## Test Plan

`npm run test`
  • Loading branch information
dhruvmanila authored Jul 12, 2024
1 parent f60bcc8 commit eb4f9dc
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 114 deletions.
54 changes: 0 additions & 54 deletions src/test/diagnostics.test.ts

This file was deleted.

92 changes: 92 additions & 0 deletions src/test/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import * as vscode from "vscode";
import * as assert from "assert";
import { getDocumentUri, activateExtension, sleep } from "./helper";

suite("E2E tests", () => {
teardown(async () => {
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
});

test("Should provide diagnostics", async () => {
await activateExtension();

const documentUri = getDocumentUri("diagnostics.py");
const document = await vscode.workspace.openTextDocument(documentUri);
await vscode.window.showTextDocument(document);

let actualDiagnostics = vscode.languages.getDiagnostics(documentUri);
if (actualDiagnostics.length === 0) {
// Wait for diagnostics to be computed
let timeout = 1000;
while (actualDiagnostics.length === 0 && timeout > 0) {
await sleep(100);
actualDiagnostics = vscode.languages.getDiagnostics(documentUri);
timeout -= 100;
}
assert.ok(actualDiagnostics.length > 0, "No diagnostics provided in 1 second");
}

actualDiagnostics = actualDiagnostics.sort((a, b) => {
return a.range.start.compareTo(b.range.start);
});

const expectedDiagnostics = [
{
message: "Import block is un-sorted or un-formatted",
range: toRange(0, 0, 4, 0),
severity: vscode.DiagnosticSeverity.Warning,
},
{
message: "`pathlib.Path` imported but unused",
range: toRange(0, 20, 0, 24),
severity: vscode.DiagnosticSeverity.Warning,
},
{
message: "Undefined name `name`",
range: toRange(5, 35, 5, 39),
severity: vscode.DiagnosticSeverity.Error,
},
];

assert.equal(actualDiagnostics.length, expectedDiagnostics.length);
actualDiagnostics.forEach((actualDiagnostic, i) => {
const expectedDiagnostic = expectedDiagnostics[i];
assert.deepEqual(
new vscode.Diagnostic(
actualDiagnostic.range,
actualDiagnostic.message,
actualDiagnostic.severity,
),
expectedDiagnostic,
);
});
});

test("Should format document", async () => {
await activateExtension();

const docUri = getDocumentUri("formatting.py");
const document = await vscode.workspace.openTextDocument(docUri);
await vscode.window.showTextDocument(document);

const originalContent = document.getText();
const expectedContent = `\
def function(
foo,
bar,
):
print("hello world")
`;
assert.notEqual(originalContent, expectedContent);

await vscode.commands.executeCommand("editor.action.formatDocument");
const formattedContent = document.getText();
assert.equal(formattedContent, expectedContent);
});
});

function toRange(startLine: number, startChar: number, endLine: number, endChar: number) {
const start = new vscode.Position(startLine, startChar);
const end = new vscode.Position(endLine, endChar);
return new vscode.Range(start, end);
}
37 changes: 0 additions & 37 deletions src/test/formatting.test.ts

This file was deleted.

27 changes: 5 additions & 22 deletions src/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,22 @@ import * as path from "path";

const EXTENSION_ID = "charliermarsh.ruff";

export async function initialize(docUri: vscode.Uri) {
const ruffConfiguration = vscode.workspace.getConfiguration("ruff");
await ruffConfiguration.update("importStrategy", "useBundled");
const pythonConfiguration = vscode.workspace.getConfiguration("python");
// Ruff's extension depends on the Python extension which also provides a language server,
// so we need to disable the Python language server to avoid special casing in the tests.
await pythonConfiguration.update("languageServer", "None");
await activateExtension(docUri);
}

async function activateExtension(docUri: vscode.Uri) {
export async function activateExtension() {
const extension = vscode.extensions.getExtension(EXTENSION_ID);
if (extension === undefined) {
throw new Error(`Extension ${EXTENSION_ID} not found`);
}
await extension.activate();
try {
const doc = await vscode.workspace.openTextDocument(docUri);
await vscode.window.showTextDocument(doc);
await sleep(1000); // Wait for server activation
} catch (e) {
console.error(e);
}
}

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

export const getDocPath = (p: string) => {
export const getDocumentPath = (p: string) => {
return path.resolve(__dirname, "../../src/testFixture", p);
};

export const getDocUri = (p: string) => {
return vscode.Uri.file(getDocPath(p));
export const getDocumentUri = (p: string) => {
return vscode.Uri.file(getDocumentPath(p));
};
1 change: 0 additions & 1 deletion src/testFixture/.gitignore

This file was deleted.

12 changes: 12 additions & 0 deletions src/testFixture/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
// Always use the bundled Ruff binary for the tests.
"ruff.importStrategy": "useBundled",

// Ruff's extension depends on the Python extension which also provides a language server,
// so we need to disable the Python language server to avoid special casing in the tests.
"python.languageServer": "None",

"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff"
}
}

0 comments on commit eb4f9dc

Please sign in to comment.