Skip to content

Commit

Permalink
Load environment file from dotenv-path relative to working directory (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Jun 13, 2024
1 parent 1ce7a05 commit 4b5ba8f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/load_dotenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ pub(crate) fn load_dotenv(
}

if let Some(path) = dotenv_path {
let path = working_directory.join(path);
if path.is_file() {
return load_from_file(&working_directory.join(path));
return load_from_file(&path);
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/dotenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ fn no_dotenv() {
.stderr("echo DEFAULT\n")
.run();
}

#[test]
fn dotenv_env_var_override() {
Test::new()
Expand All @@ -375,3 +376,21 @@ fn dotenv_env_var_override() {
.stderr("echo $DOTENV_KEY\n")
.run();
}

#[test]
fn dotenv_path_usable_from_subdir() {
Test::new()
.justfile(
"
set dotenv-path := '.custom-env'
@echo:
echo $DOTENV_KEY
",
)
.create_dir("sub")
.current_dir("sub")
.write(".custom-env", "DOTENV_KEY=dotenv-value")
.stdout("dotenv-value\n")
.run();
}
5 changes: 5 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ impl Test {
self
}

pub(crate) fn create_dir(self, path: impl AsRef<Path>) -> Self {
fs::create_dir_all(self.tempdir.path().join(path.as_ref())).unwrap();
self
}

pub(crate) fn current_dir(mut self, path: impl AsRef<Path>) -> Self {
path.as_ref().clone_into(&mut self.current_dir);
self
Expand Down

0 comments on commit 4b5ba8f

Please sign in to comment.