Ancient
view release on metacpan or search on metacpan
t/8015-file-map-grep-for.t view on Meta::CPAN
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use File::Temp qw(tempdir);
use File::Spec;
# Test file module functions work correctly in map/grep/for contexts
# This ensures call checkers properly handle $_ usage
use file;
# Create temp directory with test fixtures
my $tmpdir = tempdir(CLEANUP => 1);
# Create test files
my @test_files;
for my $name (qw(file1.txt file2.txt file3.log data.csv readme.md)) {
my $path = File::Spec->catfile($tmpdir, $name);
file::spew($path, "content of $name\n" x (length($name)));
push @test_files, $path;
}
# Create subdirectories
my @test_dirs;
for my $name (qw(subdir1 subdir2 logs)) {
my $path = File::Spec->catdir($tmpdir, $name);
file::mkdir($path);
push @test_dirs, $path;
}
# Create a symlink if possible
my $link_path = File::Spec->catfile($tmpdir, 'link_to_file1');
my $has_symlink = eval { symlink($test_files[0], $link_path) };
# Create files with different permissions
my $readable_only = File::Spec->catfile($tmpdir, 'readable_only.txt');
my $writable_file = File::Spec->catfile($tmpdir, 'writable.txt');
file::spew($readable_only, "read only content\n");
file::spew($writable_file, "writable content\n");
chmod 0444, $readable_only;
chmod 0644, $writable_file;
# Create an executable script
my $exec_script = File::Spec->catfile($tmpdir, 'script.sh');
file::spew($exec_script, "#!/bin/bash\necho hello\n");
chmod 0755, $exec_script;
# ============================================
# Path predicates in grep
# ============================================
subtest 'is_file in grep' => sub {
my @all_paths = (@test_files, @test_dirs);
my @files = grep { file::is_file($_) } @all_paths;
is(scalar(@files), scalar(@test_files), 'is_file found all files');
};
subtest 'is_dir in grep' => sub {
my @all_paths = (@test_files, @test_dirs);
my @dirs = grep { file::is_dir($_) } @all_paths;
is(scalar(@dirs), scalar(@test_dirs), 'is_dir found all directories');
};
subtest 'is_file/is_dir in map' => sub {
my @all_paths = (@test_files[0..1], @test_dirs[0..1]);
my @types = map {
file::is_file($_) ? 'file' :
file::is_dir($_) ? 'dir' : 'unknown'
} @all_paths;
is_deeply(\@types, ['file', 'file', 'dir', 'dir'], 'is_file/is_dir in map');
};
SKIP: {
skip "symlinks not available", 2 unless $has_symlink;
subtest 'is_link in grep' => sub {
my @paths = (@test_files[0..1], $link_path);
my @links = grep { file::is_link($_) } @paths;
is(scalar(@links), 1, 'is_link found the symlink');
is($links[0], $link_path, 'correct symlink found');
};
subtest 'is_link in map' => sub {
my @paths = ($test_files[0], $link_path, $test_dirs[0]);
my @results = map { file::is_link($_) ? 1 : 0 } @paths;
is_deeply(\@results, [0, 1, 0], 'is_link in map');
};
}
# ============================================
# Permission predicates in grep
# ============================================
subtest 'is_readable in grep' => sub {
my @paths = @test_files;
my @readable = grep { file::is_readable($_) } @paths;
is(scalar(@readable), scalar(@test_files), 'all test files are readable');
};
subtest 'is_writable in grep' => sub {
my @paths = ($readable_only, $writable_file, @test_files[0..1]);
my @writable = grep { file::is_writable($_) } @paths;
# readable_only should not be writable (unless root)
ok(scalar(@writable) >= 3, 'is_writable found writable files');
};
t/8015-file-map-grep-for.t view on Meta::CPAN
# ============================================
# exists in various contexts
# ============================================
subtest 'exists in grep' => sub {
my @paths = (@test_files, '/nonexistent/path/file.txt');
my @existing = grep { file::exists($_) } @paths;
is(scalar(@existing), scalar(@test_files), 'exists filtered correctly');
};
subtest 'exists in map' => sub {
my @paths = ($test_files[0], '/nonexistent', $test_dirs[0]);
my @results = map { file::exists($_) ? 1 : 0 } @paths;
is_deeply(\@results, [1, 0, 1], 'exists in map');
};
# ============================================
# mode function
# ============================================
subtest 'mode in map' => sub {
my @paths = ($test_files[0], $exec_script);
my @modes = map { file::mode($_) } @paths;
ok($modes[0] > 0, 'file has mode');
ok($modes[1] > 0, 'script has mode');
# Check exec script has execute bit
ok($modes[1] & 0111, 'script has execute permission');
};
# ============================================
# Sorting by file attributes
# ============================================
subtest 'sort by size' => sub {
my @sorted_by_size =
map { file::basename($_) }
sort { file::size($a) <=> file::size($b) }
@test_files;
is(scalar(@sorted_by_size), 5, 'sorted 5 files by size');
};
subtest 'sort by mtime' => sub {
my @sorted_by_mtime =
map { file::basename($_) }
sort { file::mtime($a) <=> file::mtime($b) }
@test_files;
is(scalar(@sorted_by_mtime), 5, 'sorted 5 files by mtime');
};
subtest 'sort by basename' => sub {
my @sorted =
sort { file::basename($a) cmp file::basename($b) }
@test_files;
my @names = map { file::basename($_) } @sorted;
is($names[0], 'data.csv', 'data.csv first alphabetically');
is($names[-1], 'readme.md', 'readme.md last alphabetically');
};
# Cleanup non-writable file for tempdir cleanup
chmod 0644, $readable_only;
done_testing();
( run in 1.760 second using v1.01-cache-2.11-cpan-5a3173703d6 )