Skip to content

Commit

Permalink
Merge pull request #1345 from jqnatividad/geocode-fix-now-json-output
Browse files Browse the repository at this point in the history
`geocode`: when producing JSON output with the now subcommands, use c…
  • Loading branch information
jqnatividad authored Oct 4, 2023
2 parents b0084b5 + 41a0766 commit 63a2013
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/cmd/geocode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,18 @@ async fn geocode_main(args: Args) -> CliResult<()> {
let engine = load_engine(geocode_index_file.clone().into(), &progress).await?;

let mut rdr = rconfig.reader()?;
let mut wtr = Config::new(&args.flag_output).writer()?;
let mut wtr = Config::new(&args.flag_output)
.quote_style(
// if we're doing a now subcommand with JSON output, we don't want the CSV writer
// to close quote the output as it will produce invalid JSON
if now_cmd && (args.flag_formatstr == "%json" || args.flag_formatstr == "%pretty-json")
{
csv::QuoteStyle::Never
} else {
csv::QuoteStyle::Necessary
},
)
.writer()?;

let headers = rdr.byte_headers()?.clone();
let sel = rconfig.selection(&headers)?;
Expand Down
8 changes: 3 additions & 5 deletions tests/test_geocode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1564,9 +1564,8 @@ fn geocode_countryinfonow_formatstr_pretty_json() {
.arg("mx")
.args(["--formatstr", "%pretty-json"]);

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![svec![
r######"{
let got: String = wrk.stdout(&mut cmd);
let expected = r######"{
"info": {
"iso": "MX",
"iso3": "MEX",
Expand Down Expand Up @@ -1594,7 +1593,6 @@ fn geocode_countryinfonow_formatstr_pretty_json() {
"capital_names": {
"en": "Mexico City"
}
}"######
]];
}"######;
assert_eq!(got, expected);
}

0 comments on commit 63a2013

Please sign in to comment.