use super::super::*;
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;
use tempfile::TempDir;
const STALE: &str = r#"[hooks]
"claim.post" = ["bl-delivery"]
"claim.pre" = ["bl-delivery"]
"close.post" = ["bl-delivery"]
"close.pre" = ["bl-delivery"]
"drop.post" = ["bl-delivery"]
"prime.post" = ["bl-delivery"]
"unclaim.post" = ["bl-delivery"]
"unclaim.pre" = ["bl-delivery"]
"#;
pub(super) struct World {
_dir: TempDir,
pub(super) edge: Edge,
pub(super) landing: PathBuf,
pub(super) root: PathBuf,
}
impl World {
pub(super) fn new() -> World {
let dir = tempfile::tempdir().expect("scratch world");
let anchor = dir.path();
let root = anchor.join("yog").join("world");
std::fs::create_dir_all(&root).expect("world root");
let tools = root.join("tools");
std::fs::create_dir_all(&tools).expect("tools dir");
for name in [tools::BL, tools::BL_DELIVERY, tools::BL_TRACKER] {
let path = tools.join(name);
std::fs::write(&path, "#!/bin/sh\nexit 0\n").expect("sibling");
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755))
.expect("sibling mode");
}
let project = anchor.join("proj");
std::fs::create_dir_all(&project).expect("project dir");
let edge = Edge::resolve(
anchor.to_path_buf(),
Some(root.join("config").to_string_lossy().into_owned()),
Some(root.join("state").to_string_lossy().into_owned()),
project,
Some("tester".to_owned()),
None,
Some(tools.join(tools::BL)),
None,
None,
false,
None,
);
let landing = edge.xdg.clone_dir(&edge.invocation_path).landing();
World {
_dir: dir,
edge,
landing,
root,
}
}
pub(super) fn found(&self) {
substrate::found_landing(
&self.landing,
&self.edge.xdg,
self.edge.exe_dir.as_deref(),
"tester",
)
.expect("found landing");
}
pub(super) fn damage(&self) {
std::fs::write(self.plugins(), STALE).expect("write stale schedule");
git(&self.landing, &["add", "-A"]).expect("stage");
git(&self.landing, &["commit", "-q", "-m", "stale seed"]).expect("commit");
}
pub(super) fn plugins(&self) -> PathBuf {
self.landing.join("config").join("plugins.toml")
}
pub(super) fn scalars(&self) -> PathBuf {
self.landing.join("config").join("balls.toml")
}
pub(super) fn schedule(&self) -> String {
std::fs::read_to_string(self.plugins()).unwrap_or_default()
}
pub(super) fn head(&self) -> String {
git(&self.landing, &["rev-parse", "HEAD"]).expect("head")
}
}