Prev / Next

2017-09-18 / Serverspec で 2 つのファイルの修正時刻を比較をする

newalias や postmap の実行漏れで、
/etc/aliases が更新されているのに /etc/aliases.db が更新されていないというようなことを確認するため、
2 つのファイルの修正時刻の比較用として、
File に be_newer_than と be_older_than を追加してみた。


とりあえず、spec/spec_helper.rb に以下を追記。

class Specinfra::Command::Base::File < Specinfra::Command::Base
  class << self
    def check_is_older_than(file, other)
      "[ #{escape(file)} -ot #{escape(other)} ]"
    end

    def check_is_newer_than(file, other)
      "[ #{escape(file)} -nt #{escape(other)} ]"
    end
  end
end

module Serverspec::Type
  class File < Base
    def newer_than?(other)
      @runner.check_file_is_newer_than(@name, other)
    end

    def older_than?(other)
      @runner.check_file_is_older_than(@name, other)
    end
  end
end


これで、以下のように be_older_than と be_newer_than が使える。

  describe file('/etc/aliases') do
    it { should be_file }
    it { should be_older_than '/etc/aliases.db' }
  end
  
  describe file('/etc/aliases.db') do
    it { should be_file }
    it { should be_newer_than '/etc/aliases' }
  end


自分としてはあると便利だと思うんだけど、
こういうことやるなら be_newer_than という名前しかないだろうと
『serverspec be_newer_than』でググっても 1 件もひっかからず、
こういう需要はないんだろうか?と気になっている。

comments powered by Disqus