From 73b6c196c4c68ce86f9da3847c890cfef56edee4 Mon Sep 17 00:00:00 2001 From: Kieran Pilkington Date: Sun, 7 Feb 2010 08:55:45 +1300 Subject: [PATCH] Add a check to ensure that the application name does not already exist, i.e. String or Module are part of ruby --- railties/lib/generators/rails/app/app_generator.rb | 11 ++++++++--- railties/test/generators/app_generator_test.rb | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/railties/lib/generators/rails/app/app_generator.rb b/railties/lib/generators/rails/app/app_generator.rb index c439ed8..b936d69 100644 --- a/railties/lib/generators/rails/app/app_generator.rb +++ b/railties/lib/generators/rails/app/app_generator.rb @@ -209,9 +209,14 @@ module Rails::Generators end def valid_app_const? - case app_const - when /^\d/ - raise Error, "Invalid application name #{app_name}. Please give a name which does not start with numbers." + if app_const !~ /^[A-Z]/ + raise Error, "Invalid application name #{app_name}. Please give a name which starts with a letter." + + # Check to make sure they don't supply app names like "Class" or "Set" which may already + # be defined classes by Ruby. This doesn't catch conflicts with gems like Rails or Rack + elsif Object.const_defined?(app_const_base) + raise Error, "Application name #{app_name} constant form (#{app_const_base}) is already in use. Please choose another application name." + end end diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 54b970c..4cc65d0 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -55,9 +55,18 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_match /Invalid value for \-\-database option/, content end - def test_invalid_application_name_raises_an_error - content = capture(:stderr){ run_generator [File.join(destination_root, "43-things")] } - assert_equal "Invalid application name 43-things. Please give a name which does not start with numbers.\n", content + def test_application_name_raises_an_error_if_not_begins_with_letter + %w{ 43-things -MyApp- }.each do |invalid_app_name| + content = capture(:stderr){ run_generator [File.join(destination_root, invalid_app_name)] } + assert_equal "Invalid application name #{invalid_app_name}. Please give a name which starts with a letter.\n", content + end + end + + def test_application_name_raises_an_error_if_name_already_used_constant + %w{ String Hash Class Module Set Symbol }.each do |ruby_class| + content = capture(:stderr){ run_generator [File.join(destination_root, ruby_class)] } + assert_equal "Application name #{ruby_class} constant form (#{ruby_class}) is already in use. Please choose another application name.\n", content + end end def test_invalid_application_name_is_fixed -- 1.6.6