|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +RSpec.describe 'config/default.yml', type: :feature do |
| 6 | + subject(:config) { RuboCop::ConfigLoader.load_file('config/default.yml') } |
| 7 | + |
| 8 | + let(:configuration_keys) { config.tap { |c| c.delete('inherit_mode') }.keys } |
| 9 | + let(:version_regexp) { /\A\d+\.\d+(?:\.\d+)?\z|\A<<next>>\z/ } |
| 10 | + |
| 11 | + shared_examples 'has a nicely formatted description' do |cop_name| |
| 12 | + it 'does not contain new lines' do |
| 13 | + description = config.dig(cop_name, 'Description') |
| 14 | + |
| 15 | + expect(description.include?("\n")).to be(false) |
| 16 | + end |
| 17 | + |
| 18 | + it 'stars from a verb' do # rubocop:disable RSpec/ExampleLength |
| 19 | + description = config.dig(cop_name, 'Description') |
| 20 | + start_with_subject = description.match(/\AThis cop (?<verb>.+?) .*/) |
| 21 | + suggestion = start_with_subject[:verb]&.capitalize if start_with_subject |
| 22 | + suggestion ||= 'a verb' |
| 23 | + |
| 24 | + expect(start_with_subject).to( |
| 25 | + be_nil, "should be started with `#{suggestion}` instead of `This cop ...`." |
| 26 | + ) |
| 27 | + end |
| 28 | + |
| 29 | + it 'has a period at EOL of description' do |
| 30 | + description = config.dig(cop_name, 'Description') |
| 31 | + |
| 32 | + expect(description).to match(/\.\z/) |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + shared_examples 'has metadata' do |cop_name| |
| 37 | + context 'with VersionAdded' do |
| 38 | + it 'required' do |
| 39 | + version = config.dig(cop_name, 'VersionAdded') |
| 40 | + expect(version).not_to be_nil |
| 41 | + end |
| 42 | + |
| 43 | + it 'nicely formatted' do |
| 44 | + version = config.dig(cop_name, 'VersionAdded') |
| 45 | + expect(version).to match(version_regexp), "should be format ('X.Y' or 'X.Y.Z' or '<<next>>')" |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context 'with VersionChanged' do |
| 50 | + it 'nicely formatted' do |
| 51 | + version = config.dig(cop_name, 'VersionChanged') |
| 52 | + next unless version |
| 53 | + |
| 54 | + expect(version).to match(version_regexp), "should be format ('X.Y' or 'X.Y.Z' or '<<next>>')" |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context 'with VersionRemoved' do |
| 59 | + it 'nicely formatted' do |
| 60 | + version = config.dig(cop_name, 'VersionRemoved') |
| 61 | + next unless version |
| 62 | + |
| 63 | + expect(version).to match(version_regexp), "should be format ('X.Y' or 'X.Y.Z' or '<<next>>')" |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + context 'with Safe' do |
| 68 | + it 'does not include `true`' do |
| 69 | + safe = config.dig(cop_name, 'Safe') |
| 70 | + expect(safe).not_to be(true), 'has unnecessary `Safe: true` config.' |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + context 'with SafeAutoCorrect' do |
| 75 | + it 'does not include unnecessary `false`' do |
| 76 | + next unless config.dig(cop_name, 'Safe') == false |
| 77 | + |
| 78 | + safe_autocorrect = config.dig(cop_name, 'SafeAutoCorrect') |
| 79 | + |
| 80 | + expect(safe_autocorrect).not_to be(false), 'has unnecessary `SafeAutoCorrect: false` config.' |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + cop_names = RuboCop::Cop::Registry.global.with_department(:Sequel).cops.map(&:cop_name) |
| 86 | + cop_names.each do |cop_name| |
| 87 | + describe "Cop #{cop_name}" do |
| 88 | + include_examples 'has a nicely formatted description', cop_name |
| 89 | + include_examples 'has metadata', cop_name |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + it 'sorts configuration keys alphabetically' do |
| 94 | + expected = configuration_keys.sort |
| 95 | + configuration_keys.each_with_index do |key, idx| |
| 96 | + expect(key).to eq expected[idx] |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + it 'sorts cop names alphabetically' do # rubocop:disable RSpec/ExampleLength |
| 101 | + previous_key = '' |
| 102 | + config_default = YAML.load_file('config/default.yml') |
| 103 | + |
| 104 | + config_default.each_key do |key| |
| 105 | + next if %w[inherit_mode AllCops].include?(key) |
| 106 | + |
| 107 | + expect(previous_key <= key).to be(true), "Cops should be sorted alphabetically. Please sort #{key}." |
| 108 | + previous_key = key |
| 109 | + end |
| 110 | + end |
| 111 | +end |
0 commit comments