perl: unobtrusively write to IO::Handle opened by TAP::Formatter::HTML
While executing actions on a web page using Test::WWW::Selenium, my perl program writes TAP testcase output to a html report created and updated by TAP::Harness.
My Selenium client must log into some password protected web pages, and this is unavoidable. So the password shows up in the TAP output.
ok 1 - open, /homepage-with-loginform.html, true
ok 2 - type, id_username, JoeSixp
ok 3 - type, id_password, secrIt
...
To remove the password from the HTML output, I have come up with the function below, but it does not work. Can anyony help out?
I created the formater object with
my $fmt = TAP::Formatter::HTML->new;
my $harness = TAP::Harness->new( { formatter => $fmt, merge => 1 } );
$harness->test_args( [ "--browser=$browser", "--config=$h{config}" ] );
my $agg = $harness->runtests(@tests);
#remove passwords from HTML report
remove_password( \%h, $fmt ); # %h is an options hash
...
sub remove_password {
# remove password from HTML report
my ( $h, $fmt ) = @_;
my $passwd = $h->{password} || "xxx-xxx";
my $outhtml = ${ $fmt->html() };
#from the TAP::Harness perldoc
#html() - This is a reference to the scalar containing the html generated on the last test run.
#Useful if you have "verbosity" set to silent, and have not provided a custom "output_fh" to write the report to.
note("replacing password with xxx-xxx in html file");
$fmt->html(\$outhtml); # does not work
$outhtml =~ s/$passwd/xxxxxx/msg; # works
{
local $/ = undef;
my $fh = $fmt->output_fh(); #An IO::Handle filehandle for printing the HTML report to.
if ( $fh->open开发者_如何转开发ed){
#
# ??? HOW DO I unobtrusively truncate the file here?
#
print $fh $outhtml; # writes back censored HTML output
}
}
}
I think that this is not a simple "how do I truncate a file using perl" question.
The file is already opened by some library code, and the code should work on unix and windows. the program should continue working with $fh , I dont want to open and close it myself, as it might confuse the TAP::Formatter::HTML module and it might stop working properly.
Update
#this gets the job done but it is kind of brutal
my $outf = $h->{outfile};
if ( $fh->opened){
$fh->close();
open $fh, ">", $outf or die "cannnot open '$outf' for truncating and writing:$!";
print $fh $outhtml;
close $fh;
}
Hmm... one possible but simplistic approach would be to override the description method from TAP::Parser::Result::Test (I am pretty sure it should be done via a subclass but too erly in the morning to figure out the Right Way).
Something along the lines of the following draft:
# Original: sub description { shift->{description} }
*TAP::Parser::Result::Test::description = sub {
my $description = shift->{description};
$description =~ s/type, id_password, .*$/type, id_password, XXX_PASSWORD_REMOVED_XXX/;
return $description;
};
精彩评论