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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#!/usr/bin/perl -w
use strict;
use warnings;
use Test2::Bundle::Extended;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;
use Test2::Tools::Exception qw< lives dies >;
use Test::MockFile;
sub test_content_with_keywords {
my ( $dirname, $dir_content ) = @_;
my $dh;
my $open;
ok(
lives( sub { $open = opendir $dh, $dirname } ),
"opendir() $dirname successful",
);
$open or return;
my @content;
ok(
lives( sub { @content = readdir($dh) } ),
"readdir() on $dirname successful",
);
is(
\@content,
$dir_content,
'Correct directory content through Perl core keywords',
);
ok(
lives( sub { closedir $dh } ),
"closedir() on $dirname successful",
);
}
my $count = 0;
my $get_dirname = sub {
$count++;
return "/foo$count";
};
subtest(
'->dir() checks' => sub {
like(
dies( sub { Test::MockFile->dir( '/etc', [ 'foo', 'bar' ], { 1 => 2 } ) } ),
qr!^\QYou cannot set stats for nonexistent dir '/etc'\E!xms,
'Cannot do TMF->dir( "/etc", [@content], { 1 => 2 } )',
);
like(
dies( sub { Test::MockFile->dir( '/etc', [ 'foo', 'bar' ] ) } ),
qr!^\QYou cannot set stats for nonexistent dir '/etc'\E!xms,
'Cannot do TMF->dir( "/etc", [@content] )',
);
}
);
subtest(
'Scenario 1: ->dir() does not create dir, keywords do' => sub {
my $dirname = $get_dirname->();
my $dir = Test::MockFile->dir($dirname);
ok( !-d $dirname, "Directory $dirname does not exist yet" );
ok( mkdir($dirname), "Directory $dirname got created" );
ok( -d $dirname, "Directory $dirname now exists" );
is(
$dir->contents(),
[qw< . .. >],
'Correct contents of directory through ->contents()',
);
test_content_with_keywords( $dirname, [qw< . .. >] );
}
);
subtest(
'Scenario 2: ->dir() on an already existing dir fails made with ->dir()' => sub {
my $dirname = $get_dirname->();
my $file = Test::MockFile->file( "$dirname/bar", 'my content' );
my $dir = Test::MockFile->dir($dirname);
ok( -d $dirname, "-d $dirname succeeds, dir exists" );
ok( !mkdir($dirname), "mkdir $dirname fails, dir already exists" );
test_content_with_keywords( $dirname, [qw< . .. bar >] );
}
);
subtest(
'Scneario 3: Undef files with ->file() do not create dirs, adding content changes dir' => sub {
my $dirname = $get_dirname->();
my $dir = Test::MockFile->dir($dirname);
ok( !-d $dirname, "-d $dirname fails, does not exist yet" );
my $file = Test::MockFile->file("$dirname/foo");
ok( !-d $dirname, "-d $dirname still fails after mocking file with no content" );
ok( mkdir($dirname), "mkdir $dirname works" );
ok( -d $dirname, "-d $dirname now succeeds" );
is(
$dir->contents(),
[qw< . .. >],
"Correct contents to $dirname",
);
test_content_with_keywords( $dirname, [qw< . .. >] );
ok( !-e "$dirname/foo", "$dirname/foo does not exist, even if $dirname does" );
$file->contents("hello");
ok( -e "$dirname/foo", "After file->contents(), $dirname/foo exists" );
is(
$dir->contents(),
[qw< . .. foo >],
"Correct updated contents to $dirname",
);
test_content_with_keywords( $dirname, [qw< . .. foo >] );
}
);
subtest(
'Scenario 4: Creating ->file() with content creates dir' => sub {
my $dirname = $get_dirname->();
my $dir = Test::MockFile->dir($dirname);
ok( !-d $dirname, "$dirname does not exist yet" );
my $file = Test::MockFile->file( "$dirname/foo", 'some content' );
ok( -d $dirname, "$dirname now exists, after creating file with content" );
ok( !mkdir($dirname), "mkdir $dirname fails, since dir already exists" );
is(
$dir->contents(),
[qw< . .. foo >],
"Correct contents to $dirname",
);
test_content_with_keywords( $dirname, [qw< . .. foo >] );
}
);
done_testing();
|