Skip to content

Commit 254d14a

Browse files
committed
Add default config
1 parent a59016b commit 254d14a

File tree

6 files changed

+203
-1
lines changed

6 files changed

+203
-1
lines changed

config/default.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This is the default configuration file.
2+
3+
Sequel:
4+
Enabled: true
5+
DocumentationBaseURL: https://github.com/rubocop/rubocop-sequel/blob/master/README.md
6+
7+
Sequel/ConcurrentIndex:
8+
Description: Encourages the creation of indexes with the `NOT VALID` option to avoid locking tables.
9+
Reference: https://www.rubydoc.info/gems/rubocop-sequel/RuboCop/Cop/Sequel/ConcurrentIndex
10+
Enabled: true
11+
VersionAdded: 0.0.1
12+
VersionChanged: 0.3.3
13+
14+
Sequel/JSONColumn:
15+
Description: >-
16+
Advocates the use of the `jsonb` column type over `json` or `hstore` for performance
17+
benefits and additional functionality.
18+
Reference: https://www.rubydoc.info/gems/rubocop-sequel/RuboCop/Cop/Sequel/JSONColumn
19+
Enabled: true
20+
Safe: false
21+
VersionAdded: 0.0.1
22+
VersionChanged: 0.3.3
23+
24+
Sequel/MigrationName:
25+
Description: >-
26+
Helps to name migration files descriptively in order to avoid the use of default or generic names.
27+
Reference: https://www.rubydoc.info/gems/rubocop-sequel/RuboCop/Cop/Sequel/MigrationName
28+
Enabled: true
29+
VersionAdded: '0.0.1'
30+
VersionChanged: '0.3.3'
31+
32+
Sequel/PartialConstraint:
33+
Description: Advises on the correct use of partial indexes by specifying the `where' argument.
34+
Reference: https://www.rubydoc.info/gems/rubocop-sequel/RuboCop/Cop/Sequel/PartialConstraint
35+
Enabled: true
36+
VersionAdded: '0.3.0'
37+
VersionChanged: '0.3.3'
38+
39+
Sequel/SaveChanges:
40+
Description: Ensures the use of save_changes instead of save to persist changes to models.
41+
Reference: https://www.rubydoc.info/gems/rubocop-sequel/RuboCop/Cop/Sequel/SaveChanges
42+
VersionAdded: '0.0.1'
43+
VersionChanged: '0.3.3'
44+
Enabled: true
45+
SafeAutoCorrect: false
46+

config/obsoletion.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#
2+
# Configuration for obsoletion.
3+
#
4+
# See: https://docs.rubocop.org/rubocop/extensions.html#config-obsoletions
5+
#
6+
extracted:
7+
Sequel/*: ~
8+
9+
removed:
10+
Sequel/ColumnDefault:
11+
reason: >-
12+
ColumnDefault cop since it's mostly outdated advice.
13+
Details [#33](https://github.com/rubocop/rubocop-sequel/issues/32)
14+

lib/rubocop-sequel.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# frozen_string_literal: true
22

33
require 'rubocop'
4-
4+
require 'rubocop/sequel'
55
require 'rubocop/sequel/version'
6+
require 'rubocop/sequel/inject'
7+
8+
RuboCop::Sequel::Inject.defaults!
9+
610
require 'rubocop/cop/sequel/concurrent_index'
711
require 'rubocop/cop/sequel/json_column'
812
require 'rubocop/cop/sequel/migration_name'

lib/rubocop/sequel.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,14 @@
33
module RuboCop
44
# RuboCop Sequel project namespace
55
module Sequel
6+
PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
7+
CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
8+
CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
9+
10+
private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
11+
12+
if ::RuboCop.const_defined?(:ConfigObsoletion)
13+
::RuboCop::ConfigObsoletion.files << PROJECT_ROOT.join('config', 'obsoletion.yml')
14+
end
615
end
716
end

lib/rubocop/sequel/inject.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Sequel
5+
# Because RuboCop doesn't yet support plugins, we have to monkey patch in a
6+
# bit of our configuration.
7+
module Inject
8+
def self.defaults!
9+
path = CONFIG_DEFAULT.to_s
10+
hash = ConfigLoader.send(:load_yaml_configuration, path)
11+
config = Config.new(hash, path).tap(&:make_excludes_absolute)
12+
puts "configuration from #{path}" if ConfigLoader.debug?
13+
config = ConfigLoader.merge_with_default(config, path, unset_nil: false)
14+
ConfigLoader.instance_variable_set(:@default_configuration, config)
15+
end
16+
end
17+
end
18+
end

spec/project_spec.rb

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

Comments
 (0)