From 07aaa4f440f5445ff41793aa2c5bac92e2e834e3 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 14 May 2024 21:31:58 -0700 Subject: [PATCH] Make `dotenv-path` relative to working directory (#2040) --- src/load_dotenv.rs | 2 +- tests/dotenv.rs | 21 +++++++++++++++++++-- tests/test.rs | 4 +--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/load_dotenv.rs b/src/load_dotenv.rs index fb129d2..f543d02 100644 --- a/src/load_dotenv.rs +++ b/src/load_dotenv.rs @@ -23,7 +23,7 @@ pub(crate) fn load_dotenv( } if let Some(path) = dotenv_path { - return load_from_file(path); + return load_from_file(&working_directory.join(path)); } let filename = dotenv_filename.map_or(DEFAULT_DOTENV_FILENAME, |s| s.as_str()); diff --git a/tests/dotenv.rs b/tests/dotenv.rs index 7c5a630..43f5e6b 100644 --- a/tests/dotenv.rs +++ b/tests/dotenv.rs @@ -186,7 +186,7 @@ fn can_set_dotenv_path_from_justfile() { Test::new() .justfile( r#" - set dotenv-path:= "subdir/.env" + set dotenv-path := "subdir/.env" foo: @echo $JUST_TEST_VARIABLE @@ -228,7 +228,7 @@ fn program_argument_has_priority_for_dotenv_path() { Test::new() .justfile( r#" - set dotenv-path:= "subdir/.env" + set dotenv-path := "subdir/.env" foo: @echo $JUST_TEST_VARIABLE @@ -245,3 +245,20 @@ fn program_argument_has_priority_for_dotenv_path() { .status(EXIT_SUCCESS) .run(); } + +#[test] +fn dotenv_path_is_relative_to_working_directory() { + Test::new() + .justfile( + " + set dotenv-path := '.env' + + foo: + @echo $DOTENV_KEY + ", + ) + .tree(tree! { subdir: { } }) + .current_dir("subdir") + .stdout("dotenv-value\n") + .run(); +} diff --git a/tests/test.rs b/tests/test.rs index a200de7..65ee30a 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -203,9 +203,7 @@ impl Test { }; let stderr = unindent(&self.stderr); - let mut dotenv_path = self.tempdir.path().to_path_buf(); - dotenv_path.push(".env"); - fs::write(dotenv_path, "DOTENV_KEY=dotenv-value").unwrap(); + fs::write(self.tempdir.path().join(".env"), "DOTENV_KEY=dotenv-value").unwrap(); let mut command = Command::new(executable_path("just"));