diff options
Diffstat (limited to 'railties')
25 files changed, 300 insertions, 175 deletions
diff --git a/railties/lib/rails/code_statistics.rb b/railties/lib/rails/code_statistics.rb index bcac0751d6..1aed2796c1 100644 --- a/railties/lib/rails/code_statistics.rb +++ b/railties/lib/rails/code_statistics.rb @@ -26,7 +26,7 @@ class CodeStatistics #:nodoc: Hash[@pairs.map{|pair| [pair.first, calculate_directory_statistics(pair.last)]}] end - def calculate_directory_statistics(directory, pattern = /.*\.rb$/) + def calculate_directory_statistics(directory, pattern = /.*\.(rb|js|coffee)$/) stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 } Dir.foreach(directory) do |file_name| @@ -39,6 +39,13 @@ class CodeStatistics #:nodoc: comment_started = false + case file_name + when /.*\.js$/ + comment_pattern = /^\s*\/\// + else + comment_pattern = /^\s*#/ + end + File.open(directory + "/" + file_name) do |f| while line = f.gets stats["lines"] += 1 @@ -55,7 +62,7 @@ class CodeStatistics #:nodoc: end stats["classes"] += 1 if line =~ /^\s*class\s+[_A-Z]/ stats["methods"] += 1 if line =~ /^\s*def\s+[_a-z]/ - stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/ + stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ comment_pattern end end end diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index 7f473c237c..8816387d34 100644 --- a/railties/lib/rails/commands.rb +++ b/railties/lib/rails/commands.rb @@ -36,9 +36,17 @@ when 'benchmarker', 'profiler' when 'console' require 'rails/commands/console' + options = Rails::Console.parse_arguments(ARGV) + + # RAILS_ENV needs to be set before config/application is required + ENV['RAILS_ENV'] = options[:environment] if options[:environment] + + # shift ARGV so IRB doesn't freak + ARGV.shift if ARGV.first && ARGV.first[0] != '-' + require APP_PATH Rails.application.require_environment! - Rails::Console.start(Rails.application) + Rails::Console.start(Rails.application, options) when 'server' # Change to the application's path if there is no config.ru file in current dir. diff --git a/railties/lib/rails/commands/console.rb b/railties/lib/rails/commands/console.rb index b95df3e545..92cee6b638 100644 --- a/railties/lib/rails/commands/console.rb +++ b/railties/lib/rails/commands/console.rb @@ -4,21 +4,12 @@ require 'irb/completion' module Rails class Console - attr_reader :options, :app, :console, :arguments - - def self.start(*args) - new(*args).start - end - - def initialize(app, arguments = ARGV) - @app = app - @arguments = arguments - app.load_console - @console = app.config.console || IRB - end + class << self + def start(*args) + new(*args).start + end - def options - @options ||= begin + def parse_arguments(arguments) options = {} OptionParser.new do |opt| @@ -31,20 +22,38 @@ module Rails opt.parse!(arguments) end + if arguments.first && arguments.first[0] != '-' + env = arguments.first + options[:environment] = %w(production development test).detect {|e| e =~ /^#{env}/} || env + end + options end end + attr_reader :options, :app, :console + + def initialize(app, options={}) + @app = app + @options = options + app.load_console + @console = app.config.console || IRB + end + def sandbox? options[:sandbox] end + def environment + options[:environment] ||= ENV['RAILS_ENV'] || 'development' + end + def environment? - options[:environment] + environment end def set_environment! - Rails.env = options[:environment] + Rails.env = environment end def debugger? @@ -53,9 +62,7 @@ module Rails def start app.sandbox = sandbox? - require_debugger if debugger? - set_environment! if environment? if sandbox? @@ -82,8 +89,3 @@ module Rails end end end - -# Has to set the RAILS_ENV before config/application is required -if ARGV.first && !ARGV.first.index("-") && env = ARGV.shift # has to shift the env ARGV so IRB doesn't freak - ENV['RAILS_ENV'] = %w(production development test).detect {|e| e =~ /^#{env}/} || env -end diff --git a/railties/lib/rails/commands/dbconsole.rb b/railties/lib/rails/commands/dbconsole.rb index cc7caffc3d..cc0552184a 100644 --- a/railties/lib/rails/commands/dbconsole.rb +++ b/railties/lib/rails/commands/dbconsole.rb @@ -5,66 +5,19 @@ require 'rbconfig' module Rails class DBConsole - attr_reader :arguments, :config - + attr_reader :config, :arguments + def self.start - new(config).start + new.start end - def self.config - config = begin - YAML.load(ERB.new(IO.read("config/database.yml")).result) - rescue SyntaxError, StandardError - require APP_PATH - Rails.application.config.database_configuration - end - - unless config[env] - abort "No database is configured for the environment '#{env}'" - end - - config[env] - end - - def self.env - if Rails.respond_to?(:env) - Rails.env - else - ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development" - end - end - - def initialize(config, arguments = ARGV) - @config, @arguments = config, arguments + def initialize(arguments = ARGV) + @arguments = arguments end def start - include_password = false - options = {} - OptionParser.new do |opt| - opt.banner = "Usage: rails dbconsole [environment] [options]" - opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v| - include_password = true - end - - opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'], - "Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode| - options['mode'] = mode - end - - opt.on("--header") do |h| - options['header'] = h - end - - opt.on("-h", "--help", "Show this help message.") do - puts opt - exit - end - - opt.parse!(arguments) - abort opt.to_s unless (0..1).include?(arguments.size) - end - + options = parse_arguments(arguments) + ENV['RAILS_ENV'] = options[:environment] || environment case config["adapter"] when /^mysql/ @@ -76,7 +29,7 @@ module Rails 'encoding' => '--default-character-set' }.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact - if config['password'] && include_password + if config['password'] && options['include_password'] args << "--password=#{config['password']}" elsif config['password'] && !config['password'].to_s.empty? args << "-p" @@ -90,7 +43,7 @@ module Rails ENV['PGUSER'] = config["username"] if config["username"] ENV['PGHOST'] = config["host"] if config["host"] ENV['PGPORT'] = config["port"].to_s if config["port"] - ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && include_password + ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && options['include_password'] find_cmd_and_exec('psql', config["database"]) when "sqlite" @@ -110,7 +63,7 @@ module Rails if config['username'] logon = config['username'] - logon << "/#{config['password']}" if config['password'] && include_password + logon << "/#{config['password']}" if config['password'] && options['include_password'] logon << "@#{config['database']}" if config['database'] end @@ -121,8 +74,73 @@ module Rails end end + def config + @config ||= begin + cfg = begin + cfg = YAML.load(ERB.new(IO.read("config/database.yml")).result) + rescue SyntaxError, StandardError + require APP_PATH + Rails.application.config.database_configuration + end + + unless cfg[environment] + abort "No database is configured for the environment '#{environment}'" + end + + cfg[environment] + end + end + + def environment + if Rails.respond_to?(:env) + Rails.env + else + ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development" + end + end + protected + def parse_arguments(arguments) + options = {} + + OptionParser.new do |opt| + opt.banner = "Usage: rails dbconsole [environment] [options]" + opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v| + options['include_password'] = true + end + + opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'], + "Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode| + options['mode'] = mode + end + + opt.on("--header") do |h| + options['header'] = h + end + + opt.on("-h", "--help", "Show this help message.") do + puts opt + exit + end + + opt.on("-e", "--environment=name", String, + "Specifies the environment to run this console under (test/development/production).", + "Default: development" + ) { |v| options[:environment] = v.strip } + + opt.parse!(arguments) + abort opt.to_s unless (0..1).include?(arguments.size) + end + + if arguments.first && arguments.first[0] != '-' + env = arguments.first + options[:environment] = %w(production development test).detect {|e| e =~ /^#{env}/} || env + end + + options + end + def find_cmd_and_exec(commands, *args) commands = Array(commands) @@ -145,8 +163,3 @@ module Rails end end end - -# Has to set the RAILS_ENV before config/application is required -if ARGV.first && !ARGV.first.index("-") && env = ARGV.first - ENV['RAILS_ENV'] = %w(production development test).detect {|e| e =~ /^#{env}/} || env -end diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb index e68d2e05c5..9ef64da3ef 100644 --- a/railties/lib/rails/commands/server.rb +++ b/railties/lib/rails/commands/server.rb @@ -32,11 +32,6 @@ module Rails opt_parser.parse! args - # Handle's environment like RAILS_ENV=production passed in directly - if index = args.index {|arg| arg.include?("RAILS_ENV")} - options[:environment] ||= args.delete_at(index).split('=').last - end - options[:server] = args.shift options end diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index 2c1742c6be..5838c9fc38 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -230,7 +230,7 @@ module Rails if defined?(JRUBY_VERSION) "gem 'therubyrhino'\n" else - "# gem 'therubyracer', platform: :ruby\n" + "# gem 'therubyracer', platforms: :ruby\n" end end diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 63703176de..b61a5fc69d 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -84,10 +84,11 @@ module Rails end def namespaced_class_path - @namespaced_class_path ||= begin - namespace_path = namespace.name.split("::").map {|m| m.underscore } - namespace_path + @class_path - end + @namespaced_class_path ||= [namespaced_path] + @class_path + end + + def namespaced_path + @namespaced_path ||= namespace.name.split("::").map {|m| m.underscore }[0] end def class_name @@ -134,7 +135,7 @@ module Rails end def route_url - @route_url ||= class_path.collect{|dname| "/" + dname }.join('') + "/" + plural_file_name + @route_url ||= class_path.collect {|dname| "/" + dname }.join + "/" + plural_file_name end # Tries to retrieve the application name or simple return application. diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt index 19cbf0e4f1..280f777cc0 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt @@ -9,8 +9,8 @@ ActiveSupport.on_load(:action_controller) do end <%- unless options.skip_active_record? -%> -# Disable root element in JSON by default. -ActiveSupport.on_load(:active_record) do - self.include_root_in_json = false -end +# To enable root element in JSON for ActiveRecord objects. +# ActiveSupport.on_load(:active_record) do +# self.include_root_in_json = true +# end <%- end -%> diff --git a/railties/lib/rails/generators/rails/controller/templates/controller.rb b/railties/lib/rails/generators/rails/controller/templates/controller.rb index ece6bbba3b..633e0b3177 100644 --- a/railties/lib/rails/generators/rails/controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/controller/templates/controller.rb @@ -1,7 +1,7 @@ <% if namespaced? -%> -require_dependency "<%= namespaced_file_path %>/application_controller" -<% end -%> +require_dependency "<%= namespaced_path %>/application_controller" +<% end -%> <% module_namespacing do -%> class <%= class_name %>Controller < ApplicationController <% actions.each do |action| -%> diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec b/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec index 82ffeebb86..568ed653b7 100644 --- a/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec +++ b/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec @@ -22,6 +22,8 @@ Gem::Specification.new do |s| <% if full? && !options[:skip_javascript] -%> # s.add_dependency "<%= "#{options[:javascript]}-rails" %>" <% end -%> +<% unless options[:skip_active_record] -%> s.add_development_dependency "<%= gem_for_database %>" +<% end -%> end diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile b/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile index 9399c9cb77..7448b386c5 100644 --- a/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile +++ b/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile @@ -1,17 +1,32 @@ source "http://rubygems.org" +<% if options[:skip_gemspec] -%> +<%= '# ' if options.dev? || options.edge? -%>gem "rails", "~> <%= Rails::VERSION::STRING %>" +<% if full? && !options[:skip_javascript] -%> +# gem "<%= "#{options[:javascript]}-rails" %>" +<% end -%> +<% else -%> # Declare your gem's dependencies in <%= name %>.gemspec. # Bundler will treat runtime dependencies like base dependencies, and # development dependencies will be added by default to the :development group. gemspec +<% end -%> +<% unless options[:javascript] == 'jquery' -%> # jquery-rails is used by the dummy application gem "jquery-rails" +<% end -%> +<% if options[:skip_gemspec] -%> +group :development do + gem "<%= gem_for_database %>" +end +<% else -%> # Declare any dependencies that are still in development here instead of in # your gemspec. These might include edge Rails or gems from your path or # Git. Remember to move these dependencies to your gemspec before releasing # your gem to rubygems.org. +<% end -%> <% if options.dev? || options.edge? -%> # Your gem is dependent on dev or edge Rails. Once you can lock this diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile b/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile index 743036362e..1369140537 100644 --- a/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile +++ b/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile @@ -14,7 +14,7 @@ RDoc::Task.new(:rdoc) do |rdoc| rdoc.rdoc_files.include('lib/**/*.rb') end -<% if full? && !options[:skip_active_record] -%> +<% if full? && !options[:skip_active_record] && !options[:skip_test_unit] -%> APP_RAKEFILE = File.expand_path("../<%= dummy_path -%>/Rakefile", __FILE__) load 'rails/tasks/engine.rake' <% end %> diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb index 0294bde582..b3e74f9b02 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -1,7 +1,7 @@ <% if namespaced? -%> require_dependency "<%= namespaced_file_path %>/application_controller" -<% end -%> +<% end -%> <% module_namespacing do -%> class <%= controller_class_name %>Controller < ApplicationController # GET <%= route_url %> diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index c3cc65ab31..a06be59759 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -190,7 +190,8 @@ module Rails end def load_tasks(app=self) - extend Rake::DSL if defined? Rake::DSL + require 'rake' + extend Rake::DSL self.class.rake_tasks.each { |block| self.instance_exec(app, &block) } # load also tasks from all superclasses diff --git a/railties/lib/rails/tasks/documentation.rake b/railties/lib/rails/tasks/documentation.rake index 4ec49eee76..2851ca4189 100644 --- a/railties/lib/rails/tasks/documentation.rake +++ b/railties/lib/rails/tasks/documentation.rake @@ -2,7 +2,7 @@ require 'rdoc/task' # Monkey-patch to remove redoc'ing and clobber descriptions to cut down on rake -T noise class RDocTaskWithoutDescriptions < RDoc::Task - include ::Rake::DSL if defined?(::Rake::DSL) + include ::Rake::DSL def define task rdoc_task_name diff --git a/railties/lib/rails/tasks/statistics.rake b/railties/lib/rails/tasks/statistics.rake index f684e71267..67a6d2d2ac 100644 --- a/railties/lib/rails/tasks/statistics.rake +++ b/railties/lib/rails/tasks/statistics.rake @@ -3,6 +3,7 @@ STATS_DIRECTORIES = [ %w(Helpers app/helpers), %w(Models app/models), %w(Mailers app/mailers), + %w(Javascripts app/assets/javascripts), %w(Libraries lib/), %w(APIs app/apis), %w(Integration\ tests test/integration), diff --git a/railties/test/application/assets_test.rb b/railties/test/application/assets_test.rb index e23a19d69c..4243a79b58 100644 --- a/railties/test/application/assets_test.rb +++ b/railties/test/application/assets_test.rb @@ -17,9 +17,21 @@ module ApplicationTests teardown_app end - def precompile! + def precompile!(env = nil) quietly do - Dir.chdir(app_path){ `bundle exec rake assets:precompile` } + precompile_task = 'bundle exec rake assets:precompile' + precompile_task += ' ' + env if env + out = Dir.chdir(app_path){ %x[ #{precompile_task} ] } + assert $?.exitstatus == 0, + "#{precompile_task} has failed: #{out}.\ + Probably you didn't install JavaScript runtime." + return out + end + end + + def clean_assets! + quietly do + assert Dir.chdir(app_path){ system('bundle exec rake assets:clean') } end end @@ -253,9 +265,8 @@ module ApplicationTests # digest is default in false, we must enable it for test environment add_to_env_config "test", "config.assets.digest = true" - quietly do - Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_ENV=test` } - end + precompile!('RAILS_ENV=test') + file = Dir["#{app_path}/public/assets/application.css"].first assert_match(/\/assets\/rails\.png/, File.read(file)) file = Dir["#{app_path}/public/assets/application-*.css"].first @@ -285,9 +296,9 @@ module ApplicationTests add_to_config "config.assets.compile = true" ENV["RAILS_ENV"] = nil - quietly do - Dir.chdir(app_path){ `bundle exec rake assets:precompile RAILS_GROUPS=assets` } - end + + precompile!('RAILS_GROUPS=assets') + file = Dir["#{app_path}/public/assets/application-*.css"].first assert_match(/\/assets\/rails-([0-z]+)\.png/, File.read(file)) end @@ -310,9 +321,7 @@ module ApplicationTests app_file "public/assets/application.css", "a { color: green; }" app_file "public/assets/subdir/broken.png", "not really an image file" - quietly do - Dir.chdir(app_path){ `bundle exec rake assets:clean` } - end + clean_assets! files = Dir["#{app_path}/public/assets/**/*", "#{app_path}/tmp/cache/assets/*"] assert_equal 0, files.length, "Expected no assets, but found #{files.join(', ')}" @@ -440,9 +449,8 @@ module ApplicationTests add_to_config "config.assets.compile = true" add_to_config "config.assets.digest = true" - quietly do - Dir.chdir(app_path){ `bundle exec rake assets:clean assets:precompile` } - end + clean_assets! + precompile! files = Dir["#{app_path}/public/assets/application-*.js"] assert_equal 1, files.length, "Expected digested application.js asset to be generated, but none found" @@ -453,9 +461,8 @@ module ApplicationTests add_to_env_config "production", "config.assets.prefix = 'production_assets'" ENV["RAILS_ENV"] = nil - quietly do - Dir.chdir(app_path){ `bundle exec rake assets:clean` } - end + + clean_assets! files = Dir["#{app_path}/public/production_assets/application.js"] assert_equal 0, files.length, "Expected application.js asset to be removed, but still exists" diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 8579a942dd..75190372ab 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -376,7 +376,7 @@ module ApplicationTests assert_equal ActiveModel::MassAssignmentSecurity::WhiteList, ActiveRecord::Base.active_authorizers[:default].class - assert_equal [""], ActiveRecord::Base.active_authorizers[:default].to_a + assert_equal [], ActiveRecord::Base.active_authorizers[:default].to_a end test "registers interceptors with ActionMailer" do diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb index 78648a16b3..91ede1cb68 100644 --- a/railties/test/commands/console_test.rb +++ b/railties/test/commands/console_test.rb @@ -3,47 +3,44 @@ require 'rails/commands/console' class Rails::ConsoleTest < ActiveSupport::TestCase class FakeConsole + def self.start; end end def setup end def test_sandbox_option - console = Rails::Console.new(app, ["--sandbox"]) + console = Rails::Console.new(app, parse_arguments(["--sandbox"])) assert console.sandbox? end def test_short_version_of_sandbox_option - console = Rails::Console.new(app, ["-s"]) + console = Rails::Console.new(app, parse_arguments(["-s"])) assert console.sandbox? end def test_debugger_option - console = Rails::Console.new(app, ["--debugger"]) + console = Rails::Console.new(app, parse_arguments(["--debugger"])) assert console.debugger? end def test_no_options - console = Rails::Console.new(app, []) + console = Rails::Console.new(app, parse_arguments([])) assert !console.debugger? assert !console.sandbox? end def test_start - app.expects(:sandbox=).with(nil) FakeConsole.expects(:start) - start - assert_match(/Loading \w+ environment \(Rails/, output) end def test_start_with_debugger - app.expects(:sandbox=).with(nil) + rails_console = Rails::Console.new(app, parse_arguments(["--debugger"])) rails_console.expects(:require_debugger).returns(nil) - FakeConsole.expects(:start) - start ["--debugger"] + silence_stream(STDOUT) { rails_console.start } end def test_start_with_sandbox @@ -56,42 +53,63 @@ class Rails::ConsoleTest < ActiveSupport::TestCase end def test_console_with_environment - app.expects(:sandbox=).with(nil) - FakeConsole.expects(:start) - start ["-e production"] + assert_match(/\sproduction\s/, output) + end + + def test_console_defaults_to_IRB + config = mock("config", :console => nil) + app = mock("app", :config => config) + app.expects(:load_console).returns(nil) - assert_match(/production/, output) + assert_equal IRB, Rails::Console.new(app).console end - def test_console_with_rails_environment - app.expects(:sandbox=).with(nil) - FakeConsole.expects(:start) + def test_default_environment_with_no_rails_env + with_rails_env nil do + start + assert_match /\sdevelopment\s/, output + end + end - start ["RAILS_ENV=production"] + def test_default_environment_with_rails_env + with_rails_env 'special-production' do + start + assert_match /\sspecial-production\s/, output + end + end + + def test_e_option + start ['-e', 'special-production'] + assert_match /\sspecial-production\s/, output + end - assert_match(/production/, output) + def test_environment_option + start ['--environment=special-production'] + assert_match /\sspecial-production\s/, output end + def test_rails_env_is_production_when_first_argument_is_p + start ['p'] + assert_match /\sproduction\s/, output + end - def test_console_defaults_to_IRB - config = mock("config", :console => nil) - app = mock("app", :config => config) - app.expects(:load_console).returns(nil) + def test_rails_env_is_test_when_first_argument_is_t + start ['t'] + assert_match /\stest\s/, output + end - assert_equal IRB, Rails::Console.new(app).console + def test_rails_env_is_development_when_argument_is_d + start ['d'] + assert_match /\sdevelopment\s/, output end private attr_reader :output - def rails_console - @rails_console ||= Rails::Console.new(app) - end - def start(argv = []) - rails_console.stubs(:arguments => argv) + rails_console = Rails::Console.new(app, parse_arguments(argv)) @output = output = capture(:stdout) { rails_console.start } end @@ -99,8 +117,21 @@ class Rails::ConsoleTest < ActiveSupport::TestCase @app ||= begin config = mock("config", :console => FakeConsole) app = mock("app", :config => config) + app.stubs(:sandbox=).returns(nil) app.expects(:load_console) app end end + + def parse_arguments(args) + Rails::Console.parse_arguments(args) + end + + def with_rails_env(env) + original_rails_env = ENV['RAILS_ENV'] + ENV['RAILS_ENV'] = env + yield + ensure + ENV['RAILS_ENV'] = original_rails_env + end end diff --git a/railties/test/commands/dbconsole_test.rb b/railties/test/commands/dbconsole_test.rb index 562b83713b..d45bdaabf5 100644 --- a/railties/test/commands/dbconsole_test.rb +++ b/railties/test/commands/dbconsole_test.rb @@ -10,35 +10,37 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase Rails::DBConsole.const_set(:APP_PATH, "erb") app_config({}) - capture_abort { Rails::DBConsole.config } + capture_abort { Rails::DBConsole.new.config } assert aborted assert_match(/No database is configured for the environment '\w+'/, output) app_config(test: "with_init") - assert_equal Rails::DBConsole.config, "with_init" + assert_equal Rails::DBConsole.new.config, "with_init" app_db_file("test:\n without_init") - assert_equal Rails::DBConsole.config, "without_init" + assert_equal Rails::DBConsole.new.config, "without_init" app_db_file("test:\n <%= Rails.something_app_specific %>") - assert_equal Rails::DBConsole.config, "with_init" + assert_equal Rails::DBConsole.new.config, "with_init" app_db_file("test:\n\ninvalid") - assert_equal Rails::DBConsole.config, "with_init" + assert_equal Rails::DBConsole.new.config, "with_init" end def test_env - assert_equal Rails::DBConsole.env, "test" + assert_equal Rails::DBConsole.new.environment, "test" + + ENV['RAILS_ENV'] = nil + ENV['RACK_ENV'] = nil Rails.stubs(:respond_to?).with(:env).returns(false) - assert_equal Rails::DBConsole.env, "test" + assert_equal Rails::DBConsole.new.environment, "development" - ENV['RAILS_ENV'] = nil ENV['RACK_ENV'] = "rack_env" - assert_equal Rails::DBConsole.env, "rack_env" + assert_equal Rails::DBConsole.new.environment, "rack_env" ENV['RAILS_ENV'] = "rails_env" - assert_equal Rails::DBConsole.env, "rails_env" + assert_equal Rails::DBConsole.new.environment, "rails_env" ensure ENV['RAILS_ENV'] = "test" end diff --git a/railties/test/commands/server_test.rb b/railties/test/commands/server_test.rb index 8039aec873..4a3ea82e3d 100644 --- a/railties/test/commands/server_test.rb +++ b/railties/test/commands/server_test.rb @@ -4,14 +4,14 @@ require 'rails/commands/server' class Rails::ServerTest < ActiveSupport::TestCase def test_environment_with_server_option - args = ["thin", "RAILS_ENV=production"] + args = ["thin", "-e", "production"] options = Rails::Server::Options.new.parse!(args) assert_equal 'production', options[:environment] assert_equal 'thin', options[:server] end def test_environment_without_server_option - args = ["RAILS_ENV=production"] + args = ["-e", "production"] options = Rails::Server::Options.new.parse!(args) assert_equal 'production', options[:environment] assert_nil options[:server] diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 26e912fd9e..5534476a6d 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -246,7 +246,7 @@ class AppGeneratorTest < Rails::Generators::TestCase if defined?(JRUBY_VERSION) assert_gem "therubyrhino" else - assert_file "Gemfile", /# gem\s+["']therubyracer["']+, platform: :ruby$/ + assert_file "Gemfile", /# gem\s+["']therubyracer["']+, platforms: :ruby$/ end end diff --git a/railties/test/generators/namespaced_generators_test.rb b/railties/test/generators/namespaced_generators_test.rb index 09169ef2d2..2ae9dc61a7 100644 --- a/railties/test/generators/namespaced_generators_test.rb +++ b/railties/test/generators/namespaced_generators_test.rb @@ -38,7 +38,9 @@ class NamespacedControllerGeneratorTest < NamespacedGeneratorTestCase def test_namespaced_controller_with_additional_namespace run_generator ["admin/account"] - assert_file "app/controllers/test_app/admin/account_controller.rb", /module TestApp/, / class Admin::AccountController < ApplicationController/ + assert_file "app/controllers/test_app/admin/account_controller.rb", /module TestApp/, / class Admin::AccountController < ApplicationController/ do |contents| + assert_match %r(require_dependency "test_app/application_controller"), contents + end end def test_helpr_is_also_namespaced diff --git a/railties/test/generators/plugin_new_generator_test.rb b/railties/test/generators/plugin_new_generator_test.rb index 58740978aa..0a235b56d5 100644 --- a/railties/test/generators/plugin_new_generator_test.rb +++ b/railties/test/generators/plugin_new_generator_test.rb @@ -58,6 +58,14 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase assert_file "test/integration/navigation_test.rb", /ActionDispatch::IntegrationTest/ end + def test_generating_test_files_in_full_mode_without_unit_test_files + run_generator [destination_root, "-T", "--full"] + + assert_no_directory "test/integration/" + assert_no_file "test" + assert_no_match(/APP_RAKEFILE/, File.read(File.join(destination_root, "Rakefile"))) + end + def test_ensure_that_plugin_options_are_not_passed_to_app_generator FileUtils.cd(Rails.root) assert_no_match(/It works from file!.*It works_from_file/, run_generator([destination_root, "-m", "lib/template.rb"])) @@ -82,6 +90,14 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase assert_file "bukkits.gemspec", /mysql/ end + def test_dont_generate_development_dependency + run_generator [destination_root, "--skip-active-record"] + + assert_file "bukkits.gemspec" do |contents| + assert_no_match(/s.add_development_dependency "sqlite3"/, contents) + end + end + def test_active_record_is_removed_from_frameworks_if_skip_active_record_is_given run_generator [destination_root, "--skip-active-record"] assert_file "test/dummy/config/application.rb", /#\s+require\s+["']active_record\/railtie["']/ @@ -269,6 +285,32 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase def test_skipping_gemspec run_generator [destination_root, "--skip-gemspec"] assert_no_file "bukkits.gemspec" + assert_file "Gemfile" do |contents| + assert_no_match('gemspec', contents) + assert_match(/gem "rails", "~> #{Rails::VERSION::STRING}"/, contents) + assert_match(/group :development do\n gem "sqlite3"\nend/, contents) + assert_no_match(/# gem "jquery-rails"/, contents) + end + end + + def test_skipping_gemspec_in_full_mode + run_generator [destination_root, "--skip-gemspec", "--full"] + assert_no_file "bukkits.gemspec" + assert_file "Gemfile" do |contents| + assert_no_match('gemspec', contents) + assert_match(/gem "rails", "~> #{Rails::VERSION::STRING}"/, contents) + assert_match(/group :development do\n gem "sqlite3"\nend/, contents) + assert_match(/# gem "jquery-rails"/, contents) + assert_no_match(/# jquery-rails is used by the dummy application\ngem "jquery-rails"/, contents) + end + end + + def test_skipping_gemspec_in_full_mode_with_javascript_option + run_generator [destination_root, "--skip-gemspec", "--full", "--javascript=prototype"] + assert_file "Gemfile" do |contents| + assert_match(/# gem "prototype-rails"/, contents) + assert_match(/# jquery-rails is used by the dummy application\ngem "jquery-rails"/, contents) + end end def test_creating_plugin_in_app_directory_adds_gemfile_entry @@ -303,12 +345,10 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase protected - def action(*args, &block) silence(:stdout){ generator.send(*args, &block) } end -protected def default_files ::DEFAULT_PLUGIN_FILES end diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 4437e2c8af..52c7fae6c6 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -1009,9 +1009,7 @@ YAML boot_rails - methods = Bukkits::Engine.helpers.public_instance_methods.collect(&:to_s).sort - expected = ["bar", "baz"] - assert_equal expected, methods + assert_equal [:bar, :baz], Bukkits::Engine.helpers.public_instance_methods.sort end test "setting priority for engines with config.railties_order" do |