Skip to content

Commit

Permalink
Call eval with file and line nb so seeds fail with proper stracktraces
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Sep 15, 2016
1 parent 9d393b2 commit 1cbc3cb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/seed-fu/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ def run_file(filename)
ActiveRecord::Base.transaction do
open(filename) do |file|
chunked_ruby = ''
file.each_line do |line|
start_line = 1
file.each_line.with_index do |line, line_index|
if line == "# BREAK EVAL\n"
eval(chunked_ruby)
eval(chunked_ruby, binding, filename, start_line)
start_line = line_index + 2
chunked_ruby = ''
else
chunked_ruby << line
end
end
eval(chunked_ruby) unless chunked_ruby == ''
eval(chunked_ruby, binding, filename, start_line) unless chunked_ruby == ''
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/with_errors/seeded_models_chunked.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# this is line 1
# this is line 2
# BREAK EVAL
raise 'on line 4'
4 changes: 4 additions & 0 deletions spec/fixtures/with_errors/seeded_models_straight.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# this is line 1
# this is line 2
# this is line 3
raise 'on line 4'
14 changes: 14 additions & 0 deletions spec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,18 @@
SeedFu.seed
SeededModel.count.should == 3
end

it "should give meaningful stacktraces" do
%w[straight chunked].each do |suffix|
begin
SeedFu.seed(File.dirname(__FILE__) + '/fixtures/with_errors', /_#{suffix}/)
fail "An exception was supposed to be raised"
rescue Exception => e
e.message.should == "on line 4"
e.backtrace.first.should =~ /seeded_models_#{suffix}.rb:4/
end
end
end


end

0 comments on commit 1cbc3cb

Please sign in to comment.