Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / t / test.t @ a9d8c625

Historique | Voir | Annoter | Télécharger (4,87 ko)

1 80cb3741 Stig Sandbeck Mathisen
# -*- perl -*-
2
3
use strict;
4
use warnings;
5
6
use Test::More;
7
use File::Find ();
8
use Capture::Tiny ':all';
9
10
use vars qw/*name *dir *prune/;
11
*name  = *File::Find::name;
12
*dir   = *File::Find::dir;
13
*prune = *File::Find::prune;
14
my $num_plugins = 0;
15
16
sub wanted {
17 beca8999 Stig Sandbeck Mathisen
    my ( $dev, $ino, $mode, $nlink, $uid, $gid, $interpreter, $arguments );
18 80cb3741 Stig Sandbeck Mathisen
19
    ( ( $dev, $ino, $mode, $nlink, $uid, $gid ) = lstat($_) )
20
        && -f _
21 beca8999 Stig Sandbeck Mathisen
        && ( ( $interpreter, $arguments ) = hashbang("$_") )
22
        && ($interpreter)
23 80cb3741 Stig Sandbeck Mathisen
        && ++$num_plugins
24 beca8999 Stig Sandbeck Mathisen
        && process_file( $_, $name, $interpreter, $arguments );
25 80cb3741 Stig Sandbeck Mathisen
}
26
27
File::Find::find( { wanted => \&wanted }, 'plugins' );
28
29
sub hashbang {
30
    my ($filename) = @_;
31
    open my $file, '<', $filename;
32
    my $firstline = <$file>;
33
    close $file;
34
35 beca8999 Stig Sandbeck Mathisen
    $firstline =~ m{ ^\#!                    # hashbang
36
                     \s*                     # optional space
37
                     (?:/usr/bin/env\s+)?    # optional /usr/bin/env
38
                     (?<interpreter>\S+)     # interpreter
39
                     (?:\s+
40
                         (?<arguments>[^\n]*)   # optional interpreter arguments
41
                     )?
42
               }xms;
43 80cb3741 Stig Sandbeck Mathisen
44 beca8999 Stig Sandbeck Mathisen
    return ($+{interpreter}, $+{arguments});
45 80cb3741 Stig Sandbeck Mathisen
}
46
47
sub process_file {
48 beca8999 Stig Sandbeck Mathisen
    my ( $file, $filename, $interpreter, $arguments ) = @_;
49 80cb3741 Stig Sandbeck Mathisen
    use v5.10.1;
50
51
    if ( $interpreter =~ m{/bin/sh} ) {
52
        subtest $filename => sub {
53
            plan tests => 2;
54 9b01da77 Stig Sandbeck Mathisen
            run_check(
55
                {   command     => [ 'sh', '-n', $file ],
56
                    description => 'sh syntax check'
57
                }
58
            );
59
            run_check(
60
                {   command     => [ 'checkbashisms', $file ],
61
                    description => 'checkbashisms'
62
                }
63
            );
64 80cb3741 Stig Sandbeck Mathisen
        };
65
    }
66 23336966 Stig Sandbeck Mathisen
    elsif ( $interpreter =~ m{/bin/ksh} ) {
67 9b01da77 Stig Sandbeck Mathisen
        run_check(
68
            {   command     => [ 'ksh', '-n', $file ],
69
                description => 'ksh syntax check',
70
                filename    => $filename
71
            }
72
        );
73 23336966 Stig Sandbeck Mathisen
    }
74 92e7aaf8 Stig Sandbeck Mathisen
    elsif ( $interpreter =~ m{bash} ) {
75 9b01da77 Stig Sandbeck Mathisen
        run_check(
76
            {   command     => [ 'bash', '-n', $file ],
77
                description => 'bash syntax check',
78
                filename    => $filename
79
            }
80
        );
81 80cb3741 Stig Sandbeck Mathisen
    }
82
    elsif ( $interpreter =~ m{perl} ) {
83 beca8999 Stig Sandbeck Mathisen
        my $command;
84
        if ($arguments =~ m{-.*T}mx) {
85
            $command = [ 'perl', '-cwT', $file ];
86
        }
87
        else {
88
            $command = [ 'perl', '-cw', $file ];
89
        }
90 9b01da77 Stig Sandbeck Mathisen
        run_check(
91 beca8999 Stig Sandbeck Mathisen
            {   command     => $command,
92 9b01da77 Stig Sandbeck Mathisen
                description => 'perl syntax check',
93
                filename    => $filename
94
            }
95
        );
96 80cb3741 Stig Sandbeck Mathisen
    }
97
    elsif ( $interpreter =~ m{python} ) {
98 9b01da77 Stig Sandbeck Mathisen
    SKIP: {
99
            skip 'need better syntax check for python', 1;
100
            run_check(
101
                {   command => [
102
                        'pylint',        '--rcfile=/dev/null',
103
                        '--errors-only', '--report=no',
104
                        $file
105
                    ],
106
                    description => 'python syntax check',
107
                    filename    => $filename
108
                }
109
            );
110
        }
111 80cb3741 Stig Sandbeck Mathisen
    }
112
    elsif ( $interpreter =~ m{php} ) {
113 9b01da77 Stig Sandbeck Mathisen
        run_check(
114
            {   command     => [ 'php', '-l', $file ],
115
                description => 'php syntax check',
116
                filename    => $filename
117
            }
118
        );
119 80cb3741 Stig Sandbeck Mathisen
    }
120
    elsif ( $interpreter =~ m{j?ruby} ) {
121 9b01da77 Stig Sandbeck Mathisen
        run_check(
122
            {   command     => [ 'ruby', '-cw', $file ],
123
                description => 'ruby syntax check',
124
                filename    => $filename
125
            }
126
        );
127 80cb3741 Stig Sandbeck Mathisen
    }
128
    elsif ( $interpreter =~ m{gawk} ) {
129 9b01da77 Stig Sandbeck Mathisen
        run_check(
130
            {   command => [
131
                    'gawk', '--source', 'BEGIN { exit(0) } END { exit(0) }',
132 80cb3741 Stig Sandbeck Mathisen
                    '--file', $file
133 9b01da77 Stig Sandbeck Mathisen
                ],
134
                description => 'gawk syntax check',
135
                filename    => $filename
136
            }
137 80cb3741 Stig Sandbeck Mathisen
        );
138
    }
139 673303f1 Stig Sandbeck Mathisen
    elsif ( $interpreter =~ m{expect} ) {
140 9b01da77 Stig Sandbeck Mathisen
    SKIP: {
141
            skip 'no idea how to check expect scripts', 1;
142
            pass("No pretending everything is ok");
143
        }
144 673303f1 Stig Sandbeck Mathisen
    }
145 80cb3741 Stig Sandbeck Mathisen
    else {
146
        fail( $filename . " unknown interpreter " . $interpreter );
147
    }
148
}
149
150 9b01da77 Stig Sandbeck Mathisen
sub run_check {
151
    my ($args)        = @_;
152
    my $check_command = $args->{command};
153
    my $description   = $args->{description};
154
    my $filename      = $args->{filename};
155
156
    my $message;
157
158
    if ($filename) {
159
        $message = sprintf( '%s: %s', $filename, $description );
160
    }
161
    else {
162
        $message = $description;
163
    }
164
165 80cb3741 Stig Sandbeck Mathisen
    my ( $stdout, $stderr, $exit ) = capture {
166
        system( @{$check_command} );
167
    };
168 9b01da77 Stig Sandbeck Mathisen
169
    ok( ( $exit == 0 ), $message );
170
171
    if ($exit) {
172 6b5e75a0 Stig Sandbeck Mathisen
        diag(
173
            sprintf(
174
                "\nCommand: %s\n\nSTDOUT:\n\n%s\n\nSTDERR:\n\n%s\n\n",
175
                join( " ", @{$check_command} ),
176
                $stdout, $stderr
177
            )
178
        );
179 80cb3741 Stig Sandbeck Mathisen
    }
180
}
181
182
done_testing($num_plugins);