testrail-cases 5.1 KB

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