@@ -6,11 +6,13 @@ package cmdcat
66import (
77 "bytes"
88 "context"
9+ "io"
910 "os"
1011 "path/filepath"
1112 "strings"
1213 "testing"
1314
15+ "github.com/kptdev/kpt/pkg/printer"
1416 "github.com/stretchr/testify/assert"
1517)
1618
@@ -28,7 +30,8 @@ func writeFile(t *testing.T, path, content string) {
2830func runCat (t * testing.T , args ... string ) (string , error ) {
2931 t .Helper ()
3032 b := & bytes.Buffer {}
31- r := GetCatRunner (context .Background (), "" )
33+ ctx := printer .WithContext (context .Background (), printer .New (nil , io .Discard ))
34+ r := GetCatRunner (ctx , "" )
3235 r .Command .SetArgs (args )
3336 r .Command .SetOut (b )
3437 r .Command .SetErr (& bytes.Buffer {})
@@ -323,11 +326,18 @@ metadata:
323326 for _ , arg := range []string {d , d + "/" , "./" + filepath .Base (d )} {
324327 t .Run (arg , func (t * testing.T ) {
325328 // Run from d's parent so "./<name>" resolves.
326- oldWd , _ := os .Getwd ()
329+ oldWd , err := os .Getwd ()
330+ if err != nil {
331+ t .Fatal (err )
332+ }
327333 if err := os .Chdir (filepath .Dir (d )); err != nil {
328334 t .Fatal (err )
329335 }
330- defer os .Chdir (oldWd )
336+ defer func () {
337+ if err := os .Chdir (oldWd ); err != nil {
338+ t .Fatal (err )
339+ }
340+ }()
331341
332342 got , err := runCat (t , arg )
333343 assert .NoError (t , err )
@@ -389,10 +399,12 @@ data:
389399 assert .Error (t , err )
390400 assert .Contains (t , err .Error (), "broken.yaml" )
391401
392- // Stderr must carry the contextual diagnostic.
402+ // Stderr must carry the contextual diagnostic. Inject a printer via
403+ // context (mirroring how run.go wires the root cobra err stream) and
404+ // assert on its err stream.
393405 var errBuf bytes.Buffer
394- r := GetCatRunner (context .Background (), "" )
395- r . errWriter = & errBuf
406+ ctx := printer . WithContext (context .Background (), printer . New ( nil , & errBuf ) )
407+ r := GetCatRunner ( ctx , "" )
396408 r .Command .SetArgs ([]string {d })
397409 r .Command .SetOut (& bytes.Buffer {})
398410 r .Command .SetErr (& bytes.Buffer {})
@@ -422,7 +434,8 @@ metadata:
422434` )
423435 _ , err := runCat (t , kpt )
424436 assert .Error (t , err )
425- assert .Contains (t , err .Error (), "not a YAML/JSON file" )
437+ assert .Contains (t , err .Error (), "Kptfile" )
438+ assert .Contains (t , err .Error (), "metadata" )
426439}
427440
428441// TestCmd_NonYAMLFileErrors: a regular non-YAML/JSON/Kptfile file must
0 commit comments