Load environment file from dotenv-path relative to working directory (#2152)

This commit is contained in:
Casey Rodarmor 2024-06-13 13:21:00 -07:00 committed by GitHub
parent 1ce7a05bef
commit 4b5ba8f6f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 1 deletions

View File

@ -24,8 +24,9 @@ pub(crate) fn load_dotenv(
} }
if let Some(path) = dotenv_path { if let Some(path) = dotenv_path {
let path = working_directory.join(path);
if path.is_file() { if path.is_file() {
return load_from_file(&working_directory.join(path)); return load_from_file(&path);
} }
} }

View File

@ -360,6 +360,7 @@ fn no_dotenv() {
.stderr("echo DEFAULT\n") .stderr("echo DEFAULT\n")
.run(); .run();
} }
#[test] #[test]
fn dotenv_env_var_override() { fn dotenv_env_var_override() {
Test::new() Test::new()
@ -375,3 +376,21 @@ fn dotenv_env_var_override() {
.stderr("echo $DOTENV_KEY\n") .stderr("echo $DOTENV_KEY\n")
.run(); .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();
}

View File

@ -94,6 +94,11 @@ impl Test {
self 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 { pub(crate) fn current_dir(mut self, path: impl AsRef<Path>) -> Self {
path.as_ref().clone_into(&mut self.current_dir); path.as_ref().clone_into(&mut self.current_dir);
self self