From c7455de10af6bbe2b2bdafee2cb682e813817ed2 Mon Sep 17 00:00:00 2001 From: Thomas Lacroix Date: Thu, 23 Apr 2020 14:02:38 +0200 Subject: [PATCH 1/2] Adds flag to disable quotes in TextFormatter --- text_formatter.go | 6 ++++++ text_formatter_test.go | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/text_formatter.go b/text_formatter.go index 2d15a23..431b5fd 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -37,6 +37,9 @@ type TextFormatter struct { // Force quoting of all values ForceQuote bool + // DisableQuote disables quoting for all values + DisableQuote bool + // Override coloring based on CLICOLOR and CLICOLOR_FORCE. - https://bixense.com/clicolors/ EnvironmentOverrideColors bool @@ -292,6 +295,9 @@ func (f *TextFormatter) needsQuoting(text string) bool { if f.QuoteEmptyFields && len(text) == 0 { return true } + if f.DisableQuote { + return false + } for _, ch := range text { if !((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || diff --git a/text_formatter_test.go b/text_formatter_test.go index 5d94ebb..debf621 100644 --- a/text_formatter_test.go +++ b/text_formatter_test.go @@ -66,6 +66,14 @@ func TestQuoting(t *testing.T) { checkQuoting(false, errors.New("invalid")) checkQuoting(true, errors.New("invalid argument")) + // Test for quoting disabled + tf.DisableQuote = true + checkQuoting(false, "") + checkQuoting(false, "abcd") + checkQuoting(false, "foo\n\rbar") + checkQuoting(false, errors.New("invalid argument")) + tf.DisableQuote = false + // Test for quoting empty fields. tf.QuoteEmptyFields = true checkQuoting(true, "") From aff00feb0ad676280f521c4d65dba116c4caec8e Mon Sep 17 00:00:00 2001 From: Thomas Lacroix Date: Thu, 23 Apr 2020 17:16:50 +0200 Subject: [PATCH 2/2] Adds additional test cases for DisableQuote --- text_formatter_test.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/text_formatter_test.go b/text_formatter_test.go index debf621..5b1cc0a 100644 --- a/text_formatter_test.go +++ b/text_formatter_test.go @@ -59,6 +59,7 @@ func TestQuoting(t *testing.T) { checkQuoting(false, "foo@bar") checkQuoting(false, "foobar^") checkQuoting(false, "+/-_^@f.oobar") + checkQuoting(true, "foo\n\rbar") checkQuoting(true, "foobar$") checkQuoting(true, "&foobar") checkQuoting(true, "x y") @@ -66,25 +67,34 @@ func TestQuoting(t *testing.T) { checkQuoting(false, errors.New("invalid")) checkQuoting(true, errors.New("invalid argument")) - // Test for quoting disabled - tf.DisableQuote = true - checkQuoting(false, "") - checkQuoting(false, "abcd") - checkQuoting(false, "foo\n\rbar") - checkQuoting(false, errors.New("invalid argument")) - tf.DisableQuote = false - // Test for quoting empty fields. tf.QuoteEmptyFields = true checkQuoting(true, "") checkQuoting(false, "abcd") + checkQuoting(true, "foo\n\rbar") checkQuoting(true, errors.New("invalid argument")) // Test forcing quotes. tf.ForceQuote = true checkQuoting(true, "") checkQuoting(true, "abcd") + checkQuoting(true, "foo\n\rbar") checkQuoting(true, errors.New("invalid argument")) + + // Test forcing quotes when also disabling them. + tf.DisableQuote = true + checkQuoting(true, "") + checkQuoting(true, "abcd") + checkQuoting(true, "foo\n\rbar") + checkQuoting(true, errors.New("invalid argument")) + + // Test disabling quotes + tf.ForceQuote = false + tf.QuoteEmptyFields = false + checkQuoting(false, "") + checkQuoting(false, "abcd") + checkQuoting(false, "foo\n\rbar") + checkQuoting(false, errors.New("invalid argument")) } func TestEscaping(t *testing.T) {