Utils.pm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # ABSTRACT: Utilities for the testrail command line functions.
  2. # PODNAME: TestRail::Utils
  3. =head1 DESCRIPTION
  4. Utilities for the testrail command line functions.
  5. =cut
  6. package TestRail::Utils;
  7. use strict;
  8. use warnings;
  9. use Pod::Perldoc 3.10;
  10. =head1 FUNCTIONS
  11. =head2 help
  12. Print the perldoc for $0 and exit.
  13. =cut
  14. sub help {
  15. @ARGV = ($0);
  16. Pod::Perldoc->run();
  17. exit 0;
  18. }
  19. =head2 userInput
  20. Wait for user input and return it.
  21. =cut
  22. sub userInput {
  23. local $| = 1;
  24. my $rt = <STDIN>;
  25. chomp $rt;
  26. return $rt;
  27. }
  28. =head2 interrogateUser($options,@keys)
  29. Wait for specified keys via userInput, and put them into $options HASHREF, if they are not already defined.
  30. Returns modified $options HASHREF.
  31. Dies if the user provides no value.
  32. =cut
  33. sub interrogateUser {
  34. my ($options,@keys) = @_;
  35. foreach my $key (@keys) {
  36. if (!$options->{$key}) {
  37. print "Type the $key for your testLink install below:\n";
  38. $options->{$key} = TestRail::Utils::userInput();
  39. die "$key cannot be blank!" unless $options->{$key};
  40. }
  41. }
  42. return $options;
  43. }
  44. =head2 parseConfig(homedir)
  45. Parse .testrailrc in the provided home directory.
  46. Returns:
  47. ARRAY - (apiurl,password,user)
  48. =cut
  49. sub parseConfig {
  50. my ($homedir,$login_only) = @_;
  51. my $results = {};
  52. my $arr =[];
  53. open(my $fh, '<', $homedir . '/.testrailrc') or return (undef,undef,undef);#couldn't open!
  54. while (<$fh>) {
  55. chomp;
  56. @$arr = split(/=/,$_);
  57. if (scalar(@$arr) != 2) {
  58. warn("Could not parse $_ in '$homedir/.testrailrc'!\n");
  59. next;
  60. }
  61. $results->{lc($arr->[0])} = $arr->[1];
  62. }
  63. close($fh);
  64. return ($results->{'apiurl'},$results->{'password'},$results->{'user'}) if $login_only;
  65. return $results;
  66. }
  67. =head2 getFilenameFromTAPLine($line)
  68. Analyze TAP output by prove and look for filename boundaries (no other way to figure out what file is run).
  69. 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.
  70. Apparently this is the TAP way of specifying the file that's run...which is highly inadequate.
  71. Inputs:
  72. STRING LINE - some line of TAP
  73. Returns:
  74. STRING filename of the test that output the TAP.
  75. =cut
  76. sub getFilenameFromTapLine {
  77. my $orig = shift;
  78. $orig =~ s/ *$//g; # Strip all trailing whitespace
  79. #Special case
  80. my ($is_skipall) = $orig =~ /(.*)\.+ skipped:/;
  81. return $is_skipall if $is_skipall;
  82. my @process_split = split(/ /,$orig);
  83. return 0 unless scalar(@process_split);
  84. my $dotty = pop @process_split; #remove the ........ (may repeat a number of times)
  85. return 0 if $dotty =~ /\d/; #Apparently looking for literal dots returns numbers too. who knew?
  86. chomp $dotty;
  87. my $line = join(' ',@process_split);
  88. #IF it ends in a bunch of dots
  89. #AND it isn't an ok/not ok
  90. #AND it isn't a comment
  91. #AND it isn't blank
  92. #THEN it's a test name
  93. return $line if ($dotty =~ /^\.+$/ && !($line =~ /^ok|not ok/) && !($line =~ /^# /) && $line);
  94. return 0;
  95. }
  96. 1;
  97. __END__
  98. =head1 SPECIAL THANKS
  99. Thanks to cPanel Inc, for graciously funding the creation of this module.