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

Commit

Permalink
Fix bug where package protections doesn't look at packs/*/.rubocop_to…
Browse files Browse the repository at this point in the history
…do.yml (#27)

* write failing test

* Fix spec

* move things to private

* Bump version

* start with set

* rubocop
  • Loading branch information
Alex Evanczuk authored Sep 22, 2022
1 parent a262692 commit 23933f3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
package_protections (2.1.0)
package_protections (2.1.1)
activesupport
parse_packwerk
rubocop
Expand Down
33 changes: 33 additions & 0 deletions lib/package_protections/private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,39 @@ def self.private_cop_config(identifier)
protected_packages.map { |p| [p.name, protection.custom_cop_config(p)] }.to_h
end
end

sig { returns(T::Array[T::Hash[T.untyped, T.untyped]]) }
def self.rubocop_todo_ymls
@rubocop_todo_ymls = T.let(@rubocop_todo_ymls, T.nilable(T::Array[T::Hash[T.untyped, T.untyped]]))
@rubocop_todo_ymls ||= begin
todo_files = Pathname.glob('**/.rubocop_todo.yml')
todo_files.map do |todo_file|
YAML.load_file(todo_file)
end
end
end

sig { void }
def self.bust_rubocop_todo_yml_cache
@rubocop_todo_ymls = nil
end

sig { params(rule: String).returns(T::Set[String]) }
def self.exclude_for_rule(rule)
excludes = T.let(Set.new, T::Set[String])

Private.rubocop_todo_ymls.each do |todo_yml|
config = todo_yml[rule]
next if config.nil?

exclude_list = config['Exclude']
next if exclude_list.nil?

excludes += exclude_list
end

excludes
end
end

private_constant :Private
Expand Down
25 changes: 2 additions & 23 deletions lib/package_protections/rubocop_protection_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,7 @@ def get_offenses_for_new_violations(new_violations)

sig { void }
def self.bust_rubocop_todo_yml_cache
@rubocop_todo_yml = nil
end

sig { returns(T.untyped) }
def self.rubocop_todo_yml
@rubocop_todo_yml = T.let(@rubocop_todo_yml, T.untyped)
@rubocop_todo_yml ||= begin
todo_file = Pathname.new('.rubocop_todo.yml')
if todo_file.exist?
YAML.load_file(todo_file)
else
{}
end
end
Private.bust_rubocop_todo_yml_cache
end

sig do
Expand All @@ -93,7 +80,7 @@ def self.rubocop_todo_yml
).returns(T::Array[Offense])
end
def get_offenses_for_existing_violations(protected_packages)
exclude_list = exclude_for_rule(cop_name)
exclude_list = Private.exclude_for_rule(cop_name)
offenses = []

protected_packages.each do |package|
Expand Down Expand Up @@ -146,13 +133,5 @@ def cop_configs(packages)
)
]
end

private

sig { params(rule: String).returns(T::Set[String]) }
def exclude_for_rule(rule)
rule_config = RubocopProtectionInterface.rubocop_todo_yml[rule] || {}
Set.new(rule_config['Exclude'] || [])
end
end
end
2 changes: 1 addition & 1 deletion package_protections.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = 'package_protections'
spec.version = '2.1.0'
spec.version = '2.1.1'
spec.authors = ['Gusto Engineers']
spec.email = ['[email protected]']
spec.summary = 'Package protections for Rails apps'
Expand Down
18 changes: 18 additions & 0 deletions spec/package_protections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,24 @@ def get_new_violations
)
end

it 'fails if any of this pack\'s files are in a pack-level rubocop TODO' do
apples_package_yml_with_namespace_protection_set_to_fail_on_any

write_file('packs/apples/app/public/tool.rb', '')
write_file('packs/apples/.rubocop_todo.yml', <<~YML.strip)
PackageProtections/NamespacedUnderPackageName:
Exclude:
- packs/apples/app/public/tool.rb
YML

offenses = PackageProtections.get_offenses(packages: get_packages, new_violations: [])
expect(offenses).to contain_exactly(1).offense
expect(offenses).to include_offense offense(
'packs/apples',
'`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'
)
end

it 'succeeds if another pack\'s file is in the rubocop TODO' do
apples_package_yml_with_namespace_protection_set_to_fail_on_any

Expand Down

0 comments on commit 23933f3

Please sign in to comment.