From 1b5736f1012f91676b87f00f4189764be591fe3b Mon Sep 17 00:00:00 2001 From: Alex Dowad Date: Wed, 4 Nov 2015 20:00:20 +0200 Subject: [PATCH] Recurse upward looking for .simplecov The SimpleCov documentation states: "To avoid [duplicated configuration], you can place a file called .simplecov in your project root." However, SimpleCov did not actually look in the "project root" for this file, unless specially configured to know where the project root is. Rather, it would simply look in the working directory. Working directory != project root. (Unfortunately.) Therefore, when looking for .simplecov, recurse upwards from the working directory until it is found (or the root directory is reached). This is comparable to the way that git recurses upwards looking for a .git directory, meaning that you can use git anywhere in your project root or below. Since there is no guarantee that you have permissions to read .simplecov, catch exceptions and display a warning message if one occurs. --- lib/simplecov/defaults.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/simplecov/defaults.rb b/lib/simplecov/defaults.rb index 5dcf86b3..09aad1c1 100644 --- a/lib/simplecov/defaults.rb +++ b/lib/simplecov/defaults.rb @@ -1,5 +1,6 @@ # Load default formatter gem require "simplecov-html" +require "pathname" SimpleCov.profiles.define "root_filter" do # Exclude all files outside of simplecov root @@ -97,5 +98,20 @@ end # Autoload config from .simplecov if present -config_path = File.join(SimpleCov.root, ".simplecov") -load config_path if File.exist?(config_path) +# Recurse upwards until we find .simplecov or reach the root directory + +config_path = Pathname.new(SimpleCov.root) +loop do + filename = config_path.join(".simplecov") + if filename.exist? + begin + load filename + rescue LoadError, StandardError + $stderr.puts "Warning: Error occurred while trying to load #{filename}. " \ + "Error message: #{$!.message}" + end + break + end + config_path, = config_path.split + break if config_path.root? +end