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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
#============================================================= -*-perl-*-
#
# t/filesystem/file.t
#
# Test the Badger::Filesystem::File module.
#
# Written by Andy Wardley <abw@wardley.org>
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
#========================================================================
use lib qw( ./lib ../lib ../../lib );
use strict;
use warnings;
use Badger::Timestamp 'TIMESTAMP';
use Badger::Filesystem 'FS File';
use Badger::Filesystem::File '@STAT_FIELDS';
use Badger::Filesystem::Directory;
use Badger::Test
tests => 60,
debug => 'Badger::Filesystem::File Badger::Filesystem::Path Badger::Filesystem',
args => \@ARGV;
our $FILE = 'Badger::Filesystem::File';
our $FS = 'Badger::Filesystem';
our $cwd = $FS->cwd;
our @tdir = -d 't' ? qw( t filesystem ) : ($FS->curdir);
our $TDIR = -d 't' ? FS->dir(qw(t filesystem)) : FS->directory;
#-----------------------------------------------------------------------
# basic file inspection
#-----------------------------------------------------------------------
my $file = $FILE->new('file.t');
ok( $file, 'created a new file' );
is( $file->name, 'file.t', 'got file name' );
ok( ! $file->volume, 'got (no) file volume' );
ok( ! $file->dir, 'got (no) file directory' );
ok( ! $file->is_absolute, 'file is not absolute' );
$file = $FILE->new([@tdir, 'file.t']);
ok( $file->exists, 'file exists' );
my @stat = (stat($file->path), -r _, -w _, -x _, -o _);
foreach my $m (@STAT_FIELDS) {
is( $file->$m, $stat[0], "file $m is " . shift(@stat) );
}
my $expect = $FS->dir($cwd, @tdir, 'file.t');
is( $file->absolute, $expect, "absolute path is $expect" );
ok( $FILE->new(path => '/example/file.foo')->is_absolute, 'file is absolute' );
ok( $FILE->new(name => 'file.foo', directory => '/example')->is_absolute, 'directory file is absolute' );
ok( $FILE->new(name => 'file.foo', dir => '/example')->is_absolute, 'dir file is absolute' );
is( $FILE->new(name => 'file.t')->name, 'file.t', 'got file using name param' );
is( $FILE->new(path => 'file.t')->name, 'file.t', 'got file using path param' );
is( $FILE->new({ name => 'file.t' })->name, 'file.t', 'got file using name param hash' );
is( $FILE->new({ path => 'file.t' })->name, 'file.t', 'got file using path param hash' );
#-----------------------------------------------------------------------
# basename() - most of this is covered in t/filesystem/path.t
# The only additional thing we need to check is that we get only the
# name of the file, not the whole path
#-----------------------------------------------------------------------
is( File('foo/bar.baz.html')->basename, 'bar.baz', 'multi-dotted basename' );
#-----------------------------------------------------------------------
# timestamps
#-----------------------------------------------------------------------
my $ts = $file->created;
is( ref($ts), TIMESTAMP, 'created_on() returned a Badger::Timestamp' );
$ts = $file->accessed;
is( ref($ts), TIMESTAMP, 'accessed_on() returned a Badger::Timestamp' );
$ts = $file->modified;
is( ref($ts), TIMESTAMP, 'modified_on() returned a Badger::Timestamp' );
#-----------------------------------------------------------------------
# create a file, delete it, touch it
#-----------------------------------------------------------------------
my $file3 = $TDIR->file('testfiles', 'newfile');
ok( $file3, 'got newfile' );
if ($file3->exists) {
ok( $file3->delete, 'deleted file' );
}
else {
pass('no existing file');
}
ok( ! $file3->exists, 'newfile does not exist' );
ok( $file3->create, 'created file' );
ok( $file3->exists, 'newfile now exists' );
ok( $file3->print("Hello World!\n"), 'printed to newfile' );
is( $file3->text, "Hello World!\n", 'read text from newfile' );
ok( $file3->touch, 'touched newfile' );
#-----------------------------------------------------------------------
# copy and move files
#-----------------------------------------------------------------------
my $file4 = $TDIR->file('testfiles', 'copyfile');
ok( $file4, 'got copyfile' );
if ($file4->exists) {
ok( $file4->delete, 'deleted copy file' );
}
else {
pass('no existing copy file');
}
ok( ! $file4->exists, 'copyfile does not exist' );
ok( $file3->copy($file4), 'copied file' );
ok( $file4->exists, 'copyfile now exists' );
my $file5 = $TDIR->file('testfiles', 'movefile');
ok( $file5, 'got movefile' );
if ($file5->exists) {
ok( $file5->delete, 'deleted move file' );
}
else {
pass('no existing move file');
}
ok( ! $file5->exists, 'movefile does not exist' );
ok( $file4->move($file5), 'moved file' );
ok( $file5->exists, 'moved now exists' );
ok( ! $file4->exists, 'copyfile no longer exists' );
#-----------------------------------------------------------------------
# copy with mkdir and mode parameters
#-----------------------------------------------------------------------
my $file6 = $TDIR->file('testfiles', 'forest', 'badger');
ok(
$file5->copy($file6, mkdir => 1, dir_mode => 0770, file_mode => 0660),
'copied file with mkdir'
);
ok( $file6->exists, 'file6 exists' );
#-----------------------------------------------------------------------
# copy from a filehandle
#-----------------------------------------------------------------------
my $file7 = $TDIR->file('testfiles', 'forest', 'ferret');
ok(
$file7->copy_from($file5->open),
'copied file from filehandle'
);
ok( $file7->exists, 'copied file created' );
my $dir = $file6->parent;
$file5->delete;
$file6->delete;
$file7->delete;
$dir->delete;
#-----------------------------------------------------------------------
# create a file with specific permissions
#-----------------------------------------------------------------------
umask(0002);
my $file8 = $TDIR->file('testfiles', 'group_write');
my $fh8 = $file8->open("w", 0664);
$fh8->print("this is a test file");
$fh8->close;
ok( $file8->writeable, 'file is writable' );
ok( $file8->permissions & 0020, 'file is group writable' );
$file8->delete;
__END__
test_file('file.t');
test_file('example/file.t');
test_file('../x/y/../z/../../floop/./././../file.t');
test_file('/tmp/foo/file.t');
test_dir('example');
test_dir('example/foo');
test_dir('../example/foo');
test_dir('/tmp/example/foo');
test_dir('../../../tmp/../example/x/../y/../foo');
sub test_file {
test_path($FILE->new(@_));
}
sub test_dir {
test_path($DIR->new(@_));
}
sub test_path {
my $path = shift;
my $cwd = $DIR->cwd;
print "\n";
print "path: $path\n";
print "is absolute? ", $path->is_absolute ? 'yes' : 'no', "\n";
print "is file? ", $path->is_file ? 'yes' : 'no', "\n";
print "is dir? ", $path->is_dir ? 'yes' : 'no', "\n";
print "is directory? ", $path->is_directory ? 'yes' : 'no', "\n";
print "directory: ", $path->directory, "\n";
print "absolute: ", $path->absolute, "\n";
print "parent: ", $path->parent, "\n";
print "collapse: ", $path->collapse, "\n";
print "from home: ", $path->relative('/Users/abw'), "\n";
print "exists: ", $path->exists ? 'yes' : 'no', "\n";
if ($path->exists) {
print "owner: ", $path->owner ? 'yes' : 'no', "\n";
print "readable: ", $path->readable ? 'yes' : 'no', "\n";
print "writeable: ", $path->writeable ? 'yes' : 'no', "\n";
print "executable: ", $path->executable ? 'yes' : 'no', "\n";
}
print "above here: ", $path->above($cwd) ? 'yes' : 'no', "\n";
print "below here: ", $path->below($cwd) ? 'yes' : 'no', "\n";
print "below /Users/abw: ", $path->below('/Users/abw') ? 'yes' : 'no', "\n";
print "below /tmp: ", $path->below('/tmp') ? 'yes' : 'no', "\n";
print "path: $path\n";
}
|