Skip to content

Commit

Permalink
Use the standard-library temp_dir function (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpringle authored Sep 1, 2023
1 parent 76222b6 commit 6b2fd2f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 43 deletions.
7 changes: 5 additions & 2 deletions firewood/examples/rev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,15 @@ impl RevisionTracker {
}
}

fn create_revisions<K, V>(&mut self, iter: impl Iterator<Item = (K, V)>) -> Result<(), DbError>
fn create_revisions<K, V>(
&mut self,
mut iter: impl Iterator<Item = (K, V)>,
) -> 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<K, V>(&mut self, k: K, v: V) -> Result<(), DbError>
Expand Down
10 changes: 5 additions & 5 deletions firewood/src/storage/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 9 additions & 27 deletions fwdctl/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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<ffi::OsStr> for DbPath {
fn as_ref(&self) -> &ffi::OsStr {
self.path.as_os_str()
}
}
impl AsRef<Path> 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)
}
}
6 changes: 4 additions & 2 deletions growth-ring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
13 changes: 6 additions & 7 deletions libaio/tests/simple_test.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down

0 comments on commit 6b2fd2f

Please sign in to comment.