comparison tests/no-tabs.t @ 1:bc8961a81af6 release

2007-08-13 22:53:14 by steve Initial revision
author steve
date Mon, 13 Aug 2007 22:53:14 +0000
parents
children 8503c495b169
comparison
equal deleted inserted replaced
0:30c6796bded8 1:bc8961a81af6
1 #!/usr/bin/perl -w
2 #
3 # Test that none of our scripts contain any literal TAB characters.
4 #
5 # Steve
6 # --
7 # $Id: no-tabs.t,v 1.1.1.1 2007-08-13 22:53:14 steve Exp $
8
9
10 use strict;
11 use File::Find;
12 use Test::More qw( no_plan );
13
14
15 #
16 # Find all the files beneath the current directory,
17 # and call 'checkFile' with the name.
18 #
19 find( { wanted => \&checkFile, no_chdir => 1 }, '.' );
20
21
22
23 #
24 # Check a file.
25 #
26 #
27 sub checkFile
28 {
29 # The file.
30 my $file = $File::Find::name;
31
32 # We don't care about directories
33 return if ( ! -f $file );
34
35 # Nor about backup files.
36 return if ( $file =~ /~$/ );
37
38 # Nor about files which start with ./debian/
39 return if ( $file =~ /^\.\/debian\// );
40
41 # See if it is a shell/perl file.
42 my $isShell = 0;
43 my $isPerl = 0;
44
45 # Read the file.
46 open( INPUT, "<", $file );
47 foreach my $line ( <INPUT> )
48 {
49 if ( ( $line =~ /\/bin\/sh/ ) ||
50 ( $line =~ /\/bin\/bash/ ) )
51 {
52 $isShell = 1;
53 }
54 if ( $line =~ /\/usr\/bin\/perl/ )
55 {
56 $isPerl = 1;
57 }
58 }
59 close( INPUT );
60
61 #
62 # We don't care about files which are neither perl nor shell.
63 #
64 if ( $isShell || $isPerl )
65 {
66 #
67 # Count TAB characters
68 #
69 my $count = countTabCharacters( $file );
70
71 is( $count, 0, "Script has no tab characters: $file" );
72 }
73 }
74
75
76
77 #
78 # Count and return the number of literal TAB characters contained
79 # in the specified file.
80 #
81 sub countTabCharacters
82 {
83 my ( $file ) = (@_);
84 my $count = 0;
85
86 open( FILE, "<", $file )
87 or die "Cannot open $file - $!";
88 foreach my $line ( <FILE> )
89 {
90 # We will count multiple tab characters in a single line.
91 while( $line =~ /(.*)\t(.*)/ )
92 {
93 $count += 1;
94 $line = $1 . $2;
95 }
96 }
97 close( FILE );
98
99 return( $count );
100 }