Skip to content

Commit

Permalink
create_dummy_app method that allows to easily create dummy applicatio…
Browse files Browse the repository at this point in the history
…n from template
  • Loading branch information
drogus committed Nov 2, 2010
1 parent fadad11 commit f9e33fc
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ def lib
def test
template "test/test_helper.rb"
template "test/%name%_test.rb"
append_file "Rakefile", <<-EOF
#{rakefile_test_tasks}
task :default => :test
EOF
if full?
template "test/integration/navigation_test.rb"
end
end

def generate_test_dummy
def generate_test_dummy(force = false)
opts = (options || {}).slice(:skip_active_record, :skip_javascript, :database, :javascript)
opts[:force] = force

invoke Rails::Generators::AppGenerator,
[ File.expand_path(dummy_path, destination_root) ], opts
Expand All @@ -70,33 +76,12 @@ def test_dummy_clean
end
end

def script
directory "script" do |content|
def script(force = false)
directory "script", :force => force do |content|
"#{shebang}\n" + content
end
chmod "script", 0755, :verbose => false
end

def rakefile_test_tasks
<<-RUBY
require 'rake/testtask'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end
RUBY
end

def dummy_path
"#{test_path}/dummy"
end

def test_path
"test"
end
end

module Generators
Expand Down Expand Up @@ -143,7 +128,7 @@ def create_test_files

def create_test_dummy_files
return if options[:skip_test_unit]
create_test_dummy(dummy_path)
create_dummy_app
end

def finish_template
Expand All @@ -153,13 +138,17 @@ def finish_template
public_task :apply_rails_template, :bundle_if_dev_or_edge

protected
def create_test_dummy(dummy_path)
def create_dummy_app(path = nil)
dummy_path(path) if path

say_status :vendor_app, dummy_path
mute do
build(:generate_test_dummy)
store_application_definition!
build(:test_dummy_config)
build(:test_dummy_clean)
# ensure that script/rails has proper dummy_path
build(:script, true)
end
end

Expand Down Expand Up @@ -205,10 +194,22 @@ def get_builder_class
defined?(::PluginBuilder) ? ::PluginBuilder : Rails::PluginBuilder
end

[:test_path, :dummy_path, :rakefile_test_tasks].each do |name|
define_method name do
builder.send(name) if builder.respond_to?(name)
end
def rakefile_test_tasks
<<-RUBY
require 'rake/testtask'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end
RUBY
end

def dummy_path(path = nil)
@dummy_path = path if path
@dummy_path || "test/dummy"
end

def mute(&block)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ end
require 'rake'
require 'rake/rdoctask'

<% unless options[:skip_test_unit] -%>
<%= rakefile_test_tasks %>

task :default => :test
<% end -%>
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = '<%= camelized %>'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

ENGINE_PATH = File.expand_path('../..', __FILE__)
load File.expand_path('../../<%= test_path %>/dummy/script/rails', __FILE__)
load File.expand_path('../../<%= dummy_path %>/script/rails', __FILE__)
1 change: 1 addition & 0 deletions railties/test/fixtures/lib/create_test_dummy_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create_dummy_app("spec/dummy")
14 changes: 10 additions & 4 deletions railties/test/fixtures/lib/plugin_builders/spec_builder.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
class PluginBuilder < Rails::PluginBuilder
def test
create_file "spec/spec_helper.rb"
append_file "Rakefile", <<-EOF
# spec tasks in rakefile
task :default => :spec
EOF
end

def test_path
"spec"
def generate_test_dummy
dummy_path("spec/dummy")
super
end

def rakefile_test_tasks
"# spec tasks in rakefile"
def skip_test_unit?
true
end
end
9 changes: 8 additions & 1 deletion railties/test/generators/plugin_new_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def test_ensure_that_plugin_options_are_not_passed_to_app_generator
assert_no_match /It works from file!.*It works_from_file/, run_generator([destination_root, "-m", "lib/template.rb"])
end

def test_ensure_that_test_dummy_can_be_generated_from_a_template
FileUtils.cd(Rails.root)
run_generator([destination_root, "-m", "lib/create_test_dummy_template.rb", "--skip-test-unit"])
assert_file "spec/dummy"
assert_no_file "test"
end

def test_database_entry_is_assed_by_default_in_full_mode
run_generator([destination_root, "--full"])
assert_file "test/dummy/config/database.yml", /sqlite/
Expand Down Expand Up @@ -143,9 +150,9 @@ def test_overriding_test_framework
FileUtils.cd(destination_root)
run_generator([destination_root, "-b", "#{Rails.root}/lib/plugin_builders/spec_builder.rb"])
assert_file 'spec/spec_helper.rb'
assert_file 'spec/dummy'
assert_file 'Rakefile', /task :default => :spec/
assert_file 'Rakefile', /# spec tasks in rakefile/
assert_file 'spec/dummy'
assert_file 'script/rails', %r{spec/dummy}
end

Expand Down

0 comments on commit f9e33fc

Please sign in to comment.