Utils.pm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # ABSTRACT: Utilities for the testrail command line functions, and their main loops.
  2. # PODNAME: TestRail::Utils
  3. package TestRail::Utils;
  4. use strict;
  5. use warnings;
  6. use Carp qw{confess cluck};
  7. use Pod::Perldoc 3.10;
  8. =head1 SCRIPT HELPER FUNCTIONS
  9. =head2 help
  10. Print the perldoc for $0 and exit.
  11. =cut
  12. sub help {
  13. @ARGV = ($0);
  14. Pod::Perldoc->run();
  15. exit 0;
  16. }
  17. =head2 userInput
  18. Wait for user input and return it.
  19. =cut
  20. sub userInput {
  21. local $| = 1;
  22. my $rt = <STDIN>;
  23. chomp $rt;
  24. return $rt;
  25. }
  26. =head2 interrogateUser($options,@keys)
  27. Wait for specified keys via userInput, and put them into $options HASHREF, if they are not already defined.
  28. Returns modified $options HASHREF.
  29. Dies if the user provides no value.
  30. =cut
  31. sub interrogateUser {
  32. my ($options,@keys) = @_;
  33. foreach my $key (@keys) {
  34. if (!$options->{$key}) {
  35. print "Type the $key for your testLink install below:\n";
  36. $options->{$key} = TestRail::Utils::userInput();
  37. die "$key cannot be blank!" unless $options->{$key};
  38. }
  39. }
  40. return $options;
  41. }
  42. =head2 parseConfig(homedir)
  43. Parse .testrailrc in the provided home directory.
  44. Returns:
  45. ARRAY - (apiurl,password,user)
  46. =cut
  47. sub parseConfig {
  48. my ($homedir,$login_only) = @_;
  49. my $results = {};
  50. my $arr =[];
  51. open(my $fh, '<', $homedir . '/.testrailrc') or return (undef,undef,undef);#couldn't open!
  52. while (<$fh>) {
  53. chomp;
  54. @$arr = split(/=/,$_);
  55. if (scalar(@$arr) != 2) {
  56. warn("Could not parse $_ in '$homedir/.testrailrc'!\n");
  57. next;
  58. }
  59. $results->{lc($arr->[0])} = $arr->[1];
  60. }
  61. close($fh);
  62. return ($results->{'apiurl'},$results->{'password'},$results->{'user'}) if $login_only;
  63. return $results;
  64. }
  65. =head2 getFilenameFromTAPLine($line)
  66. Analyze TAP output by prove and look for filename boundaries (no other way to figure out what file is run).
  67. Long story short: don't end 'unknown' TAP lines with any number of dots if you don't want it interpreted as a test name.
  68. Apparently this is the TAP way of specifying the file that's run...which is highly inadequate.
  69. Inputs:
  70. STRING LINE - some line of TAP
  71. Returns:
  72. STRING filename of the test that output the TAP.
  73. =cut
  74. sub getFilenameFromTapLine {
  75. my $orig = shift;
  76. $orig =~ s/ *$//g; # Strip all trailing whitespace
  77. #Special case
  78. my ($is_skipall) = $orig =~ /(.*)\.+ skipped:/;
  79. return $is_skipall if $is_skipall;
  80. my @process_split = split(/ /,$orig);
  81. return 0 unless scalar(@process_split);
  82. my $dotty = pop @process_split; #remove the ........ (may repeat a number of times)
  83. return 0 if $dotty =~ /\d/; #Apparently looking for literal dots returns numbers too. who knew?
  84. chomp $dotty;
  85. my $line = join(' ',@process_split);
  86. #IF it ends in a bunch of dots
  87. #AND it isn't an ok/not ok
  88. #AND it isn't a comment
  89. #AND it isn't blank
  90. #THEN it's a test name
  91. return $line if ($dotty =~ /^\.+$/ && !($line =~ /^ok|not ok/) && !($line =~ /^# /) && $line);
  92. return 0;
  93. }
  94. =head2 getRunInformation
  95. Return the relevant project definition, plan and run definition HASHREFs for the provided options.
  96. Practically all the binaries need this information, so it has been subroutined out.
  97. Dies in the event the project/plan/run could not be found.
  98. =cut
  99. sub getRunInformation {
  100. my ($tr,$opts) = @_;
  101. confess("First argument must be instance of TestRail::API") unless blessed($tr) eq 'TestRail::API';
  102. my $project = $tr->getProjectByName($opts->{'project'});
  103. confess "No such project '$opts->{project}'.\n" if !$project;
  104. my ($run,$plan);
  105. if ($opts->{'plan'}) {
  106. $plan = $tr->getPlanByName($project->{'id'},$opts->{'plan'});
  107. confess "No such plan '$opts->{plan}'!\n" if !$plan;
  108. $run = $tr->getChildRunByName($plan,$opts->{'run'}, $opts->{'configs'});
  109. } else {
  110. $run = $tr->getRunByName($project->{'id'},$opts->{'run'});
  111. }
  112. confess "No such run '$opts->{run}' matching the provided configs (if any).\n" if !$run;
  113. return ($project,$plan,$run);
  114. }
  115. 1;
  116. __END__
  117. =head1 SPECIAL THANKS
  118. Thanks to cPanel Inc, for graciously funding the creation of this module.