From 390e0fcca72858743a7d1a8d0b48a97dc52976fe Mon Sep 17 00:00:00 2001 From: Stephan Renatus Date: Fri, 4 Dec 2015 10:44:52 +0100 Subject: [PATCH] restore old find_files interface - fixes #276 - basic test for find_files --- lib/utils/find_files.rb | 20 ++++++++++++++------ test/unit/utils/find_files_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 test/unit/utils/find_files_test.rb diff --git a/lib/utils/find_files.rb b/lib/utils/find_files.rb index 4bbffe8d2..f1d8f556c 100644 --- a/lib/utils/find_files.rb +++ b/lib/utils/find_files.rb @@ -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 diff --git a/test/unit/utils/find_files_test.rb b/test/unit/utils/find_files_test.rb new file mode 100644 index 000000000..7d6c1dce5 --- /dev/null +++ b/test/unit/utils/find_files_test.rb @@ -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