TestRail.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. spawn=123
  27. sections=section1:section2:section3: ... :sectionN
  28. Note that passing configurations as filters for runs inside of plans are separated by colons.
  29. Values passed in via query string will override values in \$HOME/.testrailrc.
  30. If your system has no concept of user homes, it will look in the current directory for .testrailrc.
  31. See the documentation for the constructor of L<Test::Rail::Parser> as to why you might want to pass the aforementioned options.
  32. =head1 OVERRIDDEN METHODS
  33. =head2 load(parser)
  34. Shoves the arguments passed to the prove plugin into $ENV so that Test::Rail::Parser can get at them.
  35. Not the most elegant solution, but I see no other clear path to get those variables downrange to it's constructor.
  36. =cut
  37. sub load {
  38. my ($class, $p) = @_;
  39. my $app = $p->{app_prove};
  40. my $args = $p->{'args'};
  41. my $params = _parseConfig();
  42. my @kvp = ();
  43. my ($key,$value);
  44. foreach my $arg (@$args) {
  45. @kvp = split(/=/,$arg);
  46. if (scalar(@kvp) < 2) {
  47. print "Unrecognized Argument '$arg' to App::Prove::Plugin::Testrail, ignoring\n";
  48. next;
  49. }
  50. $key = shift @kvp;
  51. $value = join('',@kvp);
  52. $params->{$key} = $value;
  53. }
  54. $app->harness('Test::Rail::Harness');
  55. $app->merge(1);
  56. #XXX I can't figure out for the life of me any other way to pass this data. #YOLO
  57. $ENV{'TESTRAIL_APIURL'} = $params->{apiurl};
  58. $ENV{'TESTRAIL_USER'} = $params->{user};
  59. $ENV{'TESTRAIL_PASS'} = $params->{password};
  60. $ENV{'TESTRAIL_PROJ'} = $params->{project};
  61. $ENV{'TESTRAIL_RUN'} = $params->{run};
  62. $ENV{'TESTRAIL_PLAN'} = $params->{plan};
  63. $ENV{'TESTRAIL_CONFIGS'} = $params->{configs};
  64. $ENV{'TESTRAIL_VERSION'} = $params->{version};
  65. $ENV{'TESTRAIL_CASEOK'} = $params->{case_per_ok};
  66. $ENV{'TESTRAIL_STEPS'} = $params->{step_results};
  67. $ENV{'TESTRAIL_SPAWN'} = $params->{spawn};
  68. $ENV{'TESTRAIL_SECTIONS'} = $params->{sections};
  69. return $class;
  70. }
  71. sub _parseConfig {
  72. my $results = {};
  73. my $arr =[];
  74. my $homedir = my_home() || '.';
  75. open(my $fh, '<', $homedir . '/.testrailrc') or return (undef,undef,undef);#couldn't open!
  76. while (<$fh>) {
  77. chomp;
  78. @$arr = split(/=/,$_);
  79. if (scalar(@$arr) != 2) {
  80. warn("Could not parse $_ in tlreport config\n");
  81. next;
  82. }
  83. $results->{lc($arr->[0])} = $arr->[1];
  84. }
  85. close($fh);
  86. return $results;
  87. }
  88. 1;
  89. __END__
  90. =head1 SEE ALSO
  91. L<TestRail::API>
  92. L<Test::Rail::Parser>
  93. L<App::Prove>
  94. L<File::HomeDir> for the finding of .testrailrc
  95. =head1 SPECIAL THANKS
  96. Thanks to cPanel Inc, for graciously funding the creation of this module.