Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .config/jp/tools/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ pub async fn run(ctx: Context, t: Tool) -> ToolResult {
match t.name.trim_start_matches("cargo_") {
"check" => cargo_check(&ctx, t.opt("package")?).await,
"expand" => cargo_expand(&ctx, t.req("item")?, t.opt("package")?).await,
"test" => cargo_test(&ctx, t.opt("package")?, t.opt("testname")?).await,
"test" => {
cargo_test(
&ctx,
t.opt("package")?,
t.opt("testname")?,
t.opt("backtrace")?,
)
.await
}
"format" => cargo_format(&ctx, t.opt("package")?).await,
_ => unknown_tool(t),
}
Expand Down
104 changes: 100 additions & 4 deletions .config/jp/tools/src/cargo/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,22 @@ pub(crate) async fn cargo_test(
ctx: &Context,
package: Option<String>,
testname: Option<String>,
backtrace: Option<bool>,
) -> ToolResult {
cargo_test_impl(ctx, package, testname, &DuctProcessRunner)
cargo_test_impl(
ctx,
package,
testname,
backtrace.unwrap_or(false),
&DuctProcessRunner,
)
}

fn cargo_test_impl<R: ProcessRunner>(
ctx: &Context,
package: Option<String>,
testname: Option<String>,
backtrace: bool,
runner: &R,
) -> ToolResult {
let test_name = testname.unwrap_or_default();
Expand All @@ -55,7 +63,7 @@ fn cargo_test_impl<R: ProcessRunner>(
&ctx.root,
&[
("NEXTEST_EXPERIMENTAL_LIBTEST_JSON", "1"),
("RUST_BACKTRACE", "1"),
("RUST_BACKTRACE", if backtrace { "1" } else { "0" }),
],
)?;

Expand Down Expand Up @@ -117,6 +125,9 @@ fn cargo_test_impl<R: ProcessRunner>(

#[cfg(test)]
mod tests {
use std::{io, sync::Mutex};

use camino::Utf8Path;
use camino_tempfile::tempdir;
use jp_tool::Action;

Expand All @@ -134,7 +145,7 @@ mod tests {
let stdout = r#"{"type":"test","event":"ok","name":"my_test","stdout":""}"#;
let runner = MockProcessRunner::success(stdout);

let result = cargo_test_impl(&ctx, None, None, &runner)
let result = cargo_test_impl(&ctx, None, None, false, &runner)
.unwrap()
.into_content()
.unwrap();
Expand All @@ -153,7 +164,7 @@ mod tests {
let stdout = r#"{"type":"test","event":"failed","name":"my_crate$tests::my_test","stdout":"assertion failed"}"#;
let runner = MockProcessRunner::success(stdout);

let result = cargo_test_impl(&ctx, None, None, &runner)
let result = cargo_test_impl(&ctx, None, None, false, &runner)
.unwrap()
.into_content()
.unwrap();
Expand All @@ -173,4 +184,89 @@ mod tests {
</results>
```"});
}

/// A runner that captures the environment variables passed to it, so we can
/// assert on the exact values.
struct EnvCapturingRunner {
inner: MockProcessRunner,
captured_env: Mutex<Vec<(String, String)>>,
}

impl From<MockProcessRunner> for EnvCapturingRunner {
fn from(inner: MockProcessRunner) -> Self {
Self {
inner,
captured_env: Mutex::new(Vec::new()),
}
}
}

impl EnvCapturingRunner {
fn captured_env(&self) -> Vec<(String, String)> {
self.captured_env.lock().unwrap().clone()
}
}

impl ProcessRunner for EnvCapturingRunner {
fn run_with_env_and_stdin(
&self,
program: &str,
args: &[&str],
working_dir: &Utf8Path,
env: &[(&str, &str)],
stdin: Option<&str>,
) -> Result<ProcessOutput, io::Error> {
*self.captured_env.lock().unwrap() = env
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();

self.inner
.run_with_env_and_stdin(program, args, working_dir, env, stdin)
}
}

#[test]
fn test_backtrace_disabled_by_default() {
let dir = tempdir().unwrap();
let ctx = Context {
root: dir.path().to_owned(),
action: Action::Run,
};

let stdout = r#"{"type":"test","event":"ok","name":"my_test","stdout":""}"#;
let runner: EnvCapturingRunner = MockProcessRunner::success(stdout).into();
let _result = cargo_test_impl(&ctx, None, None, false, &runner).unwrap();

assert_eq!(
runner
.captured_env()
.iter()
.find(|(k, _)| k == "RUST_BACKTRACE")
.map(|(_, v)| v.as_str()),
Some("0"),
);
}

#[test]
fn test_backtrace_enabled() {
let dir = tempdir().unwrap();
let ctx = Context {
root: dir.path().to_owned(),
action: Action::Run,
};

let stdout = r#"{"type":"test","event":"ok","name":"my_test","stdout":""}"#;
let runner: EnvCapturingRunner = MockProcessRunner::success(stdout).into();
let _result = cargo_test_impl(&ctx, None, None, true, &runner).unwrap();

assert_eq!(
runner
.captured_env()
.iter()
.find(|(k, _)| k == "RUST_BACKTRACE")
.map(|(_, v)| v.as_str()),
Some("1"),
);
}
}
4 changes: 4 additions & 0 deletions .jp/mcp/tools/cargo/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ type = "string"
[conversation.tools.cargo_test.parameters.testname]
summary = "If specified, only run tests containing this string in their names."
type = "string"

[conversation.tools.cargo_test.parameters.backtrace]
summary = "If true, include Rust backtraces in test failure output. Defaults to false."
type = "boolean"