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

Commit 1fd559a

Browse files
committed
Fixes issue #3 where tests in binary crate were not launched.
1 parent 2277c68 commit 1fd559a

5 files changed

+54
-33
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to the "rust-test-lens" extension will be documented in this
44

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

7+
## 0.1.1
8+
9+
- Fixes issue [#3](https://github.com/hdevalke/rust-test-lens/issues/3) where tests in binary crate were not launched.
10+
711
## 0.1.0
812

913
- Adds a Debug codelens to the main function in binaries and examples.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "rust-test-lens",
33
"displayName": "Rust Test Lens",
44
"description": "Adds a code lens to quickly run or debug a single test for your Rust code.",
5-
"version": "0.1.0",
5+
"version": "0.1.1",
66
"publisher": "hdevalke",
77
"engines": {
88
"vscode": "^1.30.2"

src/RustCodeLensProvider.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,28 @@ export class RustCodeLensProvider implements CodeLensProvider {
8989
"--no-run",
9090
`--package=${pkg.name}`
9191
];
92-
const debugConfig = {
92+
93+
const bin = this.rustTests.getBin(uri, pkg);
94+
const filter = this.rustTests.getFilter(uri, pkg, bin);
95+
96+
if (bin !== undefined && filter.kind === "bin") {
97+
args.push(`--bin=${bin}`);
98+
}
99+
if (filter.kind === "example") {
100+
args.push(`--example=${bin}`);
101+
}
102+
103+
return {
93104
type: "lldb",
94105
request: "launch",
95106
name: `Debug ${fn} in ${basename(uri)}`,
96107
cargo: {
97108
args: args,
98-
filter: {} as any,
109+
filter: filter,
99110
},
100111
args: [fn],
101112
cwd: `${dirname(pkg.manifest_path)}`
102113
};
103-
const bin = this.rustTests.getBin(uri, pkg);
104-
if (bin !== undefined) {
105-
debugConfig.cargo.args.push(`--bin=${bin}`);
106-
}
107-
const kind = this.rustTests.getKind(uri, pkg);
108-
if (kind !== undefined) {
109-
debugConfig.cargo.filter.kind = kind;
110-
}
111-
112-
return debugConfig;
113114
}
114115
}
115116
}

src/RustTests.ts

+34-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
import { Metadata, Package } from "./cargo";
33
import { basename, dirname } from "path";
44

5+
export interface Filter {
6+
kind: undefined | string;
7+
name: undefined | string;
8+
}
9+
510
export class RustTests {
611
private readonly testMap: Map<string, Package> = new Map<string, Package>();
712
constructor(private metadata: Metadata) {
@@ -32,38 +37,47 @@ export class RustTests {
3237
return pkg;
3338
}
3439

35-
getBin(uri: string, pkg: Package): String | undefined {
36-
const name = basename(uri, '.rs');
37-
if (name === "main") {
38-
return pkg.name;
39-
} else {
40-
for (const target of pkg.targets) {
41-
if (name === target.name) {
42-
if (uri.indexOf("/bin/") !== -1) {
43-
return name;
44-
} else if ("main" === name) {
45-
return dirname(pkg.manifest_path);
46-
}
47-
}
40+
getBin(uri: string, pkg: Package): string | undefined {
41+
let main = undefined;
42+
for (const target of pkg.targets) {
43+
const source_name = basename(target.src_path, '.rs');
44+
if (source_name === 'main') {
45+
main = target.name;
46+
}
47+
if (uri === target.src_path) {
48+
return target.name;
4849
}
4950
}
50-
return undefined;
51+
return main;
5152
}
5253

5354
/// Get the kind of the target to select.
5455
/// For example
55-
getKind(uri: string, pkg: Package) : String {
56+
getFilter(uri: string, pkg: Package, bin: string | undefined): Filter {
5657
const targets = pkg.targets;
58+
let target = undefined;
5759
// fast path
5860
if (targets.length === 1) {
59-
return targets[0].kind[0];
61+
target = targets[0];
6062
}
6163
// slow path
62-
for( const target of pkg.targets) {
63-
if (target.src_path === uri) {
64-
return target.kind[0];
64+
for (const t of pkg.targets) {
65+
if (t.src_path === uri) {
66+
target = t;
67+
break;
6568
}
6669
}
67-
return "lib";
70+
let kind = undefined;
71+
let name = undefined;
72+
if (target === undefined) {
73+
kind = bin === undefined ? "lib" : "bin";
74+
} else {
75+
kind = target.kind[0];
76+
name = kind === "test" ? target.name : undefined;
77+
}
78+
return {
79+
kind: kind,
80+
name: name,
81+
};
6882
}
6983
}

src/extension.ts

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export function activate(context: ExtensionContext) {
2828

2929
let disposable = commands.registerCommand('extension.debugTest',
3030
(debugConfig) => {
31+
const dbgConfigJson = JSON.stringify(debugConfig, null, 2);
32+
outputChannel.appendLine(`Debugging: ${dbgConfigJson}`);
3133
debug.startDebugging(undefined, debugConfig)
3234
.then((r) => console.log("Result", r));
3335
});

0 commit comments

Comments
 (0)