File: new_dir_interface.t

package info (click to toggle)
libtest-mockfile-perl 0.035-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 424 kB
  • sloc: perl: 4,027; makefile: 7
file content (137 lines) | stat: -rw-r--r-- 3,744 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl -w

use strict;
use warnings;

use Test2::Bundle::Extended;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;
use Test2::Tools::Exception qw< lives dies >;
use Test::MockFile;

sub test_content_with_keywords {
    my ( $dirname, $dir_content ) = @_;

    my $dh;
    my $open;
    ok(
        lives( sub { $open = opendir $dh, $dirname } ),
        "opendir() $dirname successful",
    );

    $open or return;

    my @content;
    ok(
        lives( sub { @content = readdir($dh) } ),
        "readdir() on $dirname successful",
    );

    is(
        \@content,
        $dir_content,
        'Correct directory content through Perl core keywords',
    );

    ok(
        lives( sub { closedir $dh } ),
        "closedir() on $dirname successful",
    );
}

my $count       = 0;
my $get_dirname = sub {
    $count++;
    return "/foo$count";
};

subtest(
    '->dir() checks when going through ->new_dir()' => sub {
        like(
            dies( sub { Test::MockFile->new_dir( '/etc', { 1 => 2 } ) } ),
            qr!^\QYou cannot set stats for nonexistent dir '/etc'\E!xms,
            'Cannot do TMF->dir( "/etc", { 1 => 2 } )',
        );

        like(
            dies( sub { Test::MockFile->new_dir( '/etc', [ 'foo', 'bar' ], { 1 => 2 } ) } ),
            qr!^\QYou cannot set stats for nonexistent dir '/etc'\E!xms,
            'Cannot do TMF->dir( "/etc", [@content], { 1 => 2 }  )',
        );

        like(
            dies( sub { Test::MockFile->new_dir( '/etc', [ 'foo', 'bar' ] ) } ),
            qr!^\QYou cannot set stats for nonexistent dir '/etc'\E!xms,
            'Cannot do TMF->dir( "/etc", [@content] )',
        );
    }
);

subtest(
    'Scenario 1: ->new_dir() can create dir' => sub {
        my $dirname = $get_dirname->();
        my $dir     = Test::MockFile->new_dir($dirname);

        ok( -d $dirname, "Directory $dirname exists" );

        is(
            $dir->contents(),
            [qw< . .. >],
            'Correct contents of directory through ->contents()',
        );

        test_content_with_keywords( $dirname, [qw< . .. >] );
    }
);

subtest(
    'Scenario 2: ->new_dir() with mode sets the mode' => sub {
        my $dirname  = $get_dirname->();
        my $base_dir = Test::MockFile->new_dir("${dirname}-base");
        my $dir      = Test::MockFile->new_dir( $dirname, { 'mode' => 0300 } );

        ok( -d $base_dir->path(), "$dirname exists" );
        ok( -d $dirname,          "$dirname exists" );

        my $def_perms = sprintf '%04o', ( ( stat $base_dir->path() )[2] ^ umask ) & 07777;
        my $new_perms = sprintf '%04o', ( ( stat $dirname )[2] ^ umask ) & 07777;

        # make sure we're not getting fooled by the default permissions
        isnt( $def_perms, $new_perms, "We picked perms ($new_perms) that are not the default ($def_perms)" );

        is(
            $new_perms,
            '0300',
            'Mode was set correctly',
        );

        is(
            $dir->contents(),
            [qw< . .. >],
            "Correct contents to $dirname",
        );

        test_content_with_keywords( $dirname, [qw< . .. >] );
    }
);

subtest(
    'Scenario 3: ->new_dir() after mkdir() has an error' => sub {
        my $dirname = $get_dirname->();
        my $dir     = Test::MockFile->new_dir($dirname);

        ok( -d $dirname,      "$dirname exists" );
        ok( !mkdir($dirname), "mkdir $dirname fails, since dir already exists" );
        isnt( $! + 0, 0, "\$! is set to an error: " . ( $! + 0 ) . " ($!)" );

        is(
            $dir->contents(),
            [qw< . .. >],
            "Correct contents to $dirname",
        );

        test_content_with_keywords( $dirname, [qw< . .. >] );
    }
);

done_testing();