Skip to content

Commit

Permalink
Assert for valid application names. [#3556 status:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 3, 2010
1 parent 6716e4b commit e88ea32
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 10 additions & 2 deletions railties/lib/rails/generators/rails/app/app_generator.rb
Expand Up @@ -53,8 +53,9 @@ def initialize(*args)

def create_root
self.destination_root = File.expand_path(app_path, destination_root)
empty_directory '.'
valid_app_const?

empty_directory '.'
set_default_accessors!
FileUtils.cd(destination_root)
end
Expand Down Expand Up @@ -193,7 +194,14 @@ def app_name
end

def app_const
@app_const ||= "#{app_name.classify}::Application"
@app_const ||= "#{app_name.gsub(/\W/, '_').squeeze('_').classify}::Application"
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."
end
end

def app_secret
Expand Down
10 changes: 10 additions & 0 deletions railties/test/generators/app_generator_test.rb
Expand Up @@ -53,6 +53,16 @@ def test_invalid_database_option_raises_an_error
assert_match /Invalid value for \-\-database option/, content
end

def test_invalid_application_name_raises_an_error
content = capture(:stderr){ Rails::Generators::AppGenerator.start [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
end

def test_invalid_application_name_is_fixed
silence(:stdout){ Rails::Generators::AppGenerator.start [File.join(destination_root, "things-43")] }
assert_file "things-43/config/environment.rb", /Things43::Application/
end

def test_config_database_is_added_by_default
run_generator
assert_file "config/database.yml", /sqlite3/
Expand Down

1 comment on commit e88ea32

@trevorturk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, José! :)

Please sign in to comment.