Merge pull request #5519 from inspec/nm/file-resource-bug

Changes returns nil on file non-existence through matcher `more_permissive_than`
This commit is contained in:
Clinton Wolfe 2021-05-18 15:28:32 -04:00 committed by GitHub
commit cab833b88f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 5 deletions

View file

@ -136,10 +136,10 @@ module Inspec::Resources
alias sticky? sticky
def more_permissive_than?(max_mode = nil)
raise Inspec::Exceptions::ResourceFailed, "The file" + file.path + "doesn't seem to exist" unless exist?
raise ArgumentError, "You must proivde a value for the `maximum allowable permission` for the file." if max_mode.nil?
raise ArgumentError, "You must proivde the `maximum permission target` as a `String`, you provided: " + max_mode.class.to_s unless max_mode.is_a?(String)
raise ArgumentError, "The value of the `maximum permission target` should be a valid file mode in 4-ditgit octal format: for example, `0644` or `0777`" unless /(0)?([0-7])([0-7])([0-7])/.match?(max_mode)
return nil unless exist?
raise ArgumentError, "You must provide a value for the `maximum allowable permission` for the file." if max_mode.nil?
raise ArgumentError, "You must provide the `maximum permission target` as a `String`, you provided: " + max_mode.class.to_s unless max_mode.is_a?(String)
raise ArgumentError, "The value of the `maximum permission target` should be a valid file mode in 4-digit octal format: for example, `0644` or `0777`" unless /(0)?([0-7])([0-7])([0-7])/.match?(max_mode)
# Using the files mode and a few bit-wise calculations we can ensure a
# file is no more permisive than desired.
@ -160,7 +160,6 @@ module Inspec::Resources
max_mode = max_mode.to_i(8)
inv_mode = 0777 ^ max_mode
inv_mode & file.mode != 0
end

View file

@ -104,4 +104,9 @@ describe Inspec::Resources::FileResource do
_(proc { resource.send(:more_permissive_than?, "0888") }).must_raise(ArgumentError)
end
it "when file does not exist" do
resource = MockLoader.new(:ubuntu1404).load_resource("file", "file_does_not_exist")
assert_nil(resource.send(:more_permissive_than?, nil))
end
end