Skip to content

Commit

Permalink
explicit begin/rescue & tweaks
Browse files Browse the repository at this point in the history
We need an explicit begin here because we support Ruby 2.4, and the implicit
begin for blocks was added in 2.5.

Then, some minor tweaks to the error message and tests.
  • Loading branch information
fxn committed Sep 6, 2019
1 parent 47a94db commit a6f1a18
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
53 changes: 30 additions & 23 deletions lib/zeitwerk/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -474,32 +474,39 @@ def actual_root_dirs
# @return [void]
def set_autoloads_in_dir(dir, parent)
ls(dir) do |basename, abspath|
if ruby?(basename)
basename.slice!(-3, 3)
cname = inflector.camelize(basename, abspath).to_sym
autoload_file(parent, cname, abspath)
elsif dir?(abspath)
# In a Rails application, `app/models/concerns` is a subdirectory of
# `app/models`, but both of them are root directories.
#
# To resolve the ambiguity file name -> constant path this introduces,
# the `app/models/concerns` directory is totally ignored as a namespace,
# it counts only as root. The guard checks that.
unless root_dirs.key?(abspath)
begin
if ruby?(basename)
basename.slice!(-3, 3)
cname = inflector.camelize(basename, abspath).to_sym
autoload_subdir(parent, cname, abspath)
autoload_file(parent, cname, abspath)
elsif dir?(abspath)
# In a Rails application, `app/models/concerns` is a subdirectory of
# `app/models`, but both of them are root directories.
#
# To resolve the ambiguity file name -> constant path this introduces,
# the `app/models/concerns` directory is totally ignored as a namespace,
# it counts only as root. The guard checks that.
unless root_dirs.key?(abspath)
cname = inflector.camelize(basename, abspath).to_sym
autoload_subdir(parent, cname, abspath)
end
end
rescue ::NameError => error
path_type = ruby?(abspath) ? "file" : "directory"

raise NameError, <<~MESSAGE
#{error.message} inferred by #{inflector.class} from #{path_type}
#{abspath}
Possible ways to address this:
* Tell Zeitwerk to ignore this particular #{path_type}.
* Tell Zeitwerk to ignore one of its parent directories.
* Rename the #{path_type} to comply with the naming conventions.
* Modify the inflector to handle this case.
MESSAGE
end
rescue ::NameError => error
raise NameError, <<~MESSAGE
#{error.message}
This invalid constant name was inferred by #{inflector.class} from #{abspath}.
You can either:
- Mark this path to be ignored.
- Rename the file to respect the general convention.
- Modify the inflector to handle this case better.
MESSAGE
end
end

Expand Down
10 changes: 6 additions & 4 deletions test/lib/zeitwerk/test_exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,21 @@ class TestExceptions < LoaderTest
end
end

test "raises NameError if the inflector return an invalid constant name for a file" do
test "raises Zeitwerk::NameError if the inflector returns an invalid constant name for a file" do
files = [["foo-bar.rb", "FooBar = 1"]]
error = assert_raises Zeitwerk::NameError do
with_setup(files) {}
end
assert_includes error.message, "wrong constant name Foo-bar"
assert_includes error.message, "Tell Zeitwerk to ignore this particular file."
end

test "raises NameError if the inflector return an invalid constant name for a directory" do
files = [["foo-bar/baz.rb", "FooBar = 1"]]
error = assert_raises NameError do
test "raises Zeitwerk::NameError if the inflector returns an invalid constant name for a directory" do
files = [["foo-bar/baz.rb", "FooBar::Baz = 1"]]
error = assert_raises Zeitwerk::NameError do
with_setup(files) {}
end
assert_includes error.message, "wrong constant name Foo-bar"
assert_includes error.message, "Tell Zeitwerk to ignore this particular directory."
end
end

0 comments on commit a6f1a18

Please sign in to comment.