lib/minitest-chef-handler.rb in minitest-chef-handler-0.5.2 vs lib/minitest-chef-handler.rb in minitest-chef-handler-0.5.3

- old
+ new

@@ -5,22 +5,26 @@ require 'minitest-chef-handler/runner' require 'minitest-chef-handler/assertions' require 'minitest-chef-handler/infections' +require 'minitest-chef-handler/lookup' + module MiniTest module Chef class Handler < ::Chef::Handler + include Lookup + def initialize(options = {}) @options = options end def report # do not run tests if chef failed return if failed? - require_test_suites + require_test_suites(@options.delete(:path)) runner = Runner.new(run_status) if custom_runner? runner._run(miniunit_options) else @@ -30,61 +34,9 @@ if runner.failures.nonzero? ::Chef::Client.when_run_completes_successfully do |run_status| raise "MiniTest failed with #{runner.failures} failure(s)" end end - end - - private - - # - # Load the test suites. - # If the option "path" is specified we use it to load the tests from it. - # The option can be a string or an array of paths. - # Otherwise we load the tests according to the recipes seen. - # - def require_test_suites - paths = @options.delete(:path) || seen_recipes_paths - Array(paths).each do |path| - Dir.glob(path).each {|test_suite| require test_suite} - end - end - - # - # Collect test paths based in the recipes ran. - # It loads the tests based in the name of the cookbook and the name of the recipe. - # The tests must be under the cookbooks directory. - # - # Examples: - # - # If the seen recipes includes the recipe "foo" we try to load tests from: - # - # cookbooks/foo/tests/default_test.rb - # cookbooks/foo/tests/default/*_test.rb - # - # cookbooks/foo/specs/default_spec.rb - # cookbooks/foo/specs/default/*_spec.rb - # - # If the seen recipes includes the recipe "foo::install" we try to load tests from: - # - # cookbooks/foo/tests/install_test.rb - # cookbooks/foo/tests/install/*_test.rb - # - # cookbooks/foo/specs/install_spec.rb - # cookbooks/foo/specs/install/*_spec.rb - # - def seen_recipes_paths - run_status.node.run_state[:seen_recipes].keys.map do |recipe_name| - cookbook_name, recipe_short_name = ::Chef::Recipe.parse_recipe_name(recipe_name) - base_path = ::Chef::Config[:cookbook_path] - - file_test_pattern = "%s/%s/tests/%s_test.rb" % [base_path, cookbook_name, recipe_short_name] - dir_test_pattern = "%s/%s/tests/%s/*_test.rb" % [base_path, cookbook_name, recipe_short_name] - file_spec_pattern = "%s/%s/specs/%s_spec.rb" % [base_path, cookbook_name, recipe_short_name] - dir_spec_pattern = "%s/%s/specs/%s/*_spec.rb" % [base_path, cookbook_name, recipe_short_name] - - [file_test_pattern, dir_test_pattern, file_spec_pattern, dir_spec_pattern] - end.flatten end def miniunit_options options = [] options << ['-n', @options[:filter]] if @options[:filter]