nysus has asked for the wisdom of the Perl Monks concerning the following question:

I'm struggling to figure out how to inject colored output while running tests with prove:

use Test::Utils::Dump; use Test::Dirs; # sets up temporary scratch directories use Test; use_ok('File'); my $f = File->new(); $f->path($Test::Dirs::scratch . 'test.txt'); d $f; # d sub is in Test::Utils::Dump
My Dump utility module looks like this:
package Test::Utils::Dump; use Term::ANSIColor qw(colored); use strict; use warnings; use Data::Dumper qw(Dumper); # export the dump function use Exporter qw(import); our @EXPORT_OK = qw(d); # dump the data passed in sub d { my $data = shift; print colored ['green'], "\nDumping data...\n"; # print the data and the caller my $caller_line = "Dump from: " . (caller(0))[1] . ' line: ' . (caller(0))[2] . "\n"; print colored(['green'], "\n" . $caller_line); print Dumper($data); print $caller_line; }

When I run the test with prove, however, the output from the debug code is still in black and white. Prove is outputting some color so I know that's working. For example, "All tests successful" shows up in green. Addendum: Also, I can inject color directly into the output if I do print colored right into the test file itself. But this confuses prove unless I print to STDERR.

$PM = "Perl Monk's";
$MC = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar Parson";
$nysus = $PM . ' ' . $MC;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: How can I inject colored output to prove command output?
by kcott (Archbishop) on Jan 13, 2024 at 16:44 UTC

    G'day nysus,

    You could try replacing prove with yath which has very colourful output.

    — Ken

Re: How can I inject colored output to prove command output?
by nysus (Parson) on Jan 13, 2024 at 15:02 UTC

    Modifying my module to use diag instead of print did the trick:

    ackage Test::Utils::Dump; use Term::ANSIColor qw(colored); use Test::More; use strict; use Data::Dumper qw(Dumper); # export the dump function use Exporter qw(import); our @EXPORT_OK = qw(d); $Data::Dumper::Indent = 2; $Data::Dumper::Terse = 1; # dump the data passed in sub d { my $data = shift; my $out = ''; # print the data and the caller $caller_line = "Dump from: " . (caller(0))[1] . ' line: ' . (caller(0))[2]; diag colored(['yellow'], $caller_line); diag colored(['blue'], Dumper($data)); diag colored(['yellow'], $caller_line); }

    $PM = "Perl Monk's";
    $MC = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar Parson";
    $nysus = $PM . ' ' . $MC;
    Click here if you love Perl Monks