Skip to content
Snippets Groups Projects
Verified Commit 82b69a23 authored by Louis's avatar Louis :fire:
Browse files

Fmt & Lint

parent 9af6ca8c
No related branches found
No related tags found
No related merge requests found
hard_tabs = true
reorder_imports = true
max_width = 140
merge_derives = true
\ No newline at end of file
use crate::parser::{file, FileLine};
use nom_locate::LocatedSpan;
use std::env;
use std::env::VarError;
use std::fmt::Display;
use crate::parser::{file, FileLine};
use nom_locate::LocatedSpan;
use std::str::FromStr;
use std::string::ParseError;
......@@ -114,9 +114,9 @@ pub struct ApplyOptions {
impl ApplyOptions {
pub fn new(prefix: impl Display, overwrite: bool) -> Self {
Self {
prefix: Some(prefix.to_string()),
overwrite,
}
prefix: Some(prefix.to_string()),
overwrite,
}
}
pub fn with_prefix(prefix: impl Display) -> Self {
......@@ -124,10 +124,7 @@ impl ApplyOptions {
}
pub fn with_overwrite(overwrite: bool) -> Self {
Self {
prefix: None,
overwrite,
}
Self { prefix: None, overwrite }
}
}
......@@ -141,20 +138,20 @@ impl ApplyEnvironmentFile for EnvironmentFile {
}
}
impl <E> ApplyEnvironmentFile for Result<EnvironmentFile, E> {
impl<E> ApplyEnvironmentFile for Result<EnvironmentFile, E> {
fn apply(&self, options: ApplyOptions) {
if let Ok(file) = self {
file.apply(options);
}
}
if let Ok(file) = self {
file.apply(options);
}
}
}
impl ApplyEnvironmentFile for Option<EnvironmentFile> {
fn apply(&self, options: ApplyOptions) {
if let Some(file) = self {
file.apply(options);
}
}
if let Some(file) = self {
file.apply(options);
}
}
}
pub struct EnvFileIterator<'a> {
......@@ -251,7 +248,6 @@ fn set_from_file(file: &EnvironmentFile, options: ApplyOptions) -> Result<(), En
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
......
use crate::{EnvironmentFile};
use crate::env_file::{ApplyEnvironmentFile, ApplyOptions};
use crate::EnvironmentFile;
use std::fmt::Display;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use crate::env_file::{ApplyEnvironmentFile, ApplyOptions};
#[derive(Debug, thiserror::Error)]
#[allow(clippy::enum_variant_names)]
......@@ -49,4 +49,3 @@ pub fn dotenv_from(path: impl AsRef<Path>) -> Result<(), EnvFsError> {
env_file_from_path(path)?.apply(Default::default());
Ok(())
}
......@@ -5,8 +5,8 @@ mod env_file;
mod filesystem;
mod parser;
pub use env_file::{EnvironmentFile, EnvironmentFileError, ApplyEnvironmentFile, ApplyOptions};
pub use env_file::{ApplyEnvironmentFile, ApplyOptions, EnvironmentFile, EnvironmentFileError};
pub use parser::{FileLine, ValuePart};
#[cfg(feature = "fs")]
pub use filesystem::{dotenv, dotenv_from, dotenv_suffix, dotenv_opts};
pub use filesystem::{dotenv, dotenv_from, dotenv_opts, dotenv_suffix};
......@@ -2,7 +2,7 @@ use envish::{ApplyEnvironmentFile, ApplyOptions, EnvironmentFile};
#[test]
fn it_parses_basic_dotenv_file() {
let file_contents = r#"
let file_contents = r#"
# This value won't be set in the test, and this comment will be ignored
MY_BEST_VARIABLE=some_value
# This variable is also not defined, and it'll still be a string,
......@@ -10,19 +10,19 @@ fn it_parses_basic_dotenv_file() {
SOME_OTHER_VARIABLE=1234
"#;
std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set");
std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set");
std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set");
std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set");
let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file");
file.apply(Default::default());
let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file");
file.apply(Default::default());
assert_eq!(std::env::var("MY_BEST_VARIABLE").unwrap(), "some_value");
assert_eq!(std::env::var("SOME_OTHER_VARIABLE").unwrap(), "1234");
assert_eq!(std::env::var("MY_BEST_VARIABLE").unwrap(), "some_value");
assert_eq!(std::env::var("SOME_OTHER_VARIABLE").unwrap(), "1234");
}
#[test]
fn it_parses_dotenv_file_with_interpolation() {
let file_contents = r#"
let file_contents = r#"
# This value won't be set in the test, and this comment will be ignored
MY_BEST_VARIABLE=some_value
# This variable is also not defined, and it'll still be a string,
......@@ -32,21 +32,21 @@ fn it_parses_dotenv_file_with_interpolation() {
INTERPOLATED_VARIABLE=${SOME_OTHER_VARIABLE}567
"#;
std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set");
std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set");
std::env::var("INTERPOLATED_VARIABLE").expect_err("INTERPOLATED_VARIABLE should not be set");
std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set");
std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set");
std::env::var("INTERPOLATED_VARIABLE").expect_err("INTERPOLATED_VARIABLE should not be set");
let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file");
file.apply(Default::default());
let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file");
file.apply(Default::default());
assert_eq!(std::env::var("MY_BEST_VARIABLE").unwrap(), "some_value");
assert_eq!(std::env::var("SOME_OTHER_VARIABLE").unwrap(), "1234");
assert_eq!(std::env::var("INTERPOLATED_VARIABLE").unwrap(), "1234567");
assert_eq!(std::env::var("MY_BEST_VARIABLE").unwrap(), "some_value");
assert_eq!(std::env::var("SOME_OTHER_VARIABLE").unwrap(), "1234");
assert_eq!(std::env::var("INTERPOLATED_VARIABLE").unwrap(), "1234567");
}
#[test]
fn it_parses_dotenv_file_with_interpolation_and_prefix_option() {
let file_contents = r#"
let file_contents = r#"
# This value won't be set in the test, and this comment will be ignored
MY_BEST_VARIABLE=some_value
# This variable is also not defined, and it'll still be a string,
......@@ -56,14 +56,14 @@ fn it_parses_dotenv_file_with_interpolation_and_prefix_option() {
INTERPOLATED_VARIABLE=${SOME_OTHER_VARIABLE}567
"#;
std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set");
std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set");
std::env::var("INTERPOLATED_VARIABLE").expect_err("INTERPOLATED_VARIABLE should not be set");
std::env::var("MY_BEST_VARIABLE").expect_err("MY_BEST_VARIABLE should not be set");
std::env::var("SOME_OTHER_VARIABLE").expect_err("SOME_OTHER_VARIABLE should not be set");
std::env::var("INTERPOLATED_VARIABLE").expect_err("INTERPOLATED_VARIABLE should not be set");
let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file");
file.apply(ApplyOptions::with_prefix("APP_"));
let file = EnvironmentFile::parse(file_contents).expect("Failed to parse environment file");
file.apply(ApplyOptions::with_prefix("APP_"));
assert_eq!(std::env::var("APP_MY_BEST_VARIABLE").unwrap(), "some_value");
assert_eq!(std::env::var("APP_SOME_OTHER_VARIABLE").unwrap(), "1234");
assert_eq!(std::env::var("APP_INTERPOLATED_VARIABLE").unwrap(), "1234567");
}
\ No newline at end of file
assert_eq!(std::env::var("APP_MY_BEST_VARIABLE").unwrap(), "some_value");
assert_eq!(std::env::var("APP_SOME_OTHER_VARIABLE").unwrap(), "1234");
assert_eq!(std::env::var("APP_INTERPOLATED_VARIABLE").unwrap(), "1234567");
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment