Skip to content

Commit 9c23a19

Browse files
authored
Merge pull request #224 from Shopify/revert-220-cleaner_signatures_for_load_paths
Revert "Simplify packwerk path determination mechanics"
2 parents 446661d + 80a277b commit 9c23a19

4 files changed

Lines changed: 20 additions & 18 deletions

File tree

lib/packwerk/application_load_paths.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module ApplicationLoadPaths
99
class << self
1010
extend T::Sig
1111

12-
sig { params(root: String, environment: String).returns(T::Array[String]) }
12+
sig { params(root: String, environment: String).returns(T::Hash[String, Module]) }
1313
def extract_relevant_paths(root, environment)
1414
require_application(root, environment)
1515
all_paths = extract_application_autoload_paths
@@ -18,30 +18,30 @@ def extract_relevant_paths(root, environment)
1818
relative_path_strings(relevant_paths)
1919
end
2020

21-
sig { returns(T::Array[String]) }
21+
sig { returns(T::Hash[String, Module]) }
2222
def extract_application_autoload_paths
2323
Rails.autoloaders.inject({}) do |h, loader|
2424
h.merge(loader.root_dirs)
25-
end.keys
25+
end
2626
end
2727

2828
sig do
29-
params(all_paths: T::Array[String], bundle_path: Pathname, rails_root: Pathname)
30-
.returns(T::Array[Pathname])
29+
params(all_paths: T::Hash[String, Module], bundle_path: Pathname, rails_root: Pathname)
30+
.returns(T::Hash[Pathname, Module])
3131
end
3232
def filter_relevant_paths(all_paths, bundle_path: Bundler.bundle_path, rails_root: Rails.root)
3333
bundle_path_match = bundle_path.join("**")
3434
rails_root_match = rails_root.join("**")
3535

3636
all_paths
37-
.map { |path| Pathname.new(path).expand_path }
37+
.transform_keys { |path| Pathname.new(path).expand_path }
3838
.select { |path| path.fnmatch(rails_root_match.to_s) } # path needs to be in application directory
3939
.reject { |path| path.fnmatch(bundle_path_match.to_s) } # reject paths from vendored gems
4040
end
4141

42-
sig { params(load_paths: T::Array[Pathname], rails_root: Pathname).returns(T::Array[String]) }
42+
sig { params(load_paths: T::Hash[Pathname, Module], rails_root: Pathname).returns(T::Hash[String, Module]) }
4343
def relative_path_strings(load_paths, rails_root: Rails.root)
44-
load_paths.map { |path| Pathname.new(path).relative_path_from(rails_root).to_s }
44+
load_paths.transform_keys { |path| Pathname.new(path).relative_path_from(rails_root).to_s }
4545
end
4646

4747
private
@@ -59,7 +59,7 @@ def require_application(root, environment)
5959
end
6060
end
6161

62-
sig { params(paths: T::Array[Pathname]).void }
62+
sig { params(paths: T::Hash[T.untyped, Module]).void }
6363
def assert_load_paths_present(paths)
6464
if paths.empty?
6565
raise <<~EOS

lib/packwerk/run_context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def from_configuration(configuration)
3838
sig do
3939
params(
4040
root_path: String,
41-
load_paths: T::Array[String],
41+
load_paths: T::Hash[String, Module],
4242
inflector: T.class_of(ActiveSupport::Inflector),
4343
cache_directory: Pathname,
4444
config_path: T.nilable(String),

test/unit/application_load_paths_test.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,41 @@ class ApplicationLoadPathsTest < Minitest::Test
1111
relative_path = "app/models"
1212
absolute_path = rails_root.join(relative_path)
1313
relative_path_strings = ApplicationLoadPaths.relative_path_strings(
14-
[absolute_path],
14+
{ absolute_path => Object },
1515
rails_root: rails_root
1616
)
1717

18-
assert_equal([relative_path], relative_path_strings)
18+
assert_equal({ relative_path => Object }, relative_path_strings)
1919
end
2020

2121
test ".filter_relevant_paths excludes paths outside of the application root" do
2222
valid_paths = ["/application/app/models"]
2323
paths = valid_paths + ["/users/tobi/.gems/something/app/models", "/application/../something/"]
24+
paths = paths.each_with_object({}) { |p, h| h[p.to_s] = Object }
2425
filtered_paths = ApplicationLoadPaths.filter_relevant_paths(
2526
paths,
2627
bundle_path: Pathname.new("/application/vendor/"),
2728
rails_root: Pathname.new("/application/")
2829
)
2930

30-
assert_equal(["/application/app/models"], filtered_paths.map(&:to_s))
31+
assert_equal({ "/application/app/models" => Object }, filtered_paths.transform_keys(&:to_s))
3132
end
3233

3334
test ".filter_relevant_paths excludes paths from vendored gems" do
3435
valid_paths = ["/application/app/models"]
3536
paths = valid_paths + ["/application/vendor/something/app/models"]
37+
paths = paths.each_with_object({}) { |p, h| h[p.to_s] = Object }
3638
filtered_paths = ApplicationLoadPaths.filter_relevant_paths(
3739
paths,
3840
bundle_path: Pathname.new("/application/vendor/"),
3941
rails_root: Pathname.new("/application/")
4042
)
4143

42-
assert_equal(["/application/app/models"], filtered_paths.map(&:to_s))
44+
assert_equal({ "/application/app/models" => Object }, filtered_paths.transform_keys(&:to_s))
4345
end
4446

4547
test ".extract_relevant_paths calls out to filter the paths" do
46-
ApplicationLoadPaths.expects(:filter_relevant_paths).once.returns([Pathname.new("/fake_path").to_s])
48+
ApplicationLoadPaths.expects(:filter_relevant_paths).once.returns(Pathname.new("/fake_path").to_s => Object)
4749
ApplicationLoadPaths.expects(:require_application).with("/application", "test").once.returns(true)
4850

4951
ApplicationLoadPaths.extract_relevant_paths("/application", "test")

test/unit/cli_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CliTest < Minitest::Test
2424
offense = Offense.new(file: file_path, message: violation_message)
2525

2626
configuration = Configuration.new({ "parallel" => false })
27-
configuration.stubs(load_paths: [])
27+
configuration.stubs(load_paths: {})
2828
RunContext.any_instance.stubs(:process_file).at_least_once.returns([offense])
2929

3030
string_io = StringIO.new
@@ -49,7 +49,7 @@ class CliTest < Minitest::Test
4949
offense = Offense.new(file: file_path, message: violation_message)
5050

5151
configuration = Configuration.new({ "parallel" => false })
52-
configuration.stubs(load_paths: [])
52+
configuration.stubs(load_paths: {})
5353

5454
RunContext.any_instance.stubs(:process_file)
5555
.at_least(2)
@@ -137,7 +137,7 @@ def show_stale_violations(_offense_collection, _fileset)
137137
offense = Offense.new(file: file_path, message: violation_message)
138138

139139
configuration = Configuration.new
140-
configuration.stubs(load_paths: [])
140+
configuration.stubs(load_paths: {})
141141
RunContext.any_instance.stubs(:process_file)
142142
.returns([offense])
143143

0 commit comments

Comments
 (0)