Skip to content

Commit 2e62e10

Browse files
Address Copilot review comments (round 7)
- Add validation to error when -f arg is non-empty but no codepage parsed (e.g., ',' or whitespace-only input) - Add unit tests for comma-only, whitespace-only, and multiple-comma inputs - Fix misleading BOM comments to accurately describe BOMOverride behavior - Remove unused skipOnEncError field from test table
1 parent ff05619 commit 2e62e10

4 files changed

Lines changed: 30 additions & 7 deletions

File tree

pkg/sqlcmd/codepage.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ func ParseCodePage(arg string) (*CodePageSettings, error) {
155155
}
156156
}
157157

158+
// If a non-empty argument was provided but no codepage was parsed,
159+
// treat this as an error rather than silently disabling codepage handling.
160+
if settings.InputCodePage == 0 && settings.OutputCodePage == 0 {
161+
return nil, localizer.Errorf("invalid codepage: %s", arg)
162+
}
163+
158164
// Validate codepages
159165
if settings.InputCodePage != 0 {
160166
if _, err := GetEncoding(settings.InputCodePage); err != nil {

pkg/sqlcmd/codepage_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ func TestParseCodePage(t *testing.T) {
9292
wantErr: true,
9393
errContains: "codepage", // Error message varies by platform
9494
},
95+
{
96+
name: "comma only produces no codepage",
97+
arg: ",",
98+
wantErr: true,
99+
errContains: "invalid codepage",
100+
},
101+
{
102+
name: "whitespace only produces no codepage",
103+
arg: " ",
104+
wantErr: true,
105+
errContains: "invalid codepage",
106+
},
107+
{
108+
name: "multiple commas produce no codepage",
109+
arg: ",,,",
110+
wantErr: true,
111+
errContains: "invalid codepage",
112+
},
95113
{
96114
name: "Japanese Shift JIS",
97115
arg: "932",

pkg/sqlcmd/commands_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,10 @@ func TestExitCommandAppendsParameterToCurrentBatch(t *testing.T) {
461461

462462
func TestOutputCodePageCommand(t *testing.T) {
463463
tests := []struct {
464-
name string
465-
codepage int
466-
expectedBytes []byte
467-
inputText string
468-
skipOnEncError bool
464+
name string
465+
codepage int
466+
expectedBytes []byte
467+
inputText string
469468
}{
470469
{
471470
name: "UTF-8 output",

pkg/sqlcmd/sqlcmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,12 @@ func (s *Sqlcmd) IncludeFile(path string, processAll bool) error {
352352
reader = transform.NewReader(f, enc.NewDecoder())
353353
}
354354
} else {
355-
// UTF-8 codepage: still apply BOM stripping
355+
// UTF-8 codepage: use BOMOverride to strip UTF-8 BOM and auto-detect UTF-16 BOMs, defaulting to UTF-8 otherwise
356356
utf8bom := unicode.BOMOverride(unicode.UTF8.NewDecoder())
357357
reader = transform.NewReader(f, utf8bom)
358358
}
359359
} else {
360-
// Default: auto-detect BOM for UTF-16, fallback to UTF-8
360+
// Default: auto-detect BOMs (UTF-8/UTF-16) and decode accordingly, falling back to UTF-8 when no BOM is present
361361
utf16bom := unicode.BOMOverride(unicode.UTF8.NewDecoder())
362362
reader = transform.NewReader(f, utf16bom)
363363
}

0 commit comments

Comments
 (0)