Utils.pm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. =head1 FUNCTIONS
  10. =head2 userInput
  11. Wait for user input and return it.
  12. =cut
  13. sub userInput {
  14. local $| = 1;
  15. my $rt = <STDIN>;
  16. chomp $rt;
  17. return $rt;
  18. }
  19. =head2 parseConfig(homedir)
  20. Parse .testrailrc in the provided home directory.
  21. Returns:
  22. ARRAY - (apiurl,password,user)
  23. =cut
  24. sub parseConfig {
  25. my ($homedir,$login_only) = @_;
  26. my $results = {};
  27. my $arr =[];
  28. open(my $fh, '<', $homedir . '/.testrailrc') or return (undef,undef,undef);#couldn't open!
  29. while (<$fh>) {
  30. chomp;
  31. @$arr = split(/=/,$_);
  32. if (scalar(@$arr) != 2) {
  33. warn("Could not parse $_ in '$homedir/.testrailrc'!\n");
  34. next;
  35. }
  36. $results->{lc($arr->[0])} = $arr->[1];
  37. }
  38. close($fh);
  39. return ($results->{'apiurl'},$results->{'password'},$results->{'user'}) if $login_only;
  40. return $results;
  41. }
  42. =head2 getFilenameFromTAPLine($line)
  43. Analyze TAP output by prove and look for filename boundaries (no other way to figure out what file is run).
  44. 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.
  45. Apparently this is the TAP way of specifying the file that's run...which is highly inadequate.
  46. Inputs:
  47. STRING LINE - some line of TAP
  48. Returns:
  49. STRING filename of the test that output the TAP.
  50. =cut
  51. sub getFilenameFromTapLine {
  52. my $orig = shift;
  53. $orig =~ s/ *$//g; # Strip all trailing whitespace
  54. #Special case
  55. my ($is_skipall) = $orig =~ /(.*)\.+ skipped:/;
  56. return $is_skipall if $is_skipall;
  57. my @process_split = split(/ /,$orig);
  58. return 0 unless scalar(@process_split);
  59. my $dotty = pop @process_split; #remove the ........ (may repeat a number of times)
  60. return 0 if $dotty =~ /\d/; #Apparently looking for literal dots returns numbers too. who knew?
  61. chomp $dotty;
  62. my $line = join(' ',@process_split);
  63. #IF it ends in a bunch of dots
  64. #AND it isn't an ok/not ok
  65. #AND it isn't a comment
  66. #AND it isn't blank
  67. #THEN it's a test name
  68. return $line if ($dotty =~ /^\.+$/ && !($line =~ /^ok|not ok/) && !($line =~ /^# /) && $line);
  69. return 0;
  70. }
  71. 1;
  72. __END__
  73. =head1 SPECIAL THANKS
  74. Thanks to cPanel Inc, for graciously funding the creation of this module.