File: portable-shell.t

package info (click to toggle)
rinse 4.3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 260 kB
  • sloc: perl: 891; sh: 396; makefile: 87
file content (84 lines) | stat: -rwxr-xr-x 1,521 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/perl -w
#
#  Test that we don't use non-portable shell syntax in our hooks.
#
#  Specifically we test for:
#
# 1.  "[[" & "]]" around tests.
#
# 2.  The "function" keyword
#
# Steve
# --


use strict;
use File::Find;
use Test::More qw( no_plan );


#
#  Find all the files beneath the current directory,
# and call 'checkFile' with the name.
#
find( { wanted => \&checkFile, no_chdir => 1 }, '.' );



#
#  Check a file.
#
#  If this is a shell script then call "sh -n $name", otherwise
# return
#
sub checkFile
{
    # The file.
    my $file = $File::Find::name;

    # We don't care about directories
    return if ( ! -f $file );

    # See if it is a shell script.
    my $isShell = 0;

    # Read the file.
    open( INPUT, "<", $file );
    foreach my $line ( <INPUT> )
    {
        if ( ( $line =~ /\/bin\/sh/ ) ||
             ( $line =~ /\/bin\/bash/ ) )
        {
            $isShell = 1;
        }
    }
    close( INPUT );

    #
    #  Return if it wasn't a shell script.
    #
    return if ( ! $isShell );


    # The result
    my $result = 0;

    #
    #  Open the file and read it.
    #
    open( INPUT, "<", $file )
      or die "Failed to open '$file' - $!";

    while( my $line = <INPUT> )
    {
        # [[ or ]]
        $result += 1 if ( $line =~ /\[\[/ );
        $result += 1 if ( $line =~ /\]\]/ );

        # function
        $result += 1 if ( $line =~ /^[ \t]*function/ );
    }
    close( INPUT );

    is( $result, 0, "Shell script passes our portability check: $file" );
}