@@ -122,6 +122,8 @@ func RunTest(t *testing.T, path string, f func(t *testing.T, d *TestData) string
122122
123123// RunTestAny is like RunTest but works over a testing.TB.
124124func RunTestAny (t testing.TB , path string , f func (t testing.TB , d * TestData ) string ) {
125+ t .Helper ()
126+
125127 mode := os .O_RDONLY
126128 if * rewriteTestFiles {
127129 // We only open read-write if rewriting, so as to enable running
@@ -378,19 +380,14 @@ func runDirective(t testing.TB, r *testDataReader, f func(testing.TB, *TestData)
378380 B : actualLines ,
379381 })
380382 if err == nil {
381- t .Fatalf ("\n %s:\n %s \n output didn't match expected:\n %s" , d .Pos , d . Input , diff )
383+ t .Fatalf ("\n %s:\n %soutput didn't match expected:\n %s" , indentLines ( d .Pos ), indentLines ( d . String ()), indentLines ( diff ) )
382384 return
383385 }
384386 t .Logf ("Failed to produce diff %v" , err )
385387 }
386- t .Fatalf ("\n %s:\n %s\n expected:\n %s\n found:\n %s" , d .Pos , d . Input , d .Expected , actual )
388+ t .Fatalf ("\n %s:\n %s\n expected:\n %s\n found:\n %s" , d .Pos , indentLines ( d . String ()), indentLines ( d .Expected ), indentLines ( actual ) )
387389 } else if Verbose () {
388- input := d .Input
389- if input == "" {
390- input = "<no input to command>"
391- }
392- // TODO(tbg): it's awkward to reproduce the args, but it would be helpful.
393- t .Logf ("\n %s:\n %s [%d args]\n %s\n ----\n %s" , d .Pos , d .Cmd , len (d .CmdArgs ), input , actual )
390+ t .Logf ("\n %s:\n %s ----\n %s" , d .Pos , indentLines (d .String ()), indentLines (actual ))
394391 }
395392 return
396393}
@@ -515,6 +512,24 @@ type TestData struct {
515512 Rewrite bool
516513}
517514
515+ // FullCmd renders the command and the arguments.
516+ func (td * TestData ) FullCmd () string {
517+ fields := make ([]string , 0 , len (td .CmdArgs )+ 1 )
518+ fields = append (fields , td .Cmd )
519+ for i := range td .CmdArgs {
520+ fields = append (fields , td .CmdArgs [i ].String ())
521+ }
522+ return strings .Join (fields , " " )
523+ }
524+
525+ // String renders the entire testcase. The string ends in a newline.
526+ func (td * TestData ) String () string {
527+ if td .Input == "" {
528+ return td .FullCmd () + "\n "
529+ }
530+ return fmt .Sprintf ("%s\n %s\n " , td .FullCmd (), td .Input )
531+ }
532+
518533// HasArg checks whether the CmdArgs array contains an entry for the given key.
519534func (td * TestData ) HasArg (key string ) bool {
520535 _ , ok := td .Arg (key )
@@ -848,3 +863,20 @@ func hasBlankLine(s string) bool {
848863// want that final match to be included, so we force the end-of-line
849864// match using "\n" specifically.
850865var blankLineRe = regexp .MustCompile (`(?m)^[\t ]*\n` )
866+
867+ func indentLines (str string ) string {
868+ var b strings.Builder
869+ if str == "" {
870+ return ""
871+ }
872+ for i , l := range strings .Split (str , "\n " ) {
873+ if i > 0 {
874+ b .WriteString ("\n " )
875+ }
876+ if l != "" {
877+ b .WriteString (" " )
878+ b .WriteString (l )
879+ }
880+ }
881+ return b .String ()
882+ }
0 commit comments