Skip to content

Commit

Permalink
Create file-check functionality into utility file
Browse files Browse the repository at this point in the history
There are the similar issues as PR inspec#2302. Almost resources return false
positives when a file does not exist or is not read.

Signed-off-by: ERAMOTO Masaya <[email protected]>
  • Loading branch information
eramoto committed Mar 9, 2018
1 parent 50772a6 commit 1981749
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/utils/file_reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# encoding: utf-8
# author: ERAMOTO Masaya

module FileReader
def read_file_content(path, allow_empty = false)
# these are currently ResourceSkipped to maintain consistency with the resource
# pre-refactor (which used skip_resource). These should likely be changed to
# ResourceFailed during a major version bump.
file = inspec.file(path)
if !file.file?
raise Inspec::Exceptions::ResourceSkipped, "Can't find file: #{path}"
end

raw_content = file.content
if raw_content.nil?
raise Inspec::Exceptions::ResourceSkipped, "Can't read file: #{path}"
end

if !allow_empty && raw_content.empty?
raise Inspec::Exceptions::ResourceSkipped, "File is empty: #{path}"
end

raw_content
end
end

0 comments on commit 1981749

Please sign in to comment.