Skip to content

Commit

Permalink
Use Result in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Allan Zhang committed Aug 23, 2022
1 parent 22beeb4 commit 269a343
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 99 deletions.
19 changes: 10 additions & 9 deletions dotenv/tests/test-child-dir.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
mod common;

use dotenvy::*;
use std::{env, fs};
use std::{env, error::Error, fs, result::Result};

use crate::common::*;

#[test]
fn test_child_dir() {
let dir = make_test_dotenv().unwrap();
fn test_child_dir() -> Result<(), Box<dyn Error>> {
let dir = make_test_dotenv()?;

fs::create_dir("child").unwrap();
fs::create_dir("child")?;

env::set_current_dir("child").unwrap();
env::set_current_dir("child")?;

dotenv().ok();
assert_eq!(env::var("TESTKEY").unwrap(), "test_val");
dotenv()?;
assert_eq!(env::var("TESTKEY")?, "test_val");

env::set_current_dir(dir.path().parent().unwrap()).unwrap();
dir.close().unwrap();
env::set_current_dir(dir.path().parent().unwrap())?;
dir.close()?;
Ok(())
}
16 changes: 9 additions & 7 deletions dotenv/tests/test-default-location.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
mod common;

use dotenvy::*;
use std::env;

use std::{env, error::Error, result::Result};

use crate::common::*;

#[test]
fn test_default_location() {
let dir = make_test_dotenv().unwrap();
fn test_default_location() -> Result<(), Box<dyn Error>> {
let dir = make_test_dotenv()?;

dotenv().ok();
assert_eq!(env::var("TESTKEY").unwrap(), "test_val");
dotenv()?;
assert_eq!(env::var("TESTKEY")?, "test_val");

env::set_current_dir(dir.path().parent().unwrap()).unwrap();
dir.close().unwrap();
env::set_current_dir(dir.path().parent().unwrap())?;
dir.close()?;
Ok(())
}
19 changes: 9 additions & 10 deletions dotenv/tests/test-dotenv-iter.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
mod common;

use dotenvy::*;
use std::env;
use std::{env, error::Error, result::Result};

use crate::common::*;

#[test]
fn test_dotenv_iter() {
let dir = make_test_dotenv().unwrap();

let iter = dotenv_iter().unwrap();
fn test_dotenv_iter() -> Result<(), Box<dyn Error>> {
let dir = make_test_dotenv()?;

let iter = dotenv_iter()?;
assert!(env::var("TESTKEY").is_err());

iter.load().ok();

assert_eq!(env::var("TESTKEY").unwrap(), "test_val");
iter.load()?;
assert_eq!(env::var("TESTKEY")?, "test_val");

env::set_current_dir(dir.path().parent().unwrap()).unwrap();
dir.close().unwrap();
env::set_current_dir(dir.path().parent().unwrap())?;
dir.close()?;
Ok(())
}
15 changes: 8 additions & 7 deletions dotenv/tests/test-from-filename-iter.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
mod common;

use dotenvy::*;
use std::env;
use std::{env, error::Error, result::Result};

use crate::common::*;

#[test]
fn test_from_filename_iter() {
let dir = make_test_dotenv().unwrap();
fn test_from_filename_iter() -> Result<(), Box<dyn Error>> {
let dir = make_test_dotenv()?;

let iter = from_filename_iter(".env").unwrap();
let iter = from_filename_iter(".env")?;

assert!(env::var("TESTKEY").is_err());

iter.load().ok();
iter.load()?;

assert_eq!(env::var("TESTKEY").unwrap(), "test_val");

env::set_current_dir(dir.path().parent().unwrap()).unwrap();
dir.close().unwrap();
env::set_current_dir(dir.path().parent().unwrap())?;
dir.close()?;
Ok(())
}
15 changes: 8 additions & 7 deletions dotenv/tests/test-from-filename.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
mod common;

use dotenvy::*;
use std::env;
use std::{env, error::Error, result::Result};

use crate::common::*;

#[test]
fn test_from_filename() {
let dir = make_test_dotenv().unwrap();
fn test_from_filename() -> Result<(), Box<dyn Error>> {
let dir = make_test_dotenv()?;

from_filename(".env").ok();
from_filename(".env")?;

assert_eq!(env::var("TESTKEY").unwrap(), "test_val");
assert_eq!(env::var("TESTKEY")?, "test_val");

env::set_current_dir(dir.path().parent().unwrap()).unwrap();
dir.close().unwrap();
env::set_current_dir(dir.path().parent().unwrap())?;
dir.close()?;
Ok(())
}
19 changes: 10 additions & 9 deletions dotenv/tests/test-from-path-iter.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
mod common;

use dotenvy::*;
use std::env;
use std::{env, error::Error, result::Result};

use crate::common::*;

#[test]
fn test_from_path_iter() {
let dir = make_test_dotenv().unwrap();
fn test_from_path_iter() -> Result<(), Box<dyn Error>> {
let dir = make_test_dotenv()?;

let mut path = env::current_dir().unwrap();
let mut path = env::current_dir()?;
path.push(".env");

let iter = from_path_iter(&path).unwrap();
let iter = from_path_iter(&path)?;

assert!(env::var("TESTKEY").is_err());

iter.load().ok();
iter.load()?;

assert_eq!(env::var("TESTKEY").unwrap(), "test_val");
assert_eq!(env::var("TESTKEY")?, "test_val");

env::set_current_dir(dir.path().parent().unwrap()).unwrap();
dir.close().unwrap();
env::set_current_dir(dir.path().parent().unwrap())?;
dir.close()?;
Ok(())
}
20 changes: 10 additions & 10 deletions dotenv/tests/test-from-path.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
mod common;

use dotenvy::*;
use std::env;

use crate::common::*;
use dotenvy::*;
use std::{env, error::Error, result::Result};

#[test]
fn test_from_path() {
let dir = make_test_dotenv().unwrap();
fn test_from_path() -> Result<(), Box<dyn Error>> {
let dir = make_test_dotenv()?;

let mut path = env::current_dir().unwrap();
let mut path = env::current_dir()?;
path.push(".env");

from_path(&path).ok();
from_path(&path)?;

assert_eq!(env::var("TESTKEY").unwrap(), "test_val");
assert_eq!(env::var("TESTKEY")?, "test_val");

env::set_current_dir(dir.path().parent().unwrap()).unwrap();
dir.close().unwrap();
env::set_current_dir(dir.path().parent().unwrap())?;
dir.close()?;
Ok(())
}
24 changes: 12 additions & 12 deletions dotenv/tests/test-multiline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ mod common;

use crate::common::*;
use dotenvy::*;
use std::env;
use std::{env, error::Error, result::Result};

#[test]
fn test_multiline() {
fn test_multiline() -> Result<(), Box<dyn Error>> {
let value = "-----BEGIN PRIVATE KEY-----\n-----END PRIVATE KEY-----\\n\\\"QUOTED\\\"";
let weak = "-----BEGIN PRIVATE KEY-----\n-----END PRIVATE KEY-----\n\"QUOTED\"";
let dir = tempdir_with_dotenv(&format!(
Expand All @@ -21,26 +21,26 @@ WEAK="{}"
STRONG='{}'
"#,
value, value
))
.unwrap();
))?;

dotenv().ok();
assert_eq!(var("KEY").unwrap(), r#"my cool value"#);
dotenv()?;
assert_eq!(var("KEY")?, r#"my cool value"#);
assert_eq!(
var("KEY3").unwrap(),
var("KEY3")?,
r#"awesome "stuff"
more
on other
lines"#
);
assert_eq!(
var("KEY4").unwrap(),
var("KEY4")?,
r#"hello 'world
good ' 'morning"#
);
assert_eq!(var("WEAK").unwrap(), weak);
assert_eq!(var("STRONG").unwrap(), value);
assert_eq!(var("WEAK")?, weak);
assert_eq!(var("STRONG")?, value);

env::set_current_dir(dir.path().parent().unwrap()).unwrap();
dir.close().unwrap();
env::set_current_dir(dir.path().parent().unwrap())?;
dir.close()?;
Ok(())
}
14 changes: 7 additions & 7 deletions dotenv/tests/test-var.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
mod common;

use std::env;

use dotenvy::*;
use std::{env, error::Error, result::Result};

use crate::common::*;

#[test]
fn test_var() {
let dir = make_test_dotenv().unwrap();
fn test_var() -> Result<(), Box<dyn Error>> {
let dir = make_test_dotenv()?;

assert_eq!(var("TESTKEY").unwrap(), "test_val");
assert_eq!(var("TESTKEY")?, "test_val");

env::set_current_dir(dir.path().parent().unwrap()).unwrap();
dir.close().unwrap();
env::set_current_dir(dir.path().parent().unwrap())?;
dir.close()?;
Ok(())
}
27 changes: 12 additions & 15 deletions dotenv/tests/test-variable-substitution.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
mod common;

use dotenvy::*;
use std::env;
use std::{env, error::Error, result::Result};

use crate::common::*;

#[test]
fn test_variable_substitutions() {
fn test_variable_substitutions() -> Result<(), Box<dyn Error>> {
std::env::set_var("KEY", "value");
std::env::set_var("KEY1", "value1");

Expand All @@ -25,18 +25,14 @@ SUBSTITUTION_FOR_WEAK_QUOTES="{}"
SUBSTITUTION_WITHOUT_QUOTES={}
"#,
common_string, common_string, common_string
))
.unwrap();
))?;

assert_eq!(var("KEY").unwrap(), "value");
assert_eq!(var("KEY1").unwrap(), "value1");
assert_eq!(var("KEY_U").unwrap(), "value+valueU");
assert_eq!(var("KEY")?, "value");
assert_eq!(var("KEY1")?, "value1");
assert_eq!(var("KEY_U")?, "value+valueU");
assert_eq!(var("SUBSTITUTION_FOR_STRONG_QUOTES")?, common_string);
assert_eq!(
var("SUBSTITUTION_FOR_STRONG_QUOTES").unwrap(),
common_string
);
assert_eq!(
var("SUBSTITUTION_FOR_WEAK_QUOTES").unwrap(),
var("SUBSTITUTION_FOR_WEAK_QUOTES")?,
[
"",
"value",
Expand All @@ -49,7 +45,7 @@ SUBSTITUTION_WITHOUT_QUOTES={}
.join(">>")
);
assert_eq!(
var("SUBSTITUTION_WITHOUT_QUOTES").unwrap(),
var("SUBSTITUTION_WITHOUT_QUOTES")?,
[
"",
"value",
Expand All @@ -62,6 +58,7 @@ SUBSTITUTION_WITHOUT_QUOTES={}
.join(">>")
);

env::set_current_dir(dir.path().parent().unwrap()).unwrap();
dir.close().unwrap();
env::set_current_dir(dir.path().parent().unwrap())?;
dir.close()?;
Ok(())
}
12 changes: 6 additions & 6 deletions dotenv/tests/test-vars.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
mod common;

use std::collections::HashMap;
use std::env;
use std::{collections::HashMap, env, error::Error, result::Result};

use dotenvy::*;

use crate::common::*;

#[test]
fn test_vars() {
let dir = make_test_dotenv().unwrap();
fn test_vars() -> Result<(), Box<dyn Error>> {
let dir = make_test_dotenv()?;

let vars: HashMap<String, String> = vars().collect();

assert_eq!(vars["TESTKEY"], "test_val");

env::set_current_dir(dir.path().parent().unwrap()).unwrap();
dir.close().unwrap();
env::set_current_dir(dir.path().parent().unwrap())?;
dir.close()?;
Ok(())
}

0 comments on commit 269a343

Please sign in to comment.