Przeglądaj źródła

Fixes #3. Break stuff apart in testrail-report, add unit tests

George S. Baugh 11 lat temu
rodzic
commit
0b7e8c1974
4 zmienionych plików z 110 dodań i 18 usunięć
  1. 56 14
      bin/testrail-report
  2. 3 4
      lib/Test/Rail/Parser.pm
  3. 35 0
      t/test_multiple_files.tap
  4. 16 0
      t/testrail-report.t

+ 56 - 14
bin/testrail-report

@@ -66,6 +66,10 @@ Your TestRail install must have 3 custom statuses with the internal
 names 'skip', 'todo_pass', and 'todo_fail', to represent those
 states which TAP can have.
 
+=head2 TESTING OPTIONS:
+
+    --mock don't do any real HTTP requests.
+
 =cut
 
 use strict;
@@ -137,6 +141,10 @@ REQUIREMENTS:
   names 'skip', 'todo_pass', and 'todo_fail', to represent those
   states which TAP can have.
 
+TESTING OPTIONS:
+
+    --mock don't do any real HTTP requests.
+
 ";
     exit 0;
 }
@@ -168,7 +176,7 @@ sub parseConfig {
 
 #Main loop------------
 
-my ($help,$apiurl,$user,$password,$project,$run,$case_per_ok,$step_results);
+my ($help,$apiurl,$user,$password,$project,$run,$case_per_ok,$step_results,$mock);
 
 #parse switches
 GetOptions(
@@ -179,6 +187,7 @@ GetOptions(
     'project=s'      => \$project,
     'case-ok'        => \$case_per_ok,
     'step-results=s' => \$step_results,
+    'mock'           => \$mock,
     'help'           => \$help
 );
 
@@ -189,18 +198,28 @@ if (-e $ENV{"HOME"} . '/.testrailrc' && (!$apiurl || !$password || !$user) ) {
     ($apiurl,$password,$user) = parseConfig();
 }
 
+#XXX not even close to optimized, don't slurp in the future
 #If argument is passed use it instead of stdin
 my $file = $ARGV[0];
 die "No Such File $file" if ($file && !-e $file);
-my ($fh,$fcontents);
+my ($fh,$fcontents,@files);
+
 if ($file) {
     open($fh,'<',$file);
     while (<$fh>) {
         $_ = colorstrip($_); #strip prove brain damage
         s/^\s*//g; #Fix more brain damage
+        if ($_ =~ /(.*)\s\.\.$/) {
+            #File marker from default prove
+            unless ($_ =~ /^[ok|not ok] - /) {
+                push(@files,$fcontents) if $fcontents;
+                $fcontents = '';
+            }
+        }
         $fcontents .= $_;
     }
     close($fh);
+    push(@files,$fcontents) if $fcontents;
 } else {
     #Just read STDIN, print help if no file was passed
     if (IO::Interactive::Tiny::is_interactive()) {
@@ -214,9 +233,17 @@ if ($file) {
     while (<>) {
         $_ = colorstrip($_); #strip prove brain damage
         s/^\s*//g; #Fix prove brain damage
+        if ($_ =~ /(.*)\s\.\.$/) {
+            #File marker from default prove
+            unless ($_ =~ /^[ok|not ok] - /) {
+                push(@files,$fcontents) if $fcontents;
+                $fcontents = '';
+            }
+        }
         $fcontents .= $_;
     }
     help() if !$fcontents; #Nothing passed to stdin!
+    push(@files,$fcontents) if $fcontents;
 }
 
 #Interrogate user if they didn't provide info
@@ -252,18 +279,33 @@ if (!$run) {
     $run = userInput();
 }
 
-my $tap = Test::Rail::Parser->new({
-    'tap'                 => $fcontents,
-    'apiurl'              => $apiurl,
-    'user'                => $user,
-    'pass'                => $password,
-    'run'                 => $run,
-    'project'             => $project,
-    'case_per_ok'         => $case_per_ok,
-    'step_results'        => $step_results,
-    'merge'               => 1
-});
-$tap->run();
+my $debug = 0;
+my $browser;
+if ($mock) {
+    use Test::LWP::UserAgent::TestRailMock;
+    $browser = $Test::LWP::UserAgent::TestRailMock::mockObject;
+    $debug = 1;
+}
+
+my $tap;
+foreach my $phil (@files) {
+    print "Processing $phil...\n";
+    $tap = Test::Rail::Parser->new({
+        'tap'                 => $phil,
+        'apiurl'              => $apiurl,
+        'user'                => $user,
+        'pass'                => $password,
+        'run'                 => $run,
+        'project'             => $project,
+        'case_per_ok'         => $case_per_ok,
+        'step_results'        => $step_results,
+        'debug'               => $debug,
+        'browser'             => $browser,
+        'merge'               => 1
+    });
+    $tap->run();
+
+}
 
 print "Done.\n";
 

+ 3 - 4
lib/Test/Rail/Parser.pm

@@ -194,10 +194,9 @@ sub unknownCallback {
         $self->{'file'} = $1;
         print "PROCESSING RESULTS FROM TEST FILE: $self->{'file'}\n";
     }
-    #RAW tap
-    if ($line =~ /(.*)\s\.\./) {
-        $self->{'file'} = $1;
-        print "PROCESSING RESULTS FROM TEST FILE: $self->{'file'}\n";
+    #RAW tap #XXX this regex could be improved
+    if ($line =~ /(.*)\s\.\.$/) {
+        $self->{'file'} = $1 unless $line =~ /^[ok|not ok] - /; #a little more careful
     }
     print "$line\n" if ($line =~ /^error/i);
 }

+ 35 - 0
t/test_multiple_files.tap

@@ -0,0 +1,35 @@
+STROGGIFY POPULATION CENTERS ..
+ok 1 - STROGGIFY POPULATION CENTERS
+not ok 2 - STROGGIFY POPULATION CENTERS
+
+#   Failed test 'waa'
+#   at test.t line 4.
+# Looks like you failed 1 test of 2.
+Dubious, test returned 1 (wstat 256, 0x100)
+Failed 1/2 subtests
+
+Test Summary Report
+-------------------
+test.t (Wstat: 256 Tests: 2 Failed: 1)
+  Failed test:  2
+    Non-zero exit status: 1
+    Files=1, Tests=2,  0 wallclock secs ( 0.02 usr  0.00 sys +  0.01 cusr  0.00 csys =  0.03 CPU)
+    Result: FAIL
+
+STROGGIFY POPULATION CENTERS ..
+ok 1 - STROGGIFY POPULATION CENTERS
+not ok 2 - STROGGIFY POPULATION CENTERS
+
+#   Failed test 'waa'
+#   at test.t line 4.
+# Looks like you failed 1 test of 2.
+Dubious, test returned 1 (wstat 256, 0x100)
+Failed 1/2 subtests
+
+Test Summary Report
+-------------------
+test.t (Wstat: 256 Tests: 2 Failed: 1)
+  Failed test:  2
+    Non-zero exit status: 1
+    Files=1, Tests=2,  0 wallclock secs ( 0.02 usr  0.00 sys +  0.01 cusr  0.00 csys =  0.03 CPU)
+    Result: FAIL

+ 16 - 0
t/testrail-report.t

@@ -0,0 +1,16 @@
+use strict;
+use warnings;
+
+use Test::More 'tests' => 4;
+
+my @args = qw{bin/testrail-report --apiurl http://testrail.local --user "test@fake.fake" --password "fake" --project "CRUSH ALL HUMANS" --run "SEND T-1000 INFILTRATION UNITS BACK IN TIME" --mock t/test_multiple_files.tap};
+my $out = `@args`;
+is($? >> 8, 0, "Exit code OK reported with multiple files");
+my $matches = () = $out =~ m/Reporting result of case/ig;
+is($matches,2,"Attempts to upload multiple times");
+
+@args = qw{bin/testrail-report --apiurl http://testrail.local --user "test@fake.fake" --password "fake" --project "CRUSH ALL HUMANS" --run "SEND T-1000 INFILTRATION UNITS BACK IN TIME" --case-ok --mock t/test_multiple_files.tap};
+$out = `@args`;
+is($? >> 8, 0, "Exit code OK reported with multiple files (case-ok mode)");
+$matches = () = $out =~ m/Reporting result of case/ig;
+is($matches,4,"Attempts to upload multiple times (case-ok mode)");