Skip to content

Commit

Permalink
[Fix #7593] Do not Kernel#abort on validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
buehmann authored and bbatsov committed Dec 25, 2019
1 parent b0c6efd commit ed083fb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
20 changes: 0 additions & 20 deletions lib/rubocop/config_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ def load_yaml_configuration(absolute_path)
raise(TypeError, "Malformed configuration in #{absolute_path}")
end

check_cop_config_value(hash)

hash
end

Expand All @@ -240,24 +238,6 @@ def check_duplication(yaml_code, absolute_path)
end
end

def check_cop_config_value(hash, parent = nil)
hash.each do |key, value|
check_cop_config_value(value, key) if value.is_a?(Hash)

next unless %w[Enabled
Safe
SafeAutoCorrect
AutoCorrect].include?(key) && value.is_a?(String)

next if key == 'Enabled' && value == 'pending'

abort(
"Property #{Rainbow(key).yellow} of cop #{Rainbow(parent).yellow}" \
" is supposed to be a boolean and #{Rainbow(value).yellow} is not."
)
end
end

# Read the specified file, or exit with a friendly, concise message on
# stderr. Care is taken to use the standard OS exit code for a "file not
# found" error.
Expand Down
26 changes: 25 additions & 1 deletion lib/rubocop/config_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def initialize(config)
end

def validate
# Don't validate RuboCop's own files. Avoids infinite recursion.
check_cop_config_value(@config)

# Don't validate RuboCop's own files further. Avoids infinite recursion.
return if @config.internal?

valid_cop_names, invalid_cop_names = @config.keys.partition do |key|
Expand Down Expand Up @@ -244,5 +246,27 @@ def reject_mutually_exclusive_defaults
msg = 'Cops cannot be both enabled by default and disabled by default'
raise ValidationError, msg
end

def check_cop_config_value(hash, parent = nil)
hash.each do |key, value|
check_cop_config_value(value, key) if value.is_a?(Hash)

next unless %w[Enabled
Safe
SafeAutoCorrect
AutoCorrect].include?(key) && value.is_a?(String)

next if key == 'Enabled' && value == 'pending'

raise ValidationError, msg_not_boolean(parent, key, value)
end
end

# FIXME: Handling colors in exception messages like this is ugly.
def msg_not_boolean(parent, key, value)
"#{Rainbow('').reset}" \
"Property #{Rainbow(key).yellow} of cop #{Rainbow(parent).yellow}" \
" is supposed to be a boolean and #{Rainbow(value).yellow} is not."
end
end
end
2 changes: 1 addition & 1 deletion spec/rubocop/config_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ def cop_enabled?(cop_class)
expect do
load_file
end.to raise_error(
SystemExit,
RuboCop::ValidationError,
/supposed to be a boolean and disable is not/
)
end
Expand Down

0 comments on commit ed083fb

Please sign in to comment.