Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #11419 from simi/generator_generator_test

Added generated unit test for generator generator.
  • Loading branch information...
commit c612638a257be6cea631a1e5075f671ca328b8dc 2 parents 8df284a + 5ecd125
@guilleiguaran guilleiguaran authored
View
5 railties/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Added generated unit test for generator generator using new
+ `test:generators` rake task.
+
+ *Josef Šimánek*
+
* Removed `update:application_controller` rake task.
*Josef Šimánek*
View
1  railties/lib/rails/generators/rails/generator/USAGE
@@ -10,3 +10,4 @@ Example:
lib/generators/awesome/awesome_generator.rb
lib/generators/awesome/USAGE
lib/generators/awesome/templates/
+ test/lib/generators/awesome/awesome_generator_test.rb
View
2  railties/lib/rails/generators/rails/generator/generator_generator.rb
@@ -10,6 +10,8 @@ def create_generator_files
directory '.', generator_dir
end
+ hook_for :test_framework
+
protected
def generator_dir
View
26 railties/lib/rails/generators/test_unit/generator/generator_generator.rb
@@ -0,0 +1,26 @@
+require 'rails/generators/test_unit'
+
+module TestUnit # :nodoc:
+ module Generators # :nodoc:
+ class GeneratorGenerator < Base # :nodoc:
+ check_class_collision suffix: "GeneratorTest"
+
+ class_option :namespace, type: :boolean, default: true,
+ desc: "Namespace generator under lib/generators/name"
+
+ def create_generator_files
+ template 'generator_test.rb', File.join('test/lib/generators', class_path, "#{file_name}_generator_test.rb")
+ end
+
+ protected
+
+ def generator_path
+ if options[:namespace]
+ File.join("generators", regular_class_path, file_name, "#{file_name}_generator")
+ else
+ File.join("generators", regular_class_path, "#{file_name}_generator")
+ end
+ end
+ end
+ end
+end
View
16 railties/lib/rails/generators/test_unit/generator/templates/generator_test.rb
@@ -0,0 +1,16 @@
+require 'test_helper'
+require '<%= generator_path %>'
+
+<% module_namespacing do -%>
+class <%= class_name %>GeneratorTest < Rails::Generators::TestCase
+ tests <%= class_name %>Generator
+ destination Rails.root.join('tmp/generators')
+ setup :prepare_destination
+
+ # test "generator runs without errors" do
+ # assert_nothing_raised do
+ # run_generator ["arguments"]
+ # end
+ # end
+end
+<% end -%>
View
1  railties/lib/rails/test_help.rb
@@ -6,6 +6,7 @@
require 'active_support/test_case'
require 'action_controller/test_case'
require 'action_dispatch/testing/integration'
+require 'rails/generators/test_case'
# Config Rails backtrace in tests.
require 'rails/backtrace_cleaner'
View
6 railties/lib/rails/test_unit/testing.rake
@@ -14,7 +14,7 @@ namespace :test do
# Placeholder task for other Railtie and plugins to enhance. See Active Record for an example.
end
- task :run => ['test:units', 'test:functionals', 'test:integration']
+ task :run => ['test:units', 'test:functionals', 'test:generators', 'test:integration']
# Inspired by: http://ngauthier.com/2012/02/quick-tests-with-bash.html
desc "Run tests quickly by merging all types and not resetting db"
@@ -35,6 +35,10 @@ namespace :test do
end
end
+ Rails::TestTask.new(generators: "test:prepare") do |t|
+ t.pattern = "test/lib/generators/**/*_test.rb"
+ end
+
Rails::TestTask.new(units: "test:prepare") do |t|
t.pattern = 'test/{models,helpers,unit}/**/*_test.rb'
end
View
16 railties/test/generators/generator_generator_test.rb
@@ -16,6 +16,10 @@ def test_generator_skeleton_is_created
assert_file "lib/generators/awesome/awesome_generator.rb",
/class AwesomeGenerator < Rails::Generators::NamedBase/
+ assert_file "test/lib/generators/awesome_generator_test.rb",
+ /class AwesomeGeneratorTest < Rails::Generators::TestCase/
+ assert_file "test/lib/generators/awesome_generator_test.rb",
+ /require 'generators\/awesome\/awesome_generator'/
end
def test_namespaced_generator_skeleton
@@ -29,6 +33,10 @@ def test_namespaced_generator_skeleton
assert_file "lib/generators/rails/awesome/awesome_generator.rb",
/class Rails::AwesomeGenerator < Rails::Generators::NamedBase/
+ assert_file "test/lib/generators/rails/awesome_generator_test.rb",
+ /class Rails::AwesomeGeneratorTest < Rails::Generators::TestCase/
+ assert_file "test/lib/generators/rails/awesome_generator_test.rb",
+ /require 'generators\/rails\/awesome\/awesome_generator'/
end
def test_generator_skeleton_is_created_without_file_name_namespace
@@ -42,6 +50,10 @@ def test_generator_skeleton_is_created_without_file_name_namespace
assert_file "lib/generators/awesome_generator.rb",
/class AwesomeGenerator < Rails::Generators::NamedBase/
+ assert_file "test/lib/generators/awesome_generator_test.rb",
+ /class AwesomeGeneratorTest < Rails::Generators::TestCase/
+ assert_file "test/lib/generators/awesome_generator_test.rb",
+ /require 'generators\/awesome_generator'/
end
def test_namespaced_generator_skeleton_without_file_name_namespace
@@ -55,5 +67,9 @@ def test_namespaced_generator_skeleton_without_file_name_namespace
assert_file "lib/generators/rails/awesome_generator.rb",
/class Rails::AwesomeGenerator < Rails::Generators::NamedBase/
+ assert_file "test/lib/generators/rails/awesome_generator_test.rb",
+ /class Rails::AwesomeGeneratorTest < Rails::Generators::TestCase/
+ assert_file "test/lib/generators/rails/awesome_generator_test.rb",
+ /require 'generators\/rails\/awesome_generator'/
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.