File: 01_module.t

package info (click to toggle)
slack 0.15.2-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 412 kB
  • sloc: perl: 2,510; makefile: 132; sh: 23
file content (266 lines) | stat: -rw-r--r-- 7,261 bytes parent folder | download | duplicates (7)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/perl -w

use strict;
use Test::More tests => 40;

BEGIN {
    chdir 'test' if -d 'test';
    unshift @INC, '../src';
    use_ok("Slack");
}

use test_util;

# Make sure all the expected funtions are there
can_ok("Slack", qw(default_usage read_config get_system_exit check_system_exit get_options prompt find_files_to_install wrap_rsync wrap_rsync_fh));

# default_usage()
{
    my $usage = Slack::default_usage("qwxle");
    like($usage, qr/\AUsage: qwxle\n/, "Usage statement");
}


# read_config()
{
    my $opt = Slack::read_config(
        file => $test_config_file,
    );

    is_deeply(\%test_config, $opt, "read_config keys");
}

# get_system_exit()
{
    # clear variables
    $! = 0;
    $? = 0;

    system('true');
    eval "Slack::get_system_exit('');";
    like($@, qr#Unknown error#, "get_system_exit exit true");

    system('false');
    my $ret = Slack::get_system_exit('');
    is($ret, 1);

    system('kill -KILL $$');
    eval "Slack::get_system_exit('');";
    like($@, qr#'' caught sig 9\b#, "get_system_exit signal");
}

# check_system_exit
{
    $! = 0;
    $? = 0;

    system('false');
    eval "Slack::check_system_exit('');";
    like($@, qr#'' exited 1\b#, "check_system_exit exit false");
}

# get_options()
{
    my $e = 1; # a counter -- we check for exceptions a lot
    my $opt; # a place to store the options hashref
    my $cl_opt; # likewise for command like hash
    # We require hostname to be set, as get_options does
    # (I suppose we could skip this whole section if we can't get hostname,
    #  since get_options will just throw an exception)
    require Sys::Hostname;
    my $hostname = Sys::Hostname::hostname;

    # First, we check the setting of options and defaults in the absence
    # of a config file.
    eval {
        local @ARGV = (
            '--config=/dev/null',
            "--source=/foo/bar.$$",
        );
        $opt = Slack::get_options();
    };
    is($@, '', "get_options exception ".$e++);
    is($opt->{verbose}, 0, "get_options default verbosity");
    is($opt->{source}, "/foo/bar.$$", "get_options command line source");
    is($opt->{hostname}, $hostname, "get_options hostname");

    eval {
        local @ARGV = (
            '--config=/dev/null',
            '-vv',
        );
        $opt = Slack::get_options();
    };
    is($@, '', "get_options exception ".$e++);
    is($opt->{verbose}, 2, "get_options verbosity increments");

    eval {
        local @ARGV = (
            '--config=/dev/null',
            '-vv', '--quiet', '-v',
        );
        $opt = Slack::get_options();
    };
    is($@, '', "get_options exception ".$e++);
    is($opt->{verbose}, 1, "get_options --quiet");

    # Make sure it works if you pass in $opt, instead of getting return
    eval {
        $opt = {};
        local @ARGV = (
            '--config=/dev/null',
            '-vv',
        );
        Slack::get_options(
            opthash => $opt,
        );
    };
    is($@, '', "get_options exception ".$e++);
    is($opt->{verbose}, 2, "get_options pass in opthash");

    # Next, we check config file parsing.
    eval {
        local @ARGV = (
            "--config=$test_config_file",
        );
        $opt = Slack::get_options();
    };
    is($@, '', "get_options exception ".$e++);
    # A few extra things should be set
    local $test_config{config} = $test_config_file;
    local $test_config{hostname} = $hostname;

    is_deeply($opt, \%test_config, "get_options config keys");

    eval {
        $cl_opt = {};
        local @ARGV = (
            "--config=$test_config_file",
            "--source=/foo/bar.$$",
        );
        $opt = Slack::get_options(
            command_line_hash => $cl_opt,
        );
    };
    is($@, '', "get_options exception ".$e++);
    is($opt->{source}, "/foo/bar.$$",
        "get_options command line overrides config file");
    is($cl_opt->{source}, $opt->{source},
        "get_options command_line_hash source set");
    is($cl_opt->{config}, $test_config_file,
        "get_options command_line_hash config set");
    is(scalar keys %{$cl_opt}, 2,
        "get_options command_line_hash not over-set");

    # Next, non-standard option parsing
    eval {
        local @ARGV = (
            '--config=/dev/null',
            "--foo=$$",
            "--bar=a.$$",
            '--baz',
        );
        $opt = Slack::get_options(
            command_line_options => [
                'foo=i',
                'bar=s',
                'baz',
            ],
        );
    };
    is($@, '', "get_options exception ".$e++);
    is($opt->{foo}, $$, "get_options extra options (int)");
    is($opt->{bar}, "a.$$", "get_options extra options (string)");
    ok($opt->{baz}, "get_options extra options (boolean)");

    # Next, required options
    #   first, when everything should be OK
    eval {
        local @ARGV = (
            "--config=$test_config_file",
            "--foo=$$",
        );
        $opt = Slack::get_options(
            command_line_options => [
                'foo=i',
            ],
            required_options => [qw(foo source)],
        );
    };
    is($@, '', "get_options exception ".$e++);

    #   second, when we should throw an exception because a
    #      required option is missing.
    eval {
        local @ARGV = (
            '--config=/dev/null',
            "--foo=$$",
        );
        $opt = Slack::get_options(
            command_line_options => [
                'foo=i',
            ],
            required_options => [qw(foo source)],
        );
    };
    like($@, qr/Required option/, "get_options required options");

    # test --help with:
    {
        my $helptext = `perl -I$INC[0] -MSlack -e 'Slack::get_options' -- --help`;
        is($?, 0, "get_options --help exit code");
        like($helptext, qr/^Usage: /m, "get_options --help output");
    }

    # test --usage with:
    {
        my $helptext = `perl -I$INC[0] -MSlack -e 'Slack::get_options' -- --invalid 2>&1`;
        isnt($?, 0, "get_options usage exit code");
        like($helptext, qr/^Usage: /m, "get_options usage output");
    }

    # test --version with
    {
        my $versiontext = `perl -I$INC[0] -MSlack -e 'Slack::get_options' -- --version`;
        is($?, 0, 'get_options --version exit code');
        like($versiontext, qr/^slack version [\d\.]+$/, 'get_options --version output')
    }
}
 

# prompt
# difficult to test

# find_files_to_install
# Some of the later functional tests will test this.

# wrap_rsync
{
    eval "Slack::wrap_rsync('true');";
    is($@, '', 'wrap_rsync no exception');

    eval "Slack::wrap_rsync('false');";
    like($@, qr#'false' exited 1\b#, 'wrap_rsync exit false');
}

# wrap_rsync_fh
{
    my $tmpfile = $test_util::TEST_TMPDIR . '/output';
    my $test_text = "test\n";

    my @command = ('/bin/sh', '-c', "cat > $tmpfile");
    my ($fh) = Slack::wrap_rsync_fh(@command);
    print $fh $test_text, "\n";
    close($fh)
        or die "'@command' failed!";

    open($fh, '<', $tmpfile)
        or die "could not open $tmpfile";
    my $line = <$fh>;
    close($fh)
        or die "could not close $tmpfile";

    is($line, $test_text, 'wrap_rsync_fh cat test');
    unlink($tmpfile)
        or die "could not unlink $tmpfile";
}