2023-11-25 23:14:38 +01:00
|
|
|
use std::{path::PathBuf};
|
2023-08-12 19:50:30 +02:00
|
|
|
|
|
|
|
use crate::{build::RustVersion, source::RustSource};
|
|
|
|
|
|
|
|
pub struct RustTest {
|
2023-10-21 12:45:33 +02:00
|
|
|
client: dagger_sdk::Query,
|
2023-08-12 19:50:30 +02:00
|
|
|
registry: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RustTest {
|
2023-10-21 12:45:33 +02:00
|
|
|
pub fn new(client: dagger_sdk::Query) -> Self {
|
2023-08-12 19:50:30 +02:00
|
|
|
Self {
|
|
|
|
client,
|
|
|
|
registry: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn test(
|
|
|
|
&self,
|
|
|
|
source_path: Option<impl Into<PathBuf>>,
|
|
|
|
rust_version: impl AsRef<RustVersion>,
|
|
|
|
crates: &[&str],
|
|
|
|
extra_deps: &[&str],
|
|
|
|
) -> eyre::Result<()> {
|
|
|
|
let rust_version = rust_version.as_ref();
|
|
|
|
let source_path = source_path.map(|s| s.into());
|
|
|
|
let source = source_path.clone().unwrap_or(PathBuf::from("."));
|
|
|
|
|
|
|
|
let rust_source = RustSource::new(self.client.clone());
|
|
|
|
let (src, dep_src) = rust_source
|
|
|
|
.get_rust_src(source_path, crates.to_vec())
|
|
|
|
.await?;
|
|
|
|
let mut deps = vec!["apt", "install", "-y"];
|
|
|
|
deps.extend(extra_deps);
|
|
|
|
|
|
|
|
let rust_build_image = self
|
|
|
|
.client
|
|
|
|
.container()
|
|
|
|
.from(rust_version.to_string())
|
|
|
|
.with_exec(vec!["apt", "update"])
|
|
|
|
.with_exec(deps);
|
|
|
|
|
|
|
|
let target_cache = self.client.cache_volume(format!("rust_target_test",));
|
|
|
|
|
|
|
|
let build_options = vec!["cargo", "build", "--workspace"];
|
|
|
|
let rust_prebuild = rust_build_image
|
|
|
|
.with_workdir("/mnt/src")
|
2023-10-21 12:45:33 +02:00
|
|
|
.with_directory("/mnt/src", dep_src)
|
2023-08-12 19:50:30 +02:00
|
|
|
.with_exec(build_options)
|
2023-10-21 12:45:33 +02:00
|
|
|
.with_mounted_cache("/mnt/src/target/", target_cache);
|
2023-08-12 19:50:30 +02:00
|
|
|
|
|
|
|
let incremental_dir = rust_source
|
|
|
|
.get_rust_target_src(&source, rust_prebuild.clone(), crates.to_vec())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let rust_with_src = rust_build_image
|
|
|
|
.with_workdir("/mnt/src")
|
|
|
|
.with_directory(
|
|
|
|
"/usr/local/cargo",
|
2023-10-21 12:45:33 +02:00
|
|
|
rust_prebuild.directory("/usr/local/cargo"),
|
2023-08-12 19:50:30 +02:00
|
|
|
)
|
2023-10-21 12:45:33 +02:00
|
|
|
.with_directory("/mnt/src/target", incremental_dir)
|
|
|
|
.with_directory("/mnt/src/", src);
|
2023-08-12 19:50:30 +02:00
|
|
|
|
|
|
|
let test = rust_with_src.with_exec(vec!["cargo", "test"]);
|
|
|
|
|
|
|
|
let stdout = test.stdout().await?;
|
|
|
|
let stderr = test.stderr().await?;
|
|
|
|
println!("stdout: {}, stderr: {}", stdout, stderr);
|
|
|
|
|
2023-10-21 12:45:33 +02:00
|
|
|
test.sync().await?;
|
2023-08-12 19:50:30 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|