TestRail.pm 3.6 KB

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