From cfd03ee39e5fd5bef42e1de229a0868fae909c33 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 5 Dec 2021 21:01:44 +0200 Subject: feat: create a temporary directory for test data Sponsored-by: author --- Cargo.lock | 1 + Cargo.toml | 1 + src/bin/obnam-benchmark.rs | 2 +- src/data.rs | 13 +++++++++---- src/state.rs | 20 ++++++++++++++------ 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bd4e55c..3673319 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -587,6 +587,7 @@ dependencies = [ "structopt", "subplot-build", "subplotlib", + "tempfile", "thiserror", ] diff --git a/Cargo.toml b/Cargo.toml index 2979f4d..79ea8d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ serde = { version = "1.0.101", features = ["derive"] } serde_json = "1.0.72" serde_yaml = "0.8.21" structopt = "0.3.25" +tempfile = "3.2.0" thiserror = "1.0.30" [build-dependencies] diff --git a/src/bin/obnam-benchmark.rs b/src/bin/obnam-benchmark.rs index 418a46e..0ad8491 100644 --- a/src/bin/obnam-benchmark.rs +++ b/src/bin/obnam-benchmark.rs @@ -53,7 +53,7 @@ struct Run { impl Run { fn run(&self) -> anyhow::Result<()> { let spec = Specification::from_file(&self.spec)?; - let mut state = State::new(); + let mut state = State::new()?; let mut result = Result::default(); for step in spec.steps() { if let Some(m) = state.execute(&step)? { diff --git a/src/data.rs b/src/data.rs index aaaebc9..04376bc 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,4 +1,5 @@ use crate::specification::{Create, FileCount}; +use std::path::{Path, PathBuf}; /// Test data management for Obnam benchmarks. /// @@ -6,7 +7,9 @@ use crate::specification::{Create, FileCount}; /// that backup. That data is later cleaned up. Is all handled by this /// structure. #[derive(Debug)] -pub struct Data {} +pub struct Data { + tempdir: PathBuf, +} /// Possible errors from managing data. #[derive(Debug, thiserror::Error)] @@ -14,12 +17,14 @@ pub enum DataError { } impl Data { - pub(crate) fn new() -> Result { - Ok(Self {}) + pub(crate) fn new(tempdir: &Path) -> Result { + Ok(Self { + tempdir: tempdir.to_path_buf(), + }) } pub(crate) fn create(&self, create: &Create) -> Result<(), DataError> { - println!("create {:?}", create); + println!("create {:?} in {}", create, self.tempdir.display()); Ok(()) } diff --git a/src/state.rs b/src/state.rs index bce5115..b925656 100644 --- a/src/state.rs +++ b/src/state.rs @@ -2,10 +2,13 @@ use crate::data::{Data, DataError}; use crate::result::{Measurement, OpMeasurements, Operation}; use crate::step::Step; use std::time::Instant; +use std::path::{Path}; +use tempfile::{tempdir, TempDir}; /// Runtime state for running Obnam benchmarks. pub struct State { current_benchmark: Option, + tempdir: TempDir, } /// Possible errors from changing benchmark state. @@ -15,16 +18,21 @@ pub enum StateError { #[error("Internal error: step expected inside a benchmark")] OutOfStep, + /// Failed to create a temporary directory. + #[error(transparent)] + CreateTemp(#[from] std::io::Error), + /// Error managing test data. #[error(transparent)] Data(#[from] DataError), } impl State { - pub fn new() -> Self { - Self { + pub fn new() -> Result { + Ok(Self { current_benchmark: None, - } + tempdir: tempdir()?, + }) } fn current(&self) -> Result<&CurrentBenchmark, StateError> { @@ -39,7 +47,7 @@ impl State { let now = Instant::now(); let om = match step { Step::Start(name) => { - self.current_benchmark = Some(CurrentBenchmark::new(name)?); + self.current_benchmark = Some(CurrentBenchmark::new(name, self.tempdir.path())?); None } Step::Stop(_) => { @@ -93,10 +101,10 @@ struct CurrentBenchmark { } impl CurrentBenchmark { - fn new(name: &str) -> Result { + fn new(name: &str, tempdir: &Path) -> Result { Ok(Self { name: name.to_string(), - data: Data::new()?, + data: Data::new(tempdir)?, }) } } -- cgit v1.2.1