Skip to content

Commit

Permalink
plugin_generator_test.rb: Extract with_simulated_app
Browse files Browse the repository at this point in the history
Makes tests reads clearer by hiding details.
  • Loading branch information
henrik committed Feb 28, 2021
1 parent 2ca669f commit 14ff158
Showing 1 changed file with 26 additions and 32 deletions.
58 changes: 26 additions & 32 deletions railties/test/generators/plugin_generator_test.rb
Expand Up @@ -603,49 +603,32 @@ def test_skipping_gemspec_in_full_mode
end

def test_creating_plugin_in_app_directory_adds_gemfile_entry
# simulate application existence
gemfile_path = "#{Rails.root}/Gemfile"
Object.const_set("APP_PATH", Rails.root)
FileUtils.touch gemfile_path
File.write(gemfile_path, "#foo")
with_simulated_app do |gemfile_path|
File.write(gemfile_path, "#foo")

run_generator
run_generator

assert_file gemfile_path, /^gem 'bukkits', path: 'tmp\/bukkits'/
ensure
Object.send(:remove_const, "APP_PATH")
FileUtils.rm gemfile_path
assert_file gemfile_path, /^gem 'bukkits', path: 'tmp\/bukkits'/
end
end

def test_creating_plugin_only_specify_plugin_name_in_app_directory_adds_gemfile_entry
# simulate application existence
gemfile_path = "#{Rails.root}/Gemfile"
Object.const_set("APP_PATH", Rails.root)
FileUtils.touch gemfile_path
with_simulated_app do |gemfile_path|
FileUtils.cd(destination_root)
run_generator ["bukkits"]

FileUtils.cd(destination_root)
run_generator ["bukkits"]

assert_file gemfile_path, /gem 'bukkits', path: 'bukkits'/
ensure
Object.send(:remove_const, "APP_PATH")
FileUtils.rm gemfile_path
assert_file gemfile_path, /gem 'bukkits', path: 'bukkits'/
end
end

def test_skipping_gemfile_entry
# simulate application existence
gemfile_path = "#{Rails.root}/Gemfile"
Object.const_set("APP_PATH", Rails.root)
FileUtils.touch gemfile_path
with_simulated_app do |gemfile_path|
run_generator [destination_root, "--skip-gemfile-entry"]

run_generator [destination_root, "--skip-gemfile-entry"]

assert_file gemfile_path do |contents|
assert_no_match(/gem 'bukkits', path: 'tmp\/bukkits'/, contents)
assert_file gemfile_path do |contents|
assert_no_match(/gem 'bukkits', path: 'tmp\/bukkits'/, contents)
end
end
ensure
Object.send(:remove_const, "APP_PATH")
FileUtils.rm gemfile_path
end

def test_generating_controller_inside_mountable_engine
Expand Down Expand Up @@ -830,4 +813,15 @@ def assert_match_sqlite3(contents)
assert_match(/group :development do\n gem 'sqlite3'\nend/, contents)
end
end

def with_simulated_app
gemfile_path = "#{Rails.root}/Gemfile"
Object.const_set("APP_PATH", Rails.root)
FileUtils.touch gemfile_path

yield gemfile_path
ensure
Object.send(:remove_const, "APP_PATH")
FileUtils.rm gemfile_path
end
end

0 comments on commit 14ff158

Please sign in to comment.