Skip to content

Commit

Permalink
Clean tests
Browse files Browse the repository at this point in the history
* Clean tests

* pnpm => npm
  • Loading branch information
arniu authored Nov 25, 2022
1 parent 85bd31c commit 60dd8df
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 78 deletions.
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "dotenv-rs",
"version": "0.0.0",
"name": "scripts",
"private": true,
"scripts": {
"env2json": "node env2json"
"env2json": "node scripts/env2json"
},
"devDependencies": {
"dependencies": {
"dotenv": "^16.0.3",
"dotenv-expand": "^9.0.0"
}
Expand Down
21 changes: 0 additions & 21 deletions pnpm-lock.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion env2json.js → scripts/env2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fs = require("fs");
const DOTENV = /\.env$/;

(function main() {
const dir = path.resolve(__dirname, "tests/fixtures");
const dir = path.resolve(__dirname, "../tests/fixtures");
Promise.all(dotenvs(dir).map(convert)).then((files) => {
console.debug(`done with ${files.length} files!\n`);
});
Expand Down
45 changes: 26 additions & 19 deletions tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,37 @@ use std::io;
use std::io::prelude::*;
use tempfile::{tempdir, TempDir};

pub fn make_test_dotenv() -> io::Result<TempDir> {
make_dotenv("TEST_KEY=test_val")
}
type Exps = std::collections::HashMap<String, String>;

pub fn make_basic_dotenv() -> io::Result<TempDir> {
make_dotenv(include_str!("sample-basic.env"))
}
fn make_test(dotenv: &str, expect: &str) -> io::Result<(TempDir, Exps)> {
let cwd = tempdir()?;
std::env::set_current_dir(cwd.path())?;

pub fn make_expand_dotenv() -> io::Result<TempDir> {
make_dotenv(include_str!("sample-expand.env"))
}
let dotenv_path = cwd.path().join(".env");
let mut dotenv_file = fs::File::create(dotenv_path)?;
dotenv_file.write_all(dotenv.as_bytes())?;
dotenv_file.sync_all()?;

pub fn make_multiline_dotenv() -> io::Result<TempDir> {
make_dotenv(include_str!("sample-multiline.env"))
Ok((cwd, serde_json::from_str(expect)?))
}

fn make_dotenv(dotenv_text: &str) -> io::Result<TempDir> {
let dir = tempdir()?;
std::env::set_current_dir(dir.path())?;
pub fn with_basic_dotenv() -> io::Result<(TempDir, Exps)> {
make_test(
include_str!("sample-basic.env"),
include_str!("sample-basic.json"),
)
}

let dotenv_path = dir.path().join(".env");
let mut dotenv_file = fs::File::create(dotenv_path)?;
dotenv_file.write_all(dotenv_text.as_bytes())?;
dotenv_file.sync_all()?;
pub fn with_expand_dotenv() -> io::Result<(TempDir, Exps)> {
make_test(
include_str!("sample-expand.env"),
include_str!("sample-expand.json"),
)
}

Ok(dir)
pub fn with_multiline_dotenv() -> io::Result<(TempDir, Exps)> {
make_test(
include_str!("sample-multiline.env"),
include_str!("sample-multiline.json"),
)
}
6 changes: 3 additions & 3 deletions tests/test-dotenv-find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use std::{env, fs};

#[test]
fn test_find() -> anyhow::Result<()> {
let _cwd = make_test_dotenv()?;
let _t = with_basic_dotenv()?;

fs::create_dir("child")?;
env::set_current_dir("child")?;

assert!(env::var("TEST_KEY").is_err());
assert_eq!(dotenv::var("TEST_KEY")?, "test_val");
assert!(env::var("BASIC").is_err());
assert_eq!(dotenv::var("BASIC")?, "basic");

Ok(())
}
6 changes: 3 additions & 3 deletions tests/test-dotenv-var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::env;

#[test]
fn test_var() -> anyhow::Result<()> {
let _cwd = make_test_dotenv()?;
let _t = with_basic_dotenv()?;

assert!(env::var("TEST_KEY").is_err());
assert_eq!(dotenv::var("TEST_KEY")?, "test_val");
assert!(env::var("BASIC").is_err());
assert_eq!(dotenv::var("BASIC")?, "basic");

Ok(())
}
12 changes: 3 additions & 9 deletions tests/test-sample-basic.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
mod fixtures;
use fixtures::*;

use std::collections::HashMap;

static SAMPLE_JSON: &str = include_str!("fixtures/sample-basic.json");

#[test]
fn test_sample() -> anyhow::Result<()> {
let _cwd = make_basic_dotenv()?;

let map: HashMap<String, String> = serde_json::from_str(SAMPLE_JSON)?;
let (_t, exps) = with_basic_dotenv()?;
for (key, value) in dotenv::from_filename(".env")?.iter() {
let expected = map.get(key).unwrap();
assert_eq!(expected, &value, "check value for {}: ", key);
let expected = exps.get(key).unwrap();
assert_eq!(expected, &value, "check {}", key);
}

Ok(())
Expand Down
12 changes: 3 additions & 9 deletions tests/test-sample-expand.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
mod fixtures;
use fixtures::*;

use std::collections::HashMap;

static SAMPLE_JSON: &str = include_str!("fixtures/sample-expand.json");

#[test]
fn test_sample() -> anyhow::Result<()> {
let _cwd = make_expand_dotenv()?;

let map: HashMap<String, String> = serde_json::from_str(SAMPLE_JSON)?;
let (_t, exps) = with_expand_dotenv()?;
for (key, value) in dotenv::from_filename(".env")?.iter() {
let expected = map.get(key).unwrap();
assert_eq!(expected, &value, "check value for {}: ", key);
let expected = exps.get(key).unwrap();
assert_eq!(expected, &value, "check {}", key);
}

Ok(())
Expand Down
12 changes: 3 additions & 9 deletions tests/test-sample-multiline.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
mod fixtures;
use fixtures::*;

use std::collections::HashMap;

static SAMPLE_JSON: &str = include_str!("fixtures/sample-multiline.json");

#[test]
fn test_sample() -> anyhow::Result<()> {
let _cwd = make_multiline_dotenv()?;

let map: HashMap<String, String> = serde_json::from_str(SAMPLE_JSON)?;
let (_t, exps) = with_multiline_dotenv()?;
for (key, value) in dotenv::from_filename(".env")?.iter() {
let expected = map.get(key).unwrap();
assert_eq!(expected, &value, "check value for {}: ", key);
let expected = exps.get(key).unwrap();
assert_eq!(expected, &value, "check {}", key);
}

Ok(())
Expand Down

0 comments on commit 60dd8df

Please sign in to comment.