pub struct DnyRun {
pub main_code: String,
pub deps: String,
pub is_release: bool,
/* private fields */
}dyntest only.Expand description
单个临时 Cargo 工程的句柄:在 target/dyn_tests/dny_<id>/ 下生成工程并编译执行,Drop 时释放心跳锁。
§Feature Requirement
需要同时启用 "dyntest" 与 "std" 特性。
Fields§
§main_code: String待编译运行的 main.rs 源码。
deps: String[dependencies] 依赖声明片段。
is_release: bool是否以 --release 编译运行。
Implementations§
Source§impl DnyRun
impl DnyRun
Sourcepub fn clear_envs(&mut self) -> &mut Self
pub fn clear_envs(&mut self) -> &mut Self
Sourcepub fn cargo_config(&mut self, config: impl Into<String>) -> &mut Self
pub fn cargo_config(&mut self, config: impl Into<String>) -> &mut Self
Sourcepub fn clear_cargo_config(&mut self) -> &mut Self
pub fn clear_cargo_config(&mut self) -> &mut Self
Sourcepub fn sync_files(&self)
pub fn sync_files(&self)
Sourcepub fn build(&self, timeout: Option<Duration>) -> DnyResult
pub fn build(&self, timeout: Option<Duration>) -> DnyResult
执行 cargo build(is_release 为真时加 --release),超时则终止并记为 SystemError::Timeout;自身不 panic,失败均体现在返回的 DnyResult 中。
§Feature Requirement
需要同时启用 "dyntest" 与 "std" 特性。
§Examples
use lib_unknown::dyntest::DnyRun;
let res = DnyRun::new("fn main() {}", "").build(None);
assert!(res.ok);Sourcepub fn bin_path(&self) -> PathBuf
pub fn bin_path(&self) -> PathBuf
计算当前配置下产物可执行文件的预期路径(不保证文件已存在)。
路径由以下因素决定:
- 工程目录下的
target子目录:若.cargo/config.toml中配置了[build]段下以target开头的键(如target或target-dir), 则按配置值拼接实际输出目录; is_release决定使用release还是debugprofile 子目录;- 当前运行平台决定可执行文件后缀(Windows 为
.exe,否则无后缀)。
§Feature Requirement
需要同时启用 "dyntest" 与 "std" 特性。
§Examples
use lib_unknown::dyntest::DnyRun;
let runner = DnyRun::new("fn main() {}", "");
let build_res = runner.build(None);
assert!(build_res.ok);
let path = runner.bin_path();
assert!(path.exists(), "产物路径应在编译成功后存在: {}", path.display());Sourcepub fn run_no_build(
&self,
timeout: Option<Duration>,
build_result: Option<DnyResult>,
) -> DnyResult
pub fn run_no_build( &self, timeout: Option<Duration>, build_result: Option<DnyResult>, ) -> DnyResult
不重新编译,直接运行已有产物(产物缺失时返回 SystemError::ProcessSpawnFailed 结果)。
build_result 用于透传编译耗时,无则置零。
§Feature Requirement
需要同时启用 "dyntest" 与 "std" 特性。
§Examples
use lib_unknown::dyntest::DnyRun;
let runner = DnyRun::new("fn main() {}", "");
let build_res = runner.build(None);
assert!(build_res.ok);
let res = runner.run_no_build(None, None);
assert!(res.ok);Sourcepub fn cargo(&self, args: &[&str], timeout: Option<Duration>) -> DnyResult
pub fn cargo(&self, args: &[&str], timeout: Option<Duration>) -> DnyResult
在工程目录下执行自定义 cargo <args> 命令(如 cargo test、cargo check、
cargo clippy 等),超时则终止并记为 SystemError::Timeout;
自身不 panic,失败均体现在返回的 DnyResult 中。
耗时记为 DnyResult::run_duration,DnyResult::build_duration 恒为 Duration::ZERO。
§Note: 不透传 is_release
与 build 不同,本方法不会根据 is_release 自动追加
--release,args 将原样透传给 cargo。需要 release 行为时请调用者显式传入:
runner.cargo(&["test", "--release"], None)。
§错误语义
cargo进程启动失败 →SystemError::ProcessSpawnFailed;- 超时被终止 →
SystemError::Timeout; - 正常退出但退出码非零(如
check/test未通过)→SystemError::CargoFailed, 此时DnyResult::ok为false,stdout/stderr/exit_code保留子进程输出供诊断; - 退出码为 0 →
ok == true,system_err == SystemError::None。
§Feature Requirement
需要同时启用 "dyntest" 与 "std" 特性。
§Examples
use lib_unknown::dyntest::DnyRun;
let runner = DnyRun::new("fn main() {}", "");
let res = runner.cargo(&["check"], None);
assert!(res.ok);
// release 行为不会自动追加,需显式传入:
let res = runner.cargo(&["test", "--release"], None);
assert!(res.ok);