Skip to content

Commit

Permalink
Merge pull request #99 from Shopify/set-name-error-name
Browse files Browse the repository at this point in the history
Properly set Zeitwerk::NameError#name
  • Loading branch information
fxn committed Nov 29, 2019
2 parents 1815068 + 8fea778 commit c77975d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
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

0 comments on commit c77975d

Please sign in to comment.