This repository has been archived by the owner on Aug 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Add DataLine plugin #265
Open
exodist
wants to merge
1
commit into
master
Choose a base branch
from
DataLine
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add DataLine plugin #265
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,117 @@ | ||||||
package Test2::Plugin::DataLine; | ||||||
use strict; | ||||||
use warnings; | ||||||
|
||||||
use Test2::Event::V2; | ||||||
|
||||||
our $VERSION = '0.000154'; | ||||||
|
||||||
use B(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you consider a space to make this distinct from a function call?
Suggested change
|
||||||
|
||||||
use Test2::API qw{ | ||||||
test2_add_callback_post_load | ||||||
test2_stack | ||||||
}; | ||||||
|
||||||
sub import { | ||||||
my $class = shift; | ||||||
|
||||||
test2_add_callback_post_load(sub { | ||||||
my $hub = test2_stack()->top; | ||||||
|
||||||
$hub->pre_filter( | ||||||
sub { | ||||||
my ($hub, $event) = @_; | ||||||
|
||||||
if ($event->causes_fail) { | ||||||
my $fd = $event->facet_data; | ||||||
|
||||||
if (my $line = $.) { | ||||||
my $fh = eval '$${^LAST_FH}' || do { # Added in 5.18, the do is fallback | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn’t this check the Perl version instead of string eval? |
||||||
my $out; | ||||||
local $SIG{__WARN__} = sub { | ||||||
my $msg = shift; | ||||||
$out = $msg; | ||||||
}; | ||||||
warn "blah"; | ||||||
$out =~ m/<(.+)> line $line/ ? $1 : '?'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we make this regex more specific? What if the test name contains I don’t understand why there are |
||||||
}; | ||||||
|
||||||
$fh =~ s/^\*(main::)?//; | ||||||
|
||||||
my $msg = "Last filehandle read: <$fh> line $."; | ||||||
|
||||||
push @{$fd->{info} //= []} => { | ||||||
details => $msg, | ||||||
tag => 'DIAG', | ||||||
debug => 1, | ||||||
}; | ||||||
|
||||||
return Test2::Event::V2->new(%$fd); | ||||||
} | ||||||
} | ||||||
|
||||||
return $event; | ||||||
}, | ||||||
inherit => 1, | ||||||
); | ||||||
}); | ||||||
} | ||||||
|
||||||
1; | ||||||
|
||||||
__END__ | ||||||
|
||||||
=pod | ||||||
|
||||||
=encoding UTF-8 | ||||||
|
||||||
=head1 NAME | ||||||
|
||||||
Test2::Plugin::DataLine - Add "<$fh> line X." diagnostics to failed tests. | ||||||
|
||||||
=head1 DESCRIPTION | ||||||
|
||||||
C<warn ...> and C<die ...> will append "<$fh> line X." to warnings and | ||||||
exceptions if C<$.> is true. This is the last filehandle read, and the line | ||||||
number that was read. | ||||||
|
||||||
This plugin adds the same information to any failed tests. | ||||||
|
||||||
Example: "Last filehandle read: <DATA> line 5" | ||||||
|
||||||
=head1 SYNOPSIS | ||||||
|
||||||
use Test2::Plugin::DataLine; | ||||||
|
||||||
=head1 SOURCE | ||||||
|
||||||
The source code repository for Test2-Suite can be found at | ||||||
F<https://github.com/Test-More/Test2-Suite/>. | ||||||
|
||||||
=head1 MAINTAINERS | ||||||
|
||||||
=over 4 | ||||||
|
||||||
=item Chad Granum E<lt>[email protected]<gt> | ||||||
|
||||||
=back | ||||||
|
||||||
=head1 AUTHORS | ||||||
|
||||||
=over 4 | ||||||
|
||||||
=item Chad Granum E<lt>[email protected]<gt> | ||||||
|
||||||
=back | ||||||
|
||||||
=head1 COPYRIGHT | ||||||
|
||||||
Copyright 2018 Chad Granum E<lt>[email protected]<gt>. | ||||||
|
||||||
This program is free software; you can redistribute it and/or | ||||||
modify it under the same terms as Perl itself. | ||||||
|
||||||
See F<http://dev.perl.org/licenses/> | ||||||
|
||||||
=cut |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use Test2::V0; | ||
use Test2::API qw/intercept/; | ||
|
||
my $events = intercept { | ||
require Test2::Plugin::DataLine; | ||
Test2::Plugin::DataLine->import; | ||
|
||
package # | ||
main; | ||
|
||
is(<DATA>, 'nope', "Read data, not correct"); | ||
ok(1, "pass"); | ||
|
||
package # | ||
Foo::Bar; | ||
|
||
open(my $fh, '<', __FILE__) or die "Could not open: $!"; | ||
main::is(<$fh>, 'nope', "Read data, not correct again"); | ||
main::ok(1, "pass"); | ||
}; | ||
|
||
my @failures = map { $_->facet_data } grep { $_->causes_fail } @$events; | ||
my @other = map { $_->facet_data } grep { !$_->causes_fail } @$events; | ||
|
||
is(@failures, 2, "Got 2 failures"); | ||
|
||
like( | ||
\@failures, | ||
[ | ||
{info => bag {{tag => 'DIAG', debug => T(), details => 'Last filehandle read: <DATA> line 1'}}}, | ||
{info => bag {{tag => 'DIAG', debug => T(), details => 'Last filehandle read: <Foo::Bar::$fh> line 1'}}}, | ||
], | ||
"Added the extra diagnostics to all failures", | ||
); | ||
|
||
ok(!(grep { $_->{info} && $_->{info}->[-1]->{details} !~ m/Last filehandle read/ } @other), "Diags not added to any other events"); | ||
|
||
done_testing; | ||
|
||
__DATA__ | ||
ooga booga | ||
oga boga |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this was the name of your previous attempt. I consider DataLine a better name.