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

Commit

Permalink
Fixes issue #3 where tests in binary crate were not launched.
Browse files Browse the repository at this point in the history
  • Loading branch information
hdevalke committed Mar 25, 2019
1 parent 2277c68 commit 1fd559a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the "rust-test-lens" extension will be documented in this

Check [Keep a Changelog](https://keepachangelog.com/) for recommendations on how to structure this file.

## 0.1.1

- Fixes issue [#3](https://github.com/hdevalke/rust-test-lens/issues/3) where tests in binary crate were not launched.

## 0.1.0

- Adds a Debug codelens to the main function in binaries and examples.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "rust-test-lens",
"displayName": "Rust Test Lens",
"description": "Adds a code lens to quickly run or debug a single test for your Rust code.",
"version": "0.1.0",
"version": "0.1.1",
"publisher": "hdevalke",
"engines": {
"vscode": "^1.30.2"
Expand Down
25 changes: 13 additions & 12 deletions src/RustCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,28 @@ export class RustCodeLensProvider implements CodeLensProvider {
"--no-run",
`--package=${pkg.name}`
];
const debugConfig = {

const bin = this.rustTests.getBin(uri, pkg);
const filter = this.rustTests.getFilter(uri, pkg, bin);

if (bin !== undefined && filter.kind === "bin") {
args.push(`--bin=${bin}`);
}
if (filter.kind === "example") {
args.push(`--example=${bin}`);
}

return {
type: "lldb",
request: "launch",
name: `Debug ${fn} in ${basename(uri)}`,
cargo: {
args: args,
filter: {} as any,
filter: filter,
},
args: [fn],
cwd: `${dirname(pkg.manifest_path)}`
};
const bin = this.rustTests.getBin(uri, pkg);
if (bin !== undefined) {
debugConfig.cargo.args.push(`--bin=${bin}`);
}
const kind = this.rustTests.getKind(uri, pkg);
if (kind !== undefined) {
debugConfig.cargo.filter.kind = kind;
}

return debugConfig;
}
}
}
54 changes: 34 additions & 20 deletions src/RustTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import { Metadata, Package } from "./cargo";
import { basename, dirname } from "path";

export interface Filter {
kind: undefined | string;
name: undefined | string;
}

export class RustTests {
private readonly testMap: Map<string, Package> = new Map<string, Package>();
constructor(private metadata: Metadata) {
Expand Down Expand Up @@ -32,38 +37,47 @@ export class RustTests {
return pkg;
}

getBin(uri: string, pkg: Package): String | undefined {
const name = basename(uri, '.rs');
if (name === "main") {
return pkg.name;
} else {
for (const target of pkg.targets) {
if (name === target.name) {
if (uri.indexOf("/bin/") !== -1) {
return name;
} else if ("main" === name) {
return dirname(pkg.manifest_path);
}
}
getBin(uri: string, pkg: Package): string | undefined {
let main = undefined;
for (const target of pkg.targets) {
const source_name = basename(target.src_path, '.rs');
if (source_name === 'main') {
main = target.name;
}
if (uri === target.src_path) {
return target.name;
}
}
return undefined;
return main;
}

/// Get the kind of the target to select.
/// For example
getKind(uri: string, pkg: Package) : String {
getFilter(uri: string, pkg: Package, bin: string | undefined): Filter {
const targets = pkg.targets;
let target = undefined;
// fast path
if (targets.length === 1) {
return targets[0].kind[0];
target = targets[0];
}
// slow path
for( const target of pkg.targets) {
if (target.src_path === uri) {
return target.kind[0];
for (const t of pkg.targets) {
if (t.src_path === uri) {
target = t;
break;
}
}
return "lib";
let kind = undefined;
let name = undefined;
if (target === undefined) {
kind = bin === undefined ? "lib" : "bin";
} else {
kind = target.kind[0];
name = kind === "test" ? target.name : undefined;
}
return {
kind: kind,
name: name,
};
}
}
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export function activate(context: ExtensionContext) {

let disposable = commands.registerCommand('extension.debugTest',
(debugConfig) => {
const dbgConfigJson = JSON.stringify(debugConfig, null, 2);
outputChannel.appendLine(`Debugging: ${dbgConfigJson}`);
debug.startDebugging(undefined, debugConfig)
.then((r) => console.log("Result", r));
});
Expand Down

0 comments on commit 1fd559a

Please sign in to comment.