Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty template names to be set in the cli #624

Merged
merged 2 commits into from
Oct 31, 2024
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ All notable changes to MiniJinja are documented here.
- Reversing bytes and convergint them implicitly to strings will now work
more consistently. #619
- Added type hints for the Python binding and relaxed maturin constraint. #590
- `minijinja-cli` now allows the template name to be set to an empty
string when `--template` is used, to allow suppliying a data file. #624

## 2.4.0

Expand Down
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
Loading