Skip to content

Commit

Permalink
Enable all features by default in minijinja-cli (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
KonishchevDmitry authored Nov 4, 2024
1 parent 4f7d614 commit 009ff28
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ All notable changes to MiniJinja are documented here.
rendering. #627
- Syntax errors caused by the lexer now include the correct position of
the error. #630
- `minijinja-cli` now has all features enabled by default as documented
(that means also shell completion and ini). #633
- `minijinja-cli` now does not convert INI files to lowercase anymore. This was
an unintended behavior. #633

## 2.4.0

Expand Down
2 changes: 1 addition & 1 deletion minijinja-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ readme = "README.md"
rust-version = "1.65"

[features]
default = ["toml", "yaml", "querystring", "cbor", "datetime", "json5", "repl", "unicode", "contrib", "preserve_order"]
default = ["toml", "yaml", "querystring", "cbor", "datetime", "json5", "repl", "completions", "unicode", "ini", "contrib", "preserve_order"]
yaml = ["serde_yml"]
querystring = ["serde_qs"]
cbor = ["ciborium"]
Expand Down
4 changes: 2 additions & 2 deletions minijinja-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ The following formats are supported:
- `toml` (`*.toml`): TOML
- `cbor` (`*.cbor`): CBOR
- `querystring` (`*.qs`): URL encoded query strings
- `ini` (`*.ini`, `*.config`, `*.properties`): text only INI files
- `ini` (`*.ini`, `*.conf`, `*.config`, `*.properties`): text only INI files

For most formats there is a pretty straightforward mapping into the template
context. The only exception to this is currently INI files where sections are
effectively mandatory. If keys are placed in the unnamed section, the second
is renamed to `default`. You can use `--select` to make a section be implied:

```
minijinja-cli template.j2 input.ini --section default
minijinja-cli template.j2 input.ini --select default
```

Note that not all formats support all input types. For instance querystring
Expand Down
2 changes: 1 addition & 1 deletion minijinja-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fn load_data(
#[cfg(feature = "ini")]
"ini" => {
let contents = String::from_utf8(contents).context("invalid utf-8")?;
let mut config = configparser::ini::Ini::new();
let mut config = configparser::ini::Ini::new_cs();
config
.read(contents)
.map_err(|msg| anyhow::anyhow!("could not load ini: {}", msg))?;
Expand Down
16 changes: 11 additions & 5 deletions minijinja-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ pub static SUPPORTED_FORMATS: &[(&str, &str, &[&str])] = &[
#[cfg(feature = "cbor")]
("cbor", "CBOR", &["cbor"]),
#[cfg(feature = "ini")]
("ini", "INI / Config", &["ini", "config", "properties"]),
(
"ini",
"INI / Config",
&["ini", "conf", "config", "properties"],
),
#[cfg(not(feature = "json5"))]
("json", "JSON", &["json"]),
#[cfg(feature = "json5")]
Expand Down Expand Up @@ -79,15 +83,17 @@ pub(super) fn make_command() -> Command {
[env var: MINIJINJA_FORMAT]"))
.value_parser([
"auto",
#[cfg(feature = "cbor")]
"cbor",
#[cfg(feature = "ini")]
"ini",
"json",
#[cfg(feature = "querystring")]
"querystring",
#[cfg(feature = "yaml")]
"yaml",
#[cfg(feature = "toml")]
"toml",
#[cfg(feature = "cbor")]
"cbor",
#[cfg(feature = "yaml")]
"yaml",
]),
arg!(-a --autoescape <MODE> "Reconfigures autoescape behavior")
.long_help("\
Expand Down
7 changes: 5 additions & 2 deletions minijinja-cli/tests/snapshots/test_basic__long_help.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Arguments:
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.

This argument can be set to an empty string when --template is provided to allow a data
file to be supplied.

[default: -]

[DATA_FILE]
Expand All @@ -70,7 +73,7 @@ Options:

- auto
- cbor (CBOR): *.cbor
- ini (INI / Config): *.ini, *.config, *.properties
- ini (INI / Config): *.ini, *.conf, *.config, *.properties
- json (JSON / JSON5): *.json, *.json5
- querystring (Query String / Form Encoded): *.qs
- toml (TOML): *.toml
Expand All @@ -84,7 +87,7 @@ Options:

[env var: MINIJINJA_FORMAT]

[possible values: auto, json, querystring, yaml, toml, cbor]
[possible values: auto, cbor, ini, json, querystring, toml, yaml]

-D, --define <EXPR>
This defines an input variable for the template. This is used in addition to the input
Expand Down
4 changes: 2 additions & 2 deletions minijinja-cli/tests/snapshots/test_basic__short_help.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ Arguments:

Options:
--config-file <PATH> Alternative path to the config file.
-f, --format <FORMAT> The format of the input data [possible values: auto, json,
querystring, yaml, toml, cbor]
-f, --format <FORMAT> The format of the input data [possible values: auto, cbor, ini,
json, querystring, toml, yaml]
-D, --define <EXPR> Defines an input variable (key=value / key:=json_value)
-t, --template <TEMPLATE_STRING> Render a string template
-o, --output <FILENAME> Path to the output file [default: -]
Expand Down
39 changes: 39 additions & 0 deletions minijinja-cli/tests/test_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ macro_rules! bind_common_filters {
r"\(in .*minijinja-testfile--.*?:(\d+)\)",
"(in [TEMPLATE]:$1)",
);
settings.add_filter(r"\bminijinja-cli\.exe\b", "minijinja-cli");
let _guard = settings.bind_to_scope();
};
}
Expand Down Expand Up @@ -212,6 +213,43 @@ fn test_toml() {
"###);
}

#[test]
#[cfg(feature = "ini")]
fn test_ini_casing() {
let input = file_with_contents_and_ext("[section]\nFOO = bar", ".ini");
let tmpl = file_with_contents(r#"Hello {{ section.FOO }}!"#);

assert_cmd_snapshot!(
cli()
.arg(tmpl.path())
.arg(input.path()),
@r###"
success: true
exit_code: 0
----- stdout -----
Hello bar!
----- stderr -----
"###);

let mut tmpl = NamedTempFile::new().unwrap();
tmpl.write_all(br#"Hello {{ FOO }}!"#).unwrap();

assert_cmd_snapshot!(
cli()
.arg("--select=section")
.arg(tmpl.path())
.arg(input.path()),
@r###"
success: true
exit_code: 0
----- stdout -----
Hello bar!
----- stderr -----
"###);
}

#[test]
#[cfg(feature = "querystring")]
fn test_querystring() {
Expand Down Expand Up @@ -808,6 +846,7 @@ fn test_load_config() {
feature = "yaml",
))]
fn test_help() {
bind_common_filters!();
assert_cmd_snapshot!("short_help", cli().arg("--help"));
assert_cmd_snapshot!("long_help", cli().arg("--long-help"));
}

0 comments on commit 009ff28

Please sign in to comment.