@@ -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