Skip to content

Commit

Permalink
Prevent the test framework from being loaded in production mode
Browse files Browse the repository at this point in the history
The test framework should not be autoloaded in production mode.  Before
this commit, the testing railtie would extend AS::TestCase.  This caused
AS::TestCase to be preloaded regardless of the environment in which we
were running.

This commit just moves the code that adds line filtering support in to
the test command where we actually execute the test runner.  That allows
us to maintain the line runner feature but only load the minimal amount
of code we need.
  • Loading branch information
tenderlove committed Oct 21, 2016
1 parent 54a5b99 commit 797f1dd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions railties/lib/rails/commands/test/test_command.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "rails/command"
require "rails/test_unit/minitest_plugin"
require "rails/test_unit/line_filtering"

module Rails
module Command
Expand All @@ -11,6 +12,10 @@ def help # :nodoc:
def perform(*)
$LOAD_PATH << Rails::Command.root.join("test")

# Add test line filtering support for running test by line number
# via the command line.
ActiveSupport::TestCase.extend Rails::LineFiltering

Minitest.run_via[:rails] = true

require "active_support/testing/autorun"
Expand Down
6 changes: 0 additions & 6 deletions railties/lib/rails/test_unit/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "rails/test_unit/line_filtering"

if defined?(Rake.application) && Rake.application.top_level_tasks.grep(/^(default$|test(:|$))/).any?
ENV["RAILS_ENV"] ||= "test"
end
Expand All @@ -13,10 +11,6 @@ class TestUnitRailtie < Rails::Railtie
c.integration_tool :test_unit
end

initializer "test_unit.line_filtering" do
ActiveSupport::TestCase.extend Rails::LineFiltering
end

rake_tasks do
load "rails/test_unit/testing.rake"
end
Expand Down
9 changes: 9 additions & 0 deletions railties/test/application/runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def test_should_run_file
assert_match "42", Dir.chdir(app_path) { `bin/rails runner "bin/count_users.rb"` }
end

def test_no_minitest_loaded_in_production_mode
app_file "bin/print_features.rb", <<-SCRIPT
p $LOADED_FEATURES.grep(/minitest/)
SCRIPT
assert_match "[]", Dir.chdir(app_path) {
`RAILS_ENV=production bin/rails runner "bin/print_features.rb"`
}
end

def test_should_set_dollar_0_to_file
app_file "bin/dollar0.rb", <<-SCRIPT
puts $0
Expand Down

0 comments on commit 797f1dd

Please sign in to comment.