Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stacktrace option to test runner output #471

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@
"default": false,
"description": "Run namespace tests when opening a new file and on file save"
},
"calva.outputStackTraceOnTest": {
"type": "boolean",
"default": false,
"description": "Prints exception stacktraces in the test runner output"
},
"calva.syncReplNamespaceToCurrentFile": {
"type": "boolean",
"default": false,
Expand Down
15 changes: 14 additions & 1 deletion src/nrepl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,19 @@ export class NReplSession {
})
}

testStacktrace(ns: string, test: string, index: number) {
return new Promise<any>((resolve, reject) => {
let id = this.client.nextId;
this.messageHandlers[id] = (msg) => {
resolve(msg);
return true;
}
this.client.write({
op: "test-stacktrace", id, session: this.sessionId, ns, "var": test, "index": index
});
});
}

testNs(ns: string) {
return new Promise<any>((resolve, reject) => {
let id = this.client.nextId;
Expand Down Expand Up @@ -762,4 +775,4 @@ export class NReplEvaluation {
});
return (num);
}
}
}
1 change: 1 addition & 0 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function config() {
format: configOptions.get("formatOnSave"),
evaluate: configOptions.get("evalOnSave"),
test: configOptions.get("testOnSave"),
outputStackTraceOnTest: configOptions.get("outputStackTraceOnTest") as boolean,
syncReplNamespaceToCurrentFile: configOptions.get("syncReplNamespaceToCurrentFile"),
jackInEnv: configOptions.get("jackInEnv"),
openBrowserWhenFigwheelStarted: configOptions.get("openBrowserWhenFigwheelStarted") as boolean,
Expand Down
34 changes: 27 additions & 7 deletions src/testRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { disabledPrettyPrinter } from './printer';

let diagnosticCollection = vscode.languages.createDiagnosticCollection('calva');

function reportTests(results, errorStr, log = true) {
async function reportTests(results, errorStr, client, log = true) {
let chan = state.outputChannel(),
diagnostics = {},
total_summary: { test, error, ns, var, fail } = { test: 0, error: 0, ns: 0, var: 0, fail: 0 };
Expand All @@ -31,8 +31,13 @@ function reportTests(results, errorStr, log = true) {
msg.push(resultItem.message);
return `${msg.join(": ")}${(msg.length > 0 ? "\n" : "")}`;
}
if (a.type == "error" && log)
chan.appendLine(`ERROR in ${ns}/${test} (line ${a.line}):\n${resultMessage(a)} error: ${a.error} (${a.file})\nexpected: ${a.expected}`);
if (a.type == "error" && log) {
chan.appendLine(`ERROR in ${ns}/${test} (line ${a.line}):\n${resultMessage(a)}expected: ${a.expected} error: ${a.error} (${a.file})`);
if(state.config().outputStackTraceOnTest) {
const exception = await client.testStacktrace(ns, test, a.index);
chan.appendLine(formatException(exception));
}
}
if (a.type == "fail") {
let msg = `failure in test: ${test} context: ${a.context}, expected ${a.expected}, got: ${a.actual}`,
err = new vscode.Diagnostic(new vscode.Range(a.line - 1, 0, a.line - 1, 1000), msg, vscode.DiagnosticSeverity.Error);
Expand Down Expand Up @@ -82,11 +87,26 @@ function reportTests(results, errorStr, log = true) {
}
}

function formatException(ex) {
const basePad = 10;
const padStart = (s, n) => {
const l = s.toString().length;
return `${" ".repeat(n - l)}${s}`;
}
const maxLine = Math.max(...ex.stacktrace.map(s => s.line.toString().length));
const fileLine = (s) => `${s.file}:${padStart(s.line, maxLine)}`;
const maxPad = Math.max(...ex.stacktrace.map(s => fileLine(s).length));
return ex
.stacktrace
.map(s => `${padStart(fileLine(s), maxPad + basePad)} ${s.name}`)
.join("\n");
}

// FIXME: use cljs session where necessary
async function runAllTests(document = {}) {
let client = util.getSession(util.getFileType(document));
state.outputChannel().appendLine("Running all project tests…");
reportTests([await client.testAll()], "Running all tests");
await reportTests([await client.testAll()], "Running all tests", client);
}

function runAllTestsCommand() {
Expand Down Expand Up @@ -121,7 +141,7 @@ function runNamespaceTests(document = {}) {
if (nss.length > 1)
resultPromises.push(client.testNs(nss[1]));
let results = await Promise.all(resultPromises);
reportTests(results, "Running tests");
await reportTests(results, "Running tests", client);
}, disabledPrettyPrinter).catch(() => {});
}

Expand All @@ -134,7 +154,7 @@ async function runTestUnderCursor() {
evaluate.loadFile(doc, async () => {
state.outputChannel().appendLine(`Running test: ${test}…`);
const results = [await client.test(ns, [test])];
reportTests(results, `Running test: ${test}`);
await reportTests(results, `Running test: ${test}`, client);
}, disabledPrettyPrinter).catch(() => {});
}

Expand All @@ -152,7 +172,7 @@ function rerunTests(document = {}) {
let client = util.getSession(util.getFileType(document))
evaluate.loadFile({}, async () => {
state.outputChannel().appendLine("Running previously failed tests…");
reportTests([await client.retest()], "Retesting");
reportTests([await client.retest()], "Retesting", client);
}, disabledPrettyPrinter).catch(() => {});
}

Expand Down