From c911427cf24e82eab1e1511e42f3218f90bb4e6b Mon Sep 17 00:00:00 2001 From: Karel Miko Date: Fri, 24 Feb 2017 20:51:31 +0100 Subject: [PATCH] check-source.pl script for checking whitespace related troubles --- check-source.pl | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 check-source.pl diff --git a/check-source.pl b/check-source.pl new file mode 100755 index 0000000..e8cbd20 --- /dev/null +++ b/check-source.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +# tests source files for unwanted issues: +# - CRLF newlines +# - tabs \t +# - trailing spaces +# - unresolved merge conflicts + +use strict; +use warnings; + +use Test::More; +use File::Find 'find'; +use File::Basename 'basename'; +use File::Glob 'bsd_glob'; + +sub read_file { + my $f = shift; + open my $fh, "<:raw", $f or die "FATAL: read_rawfile() cannot open file '$f': $!"; + return do { local $/; <$fh> }; +} + +my @all_files = (bsd_glob("makefile*"), bsd_glob("*.sh"), bsd_glob("*.pl")); +find({ wanted=>sub { push @all_files, $_ if -f $_ }, no_chdir=>1 }, qw/src testprof demos/); + +my $fails = 0; +for my $file (sort @all_files) { + next unless $file =~ /\.(c|h|pl|py|sh)$/ || basename($file) =~ /^makefile/i; + my $troubles = {}; + my $lineno = 1; + my $content = read_file($file); + push @{$troubles->{crlf_line_end}}, '?' if $content =~ /\r/; + for my $l (split /\n/, $content) { + push @{$troubles->{merge_conflict}}, $lineno if $l =~ /^(<<<<<<<|=======|>>>>>>>)([^<=>]|$)/; + push @{$troubles->{trailing_space}}, $lineno if $l =~ / $/; + push @{$troubles->{tab}}, $lineno if $l =~ /\t/ && basename($file) !~ /^makefile/i; + $lineno++; + } + for my $k (sort keys %$troubles) { + warn "FAIL: [$k] $file line:" . join(",", @{$troubles->{$k}}) . "\n"; + $fails++; + } +} + +warn $fails > 0 ? "FAILED $fails\n" : "PASS\n"; \ No newline at end of file