Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit 23933f3

Browse files
author
Alex Evanczuk
authored
Fix bug where package protections doesn't look at packs/*/.rubocop_todo.yml (#27)
* write failing test * Fix spec * move things to private * Bump version * start with set * rubocop
1 parent a262692 commit 23933f3

File tree

5 files changed

+55
-25
lines changed

5 files changed

+55
-25
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
package_protections (2.1.0)
4+
package_protections (2.1.1)
55
activesupport
66
parse_packwerk
77
rubocop

lib/package_protections/private.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,39 @@ def self.private_cop_config(identifier)
128128
protected_packages.map { |p| [p.name, protection.custom_cop_config(p)] }.to_h
129129
end
130130
end
131+
132+
sig { returns(T::Array[T::Hash[T.untyped, T.untyped]]) }
133+
def self.rubocop_todo_ymls
134+
@rubocop_todo_ymls = T.let(@rubocop_todo_ymls, T.nilable(T::Array[T::Hash[T.untyped, T.untyped]]))
135+
@rubocop_todo_ymls ||= begin
136+
todo_files = Pathname.glob('**/.rubocop_todo.yml')
137+
todo_files.map do |todo_file|
138+
YAML.load_file(todo_file)
139+
end
140+
end
141+
end
142+
143+
sig { void }
144+
def self.bust_rubocop_todo_yml_cache
145+
@rubocop_todo_ymls = nil
146+
end
147+
148+
sig { params(rule: String).returns(T::Set[String]) }
149+
def self.exclude_for_rule(rule)
150+
excludes = T.let(Set.new, T::Set[String])
151+
152+
Private.rubocop_todo_ymls.each do |todo_yml|
153+
config = todo_yml[rule]
154+
next if config.nil?
155+
156+
exclude_list = config['Exclude']
157+
next if exclude_list.nil?
158+
159+
excludes += exclude_list
160+
end
161+
162+
excludes
163+
end
131164
end
132165

133166
private_constant :Private

lib/package_protections/rubocop_protection_interface.rb

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,7 @@ def get_offenses_for_new_violations(new_violations)
7171

7272
sig { void }
7373
def self.bust_rubocop_todo_yml_cache
74-
@rubocop_todo_yml = nil
75-
end
76-
77-
sig { returns(T.untyped) }
78-
def self.rubocop_todo_yml
79-
@rubocop_todo_yml = T.let(@rubocop_todo_yml, T.untyped)
80-
@rubocop_todo_yml ||= begin
81-
todo_file = Pathname.new('.rubocop_todo.yml')
82-
if todo_file.exist?
83-
YAML.load_file(todo_file)
84-
else
85-
{}
86-
end
87-
end
74+
Private.bust_rubocop_todo_yml_cache
8875
end
8976

9077
sig do
@@ -93,7 +80,7 @@ def self.rubocop_todo_yml
9380
).returns(T::Array[Offense])
9481
end
9582
def get_offenses_for_existing_violations(protected_packages)
96-
exclude_list = exclude_for_rule(cop_name)
83+
exclude_list = Private.exclude_for_rule(cop_name)
9784
offenses = []
9885

9986
protected_packages.each do |package|
@@ -146,13 +133,5 @@ def cop_configs(packages)
146133
)
147134
]
148135
end
149-
150-
private
151-
152-
sig { params(rule: String).returns(T::Set[String]) }
153-
def exclude_for_rule(rule)
154-
rule_config = RubocopProtectionInterface.rubocop_todo_yml[rule] || {}
155-
Set.new(rule_config['Exclude'] || [])
156-
end
157136
end
158137
end

package_protections.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |spec|
22
spec.name = 'package_protections'
3-
spec.version = '2.1.0'
3+
spec.version = '2.1.1'
44
spec.authors = ['Gusto Engineers']
55
spec.email = ['[email protected]']
66
spec.summary = 'Package protections for Rails apps'

spec/package_protections_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,24 @@ def get_new_violations
790790
)
791791
end
792792

793+
it 'fails if any of this pack\'s files are in a pack-level rubocop TODO' do
794+
apples_package_yml_with_namespace_protection_set_to_fail_on_any
795+
796+
write_file('packs/apples/app/public/tool.rb', '')
797+
write_file('packs/apples/.rubocop_todo.yml', <<~YML.strip)
798+
PackageProtections/NamespacedUnderPackageName:
799+
Exclude:
800+
- packs/apples/app/public/tool.rb
801+
YML
802+
803+
offenses = PackageProtections.get_offenses(packages: get_packages, new_violations: [])
804+
expect(offenses).to contain_exactly(1).offense
805+
expect(offenses).to include_offense offense(
806+
'packs/apples',
807+
'`packs/apples/app/public/tool.rb` should be namespaced under the package namespace', 'packs/apples/app/public/tool.rb', 'prevent_this_package_from_creating_other_namespaces'
808+
)
809+
end
810+
793811
it 'succeeds if another pack\'s file is in the rubocop TODO' do
794812
apples_package_yml_with_namespace_protection_set_to_fail_on_any
795813

0 commit comments

Comments
 (0)