Skip to content

Commit

Permalink
Warn if .env file is loaded and dotenv-load isn't set
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Jul 28, 2021
1 parent 9ee1a63 commit a91ea42
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 45 deletions.
21 changes: 10 additions & 11 deletions src/load_dotenv.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::common::*;

// Remove this on 2021-07-01.
#[allow(unused)]
pub(crate) fn load_dotenv(
config: &Config,
settings: &Settings,
Expand All @@ -19,15 +17,16 @@ pub(crate) fn load_dotenv(
let path = directory.join(".env");

if path.is_file() {
// Un-comment this on 2021-07-01.
//
// if settings.dotenv_load.is_none() && config.verbosity.loud() {
// if config.color.stderr().active() {
// eprintln!("{:#}", Warning::DotenvLoad);
// } else {
// eprintln!("{}", Warning::DotenvLoad);
// }
// }
if settings.dotenv_load.is_none()
&& config.verbosity.loud()
&& !std::env::var_os("JUST_SUPPRESS_DOTENV_LOAD_WARNING")
.map(|val| val.as_os_str().to_str() == Some("1"))
.unwrap_or(false)
{
Warning::DotenvLoad
.write(&mut io::stderr(), config.color.stderr())
.ok();
}

let iter = dotenv::from_path_iter(&path)?;
let mut dotenv = BTreeMap::new();
Expand Down
1 change: 1 addition & 0 deletions src/warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl Warning {
#[rustfmt::skip]
write!(w, "\
A `.env` file was found and loaded, but this behavior will change in the future.
To silence this warning and continue loading `.env` files, add:
set dotenv-load := true
Expand Down
56 changes: 32 additions & 24 deletions tests/dotenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,35 @@ test! {
stderr: "echo $DOTENV_KEY\n",
}

// Un-comment this on 2021-07-01.
//
// test! {
// name: warning,
// justfile: r#"
// foo:
// echo $DOTENV_KEY
// "#,
// stdout: "dotenv-value\n",
// stderr: "
// warning: A `.env` file was found and loaded, but this behavior will
// change in the future. To silence this warning and continue loading `.env`
// files, add:

// set dotenv-load := true

// To silence this warning and stop loading `.env` files, add:

// set dotenv-load := false

// See https://github.com/casey/just/issues/469 for more details.
// echo $DOTENV_KEY
// ",
// }
#[test]
fn warning() {
Test::new()
.justfile(
"
foo:
echo $DOTENV_KEY
",
)
.stdout("dotenv-value\n")
.stderr(
"
warning: A `.env` file was found and loaded, but this behavior will change in the future.
To \
silence this warning and continue loading `.env` files, add:
set dotenv-load := true
To silence \
this warning and stop loading `.env` files, add:
set dotenv-load := false
See https://github.com/casey/just/issues/469 \
for more details.
echo $DOTENV_KEY
",
)
.suppress_dotenv_load_warning(false)
.run();
}
35 changes: 25 additions & 10 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ macro_rules! test {
}

pub(crate) struct Test {
pub(crate) tempdir: TempDir,
pub(crate) justfile: Option<String>,
pub(crate) args: Vec<String>,
pub(crate) env: BTreeMap<String, String>,
pub(crate) stdin: String,
pub(crate) stdout: String,
pub(crate) stderr: String,
pub(crate) tempdir: TempDir,
pub(crate) justfile: Option<String>,
pub(crate) args: Vec<String>,
pub(crate) env: BTreeMap<String, String>,
pub(crate) stdin: String,
pub(crate) stdout: String,
pub(crate) stderr: String,
pub(crate) stderr_regex: Option<Regex>,
pub(crate) status: i32,
pub(crate) shell: bool,
pub(crate) status: i32,
pub(crate) shell: bool,
pub(crate) suppress_dotenv_load_warning: bool,
}

impl Test {
Expand All @@ -55,12 +56,13 @@ impl Test {
args: Vec::new(),
env: BTreeMap::new(),
justfile: Some(String::new()),
stderr_regex: None,
shell: true,
status: EXIT_SUCCESS,
stderr: String::new(),
stderr_regex: None,
stdin: String::new(),
stdout: String::new(),
suppress_dotenv_load_warning: true,
tempdir,
}
}
Expand Down Expand Up @@ -125,6 +127,11 @@ impl Test {
self.stdout = stdout.into();
self
}

pub(crate) fn suppress_dotenv_load_warning(mut self, suppress_dotenv_load_warning: bool) -> Self {
self.suppress_dotenv_load_warning = suppress_dotenv_load_warning;
self
}
}

impl Test {
Expand All @@ -150,6 +157,14 @@ impl Test {
let mut child = command
.args(self.args)
.envs(&self.env)
.env(
"JUST_SUPPRESS_DOTENV_LOAD_WARNING",
if self.suppress_dotenv_load_warning {
"1"
} else {
"0"
},
)
.current_dir(self.tempdir.path())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
Expand Down

0 comments on commit a91ea42

Please sign in to comment.