From dc99e619974382aade4cc753ccfcb33f689c28ad Mon Sep 17 00:00:00 2001 From: Martin Machacek Date: Fri, 13 Sep 2024 15:38:21 +0200 Subject: [PATCH] Fixes serializing bool values in CSV output. Closes #6326 --- src/Command.spec.ts | 12 ++++++++++++ src/Command.ts | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Command.spec.ts b/src/Command.spec.ts index 2082e346682..0e5f9a06fd8 100644 --- a/src/Command.spec.ts +++ b/src/Command.spec.ts @@ -601,6 +601,18 @@ describe('Command', () => { assert(actual.indexOf(JSON.stringify(commandOutput[0].property)) === -1); }); + it('correctly serialize bool values to csv output', async () => { + const command = new MockCommand1(); + const commandOutput = [ + { + 'property1': true, + 'property2': false + } + ]; + const actual = await command.getCsvOutput(commandOutput, { options: { output: 'csv' } }); + assert.strictEqual(actual,"property1,property2\n1,0\n"); + }); + it('passes validation when csv output specified', async () => { const cmd = new MockCommand2(); assert.strictEqual(await cmd.validate({ options: { output: 'csv' } }, cli.getCommandInfo(cmd)), true); diff --git a/src/Command.ts b/src/Command.ts index aa905ebf7ee..54c28bcd8e6 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -682,7 +682,10 @@ export default abstract class Command { quote: cli.getConfig().get(settingsNames.csvQuote), quoted: cli.getSettingWithDefaultValue(settingsNames.csvQuoted, false), // eslint-disable-next-line camelcase - quoted_empty: cli.getSettingWithDefaultValue(settingsNames.csvQuotedEmpty, false) + quoted_empty: cli.getSettingWithDefaultValue(settingsNames.csvQuotedEmpty, false), + cast: { + boolean: (value: boolean) => value ? '1' : '0' + } }); }