#![allow(dead_code, unused_imports)]
#![allow(clippy::expect_used, clippy::unwrap_used)]
use std::fs;
use std::path::{Path, PathBuf};
use tempfile::TempDir;
use voom::catalog::{Artifact, Ecosystem};
use voom::config::resolve::Flags;
use voom::run::RunOptions;
pub use voom::testing::{build, snapshot, tree};
#[must_use]
pub fn options(root: &Path) -> RunOptions {
RunOptions {
roots: vec![root.to_path_buf()],
dry_run: false,
jobs: Some(2),
one_file_system: true,
force: false,
caches: false,
verbose: true,
config: None,
progress: false,
flags: Flags::default(),
}
}
#[must_use]
pub fn options_forcing(root: &Path) -> RunOptions {
RunOptions {
force: true,
..options(root)
}
}
#[must_use]
pub fn options_enabling(root: &Path, enable: Vec<String>) -> RunOptions {
RunOptions {
flags: Flags {
enable,
..Flags::default()
},
..options(root)
}
}
#[must_use]
pub fn concrete(pattern: &str) -> String {
pattern.trim_end_matches('/').replace('*', "example")
}
#[must_use]
pub fn artifact_path(artifact: &Artifact) -> PathBuf {
artifact.segments().map(concrete).collect()
}
#[must_use]
pub fn marker_name(ecosystem: &Ecosystem) -> String {
concrete(ecosystem.markers.first().expect("every ecosystem has a marker"))
}
#[must_use]
pub fn fixture(ecosystem: &Ecosystem, artifact: &Artifact, with_marker: bool) -> (TempDir, PathBuf) {
let root = TempDir::new().expect("a temporary directory");
let relative = artifact_path(artifact);
let target = root.path().join(&relative);
if artifact.path.ends_with('/') {
fs::create_dir_all(&target).expect("the artifact directory");
fs::write(target.join("payload.bin"), b"build output").expect("artifact contents");
} else {
if let Some(parent) = target.parent() {
fs::create_dir_all(parent).expect("the artifact's parent");
}
fs::write(&target, b"build output").expect("the artifact file");
}
if with_marker {
let marker_dir = if artifact.anchor_in(ecosystem).is_inside() {
target.clone()
} else {
root.path().to_path_buf()
};
fs::write(marker_dir.join(marker_name(ecosystem)), b"marker").expect("the marker file");
}
(root, target)
}
#[must_use]
pub fn describe(ecosystem: &Ecosystem, artifact: &Artifact) -> String {
format!("{}.{} ({})", ecosystem.id, artifact.slug(), artifact.path)
}