Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly set Zeitwerk::NameError#name #99

Merged
merged 1 commit into from Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/zeitwerk/loader.rb
Expand Up @@ -494,7 +494,7 @@ def set_autoloads_in_dir(dir, parent)
rescue ::NameError => error
path_type = ruby?(abspath) ? "file" : "directory"

raise NameError, <<~MESSAGE
raise NameError.new(<<~MESSAGE, error.name)
#{error.message} inferred by #{inflector.class} from #{path_type}

#{abspath}
Expand Down
2 changes: 1 addition & 1 deletion lib/zeitwerk/loader/callbacks.rb
Expand Up @@ -14,7 +14,7 @@ def on_file_autoloaded(file)
if logger && cdef?(*cref)
log("constant #{cpath(*cref)} loaded from file #{file}")
elsif !cdef?(*cref)
raise Zeitwerk::NameError, "expected file #{file} to define constant #{cpath(*cref)}, but didn't"
raise Zeitwerk::NameError.new("expected file #{file} to define constant #{cpath(*cref)}, but didn't", cref.last)
end
end

Expand Down
17 changes: 11 additions & 6 deletions test/lib/zeitwerk/test_exceptions.rb
Expand Up @@ -5,8 +5,9 @@ class TestExceptions < LoaderTest
files = [["typo.rb", "TyPo = 1"]]
with_setup(files) do
typo_rb = File.realpath("typo.rb")
e = assert_raises(Zeitwerk::NameError) { Typo }
assert_equal "expected file #{typo_rb} to define constant Typo, but didn't", e.message
error = assert_raises(Zeitwerk::NameError) { Typo }
assert_equal "expected file #{typo_rb} to define constant Typo, but didn't", error.message
assert_equal :Typo, error.name
end
end

Expand All @@ -19,8 +20,9 @@ class TestExceptions < LoaderTest
files = [["x.rb", "Y = 1"]]
with_setup(files) do
x_rb = File.realpath("x.rb")
e = assert_raises(Zeitwerk::NameError) { loader.eager_load }
assert_equal "expected file #{x_rb} to define constant X, but didn't", e.message
error = assert_raises(Zeitwerk::NameError) { loader.eager_load }
assert_equal "expected file #{x_rb} to define constant X, but didn't", error.message
assert_equal :X, error.name
end
end

Expand All @@ -33,8 +35,9 @@ class TestExceptions < LoaderTest
files = [["cli/x.rb", "module CLI; X = 1; end"]]
with_setup(files) do
cli_x_rb = File.realpath("cli/x.rb")
e = assert_raises(Zeitwerk::NameError) { loader.eager_load }
assert_equal "expected file #{cli_x_rb} to define constant Cli::X, but didn't", e.message
error = assert_raises(Zeitwerk::NameError) { loader.eager_load }
assert_equal "expected file #{cli_x_rb} to define constant Cli::X, but didn't", error.message
assert_equal :X, error.name
end
end

Expand All @@ -50,6 +53,7 @@ class TestExceptions < LoaderTest
error = assert_raises Zeitwerk::NameError do
with_setup(files) {}
end
assert_equal :"Foo-bar", error.name
assert_includes error.message, "wrong constant name Foo-bar"
assert_includes error.message, "Tell Zeitwerk to ignore this particular file."
end
Expand All @@ -59,6 +63,7 @@ class TestExceptions < LoaderTest
error = assert_raises Zeitwerk::NameError do
with_setup(files) {}
end
assert_equal :"Foo-bar", error.name
assert_includes error.message, "wrong constant name Foo-bar"
assert_includes error.message, "Tell Zeitwerk to ignore this particular directory."
end
Expand Down