#!/usr/bin/perl -w
{
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]
;
$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
;
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();
return
CHECK_IS_FALSE;
}
);
my
$check
= -e
q[/tmp]
;
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;