Skip to content

Commit

Permalink
Fix default exclusion of vendor directories
Browse files Browse the repository at this point in the history
Excluding directories from a .rubocop.yml file works, but before this
fix, the setting of AllCops/Excludes in default.yml didn't have any
effect.
  • Loading branch information
jonas054 committed Mar 23, 2014
1 parent 4803b54 commit 7afa124
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* [#912](https://github.com/bbatsov/rubocop/issues/912): Handle top-level constant resolution in `DeprecatedClassMethods` (e.g. `::File.exists?`). ([@bbatsov][])
* [#914](https://github.com/bbatsov/rubocop/issues/914): Fixed rdoc error during gem installation. ([@bbatsov][])
* The `--only` option now enables the given cop in case it is disabled in configuration. ([@jonas054][])
* Fix path resolution so that the default exclusion of `vendor` directories works. ([@jonas054][])

## 0.19.1 (17/03/2014)

Expand Down
24 changes: 13 additions & 11 deletions lib/rubocop/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,19 @@ def patterns_to_exclude
end

def path_relative_to_config(path)
# Paths specified in .rubocop.yml files are relative to the directory
# where that file is. Paths in other config files are relative to the
# current directory. This is so that paths in config/default.yml, for
# example, are not relative to RuboCop's config directory since that
# wouldn't work.
base_dir = if File.basename(loaded_path) == ConfigLoader::DOTFILE
File.dirname(loaded_path)
else
Dir.pwd
end
relative_path(path, base_dir)
relative_path(path, base_dir_for_path_parameters)
end

# Paths specified in .rubocop.yml files are relative to the directory where
# that file is. Paths in other config files are relative to the current
# directory. This is so that paths in config/default.yml, for example, are
# not relative to RuboCop's config directory since that wouldn't work.
def base_dir_for_path_parameters
if File.basename(loaded_path) == ConfigLoader::DOTFILE
File.dirname(loaded_path)
else
Dir.pwd
end
end
end
end
7 changes: 2 additions & 5 deletions lib/rubocop/config_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ def load_file(path)
hash.delete('inherit_from')
config = Config.new(hash, path)
config.warn_unless_valid
make_excludes_absolute(config)
config
end

def make_excludes_absolute(config)
if config['AllCops'] && config['AllCops']['Excludes']
config['AllCops']['Excludes'].map! do |exclude_elem|
if exclude_elem.is_a?(String) && !exclude_elem.start_with?('/')
File.join(File.dirname(config.loaded_path), exclude_elem)
File.join(config.base_dir_for_path_parameters, exclude_elem)
else
exclude_elem
end
Expand Down Expand Up @@ -92,7 +93,6 @@ def configuration_from_file(config_file)
print 'AllCops/Excludes ' if debug?
add_excludes_from_higher_level(config, load_file(found_files.last))
end
make_excludes_absolute(config)
merge_with_default(config, config_file)
end

Expand Down Expand Up @@ -124,9 +124,6 @@ def merge_with_default(config, config_file)

def resolve_inheritance(path, hash)
base_configs(path, hash['inherit_from']).reverse_each do |base_config|
if File.basename(base_config.loaded_path) == DOTFILE
make_excludes_absolute(base_config)
end
base_config.each do |k, v|
hash[k] = hash.key?(k) ? merge(v, hash[k]) : v if v.is_a?(Hash)
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,17 @@ def interrupt
''].join("\n"))
end

it 'excludes the vendor directory by default' do
create_file('vendor/ex.rb',
['# encoding: utf-8',
'#' * 90])

cli.run(%w(--format simple))
expect($stdout.string)
.to eq(['', '0 files inspected, no offenses detected',
''].join("\n"))
end

# Being immune to bad configuration files in excluded directories has
# become important due to a bug in rubygems
# (https://github.com/rubygems/rubygems/issues/680) that makes
Expand Down

0 comments on commit 7afa124

Please sign in to comment.