testrail-cases 4.5 KB

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