From b4df25366a3c8f133f8329bc35f1d53926704b5a Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist and Chris Toomey Date: Fri, 25 Jan 2013 13:44:36 -0500 Subject: Add `rails test` command to run the test suite To run the whole test suite: $ rails test To run the test file(s): $ rails test test/unit/foo_test.rb [test/unit/bar_test.rb ...] To run the test suite $ rails test [models,helpers,units,controllers,mailers,...] For more information, see `rails test --help`. This command will eventually replacing `rake test:*`, and `rake test` command will actually invoking `rails test` instead. --- railties/lib/rails/commands/test_runner.rb | 92 ++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 railties/lib/rails/commands/test_runner.rb (limited to 'railties/lib/rails/commands') diff --git a/railties/lib/rails/commands/test_runner.rb b/railties/lib/rails/commands/test_runner.rb new file mode 100644 index 0000000000..a65b508505 --- /dev/null +++ b/railties/lib/rails/commands/test_runner.rb @@ -0,0 +1,92 @@ +require 'optparse' +require 'minitest/unit' + +module Rails + # Handling the all the logic behind +rails test+ command. + class TestRunner + class << self + # Parse the test suite name from the arguments array and pass in a list + # of file to a new +TestRunner+ object, then invoke the evaluation. If + # the argument is not a test suite name, it will be treated as a file + # name and passed to the +TestRunner+ instance right away. + def start(arguments) + case arguments.first + when nil + new(Dir['test/**/*_test.rb']).run + when 'models' + new(Dir['test/models/**/*_test.rb']).run + when 'helpers' + new(Dir['test/helpers/**/*_test.rb']).run + when 'units' + new(Dir['test/{models,helpers,unit}/**/*_test.rb']).run + when 'controllers' + new(Dir['test/controllers/**/*_test.rb']).run + when 'mailers' + new(Dir['test/mailers/**/*_test.rb']).run + when 'functionals' + new(Dir['test/{controllers,mailers,functional}/**/*_test.rb']).run + when 'integration' + new(Dir['test/integration/**/*_test.rb']).run + else + new(arguments).run + end + end + + # Print out the help message which listed all of the test suite names. + def help_message + puts "Usage: rails test [path to test file(s) or test suite type]" + puts "" + puts "Run single test file, or a test suite, under Rails'" + puts "environment. If the file name(s) or suit name is omitted," + puts "Rails will run all the test suites." + puts "" + puts "Support types of test suites:" + puts "-------------------------------------------------------------" + puts "* models (test/models/**/*)" + puts "* helpers (test/helpers/**/*)" + puts "* units (test/{models,helpers,unit}/**/*" + puts "* controllers (test/controllers/**/*)" + puts "* mailers (test/mailers/**/*)" + puts "* functionals (test/{controllers,mailers,functional}/**/*)" + puts "* integration (test/integration/**/*)" + puts "-------------------------------------------------------------" + end + end + + # Create a new +TestRunner+ object with a list of test file paths. + def initialize(files) + @files = files + Rake::Task['test:prepare'].invoke + MiniTest::Unit.output = SilentUntilSyncStream.new(MiniTest::Unit.output) + end + + # Run the test files by evaluate each of them. + def run + @files.each { |filename| load(filename) } + end + + # A null stream object which ignores everything until +sync+ has been set + # to true. This is only to be used to silence unnecessary output from + # MiniTest, as MiniTest calls +output.sync = true+ right before output the + # first test result. + class SilentUntilSyncStream < File + # Create a +SilentUntilSyncStream+ object by given a stream object that + # this stream should set +MiniTest::Unit.output+ to after +sync+ has been + # set to true. + def initialize(target_stream) + @target_stream = target_stream + super(File::NULL, 'w') + end + + # Swap +MiniTest::Unit.output+ to another stream when +sync+ is true. + def sync=(sync) + if sync + @target_stream.sync = true + MiniTest::Unit.output = @target_stream + end + + super + end + end + end +end -- cgit v1.2.3 From 176b57c5430ddae7668114994c35b3293b0a05a5 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Fri, 1 Feb 2013 17:21:09 -0500 Subject: Add support for MiniTest flags in TestRunner Any flags that got set will be passed through to MiniTest::Unit.runner, such as `-n`, `-s-, and `-v`. --- railties/lib/rails/commands/test_runner.rb | 91 ++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 30 deletions(-) (limited to 'railties/lib/rails/commands') diff --git a/railties/lib/rails/commands/test_runner.rb b/railties/lib/rails/commands/test_runner.rb index a65b508505..33c32bb94f 100644 --- a/railties/lib/rails/commands/test_runner.rb +++ b/railties/lib/rails/commands/test_runner.rb @@ -9,54 +9,85 @@ module Rails # of file to a new +TestRunner+ object, then invoke the evaluation. If # the argument is not a test suite name, it will be treated as a file # name and passed to the +TestRunner+ instance right away. - def start(arguments) - case arguments.first + def start(files, options) + case files.first when nil - new(Dir['test/**/*_test.rb']).run + new(Dir['test/**/*_test.rb'], options).run when 'models' - new(Dir['test/models/**/*_test.rb']).run + new(Dir['test/models/**/*_test.rb'], options).run when 'helpers' - new(Dir['test/helpers/**/*_test.rb']).run + new(Dir['test/helpers/**/*_test.rb'], options).run when 'units' - new(Dir['test/{models,helpers,unit}/**/*_test.rb']).run + new(Dir['test/{models,helpers,unit}/**/*_test.rb'], options).run when 'controllers' - new(Dir['test/controllers/**/*_test.rb']).run + new(Dir['test/controllers/**/*_test.rb'], options).run when 'mailers' - new(Dir['test/mailers/**/*_test.rb']).run + new(Dir['test/mailers/**/*_test.rb'], options).run when 'functionals' - new(Dir['test/{controllers,mailers,functional}/**/*_test.rb']).run + new(Dir['test/{controllers,mailers,functional}/**/*_test.rb'], options).run when 'integration' - new(Dir['test/integration/**/*_test.rb']).run + new(Dir['test/integration/**/*_test.rb'], options).run else - new(arguments).run + new(files, options).run end end - # Print out the help message which listed all of the test suite names. - def help_message - puts "Usage: rails test [path to test file(s) or test suite type]" - puts "" - puts "Run single test file, or a test suite, under Rails'" - puts "environment. If the file name(s) or suit name is omitted," - puts "Rails will run all the test suites." - puts "" - puts "Support types of test suites:" - puts "-------------------------------------------------------------" - puts "* models (test/models/**/*)" - puts "* helpers (test/helpers/**/*)" - puts "* units (test/{models,helpers,unit}/**/*" - puts "* controllers (test/controllers/**/*)" - puts "* mailers (test/mailers/**/*)" - puts "* functionals (test/{controllers,mailers,functional}/**/*)" - puts "* integration (test/integration/**/*)" - puts "-------------------------------------------------------------" + # Parse arguments and set them as option flags + def parse_arguments(arguments) + options = {} + orig_arguments = arguments.dup + + OptionParser.new do |opts| + opts.banner = "Usage: rails test [path to test file(s) or test suite type]" + + opts.separator "" + opts.separator "Run single test file, or a test suite, under Rails'" + opts.separator "environment. If the file name(s) or suit name is omitted," + opts.separator "Rails will run all the test suites." + opts.separator "" + opts.separator "Specific options:" + + opts.on '-h', '--help', 'Display this help.' do + puts opts + exit + end + + opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m| + options[:seed] = m.to_i + end + + opts.on '-v', '--verbose', "Verbose. Show progress processing files." do + options[:verbose] = true + end + + opts.on '-n', '--name PATTERN', "Filter test names on pattern (e.g. /foo/)" do |a| + options[:filter] = a + end + + opts.separator "" + opts.separator "Support types of test suites:" + opts.separator "-------------------------------------------------------------" + opts.separator "* models (test/models/**/*)" + opts.separator "* helpers (test/helpers/**/*)" + opts.separator "* units (test/{models,helpers,unit}/**/*" + opts.separator "* controllers (test/controllers/**/*)" + opts.separator "* mailers (test/mailers/**/*)" + opts.separator "* functionals (test/{controllers,mailers,functional}/**/*)" + opts.separator "* integration (test/integration/**/*)" + opts.separator "-------------------------------------------------------------" + + opts.parse! arguments + orig_arguments -= arguments + end + options end end # Create a new +TestRunner+ object with a list of test file paths. - def initialize(files) + def initialize(files, options) @files = files Rake::Task['test:prepare'].invoke + MiniTest::Unit.runner.options = options MiniTest::Unit.output = SilentUntilSyncStream.new(MiniTest::Unit.output) end -- cgit v1.2.3 From 1a0c58b2988a24a783b4f9a658ac629922125551 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Wed, 6 Feb 2013 01:03:17 -0500 Subject: Load fixtures only when running suites, or `-f` * `rails test -f` will run the test suites with all fixtures loaded * New application will now generated without `fixtures :all` line enabled by default. --- railties/lib/rails/commands/test_runner.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'railties/lib/rails/commands') diff --git a/railties/lib/rails/commands/test_runner.rb b/railties/lib/rails/commands/test_runner.rb index 33c32bb94f..ae901f05f8 100644 --- a/railties/lib/rails/commands/test_runner.rb +++ b/railties/lib/rails/commands/test_runner.rb @@ -9,7 +9,10 @@ module Rails # of file to a new +TestRunner+ object, then invoke the evaluation. If # the argument is not a test suite name, it will be treated as a file # name and passed to the +TestRunner+ instance right away. - def start(files, options) + def start(files, options = {}) + original_fixtures_options = options.delete(:fixtures) + options[:fixtures] = true + case files.first when nil new(Dir['test/**/*_test.rb'], options).run @@ -28,6 +31,7 @@ module Rails when 'integration' new(Dir['test/integration/**/*_test.rb'], options).run else + options[:fixtures] = original_fixtures_options new(files, options).run end end @@ -52,6 +56,10 @@ module Rails exit end + opts.on '-f', '--fixtures', 'Load fixtures in test/fixtures/ before running the tests' do + options[:fixtures] = true + end + opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m| options[:seed] = m.to_i end @@ -87,6 +95,15 @@ module Rails def initialize(files, options) @files = files Rake::Task['test:prepare'].invoke + + if options.delete(:fixtures) + if defined?(ActiveRecord::Base) + ActiveSupport::TestCase.send :include, ActiveRecord::TestFixtures + ActiveSupport::TestCase.fixture_path = "#{Rails.root}/test/fixtures/" + ActiveSupport::TestCase.fixtures :all + end + end + MiniTest::Unit.runner.options = options MiniTest::Unit.output = SilentUntilSyncStream.new(MiniTest::Unit.output) end -- cgit v1.2.3 From df85dfa6fa6f7ae9a0b72eb9b9a254d2d5560f38 Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic Date: Fri, 8 Feb 2013 00:53:11 +0100 Subject: Improve wording for rails test command --- railties/lib/rails/commands/test_runner.rb | 34 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'railties/lib/rails/commands') diff --git a/railties/lib/rails/commands/test_runner.rb b/railties/lib/rails/commands/test_runner.rb index ae901f05f8..92c4d4e593 100644 --- a/railties/lib/rails/commands/test_runner.rb +++ b/railties/lib/rails/commands/test_runner.rb @@ -2,13 +2,13 @@ require 'optparse' require 'minitest/unit' module Rails - # Handling the all the logic behind +rails test+ command. + # Handles all logic behind +rails test+ command. class TestRunner class << self - # Parse the test suite name from the arguments array and pass in a list - # of file to a new +TestRunner+ object, then invoke the evaluation. If - # the argument is not a test suite name, it will be treated as a file - # name and passed to the +TestRunner+ instance right away. + # Creates a new +TestRunner+ object with an array of test files to run + # based on the arguments. When no arguments are provided, it runs all test + # files. When a suite argument is provided, it runs only the test files in + # that suite. Otherwise, it runs the specified test file(s). def start(files, options = {}) original_fixtures_options = options.delete(:fixtures) options[:fixtures] = true @@ -36,18 +36,18 @@ module Rails end end - # Parse arguments and set them as option flags + # Parses arguments and sets them as option flags def parse_arguments(arguments) options = {} orig_arguments = arguments.dup OptionParser.new do |opts| - opts.banner = "Usage: rails test [path to test file(s) or test suite type]" + opts.banner = "Usage: rails test [path to test file(s) or test suite]" opts.separator "" - opts.separator "Run single test file, or a test suite, under Rails'" + opts.separator "Run a specific test file(s) or a test suite, under Rails'" opts.separator "environment. If the file name(s) or suit name is omitted," - opts.separator "Rails will run all the test suites." + opts.separator "Rails will run all tests." opts.separator "" opts.separator "Specific options:" @@ -91,7 +91,7 @@ module Rails end end - # Create a new +TestRunner+ object with a list of test file paths. + # Creates a new +TestRunner+ object with a list of test file paths. def initialize(files, options) @files = files Rake::Task['test:prepare'].invoke @@ -108,25 +108,25 @@ module Rails MiniTest::Unit.output = SilentUntilSyncStream.new(MiniTest::Unit.output) end - # Run the test files by evaluate each of them. + # Runs test files by evaluating each of them. def run @files.each { |filename| load(filename) } end # A null stream object which ignores everything until +sync+ has been set - # to true. This is only to be used to silence unnecessary output from - # MiniTest, as MiniTest calls +output.sync = true+ right before output the - # first test result. + # to true. This is only used to silence unnecessary output from MiniTest, + # as MiniTest calls +output.sync = true+ right before it outputs the first + # test result. class SilentUntilSyncStream < File - # Create a +SilentUntilSyncStream+ object by given a stream object that - # this stream should set +MiniTest::Unit.output+ to after +sync+ has been + # Creates a +SilentUntilSyncStream+ object by giving it a target stream + # object that will be assigned to +MiniTest::Unit.output+ after +sync+ is # set to true. def initialize(target_stream) @target_stream = target_stream super(File::NULL, 'w') end - # Swap +MiniTest::Unit.output+ to another stream when +sync+ is true. + # Swaps +MiniTest::Unit.output+ to another stream when +sync+ is true. def sync=(sync) if sync @target_stream.sync = true -- cgit v1.2.3 From 3ed41e579e45464aa6e6342783b77f9ec29e339c Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Sat, 9 Mar 2013 16:03:09 -0500 Subject: Make sure that `rails test` load test in test env --- railties/lib/rails/commands/test_runner.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'railties/lib/rails/commands') diff --git a/railties/lib/rails/commands/test_runner.rb b/railties/lib/rails/commands/test_runner.rb index 92c4d4e593..d8857bd183 100644 --- a/railties/lib/rails/commands/test_runner.rb +++ b/railties/lib/rails/commands/test_runner.rb @@ -56,6 +56,10 @@ module Rails exit end + opts.on '-e', '--environment NAME', String, 'Specifies the environment to run this test under' do |e| + options[:environment] = e + end + opts.on '-f', '--fixtures', 'Load fixtures in test/fixtures/ before running the tests' do options[:fixtures] = true end @@ -68,8 +72,8 @@ module Rails options[:verbose] = true end - opts.on '-n', '--name PATTERN', "Filter test names on pattern (e.g. /foo/)" do |a| - options[:filter] = a + opts.on '-n', '--name PATTERN', "Filter test names on pattern (e.g. /foo/)" do |n| + options[:filter] = n end opts.separator "" @@ -94,7 +98,9 @@ module Rails # Creates a new +TestRunner+ object with a list of test file paths. def initialize(files, options) @files = files - Rake::Task['test:prepare'].invoke + + Rails.application.load_tasks + Rake::Task['db:test:load'].invoke if options.delete(:fixtures) if defined?(ActiveRecord::Base) -- cgit v1.2.3 From d25a82280f977664e3b3d49c13bb68502718f6e2 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Tue, 12 Mar 2013 10:09:03 -0300 Subject: Stop calling "super" twice in Rails::Server#app Cache the value of "super" in a variable and use it instead. --- railties/lib/rails/commands/server.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'railties/lib/rails/commands') diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb index cdb29a8156..ddf45d196a 100644 --- a/railties/lib/rails/commands/server.rb +++ b/railties/lib/rails/commands/server.rb @@ -43,7 +43,10 @@ module Rails end def app - @app ||= super.respond_to?(:to_app) ? super.to_app : super + @app ||= begin + app = super + app.respond_to?(:to_app) ? app.to_app : app + end end def opt_parser -- cgit v1.2.3 From 9b5c0850778207998a5cb8e5fe5bde6599af81d5 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Mon, 11 Mar 2013 20:46:21 +0100 Subject: config.ru uses the effective Rack app We used to pass the Rails::Application subclass to #run. The Rails server then called #to_app to convert that class to the actual Rack application. if you surround `#run` with a call to `#map` the server no longer convertes the class to the instance and we end up with unnecessary delegation calls on every request. --- railties/lib/rails/commands/server.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'railties/lib/rails/commands') diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb index ddf45d196a..876d457359 100644 --- a/railties/lib/rails/commands/server.rb +++ b/railties/lib/rails/commands/server.rb @@ -42,6 +42,7 @@ module Rails set_environment end + # TODO: this is no longer required but we keep it for the moment to support older config.ru files. def app @app ||= begin app = super -- cgit v1.2.3 From b0557389de13103c32baee438bf2d58f40176f73 Mon Sep 17 00:00:00 2001 From: Gabe Kopley Date: Mon, 18 Mar 2013 10:42:24 -0700 Subject: More helpful message when starting server v2 of pull request based on feedback from @rafaelfranca, @schneems, and @carlosantoniodasilva --- railties/lib/rails/commands/server.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib/rails/commands') diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb index 876d457359..e3119ecf22 100644 --- a/railties/lib/rails/commands/server.rb +++ b/railties/lib/rails/commands/server.rb @@ -62,7 +62,7 @@ module Rails url = "#{options[:SSLEnable] ? 'https' : 'http'}://#{options[:Host]}:#{options[:Port]}" puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}" puts "=> Rails #{Rails.version} application starting in #{Rails.env} on #{url}" - puts "=> Call with -d to detach" unless options[:daemonize] + puts "=> Run `rails server -h` for more startup options" trap(:INT) { exit } puts "=> Ctrl-C to shutdown server" unless options[:daemonize] -- cgit v1.2.3 From f0e6ab59f681cb400043d51cb259fbd84920df09 Mon Sep 17 00:00:00 2001 From: Charlie Somerville Date: Wed, 20 Mar 2013 20:48:58 +1100 Subject: remove references to *::VERSION, replace with *.version --- railties/lib/rails/commands/application.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib/rails/commands') diff --git a/railties/lib/rails/commands/application.rb b/railties/lib/rails/commands/application.rb index 2ff29418c6..d7b8ff4439 100644 --- a/railties/lib/rails/commands/application.rb +++ b/railties/lib/rails/commands/application.rb @@ -1,7 +1,7 @@ require 'rails/version' if ['--version', '-v'].include?(ARGV.first) - puts "Rails #{Rails::VERSION::STRING}" + puts "Rails #{Rails.version}" exit(0) end -- cgit v1.2.3