Skip to content

Commit

Permalink
Make dotenv-path relative to working directory (#2040)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored May 15, 2024
1 parent caace0a commit 07aaa4f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/load_dotenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
21 changes: 19 additions & 2 deletions tests/dotenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
4 changes: 1 addition & 3 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down

0 comments on commit 07aaa4f

Please sign in to comment.