Эх сурвалжийг харах

Fix #85 by overriding TAP::Parser::make_result

Not sure if that'll cause backwards-compat issues, we'll see
George S. Baugh 10 жил өмнө
parent
commit
cd1a6aa218

+ 29 - 19
lib/Test/Rail/Parser.pm

@@ -18,8 +18,6 @@ use Scalar::Util qw{reftype};
 
 use File::Basename qw{basename};
 
-our $self;
-
 =head1 DESCRIPTION
 
 A TAP parser which will upload your test results to a TestRail install.
@@ -108,7 +106,6 @@ Step results will always be whatever status is relevant to the particular step.
 
 sub new {
     my ($class,$opts) = @_;
-    our $self;
     $opts = clone $opts; #Convenience, if we are passing over and over again...
 
     #Load our callbacks
@@ -271,7 +268,7 @@ sub new {
     }
     confess("No run ID provided, and no run with specified name exists in provided project/plan!") if !$tropts->{'run_id'};
 
-    $self = $class->SUPER::new($opts);
+    my $self = $class->SUPER::new($opts);
     if (defined($self->{'_iterator'}->{'command'}) && reftype($self->{'_iterator'}->{'command'}) eq 'ARRAY' ) {
         $self->{'file'} = $self->{'_iterator'}->{'command'}->[-1];
         print "# PROCESSING RESULTS FROM TEST FILE: $self->{'file'}\n";
@@ -306,12 +303,13 @@ Stores said filename for future use if encountered.
 
 # Look for file boundaries, etc.
 sub unknownCallback {
-    my (@args) = @_;
-    our $self;
-    my $line = $args[0]->as_string;
+    my ($test) = @_;
+    my $self = $test->{'parser'};
+    my $line = $test->as_string;
     $self->{'raw_output'} .= "$line\n";
 
-    #try to pick out the filename if we are running this on TAP in files
+    #XXX I'd love to just rely on the 'name' attr in App::Prove::State::Result::Test, but...
+    #try to pick out the filename if we are running this on TAP in files, where App::Prove is uninvolved
     my $file = TestRail::Utils::getFilenameFromTapLine($line);
     $self->{'file'} = $file if $file;
 }
@@ -325,9 +323,9 @@ Especially useful when merge=1 is passed to the constructor.
 
 # Register the current suite or test desc for use by test callback, if the line begins with the special magic words
 sub commentCallback {
-    my (@args) = @_;
-    our $self;
-    my $line = $args[0]->as_string;
+    my ($test) = @_;
+    my $self = $test->{'parser'};
+    my $line = $test->as_string;
     $self->{'raw_output'} .= "$line\n";
 
     if ($line =~ m/^#TESTDESC:\s*/) {
@@ -349,9 +347,8 @@ Otherwise, do nothing.
 =cut
 
 sub testCallback {
-    my (@args) = @_;
-    my $test = $args[0];
-    our $self;
+    my ($test) = @_;
+    my $self = $test->{'parser'};
 
     if ( $self->{'track_time'} ) {
         #Test done.  Record elapsed time.
@@ -441,7 +438,7 @@ sub testCallback {
     my $options        = $self->{'tr_opts'}->{'result_options'};
     my $custom_options = $self->{'tr_opts'}->{'result_custom_options'};
 
-    _set_result($run_id,$test_name,$status,$notes,$options,$custom_options);
+    $self->_set_result($run_id,$test_name,$status,$notes,$options,$custom_options);
     #Re-start the shot clock
     $self->{'starttime'} = time();
 
@@ -460,7 +457,7 @@ Otherwise, upload the overall results of the test to TestRail.
 =cut
 
 sub EOFCallback {
-    our $self;
+    my ($self) = @_;
 
     if ( $self->{'track_time'} ) {
         #Test done.  Record elapsed time.
@@ -498,7 +495,7 @@ sub EOFCallback {
 
 
     print "# Setting results...\n";
-    my $cres = _set_result($run_id,$test_name,$status,$notes,$options,$custom_options);
+    my $cres = $self->_set_result($run_id,$test_name,$status,$notes,$options,$custom_options);
     $self->_test_closure();
     $self->{'global_status'} = $status;
 
@@ -508,8 +505,7 @@ sub EOFCallback {
 }
 
 sub _set_result {
-    my ($run_id,$test_name,$status,$notes,$options,$custom_options) = @_;
-    our $self;
+    my ($self,$run_id,$test_name,$status,$notes,$options,$custom_options) = @_;
     my $tc;
 
     print "# Test elapsed: ".$options->{'elapsed'}."\n" if $options->{'elapsed'};
@@ -589,6 +585,20 @@ sub _test_closure {
     return $self->{'tr_opts'}->{'testrail'}->closeRun($self->{'tr_opts'}->{'run_id'});
 }
 
+=head2 make_result
+
+make_result has been overridden to make the parser object available to callbacks.
+
+=cut
+
+sub make_result {
+    my ($self,@args) = @_;
+    my $res = $self->SUPER::make_result(@args);
+    $res->{'parser'} = $self;
+    return $res;
+}
+
+
 1;
 
 __END__

+ 7 - 8
t/App-Prove-Plugin-Testrail.t

@@ -6,7 +6,7 @@ use warnings;
 use FindBin;
 use lib "$FindBin::Bin/lib";
 
-use Test::More 'tests' => 6;
+use Test::More 'tests' => 8;
 use Test::Fatal;
 use App::Prove;
 use App::Prove::Plugin::TestRail;
@@ -49,6 +49,7 @@ $prove->process_args("-PTestRail=apiurl=http://some.testlink.install/,user=someU
 is (exception {$prove->run()},undef,"Running TR parser with autoclose works correctly");
 
 #Test multi-job upload shizz
+#Note that I don't care if it even uploads, just that it *would have* done so correctly.
 $prove = App::Prove->new();
 $prove->process_args("-PTestRail=apiurl=http://some.testlink.install/,user=someUser,password=somePassword,project=TestProject,plan=FinalPlan,run=FinalRun,configs=testConfig,step_results=step_results", '-j2', 't/fake.test', 't/skipall.test');
 
@@ -60,16 +61,14 @@ subtest "Both step_result tracking and raw_output is correct (tests share parser
         my $step_results = $tres->{$test}->{'parser'}->{'tr_opts'}->{'result_custom_options'}->{'step_results'};
         my $toutput = $tres->{$test}->{'parser'}->{'raw_output'};
         note $test;
-        if ($test eq 'skipall.test') {
-            unlike($toutput,qr/NOT SO SEARED AFTER ALL/i,"Test steps in full test output");
-            if (is(ref $step_results, 'ARRAY', "step_results is ARRAY ref")) {
-                is(scalar(@$step_results),0,"No steps to upload when doing skip_all");
-            }
+        if ($test eq 't/skipall.test') {
+            unlike($toutput,qr/STORAGE TANKS SEARED/i,"Test steps in full test output");
+            isnt(ref $step_results, 'ARRAY', "step_results isnt ARRAY ref");
         } else {
-            like($toutput,qr/NOT SO SEARED AFTER ALL/i,"Test steps in full test output");
+            like($toutput,qr/STORAGE TANKS SEARED/i,"Test steps in full test output");
             unlike($toutput,qr/SKIP/i,"Skip all info in full test output");
             if (is(ref $step_results, 'ARRAY', "step_results is ARRAY ref")) {
-                is(scalar(@$step_results),2,"No steps to upload when doing skip_all");
+                is(scalar(@$step_results),2,"2 steps to upload for normal test");
             }
         }
     }