Skip to main content

DnyRun

Struct DnyRun 

Source
pub struct DnyRun {
    pub main_code: String,
    pub deps: String,
    pub is_release: bool,
    /* private fields */
}
Available on crate feature 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

Source

pub fn new(main_code: &str, deps: &str) -> Self

在 target/dyn_tests 下创建以纳秒时间戳 + 随机数命名的临时工程并同步文件。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

§Examples
use lib_unknown::dyntest::DnyRun;

let runner = DnyRun::new("fn main() {}", "");
let res = runner.build(None);
assert!(res.ok);
§Panics
  • 系统时钟早于 UNIX 纪元、无法获取当前目录或工程目录/文件创建失败时 panic。
Source

pub fn release(&mut self, release: bool) -> &mut Self

设置是否以 --release 编译运行。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

Source

pub fn env( &mut self, key: impl Into<String>, value: impl Into<String>, ) -> &mut Self

注入单个子进程环境变量。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

Source

pub fn envs<I, K, V>(&mut self, envs: I) -> &mut Self
where I: IntoIterator<Item = (K, V)>, K: Into<String>, V: Into<String>,

批量注入子进程环境变量。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

Source

pub fn clear_envs(&mut self) -> &mut Self

清空已注入的环境变量。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

Source

pub fn cargo_config(&mut self, config: impl Into<String>) -> &mut Self

设置 .cargo/config.toml 内容并立即同步到工程目录。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

§Panics
  • .cargo 目录或配置文件写入失败时 panic。
Source

pub fn clear_cargo_config(&mut self) -> &mut Self

清除 cargo 配置(内存值置空并删除已写入的配置文件,忽略删除失败)。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

Source

pub fn sync_files(&self)

将当前 Cargo.toml / main.rs / cargo 配置同步到工程目录。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

§Panics
  • 文件写入失败(磁盘不可写、权限不足)时 panic。
Source

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);
Source

pub fn run(&self, timeout: Option<Duration>) -> DnyResult

先编译后直接运行产物;编译失败时直接返回编译结果,不启动进程。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

§Examples
use lib_unknown::dyntest::DnyRun;

let res = DnyRun::new(r#"fn main() { println!("hi"); }"#, "").run(None);
assert!(res.ok);
Source

pub fn bin_path(&self) -> PathBuf

计算当前配置下产物可执行文件的预期路径(不保证文件已存在)。

路径由以下因素决定:

  • 工程目录下的 target 子目录:若 .cargo/config.toml 中配置了 [build] 段下以 target 开头的键(如 target 或 target-dir), 则按配置值拼接实际输出目录;
  • is_release 决定使用 release 还是 debug profile 子目录;
  • 当前运行平台决定可执行文件后缀(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());
Source

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);
Source

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)。

§错误语义
§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);
Source

pub fn reset_main_code(&mut self, main_code: &str)

替换 main.rs 内容并同步到工程目录(供缓存复用时复写任务代码)。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

§Panics
  • 文件写入失败时 panic。
Source

pub fn reset_deps(&mut self, deps: &str)

替换依赖声明并同步到工程目录。

§Feature Requirement

需要同时启用 "dyntest" 与 "std" 特性。

§Panics
  • 文件写入失败时 panic。

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.