Make dotenv-path relative to working directory (#2040)

This commit is contained in:
Casey Rodarmor 2024-05-14 21:31:58 -07:00 committed by GitHub
parent caace0a115
commit 07aaa4f440
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 6 deletions

View File

@ -23,7 +23,7 @@ pub(crate) fn load_dotenv(
} }
if let Some(path) = dotenv_path { 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()); let filename = dotenv_filename.map_or(DEFAULT_DOTENV_FILENAME, |s| s.as_str());

View File

@ -186,7 +186,7 @@ fn can_set_dotenv_path_from_justfile() {
Test::new() Test::new()
.justfile( .justfile(
r#" r#"
set dotenv-path:= "subdir/.env" set dotenv-path := "subdir/.env"
foo: foo:
@echo $JUST_TEST_VARIABLE @echo $JUST_TEST_VARIABLE
@ -228,7 +228,7 @@ fn program_argument_has_priority_for_dotenv_path() {
Test::new() Test::new()
.justfile( .justfile(
r#" r#"
set dotenv-path:= "subdir/.env" set dotenv-path := "subdir/.env"
foo: foo:
@echo $JUST_TEST_VARIABLE @echo $JUST_TEST_VARIABLE
@ -245,3 +245,20 @@ fn program_argument_has_priority_for_dotenv_path() {
.status(EXIT_SUCCESS) .status(EXIT_SUCCESS)
.run(); .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();
}

View File

@ -203,9 +203,7 @@ impl Test {
}; };
let stderr = unindent(&self.stderr); let stderr = unindent(&self.stderr);
let mut dotenv_path = self.tempdir.path().to_path_buf(); fs::write(self.tempdir.path().join(".env"), "DOTENV_KEY=dotenv-value").unwrap();
dotenv_path.push(".env");
fs::write(dotenv_path, "DOTENV_KEY=dotenv-value").unwrap();
let mut command = Command::new(executable_path("just")); let mut command = Command::new(executable_path("just"));