Skip to content

Commit 9f87576

Browse files
Fix file handle cleanup and add displayWidth=0 test
- Add deferred file.Close in editCommand to ensure file is closed on all error paths - Add TestFormatterFloatFormattingExtremeValuesUnlimitedWidth to test fallback with SQLCMDMAXVARTYPEWIDTH=0 - Ensures extreme values use scientific notation even with unlimited width Co-authored-by: dlevy-msft-sql <194277063+dlevy-msft-sql@users.noreply.github.com>
1 parent 85c8c8b commit 9f87576

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

pkg/sqlcmd/commands.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,10 @@ func editCommand(s *Sqlcmd, args []string, line uint) error {
549549
// Best-effort cleanup - ignore errors
550550
_ = os.Remove(fileName)
551551
}()
552+
defer func() {
553+
// Ensure file is closed on all paths
554+
_ = file.Close()
555+
}()
552556
text := s.batch.String()
553557
if s.batch.State() == "-" {
554558
text = fmt.Sprintf("%s%s", text, SqlcmdEol)
@@ -557,6 +561,7 @@ func editCommand(s *Sqlcmd, args []string, line uint) error {
557561
if err != nil {
558562
return err
559563
}
564+
// Explicitly close before launching editor (defer will close again but that's safe)
560565
if err := file.Close(); err != nil {
561566
return err
562567
}

pkg/sqlcmd/format_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,32 @@ func TestFormatterFloatFormattingExtremeValues(t *testing.T) {
227227
assert.Contains(t, output, "e-", "Output should contain scientific notation (e-) for very small values")
228228
}
229229

230+
func TestFormatterFloatFormattingExtremeValuesUnlimitedWidth(t *testing.T) {
231+
// Test that extreme float values fall back to scientific notation even when
232+
// displayWidth is 0 (unlimited), using type default widths as threshold
233+
s, buf := setupSqlCmdWithMemoryOutput(t)
234+
defer func() { _ = buf.Close() }()
235+
236+
// Leave SQLCMDMAXVARTYPEWIDTH at 0 (setupSqlCmdWithMemoryOutput default)
237+
// This sets displayWidth to 0 for all columns, testing the fallback logic
238+
// that uses type default widths (24 for FLOAT, 14 for REAL)
239+
240+
// Test query with extreme float values
241+
query := `SELECT
242+
CAST(1e100 AS FLOAT) as VeryLarge,
243+
CAST(1e-100 AS FLOAT) as VerySmall`
244+
245+
err := runSqlCmd(t, s, []string{query, "GO"})
246+
assert.NoError(t, err, "runSqlCmd returned error")
247+
248+
output := buf.buf.String()
249+
250+
// Verify that extreme values still use scientific notation even with unlimited width
251+
// (fallback should use type default widths to prevent unbounded output)
252+
assert.Contains(t, output, "e+", "Output should contain scientific notation (e+) for very large values even with unlimited width")
253+
assert.Contains(t, output, "e-", "Output should contain scientific notation (e-) for very small values even with unlimited width")
254+
}
255+
230256
func TestFormatterRealFormatting(t *testing.T) {
231257
// Test that REAL (float32) values use decimal notation for typical values
232258
// and fall back to scientific notation for extreme values

0 commit comments

Comments
 (0)