Skip to content

Commit

Permalink
Issue #192: pass ANSI on to TAP::Harness
Browse files Browse the repository at this point in the history
Otherwise remove support for coloring
  • Loading branch information
bschmalhofer committed Sep 23, 2020
1 parent f62c072 commit 124b609
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Kernel/System/Console/Command/Dev/UnitTest/Run.pm
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ sub Run {

$Kernel::OM->ObjectParamAdd(
'Kernel::System::UnitTest' => {
ANSI => $Self->{ANSI},
ANSI => $Self->{ANSI}, # as determined in Kernel::System::Console::BaseCommand
},
);

Expand Down
12 changes: 8 additions & 4 deletions Kernel/System/UnitTest.pm
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ sub new {
# allocate new hash for object
my $Self = bless {}, $Type;

$Self->{Debug} = $Param{Debug} || 0;
$Self->{ANSI} = $Param{ANSI};
$Self->{Debug} = $Param{Debug} || 0;
$Self->{ANSI} = $Param{ANSI} || 0;

return $Self;
}
Expand Down Expand Up @@ -163,6 +163,8 @@ sub Run {
my %HarnessArgs = (
timer => 1,
verbosity => $Verbosity,
# try to color the output when we are in an ANSI terminal
color => $Self->{ANSI},
# these libs are additional, $ENV{PERL5LIB} is still honored
lib => [ $Home, "$Home/Kernel/cpan-lib", "$Home/Custom" ],
);
Expand Down Expand Up @@ -250,7 +252,6 @@ sub _HandleFile {
'Kernel::System::UnitTest::Driver',
ObjectParams => {
Verbose => $Param{Verbose},
ANSI => $Self->{ANSI},
},
);

Expand Down Expand Up @@ -306,7 +307,10 @@ ANSI output is available and active, otherwise the text stays unchanged.
sub _Color {
my ( $Self, $Color, $Text ) = @_;

return $Text if !$Self->{ANSI};
# no coloring unless we are in an ANSI terminal
return $Text unless $Self->{ANSI};

# we are in an ANSI terminal
return Term::ANSIColor::color($Color) . $Text . Term::ANSIColor::color('reset');
}

Expand Down
60 changes: 7 additions & 53 deletions Kernel/System/UnitTest/Driver.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use namespace::autoclean;

# core modules
use Time::HiRes qw();
use Term::ANSIColor qw();

# CPAN modules
use Text::Diff;
Expand Down Expand Up @@ -54,7 +53,6 @@ create unit test driver object. Do not use it directly, instead use:
'Kernel::System::UnitTest::Driver',
ObjectParams => {
Verbose => $Self->{Verbose},
ANSI => $Self->{ANSI},
},
);
Expand All @@ -67,7 +65,6 @@ sub new {
# allocate new hash for object
my $Self = bless {}, $Type;

$Self->{ANSI} = $Param{ANSI};
$Self->{Verbose} = $Param{Verbose};

# When Kernel::System::UnitTest is under test itself,
Expand Down Expand Up @@ -274,37 +271,10 @@ sub IsDeeply {
}
);

# Provide colored diff.
if ( $Self->{ANSI} ) {
my @DiffLines = split( m{\n}, $Diff );
$Diff = '';

# Diff type "Table"
for my $DiffLine (@DiffLines) {

# Line changed
if ( substr( $DiffLine, 0, 1 ) eq '*' && substr( $DiffLine, -1, 1 ) eq '*' ) {
$DiffLine = $Self->_Color( 'yellow', $DiffLine );
}

# Line added
elsif ( substr( $DiffLine, 0, 1 ) eq '|' && substr( $DiffLine, -1, 1 ) eq '*' ) {
$DiffLine = $Self->_Color( 'green', $DiffLine );
}

# Line removed
elsif ( substr( $DiffLine, 0, 1 ) eq '*' && substr( $DiffLine, -1, 1 ) eq '|' ) {
$DiffLine = $Self->_Color( 'red', $DiffLine );
}

$Diff .= $DiffLine . "\n";
}
}

my $Output;
$Output .= $Self->_Color( 'yellow', "Diff" ) . ":\n$Diff\n";
$Output .= $Self->_Color( 'yellow', "Actual data" ) . ":\n$TestDump\n";
$Output .= $Self->_Color( 'yellow', "Expected data" ) . ":\n$ShouldBeDump\n";
$Output .= "Diff" . ":\n$Diff\n";
$Output .= "Actual data" . ":\n$TestDump\n";
$Output .= "Expected data" . ":\n$ShouldBeDump\n";

return $Self->_Print( 0, "$Name (is not equal, see below)\n$Output" );
}
Expand Down Expand Up @@ -347,7 +317,7 @@ sub IsNotDeeply {
}
else {
my $TestDump = $Kernel::OM->Get('Kernel::System::Main')->Dump($Test);
my $Output = $Self->_Color( 'yellow', "Actual data" ) . ":\n$TestDump\n";
my $Output = "Actual data" . ":\n$TestDump\n";
return $Self->_Print( 0, "$Name (the structures are wrongly equal, see below)\n$Output" );
}
}
Expand Down Expand Up @@ -470,10 +440,10 @@ sub _Print {
# print nothing as Kernel::System::UnitTest is tested itself
}
elsif ( $Self->{Verbose} ) {
say $Self->_Color( 'green', 'ok' ), " $Self->{TestCount} - $ShortMessage";
say 'ok', " $Self->{TestCount} - $ShortMessage";
}
else {
print $Self->_Color( 'green', "." );
print ".";
}

$Self->{ResultData}->{TestOk}++;
Expand All @@ -488,7 +458,7 @@ sub _Print {
if ( !$Self->{Verbose} ) {
say '';
}
say $Self->_Color( 'red', "not ok" ), " $Self->{TestCount} - $ShortMessage";
say "not ok", " $Self->{TestCount} - $ShortMessage";
}
$Self->{ResultData}->{TestNotOk}++;
$Self->{ResultData}->{Results}->{ $Self->{TestCount} }->{Status} = 'not ok';
Expand All @@ -512,22 +482,6 @@ sub _Print {
}
}

=head2 _Color()
this will color the given text (see Term::ANSIColor::color()) if
ANSI output is available and active, otherwise the text stays unchanged.
my $PossiblyColoredText = $CommandObject->_Color('green', $Text);
=cut

sub _Color {
my ( $Self, $Color, $Text ) = @_;

return $Text if !$Self->{ANSI};
return Term::ANSIColor::color($Color) . $Text . Term::ANSIColor::color('reset');
}

=end Internal:
=cut
Expand Down
4 changes: 2 additions & 2 deletions Kernel/System/UnitTest/RegisterDriver.pm
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ sub import {
# The default DataDiffType is used.
$Kernel::OM->ObjectParamAdd(
'Kernel::System::UnitTest::Driver' => {
Verbose => 1, # always verbose, as TAP consumer can create summaries
ANSI => 0, # no coloring, as TAP consumers can do the presentation
Verbose => 1, # always verbose, as TAP::Harness creates the summaries
ANSI => 0, # no coloring, as TAP::Harness is responsible for the presentation
}
);
$main::Self = $Kernel::OM->Get( 'Kernel::System::UnitTest::Driver' );
Expand Down

0 comments on commit 124b609

Please sign in to comment.