Projet

Général

Profil

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

root / t / test.t @ 7028ede4

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

1
# -*- 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
    my ( $dev, $ino, $mode, $nlink, $uid, $gid, $interpreter, $arguments );
18

    
19
    ( ( $dev, $ino, $mode, $nlink, $uid, $gid ) = lstat($_) )
20
        && -f _
21
        && ( ( $interpreter, $arguments ) = hashbang("$_") )
22
        && ($interpreter)
23
        && ++$num_plugins
24
        && process_file( $_, $name, $interpreter, $arguments );
25
}
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
    $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

    
44
    return ( $+{interpreter}, $+{arguments} );
45
}
46

    
47
sub process_file {
48
    my ( $file, $filename, $interpreter, $arguments ) = @_;
49
    use v5.10.1;
50

    
51
    if ( $interpreter =~ m{/bin/sh} ) {
52
        subtest $filename => sub {
53
            plan tests => 2;
54
            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
        };
65
    }
66
    elsif ( $interpreter =~ m{/bin/ksh} ) {
67
        run_check(
68
            {   command     => [ 'ksh', '-n', $file ],
69
                description => 'ksh syntax check',
70
                filename    => $filename
71
            }
72
        );
73
    }
74
    elsif ( $interpreter =~ m{bash} ) {
75
        run_check(
76
            {   command     => [ 'bash', '-n', $file ],
77
                description => 'bash syntax check',
78
                filename    => $filename
79
            }
80
        );
81
    }
82
    elsif ( $interpreter =~ m{perl} ) {
83
        my $command;
84
        if ( $arguments =~ m{-.*T}mx ) {
85
            $command = [ 'perl', '-cwT', $file ];
86
        }
87
        else {
88
            $command = [ 'perl', '-cw', $file ];
89
        }
90
        run_check(
91
            {   command     => $command,
92
                description => 'perl syntax check',
93
                filename    => $filename
94
            }
95
        );
96
    }
97
    elsif ( $interpreter =~ m{python} ) {
98
        run_check(
99
            {   command     => [ 'python', '-m', 'py_compile', $file ],
100
                description => 'python compile',
101
                filename    => $filename
102
            }
103
        );
104
    }
105
    elsif ( $interpreter =~ m{php} ) {
106
        run_check(
107
            {   command     => [ 'php', '-l', $file ],
108
                description => 'php syntax check',
109
                filename    => $filename
110
            }
111
        );
112
    }
113
    elsif ( $interpreter =~ m{j?ruby} ) {
114
        run_check(
115
            {   command     => [ 'ruby', '-cw', $file ],
116
                description => 'ruby syntax check',
117
                filename    => $filename
118
            }
119
        );
120
    }
121
    elsif ( $interpreter =~ m{gawk} ) {
122
        run_check(
123
            {   command => [
124
                    'gawk', '--source', 'BEGIN { exit(0) } END { exit(0) }',
125
                    '--file', $file
126
                ],
127
                description => 'gawk syntax check',
128
                filename    => $filename
129
            }
130
        );
131
    }
132
    elsif ( $interpreter =~ m{expect} ) {
133
    SKIP: {
134
            skip 'no idea how to check expect scripts', 1;
135
            pass("No pretending everything is ok");
136
        }
137
    }
138
    else {
139
        fail( $filename . " unknown interpreter " . $interpreter );
140
    }
141
}
142

    
143
sub run_check {
144
    my ($args)        = @_;
145
    my $check_command = $args->{command};
146
    my $description   = $args->{description};
147
    my $filename      = $args->{filename};
148

    
149
    my $message;
150

    
151
    if ($filename) {
152
        $message = sprintf( '%s: %s', $filename, $description );
153
    }
154
    else {
155
        $message = $description;
156
    }
157

    
158
    my ( $stdout, $stderr, $exit ) = capture {
159
        system( @{$check_command} );
160
    };
161

    
162
    ok( ( $exit == 0 ), $message );
163

    
164
    if ($exit) {
165
        diag(
166
            sprintf(
167
                "\nCommand: %s\n\nSTDOUT:\n\n%s\n\nSTDERR:\n\n%s\n\n",
168
                join( " ", @{$check_command} ),
169
                $stdout, $stderr
170
            )
171
        );
172
    }
173
}
174

    
175
done_testing($num_plugins);