mod cache_miss;
mod clean;
mod config_rule;
mod db_has_function_ranges;
mod diff_collect;
mod dirty;
mod drift;
mod duplicate_target_names;
mod fingerprint;
mod lib_bin_collision;
mod narrowing;
mod new_test;
mod no_profraw_leak;
mod run;
mod structural;
mod workspace;
use std::path::Path;
use std::process::{Command, Output};
pub fn cargo_affected(dir: &Path, args: &[&str]) -> Output {
let bin = env!("CARGO_BIN_EXE_cargo-affected");
Command::new(bin)
.args(args)
.current_dir(dir)
.output()
.unwrap_or_else(|e| panic!("failed to run cargo-affected: {e}"))
}
pub fn git(dir: &Path, args: &[&str]) {
let output = Command::new("git")
.args(args)
.current_dir(dir)
.output()
.unwrap_or_else(|e| panic!("failed to run git {}: {e}", args.join(" ")));
assert!(
output.status.success(),
"git {} failed: {}",
args.join(" "),
String::from_utf8_lossy(&output.stderr)
);
}
pub fn combined_output(out: &Output) -> String {
format!(
"{}{}",
String::from_utf8_lossy(&out.stderr),
String::from_utf8_lossy(&out.stdout)
)
}
pub fn git_head(dir: &Path) -> String {
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.current_dir(dir)
.output()
.expect("failed to run git rev-parse");
assert!(output.status.success(), "git rev-parse HEAD failed");
String::from_utf8_lossy(&output.stdout).trim().to_string()
}
pub fn replace_in_file(path: &Path, from: &str, to: &str) {
let content = std::fs::read_to_string(path).unwrap();
assert!(
content.contains(from),
"expected to find {from:?} in {} so the edit lands on the right line",
path.display()
);
std::fs::write(path, content.replace(from, to)).unwrap();
}
pub fn init_git_with_initial_commit(dir: &Path) {
git(dir, &["init", "-q", "-b", "main"]);
git(dir, &["config", "user.email", "test@example.com"]);
git(dir, &["config", "user.name", "Test"]);
git(dir, &["config", "core.autocrlf", "false"]);
git(dir, &["add", "."]);
git(dir, &["commit", "-q", "-m", "initial"]);
}
pub fn write_two_module_project(dir: &Path, crate_name: &str) {
std::fs::write(
dir.join("Cargo.toml"),
format!(
r#"[package]
name = "{crate_name}"
version = "0.1.0"
edition = "2021"
"#
),
)
.unwrap();
std::fs::write(dir.join(".gitignore"), "/target\n/Cargo.lock\n").unwrap();
let src = dir.join("src");
std::fs::create_dir_all(&src).unwrap();
std::fs::write(
src.join("lib.rs"),
"pub mod math;\npub mod strings;\n",
)
.unwrap();
std::fs::write(
src.join("math.rs"),
r#"pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub struct Counter {
pub n: i32,
}
pub fn multiply(a: i32, b: i32) -> i32 {
a * b
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add() {
assert_eq!(add(2, 3), 5);
}
#[test]
fn test_multiply() {
assert_eq!(multiply(3, 4), 12);
}
}
"#,
)
.unwrap();
std::fs::write(
src.join("strings.rs"),
r#"pub fn greet(name: &str) -> String {
format!("hello, {name}")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_greet() {
assert_eq!(greet("world"), "hello, world");
}
}
"#,
)
.unwrap();
}