mirror of
https://github.com/inspec/inspec
synced 2024-11-23 13:13:22 +00:00
restore old find_files interface
- fixes #276 - basic test for find_files
This commit is contained in:
parent
e2774dcb66
commit
390e0fcca7
2 changed files with 37 additions and 6 deletions
|
@ -16,7 +16,12 @@ module FindFiles
|
|||
door: 'D',
|
||||
}
|
||||
|
||||
# ignores errors
|
||||
def find_files(path, opts = {})
|
||||
find_files_or_error(path, opts) || []
|
||||
end
|
||||
|
||||
def find_files_or_error(path, opts = {})
|
||||
depth = opts[:depth]
|
||||
type = TYPES[opts[:type].to_sym]
|
||||
|
||||
|
@ -24,13 +29,16 @@ module FindFiles
|
|||
cmd += " -maxdepth #{depth.to_i}" if depth.to_i > 0
|
||||
cmd += " -type #{type}" unless type.nil?
|
||||
|
||||
result = inspec.run_command(cmd)
|
||||
result = inspec.command(cmd)
|
||||
exit_status = result.exit_status
|
||||
|
||||
return [nil, exit_status] unless exit_status == 0
|
||||
files = result.stdout.split("\n")
|
||||
.map(&:strip)
|
||||
.find_all { |x| !x.empty? }
|
||||
[files, exit_status]
|
||||
unless exit_status == 0
|
||||
warn "find_files(): exit #{exit_status} from `#{find}`"
|
||||
return nil
|
||||
end
|
||||
|
||||
result.stdout.split("\n")
|
||||
.map(&:strip)
|
||||
.find_all { |x| !x.empty? }
|
||||
end
|
||||
end
|
||||
|
|
23
test/unit/utils/find_files_test.rb
Normal file
23
test/unit/utils/find_files_test.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
# encoding: utf-8
|
||||
# author: Stephan Renatus
|
||||
|
||||
require 'helper'
|
||||
|
||||
describe FindFiles do
|
||||
let (:findfiles) do
|
||||
class FindFilesTest
|
||||
include FindFiles
|
||||
def inspec
|
||||
Inspec::Backend.create(backend: :mock)
|
||||
end
|
||||
end
|
||||
FindFilesTest.new
|
||||
end
|
||||
|
||||
describe '#find_files' do
|
||||
it 'returns an array (of findings)' do
|
||||
files = findfiles.find_files('/no/such/mock', type: 'f', depth: 1)
|
||||
files.must_equal([])
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue