# New Ticket Created by  "Paul Cochrane" 
# Please include the string:  [perl #40509]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=40509 >


Hi,

This test checks for DOS line endings in C-language files, and I guess
should go as part of the codingstd tests.

Regards,

Paul
Index: t/codingstd/line_endings.t
===================================================================
--- t/codingstd/line_endings.t	(revision 0)
+++ t/codingstd/line_endings.t	(revision 0)
@@ -0,0 +1,84 @@
+#! perl
+# Copyright (C) 2006, The Perl Foundation.
+# $Id$
+
+use strict;
+use warnings;
+
+use lib qw( . lib ../lib ../../lib );
+use Test::More tests => 1;
+use Parrot::Distribution;
+
+=head1 NAME
+
+t/codingstd/line_endings.t - checks for DOS line endings in source
+
+=head1 SYNOPSIS
+
+    # test all files
+    % prove t/codingstd/line_endings.t
+
+    # test specific files
+    % perl t/codingstd/line_endings.t src/foo.c include/parrot/bar.h
+
+=head1 DESCRIPTION
+
+Checks that source files do not have DOS (CRLF) line endings.
+
+=head1 SEE ALSO
+
+L<docs/pdds/pdd07_codingstd.pod>
+
+=cut
+
+my $DIST = Parrot::Distribution->new;
+my @files = @ARGV ? @ARGV : source_files();
+my @dos_files;
+
+foreach my $file (@files) {
+    my $buf;
+    my $path;
+
+    ## get the full path of the file
+    # if we have command line arguments, the file is the full path
+    if (@ARGV) {
+        $path = $file;
+    }
+
+    # otherwise, use the relevant Parrot:: path method
+    else {
+        $path = $file->path;
+    }
+
+    # slurp in the file
+    open( my $fh, '<', $path )
+        or die "Cannot open '$path' for reading: $!\n";
+    {
+        local $/;
+        $buf = <$fh>;
+    }
+
+    # append to the dos_files array if the code matches
+    push @dos_files => "$path\n"
+        if $buf =~ m{\015};
+}
+
+ok( !scalar(@dos_files), 'Line endings correct' )
+    or diag( "DOS line ending found in " . scalar @dos_files . " files:[EMAIL PROTECTED]" );
+
+exit;
+
+sub source_files {
+    return (
+        map( $_->files_of_type('C code'),   $DIST->c_source_file_directories ),
+        map( $_->files_of_type('C header'), $DIST->c_header_file_directories ),
+        map( $_->files_of_type('PMC code'),   $DIST->pmc_source_file_directories ),
+    );
+}
+
+# Local Variables:
+#   mode: cperl
+#   cperl-indent-level: 4
+#   fill-column: 100
+# End:
+# vim: expandtab shiftwidth=4:

Reply via email to