Skip to content

Commit

Permalink
Better error for invalid constant names
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Aug 23, 2019
1 parent 757dd73 commit 47a94db
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/zeitwerk/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ class Error < StandardError

class ReloadingDisabledError < Error
end

class NameError < ::NameError
end
end
10 changes: 10 additions & 0 deletions lib/zeitwerk/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,16 @@ def set_autoloads_in_dir(dir, parent)
autoload_subdir(parent, cname, abspath)
end
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
16 changes: 16 additions & 0 deletions test/lib/zeitwerk/test_exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,20 @@ class TestExceptions < LoaderTest
assert_raises(RuntimeError, "foo") { Raises }
end
end

test "raises NameError if the inflector return 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"
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
with_setup(files) {}
end
assert_includes error.message, "wrong constant name Foo-bar"
end
end

0 comments on commit 47a94db

Please sign in to comment.