Skip to content

Commit

Permalink
Allow empty template names to be set in the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Oct 31, 2024
1 parent 765039b commit 60c9bba
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
5 changes: 3 additions & 2 deletions minijinja-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,13 @@ pub fn execute() -> Result<i32, Error> {
.map(|x| x.as_str()),
) {
(None, Some(STDIN_STDOUT)) => (Cow::Borrowed(STDIN_STDOUT), None),
(None, Some("")) => bail!("Empty template names are only valid with --template."),
(None, Some(rel_name)) => (
Cow::Owned(cwd.join(rel_name).to_string_lossy().to_string()),
None,
),
(Some(source), Some(STDIN_STDOUT)) => (Cow::Borrowed("<string>"), Some(source.clone())),
_ => unreachable!(),
(Some(source), None | Some("")) => (Cow::Borrowed("<string>"), Some(source.clone())),
_ => bail!("When --template is used, a template cannot be passed as argument (only an empty argument is allowed)."),
};

let mut output = Output::new(matches.get_one::<PathBuf>("output").unwrap())?;
Expand Down
8 changes: 6 additions & 2 deletions minijinja-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// application and by build.rs to generate shell completions.
use std::path::PathBuf;

use clap::builder::ArgPredicate;
use clap::{arg, command, value_parser, ArgAction, Command};

const ADVANCED: &str = "Advanced";
Expand Down Expand Up @@ -324,9 +325,12 @@ pub(super) fn make_command() -> Command {
This is the path to the input template in MiniJinja/Jinja2 syntax. \
If not provided this defaults to '-' which means the template is \
loaded from stdin. When the format is set to 'auto' which is the \
default, the extension of the filename is used to detect the format.")
default, the extension of the filename is used to detect the format.\n\n\
\
This argument can be set to an empty string when --template is provided \
to allow a data file to be supplied.")
.default_value("-")
.conflicts_with("template"),
.default_value_if("template", ArgPredicate::IsPresent, None),
arg!(data_file: [DATA_FILE] "Path to the data file")
.long_help("\
Path to the data file in the given format.\n\n\
Expand Down
57 changes: 57 additions & 0 deletions minijinja-cli/tests/test_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,63 @@ fn test_template_string() {
"###);
}

#[test]
#[allow(clippy::suspicious_command_arg_space)]
fn test_empty_template_name_with_string_template() {
let input = file_with_contents_and_ext(r#"{"name": "Peter"}"#, ".json");
assert_cmd_snapshot!(
cli()
.arg("-tHello {{ name }}")
.arg("")
.arg(input.path())
.arg("--no-newline"),
@r###"
success: true
exit_code: 0
----- stdout -----
Hello Peter
----- stderr -----
"###);
}

#[test]
#[allow(clippy::suspicious_command_arg_space)]
fn test_template_name_with_string_template_fails() {
let input = file_with_contents_and_ext(r#"{"name": "Peter"}"#, ".json");
assert_cmd_snapshot!(
cli()
.arg("-tHello {{ name }}")
.arg("invalid.tmpl")
.arg(input.path())
.arg("--no-newline"),
@r###"
success: false
exit_code: 1
----- stdout -----
----- stderr -----
error: When --template is used, a template cannot be passed as argument (only an empty argument is allowed).
"###);
}

#[test]
fn test_empty_template_name_errors() {
let input = file_with_contents_and_ext(r#"{"name": "Peter"}"#, ".json");
assert_cmd_snapshot!(
cli()
.arg("")
.arg(input.path())
.arg("--no-newline"),
@r###"
success: false
exit_code: 1
----- stdout -----
----- stderr -----
error: Empty template names are only valid with --template.
"###);
}

#[test]
fn test_print_config_fully_loaded() {
assert_cmd_snapshot!(
Expand Down

0 comments on commit 60c9bba

Please sign in to comment.