File: factory.t

package info (click to toggle)
libtest-tempdir-perl 0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 324 kB
  • sloc: perl: 422; makefile: 2
file content (80 lines) | stat: -rw-r--r-- 1,994 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
use strict;
use warnings;

use Test::More 0.88;

use Path::Class;
use File::Temp qw(tempdir);

my $tmp;

BEGIN {
    use File::Spec;

    plan skip_all => "No writable temp dir" unless grep { -d && -w } File::Spec->tmpdir;
    $tmp = dir( tempdir( CLEANUP => 1 ) );
    plan skip_all => "couldn't create temp dir" unless -d $tmp && -w $tmp;
}

use ok 'Test::TempDir::Factory';

delete @ENV{qw(TEST_TEMPDIR TEST_TMPDIR TEST_TEMPDIR_CLEANUP TEST_TEMPDIR_USE_SUBDIR)};

foreach my $use_subdir ( 1, 0 ) {
    my $f = Test::TempDir::Factory->new( use_subdir => $use_subdir );

    isa_ok( $f, "Test::TempDir::Factory" );

    is( $f->dir_name, dir("tmp"), "default dir_name" );
    is( $f->t_dir, dir("t"), "default t_dir" );

    $f->t_dir($tmp);

    my $subdir = $tmp->subdir($f->dir_name);

    is( $f->base_path, $subdir, "base path" );

    ok( not(-d $f->base_path), "base path doesn't exist yet" );

    my ( $path, $lock ) = $f->create_and_lock($f->base_path);

    isa_ok( $path, "Path::Class::Dir" );

    ok( $subdir->contains($path), "preferred path used" )
        or diag "got subdir $subdir, selected path $path";

    ok( -d $path, "created" );

    my ( $fallback_path, $fallback_lock ) = $f->create_and_lock_fallback($f->base_path);

    isa_ok( $fallback_path, "Path::Class::Dir" );

    isnt( $fallback_path, $path, "fallback path is different" );

    {
        $f->lock(0);

        my ( $new_fb ) = $f->create_and_lock_fallback($f->base_path);

        isnt( $new_fb, $path, "second fallback is different from base path" );
        isnt( $new_fb, $fallback_path, "and from first fallback path" );

        rmdir $new_fb;
    }


    $f->lock(1);

    isa_ok( my $dir = $f->create, "Test::TempDir::Handle" );

    isa_ok( $dir->dir, "Path::Class::Dir" );

    ok( $subdir->contains( $dir->dir ), "created in the right place" )
        or diag "got subdir $subdir, selected dir $dir";

    rmdir $fallback_path;

    $f->base_path->rmtree({ keep_root => 0 });
}

done_testing;