From 2b551b8b93a3492b1d16f77a13b4e302a5b3a8b2 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Fri, 6 Dec 2024 13:36:45 +0100 Subject: [PATCH] Use tempdir everywhere --- examples/all-types.rs | 6 +----- examples/clear-database.rs | 7 +------ examples/cursor-append.rs | 7 +------ examples/custom-comparator.rs | 8 ++------ examples/heed3-all-types.rs | 6 +----- examples/heed3-encrypted.rs | 7 +------ examples/multi-env.rs | 9 ++------- examples/nested.rs | 6 +----- examples/rmp-serde.rs | 6 +----- heed/src/envs/encrypted_env.rs | 1 - heed/src/envs/env.rs | 1 - heed/src/envs/env_open_options.rs | 7 ++----- 12 files changed, 13 insertions(+), 58 deletions(-) diff --git a/examples/all-types.rs b/examples/all-types.rs index 1bcc17d9..01ded4b9 100644 --- a/examples/all-types.rs +++ b/examples/all-types.rs @@ -1,6 +1,4 @@ use std::error::Error; -use std::fs; -use std::path::Path; use heed::byteorder::BE; use heed::types::*; @@ -8,9 +6,7 @@ use heed::{Database, EnvOpenOptions}; use serde::{Deserialize, Serialize}; fn main() -> Result<(), Box> { - let path = Path::new("target").join("heed.mdb"); - - fs::create_dir_all(&path)?; + let path = tempfile::tempdir()?; let env = unsafe { EnvOpenOptions::new() diff --git a/examples/clear-database.rs b/examples/clear-database.rs index ce1a17d7..88f03e68 100644 --- a/examples/clear-database.rs +++ b/examples/clear-database.rs @@ -1,6 +1,4 @@ use std::error::Error; -use std::fs; -use std::path::Path; use heed::types::*; use heed::{Database, EnvOpenOptions}; @@ -8,11 +6,8 @@ use heed::{Database, EnvOpenOptions}; // In this test we are checking that we can clear database entries and // write just after in the same transaction without loosing the writes. fn main() -> Result<(), Box> { - let env_path = Path::new("target").join("clear-database.mdb"); + let env_path = tempfile::tempdir()?; - let _ = fs::remove_dir_all(&env_path); - - fs::create_dir_all(&env_path)?; let env = unsafe { EnvOpenOptions::new() .map_size(10 * 1024 * 1024) // 10MB diff --git a/examples/cursor-append.rs b/examples/cursor-append.rs index 91653ad2..0fbf1c3b 100644 --- a/examples/cursor-append.rs +++ b/examples/cursor-append.rs @@ -1,6 +1,4 @@ use std::error::Error; -use std::fs; -use std::path::Path; use heed::types::*; use heed::{Database, EnvOpenOptions, PutFlags}; @@ -8,11 +6,8 @@ use heed::{Database, EnvOpenOptions, PutFlags}; // In this test we are checking that we can append ordered entries in one // database even if there is multiple databases which already contain entries. fn main() -> Result<(), Box> { - let env_path = Path::new("target").join("cursor-append.mdb"); + let env_path = tempfile::tempdir()?; - let _ = fs::remove_dir_all(&env_path); - - fs::create_dir_all(&env_path)?; let env = unsafe { EnvOpenOptions::new() .map_size(10 * 1024 * 1024) // 10MB diff --git a/examples/custom-comparator.rs b/examples/custom-comparator.rs index e8068834..8382d735 100644 --- a/examples/custom-comparator.rs +++ b/examples/custom-comparator.rs @@ -1,7 +1,6 @@ use std::cmp::Ordering; use std::error::Error; -use std::path::Path; -use std::{fs, str}; +use std::str; use heed::EnvOpenOptions; use heed_traits::Comparator; @@ -21,11 +20,8 @@ impl Comparator for StringAsIntCmp { } fn main() -> Result<(), Box> { - let env_path = Path::new("target").join("custom-key-cmp.mdb"); + let env_path = tempfile::tempdir()?; - let _ = fs::remove_dir_all(&env_path); - - fs::create_dir_all(&env_path)?; let env = unsafe { EnvOpenOptions::new() .map_size(10 * 1024 * 1024) // 10MB diff --git a/examples/heed3-all-types.rs b/examples/heed3-all-types.rs index 3fb77be5..c00e4fb6 100644 --- a/examples/heed3-all-types.rs +++ b/examples/heed3-all-types.rs @@ -1,6 +1,4 @@ use std::error::Error; -use std::fs; -use std::path::Path; use heed3::byteorder::BE; use heed3::types::*; @@ -8,9 +6,7 @@ use heed3::{Database, EnvOpenOptions}; use serde::{Deserialize, Serialize}; fn main() -> Result<(), Box> { - let path = Path::new("target").join("heed3.mdb"); - - fs::create_dir_all(&path)?; + let path = tempfile::tempdir()?; let env = unsafe { EnvOpenOptions::new() diff --git a/examples/heed3-encrypted.rs b/examples/heed3-encrypted.rs index 7f7b9332..28cc9835 100644 --- a/examples/heed3-encrypted.rs +++ b/examples/heed3-encrypted.rs @@ -1,6 +1,4 @@ use std::error::Error; -use std::fs; -use std::path::Path; use argon2::Argon2; use chacha20poly1305::{ChaCha20Poly1305, Key}; @@ -8,13 +6,10 @@ use heed3::types::*; use heed3::EnvOpenOptions; fn main() -> Result<(), Box> { - let env_path = Path::new("target").join("encrypt.mdb"); + let env_path = tempfile::tempdir()?; let password = "This is the password that will be hashed by the argon2 algorithm"; let salt = "The salt added to the password hashes to add more security when stored"; - let _ = fs::remove_dir_all(&env_path); - fs::create_dir_all(&env_path)?; - // We choose to use argon2 as our Key Derivation Function, but you can choose whatever you want. // let mut key = Key::default(); diff --git a/examples/multi-env.rs b/examples/multi-env.rs index a8f5a181..d22a2776 100644 --- a/examples/multi-env.rs +++ b/examples/multi-env.rs @@ -1,6 +1,4 @@ use std::error::Error; -use std::fs; -use std::path::Path; use byteorder::BE; use heed::types::*; @@ -9,10 +7,8 @@ use heed::{Database, EnvOpenOptions}; type BEU32 = U32; fn main() -> Result<(), Box> { - let env1_path = Path::new("target").join("env1.mdb"); - let env2_path = Path::new("target").join("env2.mdb"); - - fs::create_dir_all(&env1_path)?; + let env1_path = tempfile::tempdir()?; + let env2_path = tempfile::tempdir()?; let env1 = unsafe { EnvOpenOptions::new() .map_size(10 * 1024 * 1024) // 10MB @@ -20,7 +16,6 @@ fn main() -> Result<(), Box> { .open(env1_path)? }; - fs::create_dir_all(&env2_path)?; let env2 = unsafe { EnvOpenOptions::new() .map_size(10 * 1024 * 1024) // 10MB diff --git a/examples/nested.rs b/examples/nested.rs index 2cbff3c6..d5a3148a 100644 --- a/examples/nested.rs +++ b/examples/nested.rs @@ -1,14 +1,10 @@ use std::error::Error; -use std::fs; -use std::path::Path; use heed::types::*; use heed::{Database, EnvOpenOptions}; fn main() -> Result<(), Box> { - let path = Path::new("target").join("heed.mdb"); - - fs::create_dir_all(&path)?; + let path = tempfile::tempdir()?; let env = unsafe { EnvOpenOptions::new() diff --git a/examples/rmp-serde.rs b/examples/rmp-serde.rs index e31c961a..e119a858 100644 --- a/examples/rmp-serde.rs +++ b/examples/rmp-serde.rs @@ -1,15 +1,11 @@ use std::error::Error; -use std::fs; -use std::path::Path; use heed::types::{SerdeRmp, Str}; use heed::{Database, EnvOpenOptions}; use serde::{Deserialize, Serialize}; fn main() -> Result<(), Box> { - let path = Path::new("target").join("heed.mdb"); - - fs::create_dir_all(&path)?; + let path = tempfile::tempdir()?; let env = unsafe { EnvOpenOptions::new() diff --git a/heed/src/envs/encrypted_env.rs b/heed/src/envs/encrypted_env.rs index 81f4277f..4f558acc 100644 --- a/heed/src/envs/encrypted_env.rs +++ b/heed/src/envs/encrypted_env.rs @@ -56,7 +56,6 @@ impl EncryptedEnv { /// use heed::types::*; /// /// # fn main() -> Result<(), Box> { - /// fs::create_dir_all(Path::new("target").join("database.mdb"))?; /// let mut env_builder = EnvOpenOptions::new(); /// let dir = tempfile::tempdir().unwrap(); /// let env = unsafe { env_builder.open(dir.path())? }; diff --git a/heed/src/envs/env.rs b/heed/src/envs/env.rs index 67137bcd..cb2ee852 100644 --- a/heed/src/envs/env.rs +++ b/heed/src/envs/env.rs @@ -83,7 +83,6 @@ impl Env { /// use heed::types::*; /// /// # fn main() -> Result<(), Box> { - /// fs::create_dir_all(Path::new("target").join("database.mdb"))?; /// let mut env_builder = EnvOpenOptions::new(); /// let dir = tempfile::tempdir().unwrap(); /// let env = unsafe { env_builder.open(dir.path())? }; diff --git a/heed/src/envs/env_open_options.rs b/heed/src/envs/env_open_options.rs index 4079dc4a..437ae085 100644 --- a/heed/src/envs/env_open_options.rs +++ b/heed/src/envs/env_open_options.rs @@ -79,7 +79,6 @@ impl EnvOpenOptions { /// use heed::types::*; /// /// # fn main() -> Result<(), Box> { - /// fs::create_dir_all(Path::new("target").join("database.mdb"))?; /// let mut env_builder = EnvOpenOptions::new(); /// unsafe { env_builder.flags(EnvFlags::NO_TLS | EnvFlags::NO_META_SYNC); } /// let dir = tempfile::tempdir().unwrap(); @@ -208,11 +207,10 @@ impl EnvOpenOptions { /// use heed3::{EnvOpenOptions, Database}; /// /// # fn main() -> Result<(), Box> { - /// let env_path = Path::new("target").join("encrypt.mdb"); + /// let env_path = tempfile::tempdir()?; /// let password = "This is the password that will be hashed by the argon2 algorithm"; /// let salt = "The salt added to the password hashes to add more security when stored"; /// - /// let _ = fs::remove_dir_all(&env_path); /// fs::create_dir_all(&env_path)?; /// /// let mut key = Key::default(); @@ -259,11 +257,10 @@ impl EnvOpenOptions { /// use heed3_encryption::{EnvOpenOptions, Database}; /// /// # fn main() -> Result<(), Box> { - /// let env_path = Path::new("target").join("encrypt.mdb"); + /// let env_path = tempfile::tempdir()?; /// let password = "This is the password that will be hashed by the argon2 algorithm"; /// let salt = "The salt added to the password hashes to add more security when stored"; /// - /// let _ = fs::remove_dir_all(&env_path); /// fs::create_dir_all(&env_path)?; /// /// let mut key = Key::default();