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

Commit

Permalink
Allow package protections to self configure (#28)
Browse files Browse the repository at this point in the history
* Revert "Use autoload when possible to lazily require internal dependencies (#22)"

This reverts commit 0f56f9c.

* Load client configuration when initializing config

* bump version

* use .rb suffix

* use guard clause
  • Loading branch information
Alex Evanczuk authored Sep 23, 2022
1 parent 23933f3 commit e8aec81
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 35 deletions.
4 changes: 1 addition & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
PATH
remote: .
specs:
package_protections (2.1.1)
package_protections (2.2.0)
activesupport
parse_packwerk
rubocop
rubocop-sorbet
sorbet-runtime
zeitwerk

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -109,7 +108,6 @@ GEM
yard-sorbet (0.6.1)
sorbet-runtime (>= 0.5)
yard (>= 0.9)
zeitwerk (2.6.0)

PLATFORMS
ruby
Expand Down
28 changes: 17 additions & 11 deletions lib/package_protections.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
# frozen_string_literal: true

# typed: strict

require 'sorbet-runtime'

require 'open3'
require 'set'
require 'parse_packwerk'
require 'rubocop'
require 'rubocop-sorbet'

require 'zeitwerk'
loader = T.unsafe(Zeitwerk::Loader).for_gem
loader.ignore("#{__dir__}/rubocop")
loader.setup

#
# Welcome to PackageProtections!
# See https://github.com/rubyatscale/package_protections#readme for more info
#
# This file is a reference for the available API to `package_protections`, but all implementation details are private
# (which is why we delegate to `Private` for the actual implementation).
#
module PackageProtections
extend T::Sig

Expand All @@ -30,6 +27,18 @@ module PackageProtections
# This is currently the only handled exception that `PackageProtections` will throw.
class IncorrectPublicApiUsageError < StandardError; end

require 'package_protections/offense'
require 'package_protections/violation_behavior'
require 'package_protections/protected_package'
require 'package_protections/per_file_violation'
require 'package_protections/protection_interface'
require 'package_protections/rubocop_protection_interface'
require 'package_protections/private'

# Implementation of rubocop-based protections
require 'rubocop/cop/package_protections/namespaced_under_package_name'
require 'rubocop/cop/package_protections/typed_public_api'

class << self
extend T::Sig

Expand All @@ -46,6 +55,7 @@ def self.all

sig { returns(Private::Configuration) }
def self.config
Private.load_client_configuration
@config = T.let(@config, T.nilable(Private::Configuration))
@config ||= Private::Configuration.new
end
Expand Down Expand Up @@ -119,7 +129,3 @@ def self.bust_cache!
RubocopProtectionInterface.bust_rubocop_todo_yml_cache
end
end

if defined?(Rubocop)
require 'rubocop/cop/package_protections'
end
13 changes: 13 additions & 0 deletions lib/package_protections/private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ def self.bust_cache!
@protected_packages_indexed_by_name = nil
@private_cop_config = nil
PackageProtections.config.bust_cache!
# This comes explicitly after `PackageProtections.config.bust_cache!` because
# otherwise `PackageProtections.config` will attempt to reload the client configuratoin.
@loaded_client_configuration = false
end

sig { params(identifier: Identifier).returns(T::Hash[T.untyped, T.untyped]) }
Expand Down Expand Up @@ -161,6 +164,16 @@ def self.exclude_for_rule(rule)

excludes
end

sig { void }
def self.load_client_configuration
@loaded_client_configuration ||= T.let(false, T.nilable(T::Boolean))
return if @loaded_client_configuration

@loaded_client_configuration = true
client_configuration = Pathname.pwd.join('config/package_protections.rb')
require client_configuration.to_s if client_configuration.exist?
end
end

private_constant :Private
Expand Down
2 changes: 0 additions & 2 deletions lib/package_protections/private/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ def bust_cache!

sig { returns(T::Array[ProtectionInterface]) }
def default_protections
require 'rubocop/cop/package_protections'

[
Private::OutgoingDependencyProtection.new,
Private::IncomingPrivacyProtection.new,
Expand Down
13 changes: 0 additions & 13 deletions lib/rubocop/cop/package_protections.rb

This file was deleted.

3 changes: 1 addition & 2 deletions 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.1'
spec.version = '2.2.0'
spec.authors = ['Gusto Engineers']
spec.email = ['[email protected]']
spec.summary = 'Package protections for Rails apps'
Expand All @@ -26,7 +26,6 @@ Gem::Specification.new do |spec|
spec.add_dependency 'rubocop'
spec.add_dependency 'rubocop-sorbet'
spec.add_dependency 'sorbet-runtime'
spec.add_dependency 'zeitwerk'

spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
Expand Down
4 changes: 2 additions & 2 deletions sorbet/rbi/todo.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# srb rbi todo

# typed: strong
module ::Zeitwerk::Loader; end
module ::RuboCop::RSpec::ExpectOffense; end
module RSpec::Matchers; end
module RuboCop::RSpec::ExpectOffense; end

class Hash
def to_yaml; end
end
16 changes: 16 additions & 0 deletions spec/package_protections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1330,4 +1330,20 @@ def get_new_violations
})
end
end

describe 'configuration' do
context 'app has a user defined configuration' do
before do
write_file('config/package_protections.rb', <<~CONFIGURATION)
PackageProtections.configure do |config|
config.globally_permitted_namespaces = ['MyNamespace']
end
CONFIGURATION
end

it 'properly configures package protections' do
expect(PackageProtections.config.globally_permitted_namespaces).to eq(['MyNamespace'])
end
end
end
end
3 changes: 1 addition & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'pry'
require 'rubocop/cop/package_protections'
require 'rubocop/rspec/support'
require 'package_protections'
require 'rubocop/rspec/support'
require 'package_protections/rspec/support'

RSpec.configure do |config|
Expand Down

0 comments on commit e8aec81

Please sign in to comment.