File: write_cache.pl

package info (click to toggle)
ddclient 3.11.2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,284 kB
  • sloc: perl: 14,597; sh: 916; makefile: 146; xml: 55
file content (49 lines) | stat: -rw-r--r-- 1,580 bytes parent folder | download | duplicates (3)
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
use Test::More;
use File::Spec::Functions;
use File::Temp;
eval { require Test::MockModule; } or plan(skip_all => $@);
SKIP: { eval { require Test::Warnings; } or skip($@, 1); }
eval { require 'ddclient'; } or BAIL_OUT($@);

my $warning;

my $module = Test::MockModule->new('ddclient');
# Note: 'mock' is used instead of 'redefine' because 'redefine' is not available in the versions of
# Test::MockModule distributed with old Debian and Ubuntu releases.
$module->mock('warning', sub {
    BAIL_OUT("warning already logged") if defined($warning);
    $warning = sprintf(shift, @_);
});
my $tmpdir = File::Temp->newdir();
my $dir = $tmpdir->dirname();
diag("temporary directory: $dir");

sub tc {
    return {
        name => shift,
        f => shift,
        warning_regex => shift,
    };
}

my @test_cases = (
    tc("create cache file",    catfile($dir, 'a', 'b', 'cachefile'),        undef),
    tc("overwrite cache file", catfile($dir, 'a', 'b', 'cachefile'),        undef),
    tc("bad directory",        catfile($dir, 'a', 'b', 'cachefile', 'bad'), qr/Failed to create/i),
    tc("bad file",             catfile($dir, 'a', 'b'),                     qr/Failed to create/i),
);

for my $tc (@test_cases) {
    $warning = undef;
    ddclient::write_cache($tc->{f});
    subtest $tc->{name} => sub {
        if (defined($tc->{warning_regex})) {
            like($warning, $tc->{warning_regex}, "expected warning message");
        } else {
            ok(!defined($warning), "no warning");
            ok(-f $tc->{f}, "cache file exists");
        }
    };
}

done_testing();