File: chmod.t

package info (click to toggle)
libtest-mockfile-perl 0.037-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 436 kB
  • sloc: perl: 4,110; makefile: 7
file content (185 lines) | stat: -rw-r--r-- 5,754 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
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
#!/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 Test2::Tools::Warnings qw< warning >;
use Test::MockFile qw< nostrict >;

use File::Temp qw< tempfile >;

my $filename = __FILE__;
my $file     = Test::MockFile->file( $filename, 'whatevs' );

subtest(
    'Defaults' => sub {
        my $dir_foo  = Test::MockFile->dir('/foo');
        my $file_bar = Test::MockFile->file( '/foo/bar', 'content' );

        ok( -d '/foo',     'Directory /foo exists' );
        ok( -f '/foo/bar', 'File /foo/bar exists' );

        my $dir_def_perm = sprintf '%04o', 0777 - umask;
        is(
            sprintf( '%04o', ( stat '/foo' )[2] & 07777 ),
            $dir_def_perm,
            "Directory /foo is set to $dir_def_perm",
        );

        # These variables are for debugging test failures
        my $umask         = sprintf '%04o', umask;
        my $perms_before  = sprintf '%04o', Test::MockFile::S_IFPERMS() & 0666;
        my $perms_after_1 = sprintf '%04o', ( Test::MockFile::S_IFPERMS() & 0666 ) ^ umask;
        my $perms_after_2 = sprintf '%04o', ( ( Test::MockFile::S_IFPERMS() & 0666 ) ^ umask ) | Test::MockFile::S_IFREG();

        my $file_def_perm = sprintf '%04o', 0666 - umask;
        is(
            sprintf( '%04o', ( stat '/foo/bar' )[2] & 07777 ),
            $file_def_perm,
            "File /foo/bar is set to $file_def_perm (umask: $umask, perms before: $perms_before, perms after 1: $perms_after_1, perms after 2: $perms_after_2)",
        );
    }
);

subtest(
    'Changing mode (real vs. mocked)' => sub {
        ok( CORE::mkdir('fooz'),         'Successfully created real directory' );
        ok( CORE::chmod( 0600, 'fooz' ), 'Successfully chmod\'ed real directory' );
        is(
            sprintf( '%04o', ( CORE::stat('fooz') )[2] & 07777 ),
            '0600',
            'CORE::chmod() set the perms correctly',
        );
        ok( CORE::rmdir('fooz'), 'Successfully deleted real directory' );

        my $dir_foo  = Test::MockFile->dir('/foo');
        my $file_bar = Test::MockFile->file( '/foo/bar', 'content' );

        ok( -d '/foo',     'Directory /foo exists' );
        ok( -f '/foo/bar', 'File /foo/bar exists' );

        chmod 0600, qw< /foo /foo/bar >;

        is(
            sprintf( '%04o', ( stat '/foo' )[2] & 07777 ),
            '0600',
            'Directory /foo is now set to 0600',
        );

        is(
            sprintf( '%04o', ( stat '/foo/bar' )[2] & 07777 ),
            '0600',
            'File /foo/bar is now set to 0600',
        );

        chmod 0777, qw< /foo /foo/bar >;

        is(
            sprintf( '%04o', ( stat '/foo' )[2] & 07777 ),
            '0777',
            'Directory /foo is now set to 0600',
        );

        is(
            sprintf( '%04o', ( stat '/foo/bar' )[2] & 07777 ),
            '0777',
            'File /foo/bar is now set to 0600',
        );
    }
);

subtest(
    'Changing mode filehandle' => sub {
      SKIP: {

            if ( $^V lt 5.28.0 ) {
                skip "Skipped: need Perl >= 5.28.0", 1;
                return;
            }

            my $test_string = "abcd\nefgh\n";
            my ( $fh_real, $filename ) = tempfile();
            print $fh_real $test_string;
            {
                note "-------------- REAL MODE --------------";
                ok chmod( 0700, $filename ), 'chmod on file';
                open( my $fh, '>', $filename );
                ok chmod( 0711, $fh ), 'chmod on filehandle';
            }

            {
                note "-------------- MOCK MODE --------------";
                my $bar = Test::MockFile->file( $filename, $test_string );
                ok chmod( 0700, $filename ), 'chmod on file';
                open( my $fh, '>', $filename );
                ok chmod( 0711, $fh ), 'chmod on filehandle';
            }

        }

        return;
    }
);

subtest(
    'Providing a string as mode mask' => sub {
        ok( CORE::mkdir('fooz'), 'Successfully created real directory' );

        my $core_chmod_res;

        like(
            warning( sub { $core_chmod_res = CORE::chmod( 'hello', 'fooz' ) } ),
            qr/^\QArgument "hello" isn't numeric in chmod\E/xms,
            'CORE::chmod() threw a warning when trying to numify',
        );

        ok( $core_chmod_res, 'Successfully chmod\'ed real directory' );

        is( $!, '', 'No observed error' );
        is(
            sprintf( '%04o', ( CORE::stat('fooz') )[2] & 07777 ),
            '0000',
            'CORE::chmod() set the perms correctly',
        );

        ok( CORE::rmdir('fooz'), 'Successfully deleted real directory' );

        # --- Mock ---

        my $dir_foo = Test::MockFile->dir('/foo');

        ok( !-d '/foo', 'Directory /foo does not exist' );

        # If we don't zero this out, nothing else will - wtf?
        $! = 0;

        ok( mkdir('/foo'), 'Successfully created mocked directory' );
        ok( -d '/foo',     'Directory /foo now exists' );

        my $chmod_res;
        like(
            warning( sub { $chmod_res = chmod 'hello', '/foo' } ),
            qr/^\QArgument "hello" isn't numeric in chmod\E/xms,
            'chmod() threw a warning when trying to numify',
        );

        ok( $chmod_res, 'Successfully chmod\'ed real directory' );

        is( $!, '', 'No observed error' );
        is(
            sprintf( '%04o', ( CORE::stat('/foo') )[2] & 07777 ),
            '0000',
            'chmod() set the perms correctly',
        );

        ok( rmdir('/foo'), 'Successfully deleted real directory' );
        ok( !-d '/foo',    'Directory /foo no longer exist' );
    }
);

done_testing();
exit;