check-source.pl 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env perl
  2. # tests source files for unwanted issues:
  3. # - CRLF newlines
  4. # - tabs \t
  5. # - trailing spaces
  6. # - unresolved merge conflicts
  7. use strict;
  8. use warnings;
  9. use Test::More;
  10. use File::Find 'find';
  11. use File::Basename 'basename';
  12. use File::Glob 'bsd_glob';
  13. sub read_file {
  14. my $f = shift;
  15. open my $fh, "<:raw", $f or die "FATAL: read_rawfile() cannot open file '$f': $!";
  16. return do { local $/; <$fh> };
  17. }
  18. my @all_files = (bsd_glob("makefile*"), bsd_glob("*.sh"), bsd_glob("*.pl"));
  19. find({ wanted=>sub { push @all_files, $_ if -f $_ }, no_chdir=>1 }, qw/src testprof demos/);
  20. my $fails = 0;
  21. for my $file (sort @all_files) {
  22. next unless $file =~ /\.(c|h|pl|py|sh)$/ || basename($file) =~ /^makefile/i;
  23. my $troubles = {};
  24. my $lineno = 1;
  25. my $content = read_file($file);
  26. push @{$troubles->{crlf_line_end}}, '?' if $content =~ /\r/;
  27. for my $l (split /\n/, $content) {
  28. push @{$troubles->{merge_conflict}}, $lineno if $l =~ /^(<<<<<<<|=======|>>>>>>>)([^<=>]|$)/;
  29. push @{$troubles->{trailing_space}}, $lineno if $l =~ / $/;
  30. push @{$troubles->{tab}}, $lineno if $l =~ /\t/ && basename($file) !~ /^makefile/i;
  31. $lineno++;
  32. }
  33. for my $k (sort keys %$troubles) {
  34. warn "FAIL: [$k] $file line:" . join(",", @{$troubles->{$k}}) . "\n";
  35. $fails++;
  36. }
  37. }
  38. warn $fails > 0 ? "FAILED $fails\n" : "PASS\n";