-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Search for
.env
file from working directory (#661)
Search for a `.env` file starting in the working directory, instead of the invocation directory.
- Loading branch information
Showing
4 changed files
with
49 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,26 @@ | ||
use crate::common::*; | ||
|
||
pub(crate) fn load_dotenv() -> RunResult<'static, BTreeMap<String, String>> { | ||
// `dotenv::dotenv_iter` should eventually be un-deprecated, see: | ||
pub(crate) fn load_dotenv( | ||
working_directory: &Path, | ||
) -> RunResult<'static, BTreeMap<String, String>> { | ||
// `dotenv::from_path_iter` should eventually be un-deprecated, see: | ||
// https://github.com/dotenv-rs/dotenv/issues/13 | ||
#![allow(deprecated)] | ||
match dotenv::dotenv_iter() { | ||
Ok(iter) => { | ||
for directory in working_directory.ancestors() { | ||
let path = directory.join(".env"); | ||
|
||
if path.is_file() { | ||
let iter = dotenv::from_path_iter(&path)?; | ||
let mut dotenv = BTreeMap::new(); | ||
for result in iter { | ||
let (key, value) = result.map_err(|dotenv_error| RuntimeError::Dotenv { dotenv_error })?; | ||
let (key, value) = result?; | ||
if env::var_os(&key).is_none() { | ||
dotenv.insert(key, value); | ||
} | ||
} | ||
Ok(dotenv) | ||
}, | ||
Err(dotenv_error) => | ||
if dotenv_error.not_found() { | ||
Ok(BTreeMap::new()) | ||
} else { | ||
Err(RuntimeError::Dotenv { dotenv_error }) | ||
}, | ||
return Ok(dotenv); | ||
} | ||
} | ||
|
||
Ok(BTreeMap::new()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use executable_path::executable_path; | ||
use std::{process, str}; | ||
|
||
use test_utilities::tmptree; | ||
|
||
#[test] | ||
fn dotenv() { | ||
let tmp = tmptree! { | ||
".env": "KEY=ROOT", | ||
sub: { | ||
".env": "KEY=SUB", | ||
justfile: "default:\n\techo KEY=$KEY", | ||
}, | ||
}; | ||
|
||
let binary = executable_path("just"); | ||
|
||
let output = process::Command::new(binary) | ||
.current_dir(tmp.path()) | ||
.arg("sub/default") | ||
.output() | ||
.expect("just invocation failed"); | ||
|
||
assert_eq!(output.status.code().unwrap(), 0); | ||
|
||
let stdout = str::from_utf8(&output.stdout).unwrap(); | ||
assert_eq!(stdout, "KEY=SUB\n"); | ||
} |