3

「umask 077」のファイルの内容を確認するために、Ruby で Chef InSpec テストを作成しています。問題は、チェックしているアレイ内のいくつかのファイルが存在しないことです。nil ファイルを除外して再プッシュしようとしていますが、とにかくすべてのファイルをチェックしようとしているようです。何かご意見は?

これが私のコードです:

control 'auth-default-umask' do
  impact 0.5
  title 'Default umask'
  desc 'DISA RHEL6 STIG (V1R2)'

  %w(/etc/profile /etc/bashrc /etc/csh.login /etc/.login).each do |umask_file|
    filecheck = []
    unless umask_file == nil
      filecheck.push(umask_file)
      describe directory(filecheck) do
        its('content') { should match /umask 077/ }
      end
    end
  end
end
4

1 に答える 1

3

ファイル名が nil であるかどうかを確認していますが、決してそうではないため、当然、常に実行されます。ファイルが存在しない場合、ファイルを除外しようとしていますか?

また、おそらくディレクトリのリストではなくディレクトリを説明したかったので、それも変更したことに注意してください。

最終的な結果は次のとおりです。

control 'auth-default-umask' do
  impact 0.5
  title 'Default umask'
  desc 'DISA RHEL6 STIG (V1R2)'

  %w(/etc/profile /etc/bashrc /etc/csh.login /etc/.login).each do |umask_file|
    filecheck = []
    if File.exists?(umask_file)  # check file existence
      filecheck.push(umask_file)
      describe directory(umask_file) do  # describe this directory
        its('content') { should match /umask 077/ }
      end
    end
  end
end

あなたが正しく行ったことは%w()、を使用してファイル名の配列を作成することです。これは、その中の各単語を単純に取り、(入力したパスの) 文字列の配列を作成します。これらだけでは意味がありませんが、 などのクラスで使用してFile、ファイルシステムのコンテキストで意味を持つようにすることができます。

File.exists?(filename)たとえば、ファイルが存在するかどうかを確認します。

ファイルを読み取るには、次を使用できますFile.open

File.open(filename, 'r') do |file|
  until file.eof?
    line = file.gets
    # do something with line
  end
end
于 2016-11-07T17:16:42.717 に答える