我编写了一个模块来检查目录是否可写或可读。 但是,我该如何测试呢?
通常,在编写模块时,您可以编写一些测试来测试模块的特性。我如何创建一个目录,该目录对于实际创建模块的测试是不可写的?
我想象的是这样的:
use strict;
use warnings;
use File::Temp qw/ tempdir /;
use Test::More tests => 5;
BEGIN { use_ok('My::DirCheck') };
my $readable_dir = tempdir();
my $check = My::DirCheck->new(directory => $readable_dir);
is($check->is_readable(), 1, 'temp dir is readable');
# FIXME: how to make it not readale?
isnt($check->is_readable(), 1, 'temp dir is not readable');
my $writable_dir = tempdir();
is($check->is_writeable(), 1, 'temp dir is writeable');
# FIXME: how to make it not writable?
isnt($check->is_writeable(), 1, 'temp dir is not writeable');
但是我该如何创建一个目录,例如将其设置为只读,然后再将其删除?
最佳答案
尝试使用 File::Path
而不是 File::Temp
, 它允许您选择能够设置您的 directories's permissions 的模式参数.将没有读取、写入或执行权限的目录的模式设置为 0000
。
假设它来自 Perl 核心,它应该可以在大多数操作系统上移植。
关于perl - 如何编写测试文件夹是否可读/可写的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18407726/