Projet

Général

Profil

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

root / t / test.t @ a6ea4c42

Historique | Voir | Annoter | Télécharger (4,88 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{/bin/zsh} ) {
83
        run_check(
84
            {   command     => [ 'zsh', '-n', $file ],
85
                description => 'zsh syntax check',
86
                filename    => $filename
87
            }
88
        );
89
    }
90
    elsif ( $interpreter =~ m{perl} ) {
91
        my $command;
92
        if ( $arguments =~ m{-.*T}mx ) {
93
            $command = [ 'perl', '-cwT', $file ];
94
        }
95
        else {
96
            $command = [ 'perl', '-cw', $file ];
97
        }
98
        run_check(
99
            {   command     => $command,
100
                description => 'perl syntax check',
101
                filename    => $filename
102
            }
103
        );
104
    }
105
    elsif ( $interpreter =~ m{python} ) {
106
        run_check(
107
            {   command     => [ 'python', '-m', 'py_compile', $file ],
108
                description => 'python compile',
109
                filename    => $filename
110
            }
111
        );
112
    }
113
    elsif ( $interpreter =~ m{php} ) {
114
        run_check(
115
            {   command     => [ 'php', '-l', $file ],
116
                description => 'php syntax check',
117
                filename    => $filename
118
            }
119
        );
120
    }
121
    elsif ( $interpreter =~ m{j?ruby} ) {
122
        run_check(
123
            {   command     => [ 'ruby', '-cw', $file ],
124
                description => 'ruby syntax check',
125
                filename    => $filename
126
            }
127
        );
128
    }
129
    elsif ( $interpreter =~ m{gawk} ) {
130
        run_check(
131
            {   command => [
132
                    'gawk', '--source', 'BEGIN { exit(0) } END { exit(0) }',
133
                    '--file', $file
134
                ],
135
                description => 'gawk syntax check',
136
                filename    => $filename
137
            }
138
        );
139
    }
140
    elsif ( $interpreter =~ m{expect} ) {
141
    SKIP: {
142
            skip 'no idea how to check expect scripts', 1;
143
            pass("No pretending everything is ok");
144
        }
145
    }
146
    else {
147
        fail( $filename . " unknown interpreter " . $interpreter );
148
    }
149
}
150

    
151
sub run_check {
152
    my ($args)        = @_;
153
    my $check_command = $args->{command};
154
    my $description   = $args->{description};
155
    my $filename      = $args->{filename};
156

    
157
    my $message;
158

    
159
    if ($filename) {
160
        $message = sprintf( '%s: %s', $filename, $description );
161
    }
162
    else {
163
        $message = $description;
164
    }
165

    
166
    my ( $stdout, $stderr, $exit ) = capture {
167
        system( @{$check_command} );
168
    };
169

    
170
    ok( ( $exit == 0 ), $message );
171

    
172
    if ($exit) {
173
        diag(
174
            sprintf(
175
                "\nCommand: %s\n\nSTDOUT:\n\n%s\n\nSTDERR:\n\n%s\n\n",
176
                join( " ", @{$check_command} ),
177
                $stdout, $stderr
178
            )
179
        );
180
    }
181
}
182

    
183
done_testing($num_plugins);