Skip to content

Commit

Permalink
Merge pull request #230 from koic/add_project_specs_for_config_defaul…
Browse files Browse the repository at this point in the history
…t_yaml

Add project specs for config/default.yml
  • Loading branch information
koic authored Apr 11, 2020
2 parents f30cf60 + f8381bb commit 7ac8eef
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
10 changes: 5 additions & 5 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Rails/AssertNot:
Rails/BelongsTo:
Description: >-
Use `optional: true` instead of `required: false` for
`belongs_to` relations'
`belongs_to` relations.
Enabled: true
VersionAdded: '0.62'

Expand Down Expand Up @@ -184,7 +184,7 @@ Rails/EnumUniqueness:
- app/models/**/*.rb

Rails/EnvironmentComparison:
Description: "Favor `Rails.env.production?` over `Rails.env == 'production'`"
Description: "Favor `Rails.env.production?` over `Rails.env == 'production'`."
Enabled: true
VersionAdded: '0.52'

Expand Down Expand Up @@ -245,7 +245,7 @@ Rails/HasManyOrHasOneDependent:
- app/models/**/*.rb

Rails/HelperInstanceVariable:
Description: 'Do not use instance variables in helpers'
Description: 'Do not use instance variables in helpers.'
Enabled: true
VersionAdded: '2.0'
Include:
Expand Down Expand Up @@ -312,7 +312,7 @@ Rails/LinkToBlank:
VersionAdded: '0.62'

Rails/NotNullColumn:
Description: 'Do not add a NOT NULL column without a default value'
Description: 'Do not add a NOT NULL column without a default value.'
Enabled: true
VersionAdded: '0.43'
Include:
Expand Down Expand Up @@ -434,7 +434,7 @@ Rails/ReversibleMigration:
- db/migrate/*.rb

Rails/SafeNavigation:
Description: "Use Ruby's safe navigation operator (`&.`) instead of `try!`"
Description: "Use Ruby's safe navigation operator (`&.`) instead of `try!`."
Enabled: true
VersionAdded: '0.43'
# This will convert usages of `try` to use safe navigation as well as `try!`.
Expand Down
77 changes: 77 additions & 0 deletions spec/project_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,83 @@
# frozen_string_literal: true

RSpec.describe 'RuboCop Rails Project', type: :feature do
describe 'default configuration file' do
subject(:config) { RuboCop::ConfigLoader.load_file('config/default.yml') }

let(:registry) { RuboCop::Cop::Cop.registry }
let(:cop_names) do
registry.with_department(:Rails).cops.map(&:cop_name)
end

let(:configuration_keys) do
config.tap { |c| c.delete('inherit_mode') }.keys
end

it 'has a nicely formatted description for all cops' do
cop_names.each do |name|
description = config[name]['Description']
expect(description.nil?).to be(false)
expect(description).not_to include("\n")
end
end

it 'requires a nicely formatted `VersionAdded` metadata for all cops' do
cop_names.each do |name|
version = config[name]['VersionAdded']
expect(version.nil?).to(be(false),
"VersionAdded is required for #{name}.")
expect(version).to(match(/\A\d+\.\d+\z/),
"#{version} should be format ('X.Y') for #{name}.")
end
end

it 'have a period at EOL of description' do
cop_names.each do |name|
description = config[name]['Description']

expect(description).to match(/\.\z/)
end
end

it 'sorts configuration keys alphabetically' do
expected = configuration_keys.sort
configuration_keys.each_with_index do |key, idx|
expect(key).to eq expected[idx]
end
end

it 'has a SupportedStyles for all EnforcedStyle ' \
'and EnforcedStyle is valid' do
errors = []
cop_names.each do |name|
enforced_styles = config[name]
.select { |key, _| key.start_with?('Enforced') }
enforced_styles.each do |style_name, style|
supported_key = RuboCop::Cop::Util.to_supported_styles(style_name)
valid = config[name][supported_key]
unless valid
errors.push("#{supported_key} is missing for #{name}")
next
end
next if valid.include?(style)

errors.push("invalid #{style_name} '#{style}' for #{name} found")
end
end

raise errors.join("\n") unless errors.empty?
end

it 'does not have nay duplication' do
fname = File.expand_path('../config/default.yml', __dir__)
content = File.read(fname)
RuboCop::YAMLDuplicationChecker.check(content, fname) do |key1, key2|
raise "#{fname} has duplication of #{key1.value} " \
"on line #{key1.start_line} and line #{key2.start_line}"
end
end
end

describe 'changelog' do
subject(:changelog) do
path = File.join(File.dirname(__FILE__), '..', 'CHANGELOG.md')
Expand Down

0 comments on commit 7ac8eef

Please sign in to comment.