diff --git a/lib/generators/devise/views_generator.rb b/lib/generators/devise/views_generator.rb index 4d7bd2ec..fe89b6fe 100644 --- a/lib/generators/devise/views_generator.rb +++ b/lib/generators/devise/views_generator.rb @@ -79,23 +79,31 @@ module Devise end def inject_makerb_content - if gemfile_exists? - append_file gemfile_path, gemfile_content + if gemfile + append_file gemfile_path, gemfile_content unless gemfile =~ gemfile_content_regexp else create_file gemfile_path, gemfile_content end end def gemfile_path - @gemfile_path ||= File.join("Gemfile") - end - - def gemfile_exists? - File.exists?(File.join(destination_root, gemfile_path)) + @gemfile_path ||= File.join(destination_root, "Gemfile") end def gemfile_content - 'gem "markerb"' + "gem 'markerb'" + end + + def gemfile_content_regexp + /gem ['|"]markerb['|"]/ + end + + def gemfile + begin + File.read(gemfile_path) + rescue + nil + end end end diff --git a/test/generators/views_generator_test.rb b/test/generators/views_generator_test.rb index 80ca671e..39befe15 100644 --- a/test/generators/views_generator_test.rb +++ b/test/generators/views_generator_test.rb @@ -34,14 +34,26 @@ class ViewsGeneratorTest < Rails::Generators::TestCase end test "Assert Gemfile got gem markerb injected" do - File.open(File.join(destination_root, "Gemfile"), 'w') {|f| f.write("gem 'rails'") } + create_gemfile("gem 'rails'") run_generator %w(--markerb) - assert_file "Gemfile", /gem \"markerb\"/ + assert_file "Gemfile", /gem 'markerb'/ + assert_file "Gemfile", /gem 'rails'/ + end + + test "Assert Gemfile got gem markerb injected only if it does not exist" do + gemfile = create_gemfile("gem 'markerb'") + run_generator %w(--markerb) + assert_equal 1, gemfile.scan("gem 'markerb'").length end test "Assert Gemfile got created with markerb if no gemfile" do run_generator %w(--markerb) - assert_file "Gemfile", /gem \"markerb\"/ + assert_file "Gemfile", /gem 'markerb'/ + end + + def create_gemfile(content=nil) + File.open(File.join(destination_root, "Gemfile"), 'w') {|f| f.write(content) } + File.read(File.join(destination_root, "Gemfile")) end def assert_files(scope = nil, options={})