Skip to content

Commit

Permalink
Reraise exception in require_file
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthomas committed Jun 13, 2017
1 parent 1827d56 commit 16e85e9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
5 changes: 3 additions & 2 deletions lib/cosmos/top_level.rb
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,10 @@ def self.require_file(filename)
begin
require filename
rescue Exception => err
msg = "Unable to require #{filename} due to #{err.message}. Ensure #{filename} is in the COSMOS lib directory."
msg = "Unable to require #{filename} due to #{err.message}. "\
"Ensure #{filename} is in the COSMOS lib directory."
Logger.error msg
raise msg
raise $!, msg, $!.backtrace
end
end

Expand Down
37 changes: 32 additions & 5 deletions spec/top_level/top_level_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,46 @@ def thread.graceful_kill

describe "require_class" do
it "requires the class represented by the filename" do
filename = File.join(Cosmos::USERPATH,"lib","my_test_class.rb")
File.delete(filename) if File.exist? filename

# Explicitly load cosmos.rb to ensure the Cosmos::USERPATH/lib
# directory is in the path
load 'cosmos.rb'

expect { Cosmos.require_class("my_test_class.rb") }.to raise_error(/Unable to require my_test_class.rb/)
filename = File.join(Cosmos::USERPATH,"lib","my_test_class.rb")
File.open(filename,'w') do |file|
file.puts "class MyTestClass"
file.puts "end"
end

Cosmos.require_class("my_test_class.rb")
klass = Cosmos.require_class("my_test_class.rb")
expect(klass).to be_a(Class)
expect(klass).to eq MyTestClass
File.delete(filename)
end
end

describe "require_file" do
it "requires the file" do
filename = File.join(Cosmos::USERPATH,"lib","my_test_file.rb")
File.delete(filename) if File.exist? filename

# Explicitly load cosmos.rb to ensure the Cosmos::USERPATH/lib
# directory is in the path
load 'cosmos.rb'
expect { Cosmos.require_file("my_test_file.rb") }.to raise_error(LoadError, /Unable to require my_test_file.rb/)

File.open(filename,'w') do |file|
file.puts "class MyTestFile"
file.puts " blah" # This will cause an error
file.puts "end"
end
expect { Cosmos.require_file("my_test_file.rb") }.to raise_error(NameError, /Unable to require my_test_file.rb/)

File.open(filename,'w') do |file|
file.puts "class MyTestFile"
file.puts "end"
end
Cosmos.require_file("my_test_file.rb")
File.delete(filename)
end
end
Expand Down Expand Up @@ -342,4 +370,3 @@ def thread.graceful_kill
end
end
end

0 comments on commit 16e85e9

Please sign in to comment.