Skip to content

Commit 3b3ca33

Browse files
Address Copilot review comments on perftrace command
- Fix regex pattern for consistency with other commands - Fix README path to use forward slashes for cross-platform - Add test cases for :PERFTRACE command parsing - Add :perftrace assertion in TestHelpCommand - Add TestPerftraceCommand for comprehensive testing
1 parent 466862f commit 3b3ca33

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ program_name sqlcmd
167167
- `:perftrace` redirects performance statistics output to a file, stderr, or stdout. Use in conjunction with `-p` flag.
168168

169169
```
170-
1> :perftrace c:\logs\perf.txt
170+
1> :perftrace c:/logs/perf.txt
171171
1> select 1
172172
2> go
173173
```

pkg/sqlcmd/commands.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func newCommands() Commands {
119119
name: "HELP",
120120
},
121121
"PERFTRACE": {
122-
regex: regexp.MustCompile(`(?im)^[\t ]*?:PERFTRACE(?:[ \t]+(.*$)|$)`),
122+
regex: regexp.MustCompile(`(?im)^[ \t]*:PERFTRACE(?:[ \t]+(.*$)|$)`),
123123
action: perftraceCommand,
124124
name: "PERFTRACE",
125125
},

pkg/sqlcmd/commands_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ func TestCommandParsing(t *testing.T) {
5656
{`RESET`, "RESET", []string{""}},
5757
{`:HELP`, "HELP", []string{""}},
5858
{`:help`, "HELP", []string{""}},
59+
{`:PERFTRACE stderr`, "PERFTRACE", []string{"stderr"}},
60+
{`:perftrace c:/logs/perf.txt`, "PERFTRACE", []string{"c:/logs/perf.txt"}},
5961
}
6062

6163
for _, test := range commands {
@@ -478,6 +480,44 @@ func TestHelpCommand(t *testing.T) {
478480
assert.Contains(t, output, ":listvar", "help should list :listvar")
479481
assert.Contains(t, output, ":out", "help should list :out")
480482
assert.Contains(t, output, ":error", "help should list :error")
483+
assert.Contains(t, output, ":perftrace", "help should list :perftrace")
481484
assert.Contains(t, output, ":r", "help should list :r")
482485
assert.Contains(t, output, "go", "help should list go")
483486
}
487+
488+
func TestPerftraceCommand(t *testing.T) {
489+
s, buf := setupSqlCmdWithMemoryOutput(t)
490+
defer buf.Close()
491+
492+
// Test empty argument returns error
493+
err := perftraceCommand(s, []string{""}, 1)
494+
assert.EqualError(t, err, InvalidCommandError("PERFTRACE", 1).Error(), "perftraceCommand with empty argument")
495+
496+
// Test redirect to stdout
497+
err = perftraceCommand(s, []string{"stdout"}, 1)
498+
assert.NoError(t, err, "perftraceCommand with stdout")
499+
assert.Equal(t, os.Stdout, s.GetStat(), "stat set to stdout")
500+
501+
// Test redirect to stderr
502+
err = perftraceCommand(s, []string{"stderr"}, 1)
503+
assert.NoError(t, err, "perftraceCommand with stderr")
504+
assert.Equal(t, os.Stderr, s.GetStat(), "stat set to stderr")
505+
506+
// Test redirect to file
507+
file, err := os.CreateTemp("", "sqlcmdperf")
508+
assert.NoError(t, err, "os.CreateTemp")
509+
defer os.Remove(file.Name())
510+
fileName := file.Name()
511+
_ = file.Close()
512+
513+
err = perftraceCommand(s, []string{fileName}, 1)
514+
assert.NoError(t, err, "perftraceCommand with file path")
515+
// Clean up by setting stat to nil
516+
s.SetStat(nil)
517+
518+
// Test variable resolution
519+
s.vars.Set("myvar", "stdout")
520+
err = perftraceCommand(s, []string{"$(myvar)"}, 1)
521+
assert.NoError(t, err, "perftraceCommand with a variable")
522+
assert.Equal(t, os.Stdout, s.GetStat(), "stat set to stdout using a variable")
523+
}

0 commit comments

Comments
 (0)