[t][TT #1122] Convert t/op/numbert.t to PIR, mgrimes++
[parrot.git] / t / codingstd / copyright.t
blob664b08e79f9377ba214523f8386aa16244a053fd
1 #! perl
2 # Copyright (C) 2007-2009, Parrot Foundation.
3 # $Id$
5 use strict;
6 use warnings;
7 use Cwd;
8 use File::Spec ();
9 use lib qw( . lib ../lib ../../lib );
10 use Parrot::Distribution;
11 use Test::More tests => 3;
13 =head1 NAME
15 t/codingstd/copyright.t - checks for an appropriate copyright
16 statement in parrot source files
18 =head1 SYNOPSIS
20     # test all files
21     % prove t/codingstd/copyright.t
23     # test specific files
24     % perl t/codingstd/copyright.t src/foo.c include/parrot/bar.h
26 =head1 DESCRIPTION
28 Ensures that the copyright statement exists in each source file and that it
29 is up to date.
31 =head1 SEE ALSO
33 L<docs/pdds/pdd07_codingstd.pod>
35 =cut
37 my $DIST = Parrot::Distribution->new;
39 my @files = @ARGV ? <@ARGV> : (
40     $DIST->get_c_language_files(),
41     $DIST->get_perl_language_files(),
42     $DIST->get_make_language_files(),
43     $DIST->get_pir_language_files(),
45 my (
46     @no_copyright_files,
47     @bad_format_copyright_files,
48     @duplicate_copyright_files,
51 my $copyright_simple =
52     qr/Copyright \(C\) \d{4}/i;
53 my $copyright_parrot =
54     qr/Copyright \(C\) (?:\d{4}\-)?\d{4}, Parrot Foundation\.\n/;
56 foreach my $file (@files) {
58     # if we have command line arguments, the file is the full path
59     # otherwise, use the relevant Parrot:: path method
60     my $path = @ARGV ? $file : $file->path;
62     my $buf = $DIST->slurp($path);
64     # does there exist a copyright statement at all?
65     if ( $buf !~ $copyright_simple ) {
66         push @no_copyright_files, $path;
67         next;
68     }
70     # is the copyright text correct?
71     # If so, remove it...
72     if ( ! ($buf =~ s/$copyright_parrot//) ) {
73         push @bad_format_copyright_files, $path;
74     }
75     # ... and then see if any other copyright notices exist.
76     elsif ($buf =~ $copyright_simple) {
77         push @duplicate_copyright_files, $path;
78     }
81 my $suggested_version=<<END_SUGGESTION;
82   Copyright (C) C<start-year>-C<last-year-modified>, Parrot Foundation.
83 To find the C<start-year>, use a command such as:
84   svn log C<filename> | grep 'lines' | tail -n 1
85 To find the C<last-year-modified>, use a command such as:
86   svn log C<filename> | grep 'lines' | head -n 1
87 END_SUGGESTION
89 # run the tests
90 ok( !scalar(@no_copyright_files), 'Copyright statement exists' )
91     or diag(
92     join
93         $/ => "No copyright statement found in " . scalar @no_copyright_files . " files:",
94     @no_copyright_files,
95     "The copyright statement should read something like:",
96     $suggested_version
97     );
99     ok( !scalar(@bad_format_copyright_files), 'Copyright statement in the right format' )
100         or diag(
101         join
102             $/ => "Bad format in copyright statement found in "
103             . scalar @bad_format_copyright_files
104             . " files:",
105         @bad_format_copyright_files,
106         "Please update to read something like:",
107         $suggested_version
108         );
110 # Certain files contain the string 'Copyright (c)' more than once
111 # because they contain heredocs for generated files, correctly cite the
112 # copyright information for non-Parrot code, etc.  We shall exclude them
113 # from our test for duplicate copyright statements.
115 my @permitted_duplicate_copyright_files = (
116     {
117         file    => 'examples/c/test_main.c',
118         reason  => 'sample code',
119     },
120     {
121         file    => 'Configure.pl',
122         reason  => 'cite automake copyright statement',
123     },
124     {
125         file    => 'config/gen/opengl.pm',
126         reason  => 'heredoc text for generated file',
127     },
128     {
129         file    => 'lib/Parrot/Configure/Messages.pm',
130         reason  => 'heredoc for print_introduction()',
131     },
132     {
133         file    => 't/tools/dev/searchops/samples.pm',
134         reason  => 'sample code used in testing',
135     },
136     {
137         file    => 'tools/build/nativecall.pl',
138         reason  => 'heredoc text for generated file',
139     },
140     {
141         file    => 'tools/build/vtable_extend.pl',
142         reason  => 'heredoc text for generated file',
143     },
144     {
145         file    => 'examples/pir/quine_ord.pir',
146         reason  => 'quine',
147     },
149 my $cwd = cwd();
150 my %permitted_duplicate_copyright_files =
151     map { ( File::Spec->catfile( $cwd, $_->{file} ) ) => 1 }
152         @permitted_duplicate_copyright_files;
154 my @non_permitted_duplicate_copyright_files =
155     grep { ! $permitted_duplicate_copyright_files{ $_ } }
156         @duplicate_copyright_files;
158 TODO: {
159     local $TODO = 'duplicate copyrights exist.';
161     ok( !scalar(@non_permitted_duplicate_copyright_files),
162         'Duplicate Copyright statements' )
163         or diag(
164         join
165             $/ => "Duplicate copyright statement found in "
166             . scalar @non_permitted_duplicate_copyright_files
167             . " files:",
168         @non_permitted_duplicate_copyright_files,
169         "Please get copyright assigned to Parrot Foundation",
170         "and remove alternate notice; or remove duplicated",
171         "notice for Parrot Foundation."
172         );
175 # Local Variables:
176 #   mode: cperl
177 #   cperl-indent-level: 4
178 #   fill-column: 100
179 # End:
180 # vim: expandtab shiftwidth=4: