File: FakeFS.pm

package info (click to toggle)
libfile-sharedir-projectdistdir-perl 1.000008-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 404 kB
  • ctags: 24
  • sloc: perl: 456; makefile: 2
file content (39 lines) | stat: -rw-r--r-- 660 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
37
38
39
use 5.006;
use strict;
use warnings;

package FakeFS;

# ABSTRACT: Inflate a directory at a given path temporarily

# AUTHORITY

use Class::Tiny qw(root), {
  files => sub { [] }
};
use Path::Tiny qw(path);

sub BUILD {
  my ( $self, $args ) = @_;
  path( $self->root )->mkpath;
}

sub add_file {
  my ( $self, $path, $content ) = @_;
  my $target = path( $self->root )->child($path);
  $target->parent->mkpath;
  $target->spew($content);
  push @{ $self->files }, $target;
}

sub DESTROY {
  my ($self) = @_;
  return if $ENV{NODELETE};
  for my $file ( @{ $self->files } ) {
    $file->remove;
  }
  path( $self->root )->remove_tree( { safe => 0 } );
}

1;