File: plugin-filetemp.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 (60 lines) | stat: -rw-r--r-- 1,427 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
#!/usr/bin/perl -w

use strict;
use warnings;

use Test2::V0;
use Test2::Plugin::NoWarnings;

BEGIN {
    skip_all("Skip for now < 5.28") unless $^V ge 5.28.0;
}

# Do not load File::Temp to ensure this can be loaded under Test::MockFile
my $has_filetemp_before_load;

BEGIN {
    $has_filetemp_before_load = $INC{'File/Temp.pm'};
}

use Test::MockFile 'strict', plugin => 'FileTemp';

ok !$has_filetemp_before_load, "File::Temp is not loaded before Test::MockFile";
ok $INC{'File/Temp.pm'},       'File::Temp is loaded';

require File::Temp;    # not really needed

{

    my ( $tmp_fh, $tmp ) = File::Temp::tempfile;

    ok lives { open( my $fh, ">", "$tmp" ) }, "we can open a tempfile";

    {
        my $tempdir = File::Temp::tempdir( CLEANUP => 1 );
        ok lives { opendir( my $dh, "$tempdir" ) }, "Can open directory from tempdir";

        ok lives { open( my $fh, ">", "$tempdir/here" ) }, "we can open a tempfile under a tempdir";
    }

    # scalar context

    {
        my $fh = File::Temp::tempfile;
        ok lives { print {$fh} "test" }, "print to a tempfile - scalar context";
    }

}

{
    my $dir = File::Temp->newdir();
    ok opendir( my $dh, "$dir" ),             "opendir - newdir";
    ok open( my $f, '>', "$dir/myfile.txt" ), "open a file created under newdir";
}

{
    my $fh = File::Temp::tempfile();
    is( scalar( ( stat $fh )[3] ), 0, "tempfile in scalar context" );
}

done_testing;