Skip to content

Commit

Permalink
Make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Evanczuk committed Oct 27, 2022
1 parent 7a4c18c commit 53c259e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 4 deletions.
14 changes: 14 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* [Understanding how to respond to new violations](#understanding-how-to-respond-to-new-violations)
* [Recording existing violations](#recording-existing-violations)
* [Understanding the list of deprecated references](#understanding-the-list-of-deprecated-references)
* [Loading extensions](#loading-extensions)

## What problem does Packwerk solve?

Expand Down Expand Up @@ -275,3 +276,16 @@ Above is an example of a constant violation entry in `deprecated_references.yml`
* `components/merchant/app/public/merchant/generate_order.rb` - path to the file containing the violated constant

Violations exist within the package that makes a violating reference. This means privacy violations of your package can be found listed in `deprecated_references.yml` files in the packages with the reference to a private constant.

# Loading Extensions

You can optionally specify ruby files that you'd like to be loaded with `packwerk` by specifying a `require` directive in `packwerk.yml`:
```yml
require:
- ./path/to/file.rb
- my_gem
```

`packwerk` will directly call `require` with these paths.
You can prefix local files with a dot to define them relative to `packwerk.yml`, or you can use absolute paths.
You can also reference the name of a gem.
1 change: 1 addition & 0 deletions lib/packwerk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Packwerk
autoload :ConstantNameInspector
autoload :ConstNodeInspector
autoload :DeprecatedReferences
autoload :ExtensionLoader
autoload :FileProcessor
autoload :FilesForProcessing
autoload :Graph
Expand Down
6 changes: 6 additions & 0 deletions lib/packwerk/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def initialize(configs = {}, config_path: nil)
@cache_directory = Pathname.new(configs["cache_directory"] || "tmp/cache/packwerk")
@config_path = config_path

if configs.key?("require")
configs["require"].each do |require_directive|
ExtensionLoader.load(require_directive, @root_path)
end
end

if configs["load_paths"]
warning = <<~WARNING
DEPRECATION WARNING: The 'load_paths' key in `packwerk.yml` is deprecated.
Expand Down
24 changes: 24 additions & 0 deletions lib/packwerk/extension_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# typed: strict
# frozen_string_literal: true

module Packwerk
# This class handles loading extensions to packwerk using the `require` directive
# in the `packwerk.yml` configuration.
class ExtensionLoader
class << self
extend T::Sig
sig { params(require_directive: String, config_dir_path: String).void }
def load(require_directive, config_dir_path)
# We want to transform the require directive to behave differently
# if it's a specific local file being required versus a gem
if require_directive.start_with?(".")
require File.join(config_dir_path, require_directive)
else
require require_directive
end
end
end
end

private_constant :ExtensionLoader
end
2 changes: 2 additions & 0 deletions test/fixtures/skeleton/config/my_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module MySpecialExtension
end
3 changes: 3 additions & 0 deletions test/fixtures/skeleton/packwerk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ include:
exclude:
- "**/temp.rb"
parallel: false
require:
- ./config/my_extension.rb
- pathname
8 changes: 4 additions & 4 deletions test/integration/custom_executable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class CustomExecutableTest < Minitest::Test

deprecated_reference_content_after_update = read_deprecated_references
expected_output = <<~EOS
📦 Packwerk is inspecting 13 files
\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.
📦 Packwerk is inspecting 14 files
\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.
📦 Finished in \\d+\\.\\d+ seconds
No offenses detected
Expand Down Expand Up @@ -115,8 +115,8 @@ class CustomExecutableTest < Minitest::Test
deprecated_reference_content_after_update =
read_deprecated_references.reject { |k, _v| k.match?(timeline_deprecated_reference_path) }
expected_output = <<~EOS
📦 Packwerk is inspecting 14 files
\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.
📦 Packwerk is inspecting 15 files
\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.
📦 Finished in \\d+\\.\\d+ seconds
No offenses detected
Expand Down

0 comments on commit 53c259e

Please sign in to comment.