Skip to content

Commit

Permalink
Rename EnvFileBuilder to EnvFileContents
Browse files Browse the repository at this point in the history
  • Loading branch information
sonro committed Aug 1, 2024
1 parent 6fa82bc commit 4dfeed7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 44 deletions.
49 changes: 29 additions & 20 deletions test_util/src/env_file.rs
Original file line number Diff line number Diff line change
@@ -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<u8>,
}

/// `.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<u8>,
}

impl EnvFileBuilder {
impl EnvFileContents {
pub const fn new() -> Self {
Self {
contents: Vec::new(),
Expand Down Expand Up @@ -102,76 +111,76 @@ impl EnvFileBuilder {
}
}

impl From<EnvFileBuilder> for Vec<u8> {
fn from(builder: EnvFileBuilder) -> Self {
impl From<EnvFileContents> for Vec<u8> {
fn from(builder: EnvFileContents) -> Self {
builder.into_owned_bytes()
}
}

impl From<Vec<u8>> for EnvFileBuilder {
impl From<Vec<u8>> for EnvFileContents {
fn from(contents: Vec<u8>) -> Self {
Self { contents }
}
}

impl From<String> for EnvFileBuilder {
impl From<String> for EnvFileContents {
fn from(contents: String) -> Self {
Self {
contents: contents.into_bytes(),
}
}
}

impl AsRef<[u8]> for EnvFileBuilder {
impl AsRef<[u8]> for EnvFileContents {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}

impl PartialEq<String> for EnvFileBuilder {
impl PartialEq<String> for EnvFileContents {
fn eq(&self, other: &String) -> bool {
self.as_bytes() == other.as_bytes()
}
}

impl PartialEq<str> for EnvFileBuilder {
impl PartialEq<str> for EnvFileContents {
fn eq(&self, other: &str) -> bool {
self.as_bytes() == other.as_bytes()
}
}

impl PartialEq<Vec<u8>> for EnvFileBuilder {
impl PartialEq<Vec<u8>> for EnvFileContents {
fn eq(&self, other: &Vec<u8>) -> 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<EnvFileBuilder> for String {
fn eq(&self, other: &EnvFileBuilder) -> bool {
impl PartialEq<EnvFileContents> for String {
fn eq(&self, other: &EnvFileContents) -> bool {
self.as_bytes() == other.as_bytes()
}
}

impl PartialEq<EnvFileBuilder> for &str {
fn eq(&self, other: &EnvFileBuilder) -> bool {
impl PartialEq<EnvFileContents> for &str {
fn eq(&self, other: &EnvFileContents) -> bool {
self.as_bytes() == other.as_bytes()
}
}

impl PartialEq<EnvFileBuilder> for Vec<u8> {
fn eq(&self, other: &EnvFileBuilder) -> bool {
impl PartialEq<EnvFileContents> for Vec<u8> {
fn eq(&self, other: &EnvFileContents) -> bool {
self == other.as_bytes()
}
}

impl PartialEq<EnvFileBuilder> for &[u8] {
fn eq(&self, other: &EnvFileBuilder) -> bool {
impl PartialEq<EnvFileContents> for &[u8] {
fn eq(&self, other: &EnvFileContents) -> bool {
*self == other.as_bytes()
}
}
2 changes: 1 addition & 1 deletion test_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! - multiple `.env` files,
//! - custom env file name/path.
//!
//! Customize your env files using [`EnvFileBuilder`].
//! Customize your env files using [`EnvFileContents`].
//!
//! In your tests, call your environment altering functions such as the
//! [`dotenvy`] API, then make use of the `assert_` helpers, such as
Expand Down
9 changes: 2 additions & 7 deletions test_util/src/testenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use std::{
};
use tempfile::{tempdir, TempDir};

use crate::EnvFile;

/// Env var convenience type.
type EnvMap = HashMap<String, String>;

Expand All @@ -33,13 +35,6 @@ pub struct TestEnv {
env_files: Vec<EnvFile>,
}

#[derive(Debug, Clone)]
/// Simple path and byte contents representing a `.env` file
pub struct EnvFile {
pub path: PathBuf,
pub contents: Vec<u8>,
}

/// Run a test closure within a test environment.
///
/// Resets the environment variables, loads the [`TestEnv`], then runs the test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@ 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);
}

#[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);
}

#[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)]);
Expand All @@ -39,58 +39,58 @@ 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);
}

#[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);
}

#[test]
fn add_strln() {
let mut efb = EnvFileBuilder::new();
let mut efb = EnvFileContents::new();
efb.add_strln("test");
assert_eq!("test\n", efb);
}

#[test]
fn from_vec_u8() {
let vec: Vec<u8> = 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();
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion test_util/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion test_util/src/tests/testenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4dfeed7

Please sign in to comment.