Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/handlers/get_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn get_value(options: &Options) -> bool {
let env_object = options.env_object.as_ref().unwrap();
let mut all_found = true;

let keys: Vec<String> = if options.target_keys.is_empty() {
let keys: Vec<String> = if options.return_all_keys {
env_object.keys().cloned().collect()
} else {
options.target_keys.clone()
Expand Down
100 changes: 100 additions & 0 deletions tests/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use assert_cmd::Command;
use predicates::prelude::*;
use std::path::Path;

fn bin() -> Command {
Command::cargo_bin("dotenv").unwrap()
}

fn env_path() -> String {
let here = Path::new(env!("CARGO_MANIFEST_DIR"));
here.join("tests/.env.test").to_string_lossy().to_string()
}

#[test]
fn debug_reports_keys_and_file() {
bin()
.arg("NAME")
.arg("--debug")
.arg("--file")
.arg(env_path())
.assert()
.success()
.stdout("dotenv-cli\n")
.stderr(
predicate::str::contains("Keys: [\"NAME\"]")
.and(predicate::str::contains(".env.test"))
.and(predicate::str::contains("Options assembled")),
);
}

#[test]
fn debug_reports_json_defaulting_and_wildcards() {
bin()
.arg("NESTED_*")
.arg("--debug")
.arg("--file")
.arg(env_path())
.assert()
.success()
.stderr(predicate::str::contains("Wildcard found"));
}

#[test]
fn debug_reports_json_defaulting_for_multiple_keys() {
bin()
.arg("NAME")
.arg("EMPTY")
.arg("--debug")
.arg("--file")
.arg(env_path())
.assert()
.success()
.stderr(predicate::str::contains(
"Key count (0 or >1) defaulting to JSON",
));
}

#[test]
fn debug_reports_file_during_validate() {
bin()
.arg("--validate")
.arg("--debug")
.arg("--file")
.arg(env_path())
.assert()
.success()
.stderr(predicate::str::contains("File: "));
}

#[test]
fn debug_reports_command_before_running_it() {
bin()
.arg("--debug")
.arg("--file")
.arg(env_path())
.arg("--")
.arg("true")
.assert()
.success()
.stderr(
predicate::str::contains("Command: [\"true\"]")
.and(predicate::str::contains("Running: true")),
);
}

#[test]
fn debug_reports_variables_skipped_because_already_set() {
bin()
.arg("--debug")
.arg("--file")
.arg(env_path())
.env("NAME", "from-the-shell")
.arg("--")
.arg("true")
.assert()
.success()
.stderr(predicate::str::contains(
"Skipping NAME (already set in the environment)",
));
}
89 changes: 89 additions & 0 deletions tests/wildcard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use assert_cmd::Command;
use std::path::Path;

fn bin() -> Command {
Command::cargo_bin("dotenv").unwrap()
}

fn env_path() -> String {
let here = Path::new(env!("CARGO_MANIFEST_DIR"));
here.join("tests/.env.test").to_string_lossy().to_string()
}

fn keys_for(pattern: &str) -> Vec<String> {
let output = bin()
.arg(pattern)
.arg("--file")
.arg(env_path())
.assert()
.success()
.get_output()
.stdout
.clone();
let value: serde_json::Value = serde_json::from_slice(&output).unwrap();
value.as_object().unwrap().keys().cloned().collect()
}

#[test]
fn trailing_wildcard_matches_by_prefix() {
assert_eq!(keys_for("NESTED_*"), vec!["NESTED_VAR1", "NESTED_VAR2"]);
}

#[test]
fn leading_wildcard_matches_by_suffix() {
assert_eq!(
keys_for("*_MULTI"),
vec!["DOUBLE_MULTI", "SINGLE_MULTI", "CORRECT_MULTI"]
);
}

#[test]
fn wildcard_on_both_sides_matches_the_middle() {
assert_eq!(keys_for("*_MULTI_*"), vec!["LIST_MULTI_LINE"]);
}

#[test]
fn wildcard_output_resolves_nested_variables() {
bin()
.arg("NESTED_VAR2*")
.arg("--file")
.arg(env_path())
.assert()
.success()
.stdout("{\"NESTED_VAR2\":\"Hello World\"}\n");
}

#[test]
fn wildcard_forces_json_even_for_a_single_match() {
bin()
.arg("NAM*")
.arg("--file")
.arg(env_path())
.assert()
.success()
.stdout("{\"NAME\":\"dotenv-cli\"}\n");
}

#[test]
fn wildcard_matching_nothing_returns_an_empty_object_not_the_whole_file() {
bin()
.arg("ZZZ_*")
.arg("--file")
.arg(env_path())
.assert()
.success()
.stdout("{}\n");
}

#[test]
fn no_json_disables_wildcard_expansion() {
// With --no-json the pattern is treated as a literal key name, which does
// not exist, so the lookup fails.
bin()
.arg("NESTED_*")
.arg("--no-json")
.arg("--file")
.arg(env_path())
.assert()
.failure();
}