Skip to content
This repository has been archived by the owner on May 9, 2021. It is now read-only.

Commit

Permalink
feat: allow arguments for main and test binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
hdevalke committed Jan 12, 2020
1 parent a717075 commit 0aa15ff
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status](https://travis-ci.org/hdevalke/rust-test-lens.svg?branch=master)](https://travis-ci.org/hdevalke/rust-test-lens)

A code lens to debug test in rust source files.
A code lens to debug rust tests and binaries.

![test lens](images/test_codelens.png)

Expand All @@ -14,4 +14,8 @@ Depends on the LLDB extension [vscode-lldb](https://marketplace.visualstudio.com

This extension can be enabled/disabled using the following setting:

* `rust_test_lens.enable`: true/false to enable/disable this extension
* `rust-test-lens.enable`: true/false to enable/disable this extension
* `rust-test-lens.args.main`: An array of strings with the application arguments.
* `rust-test-lens.args.tests`: An array of strings with extra arguments.
Defaults to `--no-capture`.
Run `cargo test -- --help` to see all possible options.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,24 @@
],
"configuration": {
"type": "object",
"title": "Rust Test Lens configuration",
"title": "Rust Test Lens",
"properties": {
"rust-test-lens.enabled": {
"type": "boolean",
"default": true,
"description": "Enable or disable this extension"
},
"rust-test-lens.args.main": {
"type": "array",
"default": [],
"description": "Enable or disable this extension"
},
"rust-test-lens.args.tests": {
"type": "array",
"default": [
"--nocapture"
],
"description": "Enable or disable this extension"
}
}
}
Expand Down
21 changes: 14 additions & 7 deletions src/RustCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { basename, dirname } from "path";
export class RustCodeLensProvider implements CodeLensProvider {
constructor(
private _onDidChange: EventEmitter<void>,
private rustTests: RustTests
private rustTests: RustTests,
private main_args: string[],
private test_args: string[]
) {}

get onDidChangeCodeLenses(): Event<void> {
Expand All @@ -35,6 +37,11 @@ export class RustCodeLensProvider implements CodeLensProvider {
return lenses;
}

public update_args(main_args: string[], test_args: string[]) {
this.main_args = main_args;
this.test_args = test_args;
}

private mainMethodLenses(doc: TextDocument): any {
const text = doc.getText();
const reFnMain = /fn\s+(main)\s*\(\s*\)/g;
Expand Down Expand Up @@ -90,10 +97,10 @@ export class RustCodeLensProvider implements CodeLensProvider {
): DebugConfiguration | undefined {
const pkg = this.rustTests.getPackage(fn, doc.uri);
if (pkg) {
const args =
fn === "main"
? ["build", `--package=${pkg.name}`]
: ["test", "--no-run", `--package=${pkg.name}`];
const is_main = fn === "main";
const args = is_main
? ["build", `--package=${pkg.name}`]
: ["test", "--no-run", `--package=${pkg.name}`];

const bin = this.rustTests.getBin(doc.fileName, pkg);
const filter = this.rustTests.getFilter(doc.fileName, pkg, bin);
Expand All @@ -106,7 +113,7 @@ export class RustCodeLensProvider implements CodeLensProvider {
}

args.push(`--manifest-path=${pkg.manifest_path}`);

const extra_args = is_main ? this.main_args : [fn, ...this.test_args];
return {
type: "lldb",
request: "launch",
Expand All @@ -115,7 +122,7 @@ export class RustCodeLensProvider implements CodeLensProvider {
args: args,
filter: filter
},
args: [fn],
args: extra_args,
cwd: `${dirname(pkg.manifest_path)}`
};
}
Expand Down
46 changes: 30 additions & 16 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
debug,
OutputChannel,
workspace,
WorkspaceFolder
WorkspaceFolder,
Disposable
} from "vscode";
import { RustCodeLensProvider } from "./RustCodeLensProvider";
import { RustTests } from "./RustTests";
Expand Down Expand Up @@ -36,27 +37,30 @@ export async function activate(context: ExtensionContext) {
}
if (metaMap.size !== 0) {
const rustTests: RustTests = new RustTests(metaMap);
const main_args = config.get("args.main", []);
const test_args = config.get("args.tests", ["--nocapture"]);
const codeLensProvider = new RustCodeLensProvider(
onDidChange,
rustTests
rustTests,
main_args,
test_args
);
context.subscriptions.push(
languages.registerCodeLensProvider(
{ scheme: "file", language: "rust" },
codeLensProvider
)
const disposable = languages.registerCodeLensProvider(
{ scheme: "file", language: "rust" },
codeLensProvider
);
let disposable = commands.registerCommand(
"extension.debugTest",
debugConfig => {
const json = JSON.stringify(debugConfig, null, 2);
outputChannel.appendLine(`Debugging: ${json}`);
debug
.startDebugging(undefined, debugConfig)
.then(r => console.log("Result", r));
}

context.subscriptions.push(
workspace.onDidChangeConfiguration(e => {
const config = workspace.getConfiguration("rust-test-lens");
const main_args: string[] = config.get("args.main", []);
const test_args = config.get("args.tests", ["--nocapture"]);
codeLensProvider.update_args(main_args, test_args);
})
);

context.subscriptions.push(disposable);
context.subscriptions.push(registerCmdDebugTest());
}
} else {
outputChannel.append(
Expand All @@ -66,5 +70,15 @@ export async function activate(context: ExtensionContext) {
}
}

function registerCmdDebugTest(): Disposable {
return commands.registerCommand("extension.debugTest", debugConfig => {
const json = JSON.stringify(debugConfig, null, 2);
outputChannel.appendLine(`Debugging: ${json}`);
debug
.startDebugging(undefined, debugConfig)
.then(r => console.log("Result", r));
});
}

// this method is called when your extension is deactivated
export function deactivate() {}

0 comments on commit 0aa15ff

Please sign in to comment.