From 41a0766d1ae90c29313c9e4741ec55669dd72480 Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Wed, 4 Oct 2023 19:29:55 -0400 Subject: [PATCH] `geocode`: when producing JSON output with the now subcommands, use csv::QuoteStyle::Never so we produce valid JSON output --- src/cmd/geocode.rs | 13 ++++++++++++- tests/test_geocode.rs | 8 +++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/cmd/geocode.rs b/src/cmd/geocode.rs index f0acefc41..77c41a890 100644 --- a/src/cmd/geocode.rs +++ b/src/cmd/geocode.rs @@ -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)?; diff --git a/tests/test_geocode.rs b/tests/test_geocode.rs index eade31850..8a355a9ad 100644 --- a/tests/test_geocode.rs +++ b/tests/test_geocode.rs @@ -1564,9 +1564,8 @@ fn geocode_countryinfonow_formatstr_pretty_json() { .arg("mx") .args(["--formatstr", "%pretty-json"]); - let got: Vec> = 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", @@ -1594,7 +1593,6 @@ fn geocode_countryinfonow_formatstr_pretty_json() { "capital_names": { "en": "Mexico City" } -}"###### - ]]; +}"######; assert_eq!(got, expected); }