use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};
use super::RunArtifacts;
const RANDOM_PLAN_ID_LEN: usize = 5;
fn random_plan_request_filename() -> String {
format!(
"plan_{}.md",
crate::alnum_id::random_alnum(RANDOM_PLAN_ID_LEN)
)
}
pub(crate) fn ensure_quality_gates_log_file(artifacts: &RunArtifacts) -> std::io::Result<()> {
let path = artifacts.quality_gates_log_path();
std::fs::write(&path, "")
}
pub(crate) fn init_quality_gates_log_pending(artifacts: &RunArtifacts) -> std::io::Result<()> {
std::fs::write(
artifacts.quality_gates_log_path(),
crate::malvin_constants::QUALITY_GATES_LOG_PENDING,
)
}
pub(crate) fn ensure_exp_log_file(artifacts: &RunArtifacts) -> std::io::Result<PathBuf> {
write_empty_exp_log(&artifacts.exp_log_path())
}
pub(crate) fn ensure_gate_exp_log_file(
artifacts: &RunArtifacts,
iteration: usize,
) -> std::io::Result<PathBuf> {
write_empty_exp_log(&artifacts.gate_exp_log_path(iteration))
}
fn write_empty_exp_log(exp_log_path: &Path) -> std::io::Result<PathBuf> {
let exp_parent = exp_log_path.parent().ok_or_else(|| {
Error::new(
ErrorKind::InvalidInput,
"exp log path has no parent directory",
)
})?;
std::fs::create_dir_all(exp_parent)?;
std::fs::write(exp_log_path, "")?;
Ok(exp_log_path.to_path_buf())
}
pub fn create_run_artifacts(
plan_source: &Path,
base_dir: Option<&Path>,
) -> std::io::Result<RunArtifacts> {
create_run_artifacts_opts(
plan_source,
base_dir,
crate::run_id::RunDirOptions::default(),
)
}
pub fn create_run_artifacts_opts(
plan_source: &Path,
base_dir: Option<&Path>,
opts: crate::run_id::RunDirOptions,
) -> std::io::Result<RunArtifacts> {
let run_dir = crate::run_id::create_run_dir(base_dir, opts)?;
let plan_target = run_dir.join(random_plan_request_filename());
std::fs::copy(plan_source, &plan_target)?;
let artifacts = RunArtifacts {
run_dir,
plan_path: plan_target,
work_dir: plan_source
.parent()
.filter(|p| !p.as_os_str().is_empty())
.map_or_else(|| PathBuf::from("."), Path::to_path_buf),
};
ensure_exp_log_file(&artifacts)?;
ensure_quality_gates_log_file(&artifacts)?;
crate::write_work_dir_manifest(&artifacts.run_dir, &artifacts.work_dir)?;
#[cfg(not(test))]
crate::stdout_log_path::set_stdout_log_path(Some(artifacts.stdout_log_path()));
Ok(artifacts)
}
pub fn create_run_artifacts_from_text(
plan_text: &str,
base_dir: Option<&Path>,
) -> std::io::Result<RunArtifacts> {
create_run_artifacts_from_text_opts(
plan_text,
base_dir,
crate::run_id::RunDirOptions::default(),
)
}
pub fn create_run_artifacts_from_text_opts(
plan_text: &str,
base_dir: Option<&Path>,
opts: crate::run_id::RunDirOptions,
) -> std::io::Result<RunArtifacts> {
let work_dir = base_dir.unwrap_or_else(|| Path::new(".")).to_path_buf();
let run_dir = crate::run_id::create_run_dir(base_dir, opts)?;
let plan_target = run_dir.join(random_plan_request_filename());
std::fs::write(&plan_target, plan_text)?;
let artifacts = RunArtifacts {
run_dir,
plan_path: plan_target,
work_dir,
};
ensure_exp_log_file(&artifacts)?;
ensure_quality_gates_log_file(&artifacts)?;
crate::write_work_dir_manifest(&artifacts.run_dir, &artifacts.work_dir)?;
#[cfg(not(test))]
crate::stdout_log_path::set_stdout_log_path(Some(artifacts.stdout_log_path()));
Ok(artifacts)
}
#[cfg(test)]
#[allow(unused_imports)]
mod kiss_cov_gate_refs {
use super::*;
#[test]
fn kiss_cov_unit_names() {
let _ = ensure_exp_log_file;
let _ = write_empty_exp_log;
let _ = init_quality_gates_log_pending;
}
}