diff --git a/CHANGELOG.md b/CHANGELOG.md index c71dce8..61bcb54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/package.json b/package.json index 409945c..b57af65 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" diff --git a/src/RustCodeLensProvider.ts b/src/RustCodeLensProvider.ts index bfeda80..fe4b0eb 100644 --- a/src/RustCodeLensProvider.ts +++ b/src/RustCodeLensProvider.ts @@ -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; } } } \ No newline at end of file diff --git a/src/RustTests.ts b/src/RustTests.ts index 4367db5..d0c969f 100644 --- a/src/RustTests.ts +++ b/src/RustTests.ts @@ -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 = new Map(); @@ -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; + } } diff --git a/src/cargo.ts b/src/cargo.ts index 83b3828..d17fd66 100644 --- a/src/cargo.ts +++ b/src/cargo.ts @@ -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 { @@ -21,7 +27,7 @@ export interface Metadata { type StrSink = (data: string) => void; export async function metadata(onStdErr?: StrSink, - retry = false): Promise { + retry = false): Promise { let meta = ""; const cargoArgs = [ "metadata", @@ -47,10 +53,11 @@ async function runCargo(args?: ReadonlyArray, onStdOut?: StrSink, onStdErr?: StrSink): Promise { return new Promise((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 => { diff --git a/src/test/RustTests.test.ts b/src/test/RustTests.test.ts index 0cc1650..f7cf8b4 100644 --- a/src/test/RustTests.test.ts +++ b/src/test/RustTests.test.ts @@ -13,6 +13,7 @@ suite("RustTests Tests", function () { ], version: "0.1.2", manifest_path: "./root_crate/other_sub_crate/Cargo.toml", + targets: [] }, { name: "root_crate", authors: [ @@ -20,6 +21,7 @@ suite("RustTests Tests", function () { ], version: "0.1.2", manifest_path: "./root_crate/Cargo.toml", + targets: [] }, { name: "sub_crate", authors: [ @@ -27,6 +29,7 @@ suite("RustTests Tests", function () { ], version: "0.1.2", manifest_path: "./root_crate/sub_crate/Cargo.toml", + targets: [] }], target_directory: "target", version: 1,