rt 4.0.23
[freeside.git] / rt / t / 99-policy.t
1 use strict;
2 use warnings;
3
4 use RT::Test nodb => 1;
5 use File::Find;
6
7 my @files;
8 find( sub { push @files, $File::Find::name if -f },
9       qw{etc lib share t bin sbin devel/tools} );
10 if ( my $dir = `git rev-parse --git-dir 2>/dev/null` ) {
11     # We're in a git repo, use the ignore list
12     chomp $dir;
13     my %ignores;
14     $ignores{ $_ }++ for grep $_, split /\n/,
15         `git ls-files -o -i --exclude-standard .`;
16     @files = grep {not $ignores{$_}} @files;
17 }
18
19 sub check {
20     my $file = shift;
21     my %check = (
22         strict   => 0,
23         warnings => 0,
24         shebang  => 0,
25         exec     => 0,
26         bps_tag  => 0,
27         @_,
28     );
29
30     if ($check{strict} or $check{warnings} or $check{shebang} or $check{bps_tag}) {
31         local $/;
32         open my $fh, '<', $file or die $!;
33         my $content = <$fh>;
34
35         like(
36             $content,
37             qr/^use strict(?:;|\s+)/m,
38             "$file has 'use strict'"
39         ) if $check{strict};
40
41         like(
42             $content,
43             qr/^use warnings(?:;|\s+)/m,
44             "$file has 'use warnings'"
45         ) if $check{warnings};
46
47         if ($check{shebang} == 1) {
48             like( $content, qr/^#!/, "$file has shebang" );
49         } elsif ($check{shebang} == -1) {
50             unlike( $content, qr/^#!/, "$file has no shebang" );
51         }
52
53         $check{bps_tag} = -1 if $check{bps_tag} == 1
54             and not $content =~ /Copyright\s+\(c\)\s+\d\d\d\d-\d\d\d\d Best Practical Solutions/i
55                 and $file =~ /(?:ckeditor|scriptaculous|superfish|tablesorter|farbtastic)/i;
56         $check{bps_tag} = -1 if $check{bps_tag} == 1
57             and not $content =~ /Copyright\s+\(c\)\s+\d\d\d\d-\d\d\d\d Best Practical Solutions/i
58                 and ($content =~ /\b(copyright|GPL|Public Domain)\b/i
59                   or /\(c\)\s+\d\d\d\d(?:-\d\d\d\d)?/i);
60         if ($check{bps_tag} == 1) {
61             like( $content, qr/[B]EGIN BPS TAGGED BLOCK {{{/, "$file has BPS license tag");
62         } elsif ($check{bps_tag} == -1) {
63             unlike( $content, qr/[B]EGIN BPS TAGGED BLOCK {{{/, "$file has no BPS license tag");
64         }
65     }
66
67     my $executable = ( stat $file )[2] & 0100;
68     if ($check{exec} == 1) {
69         if ( $file =~ /\.in$/ ) {
70             ok( !$executable, "$file permission is u-x (.in will add +x)" );
71         } else {
72             ok( $executable, "$file permission is u+x" );
73         }
74     } elsif ($check{exec} == -1) {
75         ok( !$executable, "$file permission is u-x" );
76     }
77 }
78
79 check( $_, shebang => -1, exec => -1, warnings => 1, strict => 1, bps_tag => 1 )
80     for grep {m{^lib/.*\.pm$}} @files;
81
82 check( $_, shebang => -1, exec => -1, warnings => 1, strict => 1, bps_tag => -1 )
83     for grep {m{^t/.*\.t$}} @files;
84
85 check( $_, shebang => 1, exec => 1, warnings => 1, strict => 1, bps_tag => 1 )
86     for grep {m{^s?bin/}} @files;
87
88 check( $_, shebang => 1, exec => 1, warnings => 1, strict => 1, bps_tag => 1 )
89     for grep {m{^devel/tools/} and not m{/(localhost\.(crt|key)|mime\.types)$}} @files;
90
91 check( $_, exec => -1, bps_tag => not m{\.(png|gif|jpe?g)$} )
92     for grep {m{^share/html/}} @files;
93
94 check( $_, exec => -1 )
95     for grep {m{^share/(po|fonts)/}} @files;
96
97 check( $_, exec => -1 )
98     for grep {m{^t/data/}} @files;
99
100 check( $_, exec => -1, bps_tag => -1 )
101     for grep {m{^etc/upgrade/[^/]+/}} @files;