From 1cbc3cb3f7c5ae3ebe8c966180a9129333926c25 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Wed, 14 Sep 2016 20:53:06 -0400 Subject: [PATCH] Call eval with file and line nb so seeds fail with proper stracktraces --- lib/seed-fu/runner.rb | 8 +++++--- spec/fixtures/with_errors/seeded_models_chunked.rb | 4 ++++ .../fixtures/with_errors/seeded_models_straight.rb | 4 ++++ spec/runner_spec.rb | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 spec/fixtures/with_errors/seeded_models_chunked.rb create mode 100644 spec/fixtures/with_errors/seeded_models_straight.rb diff --git a/lib/seed-fu/runner.rb b/lib/seed-fu/runner.rb index d042aa8..6a583b7 100644 --- a/lib/seed-fu/runner.rb +++ b/lib/seed-fu/runner.rb @@ -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 diff --git a/spec/fixtures/with_errors/seeded_models_chunked.rb b/spec/fixtures/with_errors/seeded_models_chunked.rb new file mode 100644 index 0000000..5041155 --- /dev/null +++ b/spec/fixtures/with_errors/seeded_models_chunked.rb @@ -0,0 +1,4 @@ +# this is line 1 +# this is line 2 +# BREAK EVAL +raise 'on line 4' diff --git a/spec/fixtures/with_errors/seeded_models_straight.rb b/spec/fixtures/with_errors/seeded_models_straight.rb new file mode 100644 index 0000000..b177a40 --- /dev/null +++ b/spec/fixtures/with_errors/seeded_models_straight.rb @@ -0,0 +1,4 @@ +# this is line 1 +# this is line 2 +# this is line 3 +raise 'on line 4' diff --git a/spec/runner_spec.rb b/spec/runner_spec.rb index 85f1581..581e850 100644 --- a/spec/runner_spec.rb +++ b/spec/runner_spec.rb @@ -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