From 0835527d6bb970cedd33633503506e41156ab780 Mon Sep 17 00:00:00 2001 From: bogdanvlviv Date: Mon, 7 Aug 2017 04:31:14 +0000 Subject: `rails new` runs `rails active_storage:install` Omit `rails activestorage:install` for jdbcmysql, jdbc and shebang tests AppGeneratorTest#test_config_jdbcmysql_database rails aborted! LoadError: Could not load 'active_record/connection_adapters/mysql_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile. (compressed) bin/rails:4:in `
' Tasks: TOP => activestorage:install => environment (See full trace by running task with --trace) AppGeneratorTest#test_config_jdbc_database rails aborted! LoadError: Could not load 'active_record/connection_adapters/jdbc_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile. (compressed) bin/rails:4:in `
' Tasks: TOP => activestorage:install => environment (See full trace by running task with --trace) AppGeneratorTest#test_shebang_is_added_to_rails_file /home/ubuntu/.rbenv/versions/2.4.1/bin/ruby: no Ruby script found in input (LoadError) Prevent PendingMigrationError in tests * Run `bin/rails db:migrate RAILS_ENV=test` in test_cases before start tests to prevent PendingMigrationError * FileUtils.rm_r("db/migrate") * --skip-active-storage Fix failed tests in `railties/test/railties/engine_test.rb` Related to #30111 Imporve `SharedGeneratorTests#test_default_frameworks_are_required_when_others_are_removed` - Explicitly skip active_storage - Ensure that skipped frameworks are commented - Ensure that default frameworks are not commented Fix error `Errno::ENOSPC: No space left on device - sendfile` Since `rails new` runs `rails active_storage:install` that boots an app. Since adding Bootsnap 0312a5c67e35b960e33677b5358c539f1047e4e1 during booting an app, it creates the cache: 264K tmp/cache/bootsnap-load-path-cache 27M tmp/cache/bootsnap-compile-cache * teardown_app must remove app --- railties/test/application/test_runner_test.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'railties/test/application/test_runner_test.rb') diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index e92a0466dd..30bd283b48 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -11,6 +11,7 @@ module ApplicationTests def setup build_app create_schema + remove_migrations end def teardown @@ -727,6 +728,10 @@ module ApplicationTests app_file "db/schema.rb", "" end + def remove_migrations + Dir.chdir(app_path) { FileUtils.rm_rf("db/migrate") } + end + def create_test_file(path = :unit, name = "test", pass: true) app_file "test/#{path}/#{name}_test.rb", <<-RUBY require 'test_helper' -- cgit v1.2.3 From 8e1dca10cd98bdc8bb00592f3d90926309b22a18 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Wed, 8 Nov 2017 12:32:12 +0900 Subject: Remove unnecessary migration deletion Since isolation application is generated with the `--skip-gemfile` option, so `active_storage:install` is not executed. --- railties/test/application/test_runner_test.rb | 5 ----- 1 file changed, 5 deletions(-) (limited to 'railties/test/application/test_runner_test.rb') diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index 30bd283b48..e92a0466dd 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -11,7 +11,6 @@ module ApplicationTests def setup build_app create_schema - remove_migrations end def teardown @@ -728,10 +727,6 @@ module ApplicationTests app_file "db/schema.rb", "" end - def remove_migrations - Dir.chdir(app_path) { FileUtils.rm_rf("db/migrate") } - end - def create_test_file(path = :unit, name = "test", pass: true) app_file "test/#{path}/#{name}_test.rb", <<-RUBY require 'test_helper' -- cgit v1.2.3 From da225c0db640aec1df0920e86c1eb4ca35d82073 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 6 Dec 2017 17:33:16 -0800 Subject: Fix Rails environment when running tests with Ruby I frequently run tests with `ruby`, not with a runner like `rake` or `rails`. When running the test with just `ruby` the `RAILS_ENV` environment variable did not get set to "test", and this would cause the tests to fail (and even mutate the development database!) This commit adds integration tests for running tests with just `ruby` and ensures the environment gets defaulted to "test". I also added a test to ensure that passing an environment to `-e` actually works (and fixed that case too). An interesting / annoying thing is that Minitest picks up it's plugins by asking RubyGems for a list of files: https://github.com/seattlerb/minitest/blob/ca6a71ca901016db09a5ad466b4adea4b52a504a/lib/minitest.rb#L92-L100 This means that RubyGems needs to somehow know about the file before it can return it to Minitest. Since we are not packaging Rails as a Gem before running the integration tests on it (duh, why would you do that?), RubyGems doesn't know about the file, so it can't tell Minitest, so Minitest doesn't automatically require it. This means I had to manually require and insert the plugin in our integration test. I've left comments about that in the test as well. Ugh. --- railties/test/application/test_runner_test.rb | 56 +++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'railties/test/application/test_runner_test.rb') diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index e92a0466dd..27983c3b55 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -569,6 +569,26 @@ module ApplicationTests assert_match "AccountTest#test_truth", output, "passing TEST= should run selected test" end + def test_running_with_ruby_gets_test_env_by_default + file = create_test_for_env("test") + results = Dir.chdir(app_path) { + `ruby -Ilib:test #{file}`.each_line.map { |line| JSON.parse line } + } + assert_equal 1, results.length + failures = results.first["failures"] + flunk(failures.first) unless failures.empty? + end + + def test_running_with_ruby_can_set_env_via_cmdline + file = create_test_for_env("development") + results = Dir.chdir(app_path) { + `ruby -Ilib:test #{file} -e development`.each_line.map { |line| JSON.parse line } + } + assert_equal 1, results.length + failures = results.first["failures"] + flunk(failures.first) unless failures.empty? + end + def test_rake_passes_multiple_TESTOPTS_to_minitest create_test_file :models, "account" output = Dir.chdir(app_path) { `bin/rake test TESTOPTS='-v --seed=1234'` } @@ -727,6 +747,42 @@ module ApplicationTests app_file "db/schema.rb", "" end + def create_test_for_env(env) + app_file "test/models/environment_test.rb", <<-RUBY + require 'test_helper' + class JSONReporter < Minitest::AbstractReporter + def record(result) + puts JSON.dump(klass: result.class.name, + name: result.name, + failures: result.failures, + assertions: result.assertions, + time: result.time) + end + end + + def Minitest.plugin_json_reporter_init(opts) + Minitest.reporter.reporters.clear + Minitest.reporter.reporters << JSONReporter.new + end + + Minitest.extensions << "rails" + Minitest.extensions << "json_reporter" + + # Minitest uses RubyGems to find plugins, and since RubyGems + # doesn't know about the Rails installation we're pointing at, + # Minitest won't require the Rails minitest plugin when we run + # these integration tests. So we have to manually require the + # Minitest plugin here. + require 'minitest/rails_plugin' + + class EnvironmentTest < ActiveSupport::TestCase + def test_environment + assert_equal #{env.dump}, ENV["RAILS_ENV"] + end + end + RUBY + end + def create_test_file(path = :unit, name = "test", pass: true) app_file "test/#{path}/#{name}_test.rb", <<-RUBY require 'test_helper' -- cgit v1.2.3 From a58543dbb1ea52f3cb0c98d054ffd7bc7a373765 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 8 Dec 2017 13:23:31 -0800 Subject: Add failing test for wrong database connection When tests are run with just `ruby`, the RAILS_ENV is set to `development` too early, and we connect to the development database rather than the test database. --- railties/test/application/test_runner_test.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'railties/test/application/test_runner_test.rb') diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index 27983c3b55..17333b6ac9 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -570,6 +570,10 @@ module ApplicationTests end def test_running_with_ruby_gets_test_env_by_default + # Subshells inherit `ENV`, so we need to ensure `RAILS_ENV` is set to + # nil before we run the tests in the test app. + re, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], nil + file = create_test_for_env("test") results = Dir.chdir(app_path) { `ruby -Ilib:test #{file}`.each_line.map { |line| JSON.parse line } @@ -577,9 +581,16 @@ module ApplicationTests assert_equal 1, results.length failures = results.first["failures"] flunk(failures.first) unless failures.empty? + + ensure + ENV["RAILS_ENV"] = re end def test_running_with_ruby_can_set_env_via_cmdline + # Subshells inherit `ENV`, so we need to ensure `RAILS_ENV` is set to + # nil before we run the tests in the test app. + re, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], nil + file = create_test_for_env("development") results = Dir.chdir(app_path) { `ruby -Ilib:test #{file} -e development`.each_line.map { |line| JSON.parse line } @@ -587,6 +598,9 @@ module ApplicationTests assert_equal 1, results.length failures = results.first["failures"] flunk(failures.first) unless failures.empty? + + ensure + ENV["RAILS_ENV"] = re end def test_rake_passes_multiple_TESTOPTS_to_minitest @@ -777,6 +791,9 @@ module ApplicationTests class EnvironmentTest < ActiveSupport::TestCase def test_environment + test_db = ActiveRecord::Base.configurations[#{env.dump}]["database"] + db_file = ActiveRecord::Base.connection_config[:database] + assert_match(test_db, db_file) assert_equal #{env.dump}, ENV["RAILS_ENV"] end end -- cgit v1.2.3 From a50b8ea3503f5642330c1e6b94320115796b4bab Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 8 Dec 2017 13:42:01 -0800 Subject: Set the Rails environment from an environment variable Option parsing happens too late to have any impact on the Rails environment. Rails accesses the environment name and memoizes it too early in the boot process for a commandline option to have any impact on the database connection, so we'll change this test to set the environment from an environment variable (and ensure it still works when running tests with `ruby`) --- railties/test/application/test_runner_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/test/application/test_runner_test.rb') diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index 17333b6ac9..a01325fdb8 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -593,7 +593,7 @@ module ApplicationTests file = create_test_for_env("development") results = Dir.chdir(app_path) { - `ruby -Ilib:test #{file} -e development`.each_line.map { |line| JSON.parse line } + `RAILS_ENV=development ruby -Ilib:test #{file}`.each_line.map { |line| JSON.parse line } } assert_equal 1, results.length failures = results.first["failures"] -- cgit v1.2.3