diff --git a/firewood/examples/rev.rs b/firewood/examples/rev.rs index 44ce44f14..91402ddee 100644 --- a/firewood/examples/rev.rs +++ b/firewood/examples/rev.rs @@ -118,12 +118,15 @@ impl RevisionTracker { } } - fn create_revisions(&mut self, iter: impl Iterator) -> Result<(), DbError> + fn create_revisions( + &mut self, + mut iter: impl Iterator, + ) -> Result<(), DbError> where K: AsRef<[u8]>, V: AsRef<[u8]>, { - iter.map(|(k, v)| self.create_revision(k, v)).collect() + iter.try_for_each(|(k, v)| self.create_revision(k, v)) } fn create_revision(&mut self, k: K, v: V) -> Result<(), DbError> diff --git a/firewood/src/storage/buffer.rs b/firewood/src/storage/buffer.rs index 8ed871d92..f360bdb5a 100644 --- a/firewood/src/storage/buffer.rs +++ b/firewood/src/storage/buffer.rs @@ -635,17 +635,17 @@ mod tests { #[test] #[ignore = "ref: https://github.com/ava-labs/firewood/issues/45"] fn test_buffer_with_undo() { - let tmpdb = [ - &std::env::var("CARGO_TARGET_DIR").unwrap_or("/tmp".to_string()), - "sender_api_test_db", - ]; + let temp_dir = option_env!("CARGO_TARGET_DIR") + .map(PathBuf::from) + .unwrap_or(std::env::temp_dir()); + let tmpdb = [temp_dir.to_str().unwrap(), "sender_api_test_db"]; let buf_cfg = DiskBufferConfig::builder().max_buffered(1).build(); let wal_cfg = WalConfig::builder().build(); let disk_requester = init_buffer(buf_cfg, wal_cfg); // TODO: Run the test in a separate standalone directory for concurrency reasons - let path = std::path::PathBuf::from(r"/tmp/firewood"); + let path = temp_dir.join("firewood"); let (root_db_path, reset) = file::open_dir(path, file::Options::Truncate).unwrap(); // file descriptor of the state directory diff --git a/fwdctl/tests/cli.rs b/fwdctl/tests/cli.rs index f4eae562a..111f3cb39 100644 --- a/fwdctl/tests/cli.rs +++ b/fwdctl/tests/cli.rs @@ -2,10 +2,11 @@ use anyhow::{anyhow, Result}; use assert_cmd::Command; use predicates::prelude::*; use serial_test::serial; -use std::fs::remove_dir_all; +use std::{fs::remove_dir_all, path::PathBuf}; const PRG: &str = "fwdctl"; const VERSION: &str = env!("CARGO_PKG_VERSION"); + // Removes the firewood database on disk fn fwdctl_delete_db() -> Result<()> { if let Err(e) = remove_dir_all(tmpdb::path()) { @@ -215,35 +216,16 @@ fn fwdctl_dump() -> Result<()> { // of this in different directories will have different databases mod tmpdb { - use std::{ - ffi, - path::{Path, PathBuf}, - }; + use super::*; const FIREWOOD_TEST_DB_NAME: &str = "test_firewood"; const TARGET_TMP_DIR: Option<&str> = option_env!("CARGO_TARGET_TMPDIR"); - #[derive(Debug)] - pub struct DbPath { - path: PathBuf, - } - - impl AsRef for DbPath { - fn as_ref(&self) -> &ffi::OsStr { - self.path.as_os_str() - } - } - impl AsRef for DbPath { - fn as_ref(&self) -> &Path { - &self.path - } - } - pub fn path() -> DbPath { - let path = Path::new( - TARGET_TMP_DIR.unwrap_or(&std::env::var("TMPDIR").unwrap_or("/tmp".to_string())), - ) - .join(FIREWOOD_TEST_DB_NAME); - - DbPath { path } + pub fn path() -> PathBuf { + TARGET_TMP_DIR + .map(PathBuf::from) + .or_else(|| std::env::var("TMPDIR").ok().map(PathBuf::from)) + .unwrap_or(std::env::temp_dir()) + .join(FIREWOOD_TEST_DB_NAME) } } diff --git a/growth-ring/src/lib.rs b/growth-ring/src/lib.rs index 4e5c69070..0880434b9 100644 --- a/growth-ring/src/lib.rs +++ b/growth-ring/src/lib.rs @@ -349,7 +349,9 @@ mod tests { } fn get_temp_walfile_path(file: &str, line: u32) -> PathBuf { - let path = option_env!("CARGO_TARGET_TMPDIR").unwrap_or("/tmp"); - Path::new(path).join(format!("{}_{}", file.replace('/', "-"), line)) + let path = option_env!("CARGO_TARGET_TMPDIR") + .map(PathBuf::from) + .unwrap_or(std::env::temp_dir()); + path.join(format!("{}_{}", file.replace('/', "-"), line)) } } diff --git a/libaio/tests/simple_test.rs b/libaio/tests/simple_test.rs index 3a11c5435..86eea7638 100644 --- a/libaio/tests/simple_test.rs +++ b/libaio/tests/simple_test.rs @@ -1,12 +1,11 @@ use aiofut::AioBuilder; -use futures::executor::LocalPool; -use futures::future::FutureExt; -use futures::task::LocalSpawnExt; -use std::os::unix::io::AsRawFd; -use std::path::Path; +use futures::{executor::LocalPool, future::FutureExt, task::LocalSpawnExt}; +use std::{os::unix::io::AsRawFd, path::PathBuf}; -fn tmp_dir() -> &'static Path { - Path::new(option_env!("CARGO_TARGET_TMPDIR").unwrap_or("/tmp")) +fn tmp_dir() -> PathBuf { + option_env!("CARGO_TARGET_TMPDIR") + .map(PathBuf::from) + .unwrap_or(std::env::temp_dir()) } #[test]