restore old find_files interface

- fixes #276
- basic test for find_files
This commit is contained in:
Stephan Renatus 2015-12-04 10:44:52 +01:00
parent e2774dcb66
commit 390e0fcca7
2 changed files with 37 additions and 6 deletions

View file

@ -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

View 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