File: mock-setting-errno.t

package info (click to toggle)
liboverload-filecheck-perl 0.013-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,088 kB
  • sloc: perl: 2,030; ansic: 97; makefile: 8
file content (120 lines) | stat: -rw-r--r-- 2,725 bytes parent folder | download
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
#!/usr/bin/perl -w

# Copyright (c) 2018, cPanel, LLC.
# All rights reserved.
# http://cpanel.net
#
# This is free software; you can redistribute it and/or modify it under the
# same terms as Perl itself. See L<perlartistic>.

use strict;
use warnings;

use Test2::Bundle::Extended;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;

use Overload::FileCheck q{:all};
use Errno ();

{
    note "no mocks at this point";

    ok -e q[/tmp],           "/tmp/exits";
    ok !-e q[/do/not/exist], "/do/not/exist";

    my ( $check, $errno_int );

    $check = -e q[/do/not/exist];

    # do not check the errno string, as it depends on the locale
    $errno_int = int($!);

    ok !$check, "file does not exist";

    is $errno_int, Errno::ENOENT(), "ERRNO int value set";

    $check     = -e $^X;
    $errno_int = int($!);

    ok $check, q[$^X exists];
    is $errno_int, Errno::ENOENT(), "ERRNO was not reset";
}

{
    local $! = 0;

    my $existing_file = q[/there];
    my $missing_file  = q[/not-there];

    mock_file_check(
        '-e' => sub {
            my $f = shift;
            return CHECK_IS_FALSE if $f eq $missing_file;
            return CHECK_IS_TRUE  if $f eq $existing_file;

            # we do not know and let perl check it for us
            return FALLBACK_TO_REAL_OP;

        }
    );

    my ( $check, $errno_int );

    is int($!), 0, 'errno=0 at startup';

    note "check existing file";
    $check     = -e $existing_file;
    $errno_int = int($!);

    ok $check, 'existing_file is there';
    is $errno_int, 0, '$! is not set';

    note "check missing file";
    $check     = -e $missing_file;
    $errno_int = int($!);

    ok !$check, 'missing_file not there';
    is $errno_int, Errno::ENOENT(), '$! is set to the default value';

    note "check existing file again";
    $check     = -e $existing_file;
    $errno_int = int($!);

    ok $check, 'existing_file is there';
    is $errno_int, Errno::ENOENT(), '$! was not reset';

    ok -e $^X, q[$^X exists];
    is int($!), Errno::ENOENT(), '$! was not reset when fallback to original OP';

    unmock_all_file_checks();
}

{
    note "User provide its own ERRNO error";
    local $! = 0;

    note "we are mocking -e => 1";
    mock_file_check(
        '-e' => sub {
            my $f = shift;
            note "mocked -e called....";

            $! = Errno::EINTR();    # set errno

            return CHECK_IS_FALSE;
        }
    );

    my $check = -e q[/tmp];

    # do not check the errno string, as it depends on the locale
    my $errno_int = int($!);

    ok !$check, "/tmp does not exist";
    is $errno_int, Errno::EINTR(), "ERRNO int value set to Errno::EINTR()";

    unmock_all_file_checks();
}

done_testing;