diff --git a/test_util/src/env_file.rs b/test_util/src/env_file.rs index d66929e7..58f145bc 100644 --- a/test_util/src/env_file.rs +++ b/test_util/src/env_file.rs @@ -1,12 +1,21 @@ -/// Advanced test-env file constructor. +use std::path::PathBuf; + +#[derive(Debug, Clone)] +/// Simple path and byte contents representing a `.env` file +pub struct EnvFile { + pub path: PathBuf, + pub contents: Vec, +} + +/// `.env` file builder. /// /// Represented as bytes to allow for advanced manipulation and BOM testing. #[derive(Debug, Default, Clone, PartialEq, Eq)] -pub struct EnvFileBuilder { +pub struct EnvFileContents { contents: Vec, } -impl EnvFileBuilder { +impl EnvFileContents { pub const fn new() -> Self { Self { contents: Vec::new(), @@ -102,19 +111,19 @@ impl EnvFileBuilder { } } -impl From for Vec { - fn from(builder: EnvFileBuilder) -> Self { +impl From for Vec { + fn from(builder: EnvFileContents) -> Self { builder.into_owned_bytes() } } -impl From> for EnvFileBuilder { +impl From> for EnvFileContents { fn from(contents: Vec) -> Self { Self { contents } } } -impl From for EnvFileBuilder { +impl From for EnvFileContents { fn from(contents: String) -> Self { Self { contents: contents.into_bytes(), @@ -122,56 +131,56 @@ impl From for EnvFileBuilder { } } -impl AsRef<[u8]> for EnvFileBuilder { +impl AsRef<[u8]> for EnvFileContents { fn as_ref(&self) -> &[u8] { self.as_bytes() } } -impl PartialEq for EnvFileBuilder { +impl PartialEq for EnvFileContents { fn eq(&self, other: &String) -> bool { self.as_bytes() == other.as_bytes() } } -impl PartialEq for EnvFileBuilder { +impl PartialEq for EnvFileContents { fn eq(&self, other: &str) -> bool { self.as_bytes() == other.as_bytes() } } -impl PartialEq> for EnvFileBuilder { +impl PartialEq> for EnvFileContents { fn eq(&self, other: &Vec) -> bool { self.as_bytes() == other } } -impl PartialEq<[u8]> for EnvFileBuilder { +impl PartialEq<[u8]> for EnvFileContents { fn eq(&self, other: &[u8]) -> bool { self.as_bytes() == other } } -impl PartialEq for String { - fn eq(&self, other: &EnvFileBuilder) -> bool { +impl PartialEq for String { + fn eq(&self, other: &EnvFileContents) -> bool { self.as_bytes() == other.as_bytes() } } -impl PartialEq for &str { - fn eq(&self, other: &EnvFileBuilder) -> bool { +impl PartialEq for &str { + fn eq(&self, other: &EnvFileContents) -> bool { self.as_bytes() == other.as_bytes() } } -impl PartialEq for Vec { - fn eq(&self, other: &EnvFileBuilder) -> bool { +impl PartialEq for Vec { + fn eq(&self, other: &EnvFileContents) -> bool { self == other.as_bytes() } } -impl PartialEq for &[u8] { - fn eq(&self, other: &EnvFileBuilder) -> bool { +impl PartialEq for &[u8] { + fn eq(&self, other: &EnvFileContents) -> bool { *self == other.as_bytes() } } diff --git a/test_util/src/testenv.rs b/test_util/src/testenv.rs index 584913d2..8e5f4ed1 100644 --- a/test_util/src/testenv.rs +++ b/test_util/src/testenv.rs @@ -8,6 +8,8 @@ use std::{ }; use tempfile::{tempdir, TempDir}; +use crate::EnvFile; + /// Env var convenience type. type EnvMap = HashMap; @@ -33,13 +35,6 @@ pub struct TestEnv { env_files: Vec, } -#[derive(Debug, Clone)] -/// Simple path and byte contents representing a `.env` file -pub struct EnvFile { - pub path: PathBuf, - pub contents: Vec, -} - /// Run a test closure within a test environment. /// /// Resets the environment variables, loads the [`TestEnv`], then runs the test diff --git a/test_util/src/tests/env_file_builder.rs b/test_util/src/tests/env_file.rs similarity index 74% rename from test_util/src/tests/env_file_builder.rs rename to test_util/src/tests/env_file.rs index 33108a2e..c3b8b084 100644 --- a/test_util/src/tests/env_file_builder.rs +++ b/test_util/src/tests/env_file.rs @@ -2,19 +2,19 @@ use super::*; #[test] fn new_builds_empty() { - let efb = EnvFileBuilder::new(); + let efb = EnvFileContents::new(); assert!(efb.is_empty()); } #[test] fn default_builds_empty() { - let efb = EnvFileBuilder::default(); + let efb = EnvFileContents::default(); assert!(efb.is_empty()); } #[test] fn add_key_empty_value() { - let mut efb = EnvFileBuilder::new(); + let mut efb = EnvFileContents::new(); efb.add_key_value(TEST_KEY, ""); let expected = format!("{TEST_KEY}=\n"); assert_eq!(expected, efb); @@ -22,7 +22,7 @@ fn add_key_empty_value() { #[test] fn add_key_value() { - let mut efb = EnvFileBuilder::new(); + let mut efb = EnvFileContents::new(); efb.add_key_value(TEST_KEY, TEST_VALUE); let expected = format!("{TEST_KEY}={TEST_VALUE}\n"); assert_eq!(expected, efb); @@ -30,7 +30,7 @@ fn add_key_value() { #[test] fn add_multiple_key_values() { - let mut efb = EnvFileBuilder::new(); + let mut efb = EnvFileContents::new(); efb.add_key_value(TEST_KEY, TEST_VALUE); efb.add_key_value(EXISTING_KEY, OVERRIDING_VALUE); let expected = expected_env_file(&[(TEST_KEY, TEST_VALUE), (EXISTING_KEY, OVERRIDING_VALUE)]); @@ -39,7 +39,7 @@ fn add_multiple_key_values() { #[test] fn add_vars() { - let mut efb = EnvFileBuilder::new(); + let mut efb = EnvFileContents::new(); efb.add_vars(CUSTOM_VARS); let expected = expected_env_file(CUSTOM_VARS); assert_eq!(expected, efb); @@ -47,28 +47,28 @@ fn add_vars() { #[test] fn add_str() { - let mut efb = EnvFileBuilder::new(); + let mut efb = EnvFileContents::new(); efb.add_str("test"); assert_eq!("test", efb); } #[test] fn add_bytes() { - let mut efb = EnvFileBuilder::new(); + let mut efb = EnvFileContents::new(); efb.add_bytes(b"test"); assert_eq!("test", efb); } #[test] fn add_byte() { - let mut efb = EnvFileBuilder::new(); + let mut efb = EnvFileContents::new(); efb.add_byte(b't'); assert_eq!("t", efb); } #[test] fn insert_utf8_bom() { - let mut efb = EnvFileBuilder::new(); + let mut efb = EnvFileContents::new(); efb.add_str("test"); efb.insert_utf8_bom(); assert_eq!("\u{FEFF}test", efb); @@ -76,7 +76,7 @@ fn insert_utf8_bom() { #[test] fn add_strln() { - let mut efb = EnvFileBuilder::new(); + let mut efb = EnvFileContents::new(); efb.add_strln("test"); assert_eq!("test\n", efb); } @@ -84,13 +84,13 @@ fn add_strln() { #[test] fn from_vec_u8() { let vec: Vec = Vec::from(create_test_env_file()); - let efb = EnvFileBuilder::from(vec); + let efb = EnvFileContents::from(vec); assert_eq!(create_test_env_file(), efb); } #[test] fn to_vec_u8() { - let mut efb = EnvFileBuilder::new(); + let mut efb = EnvFileContents::new(); efb.add_str(create_test_env_file().as_str()); let vec = Vec::from(efb); let expected = create_test_env_file().into_bytes(); @@ -99,6 +99,6 @@ fn to_vec_u8() { #[test] fn from_string() { - let efb = EnvFileBuilder::from(create_test_env_file()); + let efb = EnvFileContents::from(create_test_env_file()); assert_eq!(create_test_env_file(), efb); } diff --git a/test_util/src/tests/mod.rs b/test_util/src/tests/mod.rs index 645dbfc9..74b44e9e 100644 --- a/test_util/src/tests/mod.rs +++ b/test_util/src/tests/mod.rs @@ -4,7 +4,7 @@ use std::path::Path; use super::*; -mod env_file_builder; +mod env_file; mod testenv; const TEST_KEY: &str = "TEST_KEY"; diff --git a/test_util/src/tests/testenv.rs b/test_util/src/tests/testenv.rs index 8f605062..21805cf7 100644 --- a/test_util/src/tests/testenv.rs +++ b/test_util/src/tests/testenv.rs @@ -147,7 +147,7 @@ mod init_with_env_file { } fn init_custom_bom_env_file_testenv() -> TestEnv { - let mut efb = EnvFileBuilder::new(); + let mut efb = EnvFileContents::new(); efb.add_key_value(TEST_KEY, TEST_VALUE); efb.insert_utf8_bom(); let env_file = efb.into_owned_string();