File: with_tempd.t

package info (click to toggle)
libtest-roo-perl 1.004-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 376 kB
  • sloc: perl: 522; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 897 bytes parent folder | download | duplicates (3)
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
use Test::Roo;
use File::pushd qw/tempd/;
use Cwd qw/getcwd/;

has tempdir => (
    is => 'lazy',
    isa => sub { shift->isa('File::pushd') },
    clearer => 1,
);

# tempd changes directory until the object is destroyed
# and the fixture caches the object until cleared
sub _build_tempdir { return tempd() }

# building attribute will change to temp directory
before each_test => sub { shift->tempdir };

# clearing attribute will change to original directory
after each_test => sub { shift->clear_tempdir };

# do stuff in a temp directory
test 'first test' => sub {
    my $self = shift;
    is( $self->tempdir, getcwd(), "cwd is " . $self->tempdir );
    # ... more tests ...
};

# do stuff in a separate, fresh temp directory
test 'second test' => sub {
    my $self = shift;
    is( $self->tempdir, getcwd(), "cwd is " . $self->tempdir );
    # ... more tests ...
};

run_me;
done_testing;