aboutsummaryrefslogtreecommitdiffstats
path: root/t/gprintify
blob: 9069d1d9f2872b6d4bf49c54a9d5f7e5df7b683a (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/perl
# $Id: gprintify 257533 2009-05-23 12:45:15Z guillomovitch $

use strict;
use warnings;
use Test::More;
use File::Temp qw/tempdir/;
use File::Path qw/make_path/;
use FindBin qw/$Bin/;
use lib "$Bin/../lib";
use Digest::MD5;

my @string_results = (
    [ 'result: $foo', 'result: %s', ' "$foo"' ],
    [ 'result: $foo,$bar', 'result: %s,%s', ' "$foo" "$bar"' ],
    [ 'result: \$foo', 'result: \$foo', '' ],
    [ 'result: \$foo,$bar', 'result: \$foo,%s', ' "$bar"' ],
    [ 'result: ${foo}', 'result: %s', ' "${foo}"', '' ],
    [ 'result: ${foo##*/}', 'result: %s', ' "${foo##*/}"' ],
    [ 'result: ${foo%%*/}', 'result: %s', ' "${foo%%*/}"' ],
    [ 'result: ${foo#*/}', 'result: %s', ' "${foo#*/}"' ],
    [ 'result: ${foo%*/}', 'result: %s', ' "${foo%*/}"' ]
);
my @start_results = (
    [ 'echo "result: $foo"', 'gprintf "', '\n"' ],
    [ '# echo "result: $foo"', '# gprintf "', '\n"' ],
    [ 'echo -n "result: $foo"', 'gprintf "', '"' ],
    [ 'echo -e "result: $foo"', 'gprintf "', '\n"' ],
);

my @line_results = (
    [ 
        'echo "result: $foo"' . "\n",
        'gprintf "result: %s\n" "$foo"' . "\n"
    ],
    [ 
        'echo "result: \"$foo\""' . "\n",
        'gprintf "result: \"%s\"\n" "$foo"' . "\n"
    ],
    [
        'test "$foo" != "AB"',
        'test "$foo" != "AB"',
    ]
);

plan tests =>
    4 + (@string_results * 2) + (@start_results * 2) + (@line_results);

# test loading
ok(require("gprintify"), "loading file OK");

# test string function
foreach my $result (@string_results) {
    my ($new_string, $variables) = process_string($result->[0]);
    is($new_string, $result->[1], "new string OK");
    is($variables, $result->[2], "variable OK");
}

# test start function
foreach my $result (@start_results) {
    my ($new_start, $string_end) = process_start($result->[0]);
    is($new_start, $result->[1], "new start OK");
    is($string_end, $result->[2], "string end OK");
}

# test line function
foreach my $result (@line_results) {
    my ($new_line) = process_line($result->[0]);
    is($new_line, $result->[1], "new line OK");
}

# test the script itself
my ($buildroot, $script, $before, $after);

($buildroot, $script) = setup(<<'EOF');
echo "Usage: $0 {start|stop|status}"
EOF

$before = get_md5($script);
run($buildroot);
$after = get_md5($script);

is(
    $before,
    $after,
    'service not sourcing /etc/init.d/functions should not be modified'
);

($buildroot, $script) = setup(<<'EOF');
. /etc/init.d/functions
echo "Usage: $0 {start|stop|status}"
EOF

$before = get_md5($script);
run($buildroot);
$after = get_md5($script);

isnt(
    $before,
    $after,
    'service sourcing /etc/init.d/functions should be modified'
);

($buildroot, $script) = setup(<<'EOF');
. /etc/init.d/functions
echo "Usage: $0 {start|stop|status}"
EOF

$before = get_md5($script);
$ENV{EXCLUDE_FROM_GPRINTIFICATION} = 'test';
run($buildroot);
$after = get_md5($script);

is(
    $before,
    $after,
    'EXCLUDE_FROM_GPRINTIFICATION should prevent service modification'
);

sub setup {
    my ($content) = @_;

    my $buildroot = tempdir(CLEANUP => ($ENV{TEST_DEBUG} ? 0 : 1));

    my $initrddir = $buildroot . '/etc/rc.d/init.d';
    my $script = $initrddir . '/test';

    make_path($initrddir);
    open(my $out, '>', $script) or die "can't write to $script: $!";
    print $out $content;
    close($out);

    return ($buildroot, $script);
}

sub run {
    my ($buildroot) = @_;

    $ENV{RPM_BUILD_ROOT} = $buildroot;
    system("$Bin/../gprintify");
}

sub get_md5 {
    my ($file) = @_;
    open(my $in, '<', $file) or die "can't read $file: $!";
    binmode($in);
    my $md5 = Digest::MD5->new();
    $md5->addfile($in);
    close($in);
    return $md5->hexdigest();
}