testrail-cases 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/perl
  2. # ABSTRACT: get information about cases inside various testsuites/sections.
  3. # PODNAME: TestRail::Bin::Cases
  4. =head1 SYNOPSIS
  5. testrail-cases [OPTIONS]
  6. require `which testrail-cases`;
  7. TestRail::Bin::Cases::run('args' => @args);
  8. =head1 DESCRIPTION
  9. testrail-cases - get information about cases inside various testsuites/sections.
  10. By default will tell you which cases are in both the testsuite and directory passed.
  11. Can be used as the modulino TestRail::Bin::Cases.
  12. Has a single 'run' function which accepts a hash with the 'args' parameter being the array of arguments.
  13. =head1 PARAMETERS:
  14. =head2 MANDATORY PARAMETERS
  15. =over 4
  16. --apiurl : full URL to get to TestRail index document
  17. --password : Your TestRail Password, or a valid API key (TestRail 4.2 and above).
  18. --user : Your TestRail User Name.
  19. -j --project : desired project name.
  20. -t --testsuite : desired testsuite name to search for cases within. May be passed multiple times.
  21. -d --directory : directory to search for tests to correlate with TestRail cases. May be passed multiple times.
  22. =back
  23. All mandatory options not passed with the above switches, or in your ~/.testrailrc will be prompted for.
  24. =head2 SEMI-OPTIONAL PARAMETERS
  25. =over 4
  26. -m --missing : Only show cases which are in the directory passed, but not TestRail. Mutually exclusive with orphans.
  27. -o --orphans : Only show cases which are in TestRail, but not the directory passed. Mutually exclusive with missing.
  28. -n --no-recurse : do not recurse subdirectories when considering what tests need adding/updating/pruning.
  29. -e --encoding : Character encoding of arguments. Defaults to UTF-8. See L<Encode::Supported> for supported encodings.
  30. =back
  31. =head2 OPTIONAL PARAMETERS
  32. =over 4
  33. --type : Filter cases to make syncing judgements against type(s). May be passed multiple times.
  34. --section : Filter cases to make syncing judgements against a specific section.
  35. --extension : only list files ending in the provided string (e.g. .pl, .pm, .t, .test)
  36. =back
  37. =head1 CONFIGURATION FILE
  38. In your \$HOME, (or the current directory, if your system has no concept of a home directory) put a file called .testrailrc with key=value syntax separated by newlines.
  39. Valid Keys are the same as documented by L<App::Prove::Plugin::TestRail>.
  40. All options specified thereby are overridden by passing the command-line switches above.
  41. =head1 MISCELLANEOUS OPTIONS:
  42. =over 4
  43. --help : show this output
  44. --test : print which tests would be added/updated/removed, but don't actually do anything
  45. =back
  46. =cut
  47. package TestRail::Bin::Cases;
  48. use strict;
  49. use warnings;
  50. use utf8;
  51. use TestRail::API;
  52. use TestRail::Utils;
  53. use TestRail::Utils::Find;
  54. use Getopt::Long qw{GetOptionsFromArray};
  55. use File::HomeDir qw{my_home};
  56. if (!caller()) {
  57. my ($out,$code) = run('args' => \@ARGV);
  58. print $out;
  59. exit $code;
  60. }
  61. sub run {
  62. my %params = @_;
  63. my $opts ={};
  64. #Parse config file if we are missing api url/key or user
  65. my $homedir = my_home() || '.';
  66. if (-e $homedir . '/.testrailrc') {
  67. $opts = TestRail::Utils::parseConfig($homedir);
  68. }
  69. GetOptionsFromArray($params{'args'},
  70. 'apiurl=s' => \$opts->{'apiurl'},
  71. 'password=s' => \$opts->{'password'},
  72. 'user=s' => \$opts->{'user'},
  73. 'j|project=s' => \$opts->{'project'},
  74. 't|testsuite=s' => \$opts->{'testsuite'},
  75. 'd|directory=s' => \$opts->{'directory'},
  76. 'm|missing' => \$opts->{'missing'},
  77. 'o|orphans' => \$opts->{'orphans'},
  78. 'n|no-recurse' => \$opts->{'no-recurse'},
  79. 'e|encoding=s' => \$opts->{'encoding'},
  80. 'section=s' => \$opts->{'section'},
  81. 'type=s@' => \$opts->{'types'},
  82. 'extension=s' => \$opts->{'extension'},
  83. 'h|help' => \$opts->{'help'},
  84. 'test' => \$opts->{'test'},
  85. );
  86. if ($opts->{help}) { return ('',TestRail::Utils::help()); }
  87. $opts->{'browser'} = $params{'browser'};
  88. #Mutual exclusivity
  89. $opts->{'no-missing'} = !$opts->{'missing'};
  90. $opts->{'update'} = !($opts->{'orphans'} || $opts->{'missing'});
  91. die("orphans and mising options are mutually exclusive.") if $opts->{'orphans'} && $opts->{'missing'};
  92. delete $opts->{'missing'};
  93. TestRail::Utils::interrogateUser($opts,qw{apiurl user password project testsuite directory});
  94. my $tr = TestRail::Utils::getHandle($opts);
  95. my $cases = TestRail::Utils::Find::getCases($opts,$tr);
  96. die "No cases in TestRail!\n" unless $cases;
  97. my $tests = TestRail::Utils::Find::findCases($opts,@$cases);
  98. my (@update,@add,@orphan);
  99. @update = map {$_->{'title'}} @{$tests->{'update'}} if ref $tests->{'update'} eq 'ARRAY';
  100. @add = map {$_->{'title'}} @{$tests->{'orphans'}} if ref $tests->{'orphans'} eq 'ARRAY';
  101. @orphan = @{$tests->{'missing'}} if ref $tests->{'missing'} eq 'ARRAY';
  102. my $out = '';
  103. $out .= join("\n",@update);
  104. $out .= join("\n",@add);
  105. $out .= join("\n",@orphan);
  106. $out .= "\n";
  107. return ($out,0);
  108. }
  109. 1;
  110. __END__
  111. L<TestRail::API>
  112. L<File::HomeDir> for the finding of .testrailrc
  113. =head1 SPECIAL THANKS
  114. Thanks to cPanel Inc, for graciously funding the creation of this distribution.