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

Commit

Permalink
solve problem with multiple binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
hdevalke committed Dec 30, 2018
1 parent 30be073 commit 56299d6
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 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.0.4

- Works now if the package contains multiple binaries.

## 0.0.3

- Fix issue with workspace containing a root crate [#1](https://github.com/hdevalke/rust-test-lens/issues/1)
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"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.0.3",
"version": "0.0.4",
"publisher": "hdevalke",
"engines": {
"vscode": "^1.25.0"
"vscode": "^1.30.1"
},
"categories": [
"Other",
Expand Down Expand Up @@ -35,12 +35,12 @@
"test": "npm run compile && node ./node_modules/vscode/bin/test"
},
"devDependencies": {
"@types/mocha": "^5.2.4",
"@types/node": "^10.5.2",
"tslint": "^5.10.0",
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.18",
"tslint": "^5.12.0",
"typescript": "^2.9.2",
"vsce": "^1.44.0",
"vscode": "^1.1.18"
"vsce": "^1.54.0",
"vscode": "^1.1.26"
},
"extensionDependencies": [
"vadimcn.vscode-lldb"
Expand Down
13 changes: 9 additions & 4 deletions src/RustCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,25 @@ export class RustCodeLensProvider implements CodeLensProvider {
createDebugConfig(fn: string, uri: string): DebugConfiguration | undefined {
const pkg = this.rustTests.getPackage(fn, uri);
if (pkg) {
return {
const debugConfig = {
type: "lldb",
request: "launch",
name: `Debug ${fn} in ${basename(uri)}`,
cargo: {
"args": [
args: [
"test",
"--no-run",
`--package=${pkg.name}`
]
},
"args": [fn],
"cwd": "${workspaceFolder}"
args: [fn],
cwd: "${workspaceFolder}"
};
const bin = this.rustTests.getBin(uri, pkg);
if (bin !== undefined) {
debugConfig.cargo.args.push(`--bin=${bin}`);
}
return debugConfig;
}
}
}
20 changes: 19 additions & 1 deletion src/RustTests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
import { Metadata, Package } from "./cargo";
import { dirname } from "path";
import { basename, dirname } from "path";

export class RustTests {
private readonly testMap: Map<string, Package> = new Map<string, Package>();
Expand Down Expand Up @@ -31,4 +31,22 @@ 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);
}
}
}
}
return undefined;
}
}
13 changes: 10 additions & 3 deletions src/cargo.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
'use strict';
// Helper function and interfaces to work with cargo metadata.
import { workspace } from "vscode";
import { spawn } from "child_process";
import { spawn, SpawnOptions } from "child_process";

export interface Target {
name: string;
src_path: string;
}

export interface Package {
name: string;
authors: string[];
version: string;
manifest_path: string;
targets: Target[];
}

export interface Metadata {
Expand All @@ -21,7 +27,7 @@ export interface Metadata {
type StrSink = (data: string) => void;

export async function metadata(onStdErr?: StrSink,
retry = false): Promise<Metadata> {
retry = false): Promise<Metadata> {
let meta = "";
const cargoArgs = [
"metadata",
Expand All @@ -47,10 +53,11 @@ async function runCargo(args?: ReadonlyArray<string>, onStdOut?: StrSink,
onStdErr?: StrSink): Promise<number> {
return new Promise<number>((resolve, reject) => {
const workspaceFolders = workspace.workspaceFolders;
const options = {
const options: SpawnOptions = {
cwd: workspaceFolders ? workspaceFolders[0].uri.fsPath : undefined,
stdio: ['ignore', 'pipe', 'pipe'],
};

const proc = spawn("cargo", args, options);
proc.on('error',
err => {
Expand Down
3 changes: 3 additions & 0 deletions src/test/RustTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ suite("RustTests Tests", function () {
],
version: "0.1.2",
manifest_path: "./root_crate/other_sub_crate/Cargo.toml",
targets: []
}, {
name: "root_crate",
authors: [
"author"
],
version: "0.1.2",
manifest_path: "./root_crate/Cargo.toml",
targets: []
}, {
name: "sub_crate",
authors: [
"author"
],
version: "0.1.2",
manifest_path: "./root_crate/sub_crate/Cargo.toml",
targets: []
}],
target_directory: "target",
version: 1,
Expand Down

0 comments on commit 56299d6

Please sign in to comment.