Skip to content

Commit ac96fce

Browse files
fix: apply FormatterOption values on the ascii result-format path
NewSQLCmdDefaultFormatter previously returned the ascii formatter without applying caller-supplied FormatterOption values, so -j raw-errors silently did nothing when the ascii output format was selected. Apply options to the embedded sqlCmdFormatterType in both branches and add a regression test.
1 parent 3b47c97 commit ac96fce

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

pkg/sqlcmd/format.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,29 @@ func WithRawErrors(raw bool) FormatterOption {
101101

102102
// NewSQLCmdDefaultFormatter returns a Formatter based on the configuration.
103103
// It returns an ASCII formatter if the format is set to "ascii", otherwise it returns a formatter that mimics the original ODBC-based sqlcmd formatter.
104-
// Any FormatterOption values passed via opts (e.g. WithRawErrors) are applied to the ODBC-mimicking formatter; the ASCII formatter ignores them.
104+
// Any FormatterOption values (e.g. WithRawErrors) are applied to the returned formatter.
105105
func NewSQLCmdDefaultFormatter(vars *Variables, removeTrailingSpaces bool, ccb ControlCharacterBehavior, opts ...FormatterOption) Formatter {
106106
if vars.Format() == "ascii" {
107-
return NewSQLCmdAsciiFormatter(vars, removeTrailingSpaces, ccb)
107+
f := NewSQLCmdAsciiFormatter(vars, removeTrailingSpaces, ccb).(*asciiFormatter)
108+
applyFormatterOptions(f.sqlCmdFormatterType, opts)
109+
return f
108110
}
109111
f := &sqlCmdFormatterType{
110112
removeTrailingSpaces: removeTrailingSpaces,
111113
format: "horizontal",
112114
colorizer: color.New(false),
113115
ccb: ccb,
114116
}
117+
applyFormatterOptions(f, opts)
118+
return f
119+
}
120+
121+
func applyFormatterOptions(f *sqlCmdFormatterType, opts []FormatterOption) {
115122
for _, opt := range opts {
116123
if opt != nil {
117124
opt(f)
118125
}
119126
}
120-
return f
121127
}
122128

123129
// Adds the given string to the current line, wrapping it based on the screen width setting

pkg/sqlcmd/format_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,17 @@ func TestAddErrorWithRawErrorsKeepsMssqlPrefix(t *testing.T) {
190190
assert.Contains(t, got, "Msg 50000, Level 16, State 1, Server server, Line 7")
191191
assert.Contains(t, got, "mssql: Something failed")
192192
}
193+
194+
// TestAddErrorWithRawErrorsAppliesToAsciiFormatter guards against silently dropping
195+
// FormatterOption values when the caller has selected the ascii result format.
196+
func TestAddErrorWithRawErrorsAppliesToAsciiFormatter(t *testing.T) {
197+
out, errOut := new(strings.Builder), new(strings.Builder)
198+
vars := InitializeVariables(false)
199+
vars.Set(SQLCMDFORMAT, "ascii")
200+
f := NewSQLCmdDefaultFormatter(vars, false, ControlIgnore, WithRawErrors(true))
201+
f.BeginBatch("", vars, out, errOut)
202+
203+
f.AddError(mssql.Error{Number: 50000, State: 1, Class: 16, Message: "Something failed", ServerName: "server", LineNo: 7})
204+
205+
assert.Contains(t, errOut.String(), "mssql: Something failed", "ascii formatter must honor WithRawErrors")
206+
}

0 commit comments

Comments
 (0)