meta.t 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright (C) 2015 Alex Jakimenko <alex.jakimenko@gmail.com>
  2. # Copyright (C) 2015 Alex Schroeder <alex@gnu.com>
  3. #
  4. # This program is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU General Public License as published by the Free Software
  6. # Foundation, either version 3 of the License, or (at your option) any later
  7. # version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along with
  14. # this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use warnings;
  17. use v5.10;
  18. use utf8;
  19. package OddMuse;
  20. require 't/test.pl';
  21. use Test::More tests => 11;
  22. use File::Basename;
  23. use Pod::Strip;
  24. use Pod::Simple::TextContent;
  25. my @modules = grep { $_ ne 'modules/404handler.pl' } <modules/*.pl>;
  26. my @other = 'wiki.pl';
  27. my @badModules;
  28. @badModules = grep { (stat $_)[2] != oct '100644' } @modules;
  29. unless (ok(@badModules == 0, 'Consistent file permissions of modules')) {
  30. diag(sprintf "$_ has %o but 100644 was expected", (stat $_)[2]) for @badModules;
  31. diag("▶▶▶ Use this command to fix it: chmod 644 @badModules");
  32. }
  33. @badModules = grep { ReadFile($_) !~ / ^ use \s+ strict; /mx } @modules;
  34. unless (ok(@badModules == 0, '"use strict;" in modules')) {
  35. diag(qq{$_ has no "use strict;"}) for @badModules;
  36. }
  37. @badModules = grep { ReadFile($_) !~ / ^ use \s+ v5\.10; /mx } @modules;
  38. unless (ok(@badModules == 0, '"use v5.10;" in modules')) {
  39. diag(qq{$_ has no "use v5.10;"}) for @badModules;
  40. diag(q{Minimum perl version for the core is v5.10, it seems like there is no reason not to have "use v5.10;" everywhere else.});
  41. }
  42. @badModules = grep {
  43. my $code = ReadFile($_);
  44. # warn "Looking at $_: " . length($code);
  45. # check Perl source code
  46. my $perl;
  47. my $pod_stripper = Pod::Strip->new;
  48. $pod_stripper->output_string(\$perl);
  49. $pod_stripper->parse_string_document($code);
  50. $perl =~ s/#.*//g;
  51. my $bad_perl = $perl !~ / ^ use \s+ utf8; /mx && $perl =~ / ([[:^ascii:]]+) /x;
  52. diag(qq{$_ has no "use utf8;" but contains non-ASCII characters in Perl code, eg. "$1"}) if $bad_perl;
  53. # check POD
  54. my $pod;
  55. my $pod_text = Pod::Simple::TextContent->new;
  56. $pod_text->output_string(\$pod);
  57. $pod_text->parse_string_document($code);
  58. my $bad_pod = $code !~ / ^ =encoding \s+ utf8 /mx && $pod =~ / ([[:^ascii:]]+) /x;
  59. diag(qq{$_ has no "=encoding utf8" but contains non-ASCII characters in POD, eg. "$1"}) if $bad_pod;
  60. $bad_perl || $bad_pod;
  61. } @modules;
  62. ok(@badModules == 0, 'utf8 in modules');
  63. SKIP: {
  64. skip 'documentation tests, we did not try to document every module yet', 1;
  65. @badModules = grep { ReadFile($_) !~ / ^ AddModuleDescription\(' [^\']+ ', /mx } @modules;
  66. unless (ok(@badModules == 0, 'link to the documentation in modules')) {
  67. diag(qq{$_ has no link to the documentation}) for @badModules;
  68. }
  69. }
  70. @badModules = grep { ReadFile($_) =~ / ^ package \s+ OddMuse; /imx } @modules;
  71. unless (ok(@badModules == 0, 'no "package OddMuse;" in modules')) {
  72. diag(qq{$_ has "package OddMuse;"}) for @badModules;
  73. diag(q{When we do "do 'somemodule.pl';" it ends up being in the same namespace of a caller, so there is no need to use "package OddMuse;"});
  74. }
  75. @badModules = grep { ReadFile($_) =~ / ^ use \s+ vars /mx } @modules;
  76. unless (ok(@badModules == 0, 'no "use vars" in modules')) {
  77. diag(qq{$_ is using "use vars"}) for @badModules;
  78. diag('▶▶▶ Use "our ($var, ...)" instead of "use vars qw($var ...)"');
  79. diag(q{▶▶▶ Use this command to do automatic conversion: perl -0pi -e 's/^([\t ]*)use vars qw\s*\(\s*(.*?)\s*\);/$x = $2; $x =~ s{(?<=\w)\b(?!$)}{,}g;"$1our ($x);"/gems' } . "@badModules");
  80. }
  81. @badModules = grep { ReadFile($_) =~ / [ \t]+ $ /mx } @modules, @other;
  82. unless (ok(@badModules == 0, 'no trailing whitespace in modules (and other perl files)')) {
  83. diag(qq{$_ has trailing whitespace}) for @badModules;
  84. diag(q{▶▶▶ Use this command to do automatic trailing whitespace removal: perl -pi -e 's/[ \t]+$//g' } . "@badModules");
  85. }
  86. @badModules = grep { ReadFile($_) =~ / This (program|file) is free software /x } @modules;
  87. unless (ok(@badModules == 0, 'license is specified in every module')) {
  88. diag(qq{$_ has no license specified}) for @badModules;
  89. }
  90. @badModules = grep {
  91. my ($name, $path, $suffix) = fileparse($_, '.pl');
  92. ReadFile($_) !~ /^AddModuleDescription\('$name.pl'/mx;
  93. } @modules;
  94. unless (ok(@badModules == 0, 'AddModuleDescription is used in every module')) {
  95. diag(qq{$_ does not use AddModuleDescription}) for @badModules;
  96. }
  97. # we have to use shell to redirect the output :(
  98. @badModules = grep { system("perl -cT \Q$_\E > /dev/null 2>&1") != 0 } @modules;
  99. unless (ok(@badModules == 0, 'modules are syntatically correct')) {
  100. diag(qq{$_ has syntax errors}) for @badModules;
  101. diag("▶▶▶ Use this command to see the problems: for f in @badModules; do perl -c \$f; done");
  102. }