【问题标题】:Serverspec: Check if directory is emptyServerspec:检查目录是否为空
【发布时间】:2015-09-14 16:27:44
【问题描述】:

我想检查现有目录是否为空(不包含任何文件或子目录)。

我尝试了以下方法:

describe file('/path/to/file') do
    it { should be_empty }
end

但这不起作用。 (当然,因为文档中没有提到。)

重要提示:我不想测试,如果目录存在 - 它确实存在并且我无法更改。

我的解决方案现在如下所示:

describe command('ls /data/nginx/cache/services | grep -q .') do
    its(:exit_status) { should eq 1 }
end

但这不是使用 Serverspec 资源类型“文件”。有没有更好的方法来测试文件夹是否为空?

【问题讨论】:

  • 你不能做类似 Dir["/path/to/file/*"].empty 的事情吗?
  • 只是猜测,但可能是这样的:decribe file('/path/to/dir/*') do it{ should_not exist } end

标签: ruby chef-infra test-kitchen serverspec


【解决方案1】:

在文件资源中测试 Glob

执行此操作的一种方法是在遍历不应包含文件的目录时测试一个空数组。例如:

describe file('/path/to/dir') do
  it { should be_a_directory }
  it 'should be an empty directory' do
    Dir.glob('/path/to/dir/*').should eq []
  end
end

【讨论】:

  • 您好 Todd,我目前在检查空目录、使用 Serverspec 和 Docker 后端进行测试时遇到了类似的问题。见stackoverflow.com/questions/48644823/…。也许你可以帮忙?谢谢。
猜你喜欢
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 2011-11-21
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-04
相关资源
最近更新 更多