TestRail.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # ABSTRACT: Upload your TAP results to TestRail in realtime
  2. # PODNAME: App::Prove::Plugin::TestRail
  3. package App::Prove::Plugin::TestRail;
  4. use strict;
  5. use warnings;
  6. use utf8;
  7. use File::HomeDir qw{my_home};
  8. =head1 SYNOPSIS
  9. `prove -PTestRail='apiurl=http://some.testlink.install/,user=someUser,password=somePassword,project=TestProject,run=TestRun,plan=TestPlan,configs=Config1:Config2:Config3,version=0.014' sometest.t`
  10. =cut
  11. =head1 DESCRIPTION
  12. Prove plugin to upload test results to TestRail installations.
  13. Accepts input in the standard Prove plugin fashion (-Ppluginname='key=value,key=value,key=value...'), but will also parse a config file.
  14. When fed in prove plugin style, key=value input is expected.
  15. If \$HOME/.testrailrc exists, it will be parsed for any of these values in a newline separated key=value list. Example:
  16. apiurl=http://some.testrail.install
  17. user=someGuy
  18. password=superS3cret
  19. project=TestProject
  20. run=TestRun
  21. plan=GosPlan
  22. configs=config1:config2:config3: ... :configN
  23. version=xx.xx.xx.xx
  24. case_per_ok=0
  25. step_results=sr_sys_name
  26. Note that passing configurations as filters for runs inside of plans are separated by colons.
  27. Values passed in via query string will override values in \$HOME/.testrailrc.
  28. If your system has no concept of user homes, it will look in the current directory for .testrailrc.
  29. =head1 OVERRIDDEN METHODS
  30. =head2 load(parser)
  31. Shoves the arguments passed to the prove plugin into $ENV so that Test::Rail::Parser can get at them.
  32. Not the most elegant solution, but I see no other clear path to get those variables downrange to it's constructor.
  33. =cut
  34. sub load {
  35. my ($class, $p) = @_;
  36. my $app = $p->{app_prove};
  37. my $args = $p->{'args'};
  38. my $params = _parseConfig();
  39. my @kvp = ();
  40. my ($key,$value);
  41. foreach my $arg (@$args) {
  42. @kvp = split(/=/,$arg);
  43. if (scalar(@kvp) < 2) {
  44. print "Unrecognized Argument '$arg' to App::Prove::Plugin::Testrail, ignoring\n";
  45. next;
  46. }
  47. $key = shift @kvp;
  48. $value = join('',@kvp);
  49. $params->{$key} = $value;
  50. }
  51. $app->harness('Test::Rail::Harness');
  52. $app->merge(1);
  53. #XXX I can't figure out for the life of me any other way to pass this data. #YOLO
  54. $ENV{'TESTRAIL_APIURL'} = $params->{apiurl};
  55. $ENV{'TESTRAIL_USER'} = $params->{user};
  56. $ENV{'TESTRAIL_PASS'} = $params->{password};
  57. $ENV{'TESTRAIL_PROJ'} = $params->{project};
  58. $ENV{'TESTRAIL_RUN'} = $params->{run};
  59. $ENV{'TESTRAIL_PLAN'} = $params->{plan};
  60. $ENV{'TESTRAIL_CONFIGS'} = $params->{configs};
  61. $ENV{'TESTRAIL_VERSION'} = $params->{version};
  62. $ENV{'TESTRAIL_CASEOK'} = $params->{case_per_ok};
  63. $ENV{'TESTRAIL_STEPS'} = $params->{step_results};
  64. return $class;
  65. }
  66. sub _parseConfig {
  67. my $results = {};
  68. my $arr =[];
  69. my $homedir = my_home() || '.';
  70. open(my $fh, '<', $homedir . '/.testrailrc') or return (undef,undef,undef);#couldn't open!
  71. while (<$fh>) {
  72. chomp;
  73. @$arr = split(/=/,$_);
  74. if (scalar(@$arr) != 2) {
  75. warn("Could not parse $_ in tlreport config\n");
  76. next;
  77. }
  78. $results->{lc($arr->[0])} = $arr->[1];
  79. }
  80. close($fh);
  81. return $results;
  82. }
  83. 1;
  84. __END__
  85. =head1 SEE ALSO
  86. L<TestRail::API>
  87. L<Test::Rail::Parser>
  88. L<App::Prove>
  89. L<File::HomeDir> for the finding of .testrailrc
  90. =head1 SPECIAL THANKS
  91. Thanks to cPanel Inc, for graciously funding the creation of this module.