From 02ba585f8d3fb8fcff53f22b5bbd10bcc8c8c037 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Thu, 5 Jan 2017 04:13:46 +1030 Subject: Simplify the version specifier generated by prereleases "~> 1.2.3.pre4" will automatically allow "1.2.4" -- no need for an explicit range. --- railties/test/generators/generator_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'railties/test') diff --git a/railties/test/generators/generator_test.rb b/railties/test/generators/generator_test.rb index 904bade658..4444b3a56e 100644 --- a/railties/test/generators/generator_test.rb +++ b/railties/test/generators/generator_test.rb @@ -88,12 +88,12 @@ module Rails specifier_for = -> v { generator.send(:rails_version_specifier, Gem::Version.new(v)) } assert_equal "~> 4.1.13", specifier_for["4.1.13"] - assert_equal [">= 4.1.6.rc1", "< 4.2"], specifier_for["4.1.6.rc1"] + assert_equal "~> 4.1.6.rc1", specifier_for["4.1.6.rc1"] assert_equal ["~> 4.1.7", ">= 4.1.7.1"], specifier_for["4.1.7.1"] assert_equal ["~> 4.1.7", ">= 4.1.7.1.2"], specifier_for["4.1.7.1.2"] - assert_equal [">= 4.1.7.1.rc2", "< 4.2"], specifier_for["4.1.7.1.rc2"] - assert_equal [">= 4.2.0.beta1", "< 4.3"], specifier_for["4.2.0.beta1"] - assert_equal [">= 5.0.0.beta1", "< 5.1"], specifier_for["5.0.0.beta1"] + assert_equal ["~> 4.1.7", ">= 4.1.7.1.rc2"], specifier_for["4.1.7.1.rc2"] + assert_equal "~> 4.2.0.beta1", specifier_for["4.2.0.beta1"] + assert_equal "~> 5.0.0.beta1", specifier_for["5.0.0.beta1"] end end end -- cgit v1.2.3 From 4459a18db6fc9294d1f2553e45f59c859602dbad Mon Sep 17 00:00:00 2001 From: Fumiaki MATSUSHIMA Date: Wed, 4 Jan 2017 23:55:15 +0900 Subject: Fix generator command for nested (namespaced) rails engine (take 2) Rewrite https://github.com/rails/rails/pull/27550 085546df45 was reverted (b6ffb5efcb) because it change the return of `namespaced_path` from String to Array. ---------------- If we create nested (namespaced) rails engine such like bukkits-admin, `bin/rails g scaffold User name:string age:integer` will create `bukkits-admin/app/controllers/bukkits/users_controller.rb` but it should create `bukkits-admin/app/controllers/bukkits/admin/users_controller.rb`. In #6643, we changed `namespaced_path` as root path because we supposed application_controller is always in root but nested rails engine's application_controller will not. --- railties/test/generators/scaffold_generator_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'railties/test') diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 6b7e2c91d7..e2b2acab0f 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -492,6 +492,26 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end end + def test_scaffold_tests_pass_by_default_inside_namespaced_mountable_engine + Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits-admin --mountable` } + + engine_path = File.join(destination_root, "bukkits-admin") + + Dir.chdir(engine_path) do + quietly do + `bin/rails g scaffold User name:string age:integer; + bin/rails db:migrate` + end + + assert_file "bukkits-admin/app/controllers/bukkits/admin/users_controller.rb" do |content| + assert_match(/module Bukkits::Admin/, content) + assert_match(/class UsersController < ApplicationController/, content) + end + + assert_match(/8 runs, 10 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`) + end + end + def test_scaffold_tests_pass_by_default_inside_full_engine Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --full` } -- cgit v1.2.3 From 6affdbee2fbef912542930a79f49545a4bffb063 Mon Sep 17 00:00:00 2001 From: Shota Iguchi Date: Thu, 16 Feb 2017 16:47:43 +0900 Subject: Add test for generate namespaced integration test --- railties/test/generators/integration_test_generator_test.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'railties/test') diff --git a/railties/test/generators/integration_test_generator_test.rb b/railties/test/generators/integration_test_generator_test.rb index 8bcc02440a..9358b63bd4 100644 --- a/railties/test/generators/integration_test_generator_test.rb +++ b/railties/test/generators/integration_test_generator_test.rb @@ -3,10 +3,14 @@ require "rails/generators/rails/integration_test/integration_test_generator" class IntegrationTestGeneratorTest < Rails::Generators::TestCase include GeneratorsTestHelper - arguments %w(integration) def test_integration_test_skeleton_is_created - run_generator + run_generator %w(integration) assert_file "test/integration/integration_test.rb", /class IntegrationTest < ActionDispatch::IntegrationTest/ end + + def test_namespaced_integration_test_skeleton_is_created + run_generator %w(iguchi/integration) + assert_file "test/integration/iguchi/integration_test.rb", /class Iguchi::IntegrationTest < ActionDispatch::IntegrationTest/ + end end -- cgit v1.2.3 From b66b5378baf3804e9985a7cb9630f6d5a3ddcf97 Mon Sep 17 00:00:00 2001 From: Eugene Kenny Date: Fri, 17 Feb 2017 23:56:48 +0000 Subject: Clear active connections after initialization Any connections that were checked out during initialization should be checked back in before the first request is processed, for two reasons: - Returning the connection to the pool allows it to be health checked before it's used again. If the connection dies before the first request arrives, the health check will replace it with a new one. - If the thread that initialized Rails is not the same thread that will be performing work, checking in the connection will allow it to be reused instead of being stuck to the initialization thread forever. --- railties/test/application/initializers/frameworks_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'railties/test') diff --git a/railties/test/application/initializers/frameworks_test.rb b/railties/test/application/initializers/frameworks_test.rb index 90927159dd..eb2c578f91 100644 --- a/railties/test/application/initializers/frameworks_test.rb +++ b/railties/test/application/initializers/frameworks_test.rb @@ -262,5 +262,13 @@ module ApplicationTests Rails.env = orig_rails_env if orig_rails_env end end + + test "connections checked out during initialization are returned to the pool" do + app_file "config/initializers/active_record.rb", <<-RUBY + ActiveRecord::Base.connection + RUBY + require "#{app_path}/config/environment" + assert !ActiveRecord::Base.connection_pool.active_connection? + end end end -- cgit v1.2.3 From 38c2373b5bc4bc66cc424c5e0d6cce5d4ad94df0 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Sat, 18 Feb 2017 10:59:46 +0900 Subject: Do not run `git init` in dummy application --- railties/test/generators/plugin_generator_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'railties/test') diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index ddfbc1a698..eaf1199601 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -491,6 +491,7 @@ class PluginGeneratorTest < Rails::Generators::TestCase assert_no_directory "test/dummy/doc" assert_no_directory "test/dummy/test" assert_no_directory "test/dummy/vendor" + assert_no_directory "test/dummy/.git" end def test_skipping_test_files -- cgit v1.2.3 From 9730b1dba6d1bb9684a54915ac3735d9c0eade26 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sat, 27 Aug 2016 16:48:24 -0400 Subject: Add tests for system testing * Adds test case test * Adds driver adapter test * Adds tests for capybara seleium driver (testing the settings not actually opening the browser to test capybara w/ selenium because that would so so so slow) * Adds tests for rack test driver * Adds tests for generators --- railties/test/generators/app_generator_test.rb | 21 ++++++++++++++++++++- railties/test/generators/scaffold_generator_test.rb | 5 +++++ .../test/generators/system_test_generator_test.rb | 12 ++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 railties/test/generators/system_test_generator_test.rb (limited to 'railties/test') diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index ce29d93d6e..687aed2c47 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -42,6 +42,7 @@ DEFAULT_APP_FILES = %w( test/helpers test/mailers test/integration + test/system vendor tmp tmp/cache @@ -805,8 +806,26 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_equal 4, @sequence_step end - private + def test_system_tests_directory_generated + run_generator + + assert_file("test/system/.keep") + assert_directory("test/system") + end + + def test_system_tests_are_not_generated_on_system_test_skip + run_generator [destination_root, "--skip-system-test"] + + assert_no_directory("test/system") + end + + def test_system_tests_are_not_generated_on_test_skip + run_generator [destination_root, "--skip-test"] + assert_no_directory("test/system") + end + + private def stub_rails_application(root) Rails.application.config.root = root Rails.application.class.stub(:name, "Myapp") do diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index e2b2acab0f..d523b93ac1 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -62,6 +62,11 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_match(/patch product_line_url\(@product_line\), params: \{ product_line: \{ product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \} \}/, test) end + # System tests + assert_file "test/system/product_lines_test.rb" do |test| + assert_match(/class ProductLinesTest < Rails::SystemTestCase/, test) + end + # Views assert_no_file "app/views/layouts/product_lines.html.erb" diff --git a/railties/test/generators/system_test_generator_test.rb b/railties/test/generators/system_test_generator_test.rb new file mode 100644 index 0000000000..3a9733556e --- /dev/null +++ b/railties/test/generators/system_test_generator_test.rb @@ -0,0 +1,12 @@ +require "generators/generators_test_helper" +require "rails/generators/rails/system_test/system_test_generator" + +class SystemTestGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + arguments %w(user) + + def test_system_test_skeleton_is_created + run_generator + assert_file "test/system/users_test.rb", /class UsersTest < Rails::SystemTestCase/ + end +end -- cgit v1.2.3 From 5bf0aa6745db27c45c0778f9f6e9046f9ee9fb94 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 6 Nov 2016 18:55:15 -0500 Subject: Turn system testing into it's own gem and rename Renames `Rails::SystemTestCase` to `ActionSystemTest` and moves it to a gem under the Rails name. We need to name the class `ActionSystemTestCase` because the gem expects a module but tests themselves expect a class. Adds MIT-LICENSE, CHANGELOG, and README for the future. --- railties/test/generators/scaffold_generator_test.rb | 2 +- railties/test/generators/system_test_generator_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'railties/test') diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index d523b93ac1..43b8ce99ed 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -64,7 +64,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase # System tests assert_file "test/system/product_lines_test.rb" do |test| - assert_match(/class ProductLinesTest < Rails::SystemTestCase/, test) + assert_match(/class ProductLinesTest < ActionSystemTestCase/, test) end # Views diff --git a/railties/test/generators/system_test_generator_test.rb b/railties/test/generators/system_test_generator_test.rb index 3a9733556e..b6cf75b586 100644 --- a/railties/test/generators/system_test_generator_test.rb +++ b/railties/test/generators/system_test_generator_test.rb @@ -7,6 +7,6 @@ class SystemTestGeneratorTest < Rails::Generators::TestCase def test_system_test_skeleton_is_created run_generator - assert_file "test/system/users_test.rb", /class UsersTest < Rails::SystemTestCase/ + assert_file "test/system/users_test.rb", /class UsersTest < ActionSystemTestCase/ end end -- cgit v1.2.3 From 1a0ca84a064b07ecab798793a3d7ebe89bb6367c Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 19 Feb 2017 11:50:42 -0500 Subject: Move and rename system tests * Move system tests back into Action Pack * Rename `ActionSystemTest` to `ActionDispatch::SystemTestCase` * Remove private base module and only make file for public `SystemTestCase` class, name private module `SystemTesting` * Rename `ActionSystemTestCase` to `ApplicationSystemTestCase` * Update corresponding documentation and guides * Delete old `ActionSystemTest` files --- railties/test/generators/scaffold_generator_test.rb | 2 +- railties/test/generators/system_test_generator_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'railties/test') diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 43b8ce99ed..436fbd5d73 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -64,7 +64,7 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase # System tests assert_file "test/system/product_lines_test.rb" do |test| - assert_match(/class ProductLinesTest < ActionSystemTestCase/, test) + assert_match(/class ProductLinesTest < ApplicationSystemTestCase/, test) end # Views diff --git a/railties/test/generators/system_test_generator_test.rb b/railties/test/generators/system_test_generator_test.rb index b6cf75b586..e8e561ec49 100644 --- a/railties/test/generators/system_test_generator_test.rb +++ b/railties/test/generators/system_test_generator_test.rb @@ -7,6 +7,6 @@ class SystemTestGeneratorTest < Rails::Generators::TestCase def test_system_test_skeleton_is_created run_generator - assert_file "test/system/users_test.rb", /class UsersTest < ActionSystemTestCase/ + assert_file "test/system/users_test.rb", /class UsersTest < ApplicationSystemTestCase/ end end -- cgit v1.2.3 From bd163bdadf044e75a4812489d9f53a9feeb3cec7 Mon Sep 17 00:00:00 2001 From: Roberto Miranda Date: Fri, 17 Feb 2017 15:14:35 -0500 Subject: Use Puma 3.7.x ref this commit seems that has not been merged into 3.7 https://github.com/puma/puma/commit/42bec4600c51ab8a1c1ee5a0e1b738a4ffd82bf2 --- railties/test/generators/app_generator_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/test') diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index ce29d93d6e..8c0f1a2725 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -339,7 +339,7 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_generator_defaults_to_puma_version run_generator [destination_root] - assert_gem "puma", "'~> 3.0'" + assert_gem "puma", "'~> 3.7'" end def test_generator_if_skip_puma_is_given -- cgit v1.2.3 From c1b64429b1af45a4a526b3b5bb5a306a0d51e28a Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Tue, 21 Feb 2017 08:25:15 +0100 Subject: Fix run_via[]= backwards compatibility. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` Minitest.run_via[:rails] = true ``` 👆 would break because a simple alias won't catch the second true argument there. --- railties/test/application/test_runner_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'railties/test') diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index d3d5b6d6dd..ee03d8b86c 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -15,6 +15,16 @@ module ApplicationTests teardown_app end + def test_run_via_backwardscompatibility + require "rails/test_unit/minitest_plugin" + + assert_nothing_raised do + Minitest.run_via[:ruby] = true + end + + assert_predicate Minitest.run_via, :ruby? + end + def test_run_single_file create_test_file :models, "foo" create_test_file :models, "bar" -- cgit v1.2.3 From ce7d5fb2e6ffa9ec323510aaff51f10b15f1649a Mon Sep 17 00:00:00 2001 From: Andrew White Date: Wed, 20 Jan 2016 15:03:10 +0000 Subject: Add support for defining custom url helpers in routes.rb Allow the definition of custom url helpers that will be available automatically wherever standard url helpers are available. The current solution is to create helper methods in ApplicationHelper or some other helper module and this isn't a great solution since the url helper module can be called directly or included in another class which doesn't include the normal helper modules. Reference #22512. --- railties/test/application/routing_test.rb | 35 +++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'railties/test') diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index c515e2b270..00c5285caa 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -263,7 +263,10 @@ module ApplicationTests assert_equal "WIN", last_response.body end - { "development" => "baz", "production" => "bar" }.each do |mode, expected| + { + "development" => ["baz", "http://www.apple.com"], + "production" => ["bar", "http://www.microsoft.com"] + }.each do |mode, (expected_action, expected_url)| test "reloads routes when configuration is changed in #{mode}" do controller :foo, <<-RUBY class FooController < ApplicationController @@ -274,12 +277,19 @@ module ApplicationTests def baz render plain: "baz" end + + def custom + render plain: custom_url + end end RUBY app_file "config/routes.rb", <<-RUBY Rails.application.routes.draw do get 'foo', to: 'foo#bar' + get 'custom', to: 'foo#custom' + + url_helper(:custom) { "http://www.microsoft.com" } end RUBY @@ -288,16 +298,25 @@ module ApplicationTests get "/foo" assert_equal "bar", last_response.body + get "/custom" + assert_equal "http://www.microsoft.com", last_response.body + app_file "config/routes.rb", <<-RUBY Rails.application.routes.draw do get 'foo', to: 'foo#baz' + get 'custom', to: 'foo#custom' + + url_helper(:custom) { "http://www.apple.com" } end RUBY sleep 0.1 get "/foo" - assert_equal expected, last_response.body + assert_equal expected_action, last_response.body + + get "/custom" + assert_equal expected_url, last_response.body end end @@ -358,6 +377,10 @@ module ApplicationTests def index render plain: "foo" end + + def custom + render text: custom_url + end end RUBY @@ -443,16 +466,19 @@ module ApplicationTests app_file "config/routes.rb", <<-RUBY Rails.application.routes.draw do get ':locale/foo', to: 'foo#index', as: 'foo' + url_helper(:microsoft) { 'http://www.microsoft.com' } end RUBY get "/en/foo" assert_equal "foo", last_response.body assert_equal "/en/foo", Rails.application.routes.url_helpers.foo_path(locale: "en") + assert_equal "http://www.microsoft.com", Rails.application.routes.url_helpers.microsoft_url app_file "config/routes.rb", <<-RUBY Rails.application.routes.draw do get ':locale/bar', to: 'bar#index', as: 'foo' + url_helper(:apple) { 'http://www.apple.com' } end RUBY @@ -464,6 +490,11 @@ module ApplicationTests get "/en/bar" assert_equal "bar", last_response.body assert_equal "/en/bar", Rails.application.routes.url_helpers.foo_path(locale: "en") + assert_equal "http://www.apple.com", Rails.application.routes.url_helpers.apple_url + + assert_raises NoMethodError do + assert_equal "http://www.microsoft.com", Rails.application.routes.url_helpers.microsoft_url + end end test "resource routing with irregular inflection" do -- cgit v1.2.3 From 47a27e8950ad00654e2ba0420cefd87269e08055 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Tue, 23 Feb 2016 09:59:33 +0000 Subject: Rename url_helper to direct --- railties/test/application/routing_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'railties/test') diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index 00c5285caa..98673f5c1a 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -289,7 +289,7 @@ module ApplicationTests get 'foo', to: 'foo#bar' get 'custom', to: 'foo#custom' - url_helper(:custom) { "http://www.microsoft.com" } + direct(:custom) { "http://www.microsoft.com" } end RUBY @@ -306,7 +306,7 @@ module ApplicationTests get 'foo', to: 'foo#baz' get 'custom', to: 'foo#custom' - url_helper(:custom) { "http://www.apple.com" } + direct(:custom) { "http://www.apple.com" } end RUBY @@ -466,7 +466,7 @@ module ApplicationTests app_file "config/routes.rb", <<-RUBY Rails.application.routes.draw do get ':locale/foo', to: 'foo#index', as: 'foo' - url_helper(:microsoft) { 'http://www.microsoft.com' } + direct(:microsoft) { 'http://www.microsoft.com' } end RUBY @@ -478,7 +478,7 @@ module ApplicationTests app_file "config/routes.rb", <<-RUBY Rails.application.routes.draw do get ':locale/bar', to: 'bar#index', as: 'foo' - url_helper(:apple) { 'http://www.apple.com' } + direct(:apple) { 'http://www.apple.com' } end RUBY -- cgit v1.2.3 From 3bf47b018be912fc7946342315e67b2ac6c33eaf Mon Sep 17 00:00:00 2001 From: Andrew White Date: Mon, 20 Feb 2017 20:22:42 +0000 Subject: Add custom polymorphic mapping Allow the use of `direct` to specify custom mappings for polymorphic_url, e.g: resource :basket direct(class: "Basket") { [:basket] } This will then generate the following: >> link_to "Basket", @basket => Basket More importantly it will generate the correct url when used with `form_for`. Fixes #1769. --- railties/test/application/routing_test.rb | 102 ++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 4 deletions(-) (limited to 'railties/test') diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index 98673f5c1a..6c34c72564 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -264,9 +264,9 @@ module ApplicationTests end { - "development" => ["baz", "http://www.apple.com"], - "production" => ["bar", "http://www.microsoft.com"] - }.each do |mode, (expected_action, expected_url)| + "development" => ["baz", "http://www.apple.com", "/dashboard"], + "production" => ["bar", "http://www.microsoft.com", "/profile"] + }.each do |mode, (expected_action, expected_url, expected_mapping)| test "reloads routes when configuration is changed in #{mode}" do controller :foo, <<-RUBY class FooController < ApplicationController @@ -281,6 +281,25 @@ module ApplicationTests def custom render plain: custom_url end + + def mapping + render plain: url_for(User.new) + end + end + RUBY + + app_file "app/models/user.rb", <<-RUBY + class User + extend ActiveModel::Naming + include ActiveModel::Conversion + + def model_name + @_model_name ||= ActiveModel::Name.new(self.class, nil, "User") + end + + def persisted? + false + end end RUBY @@ -288,8 +307,10 @@ module ApplicationTests Rails.application.routes.draw do get 'foo', to: 'foo#bar' get 'custom', to: 'foo#custom' + get 'mapping', to: 'foo#mapping' direct(:custom) { "http://www.microsoft.com" } + direct(class: "User") { "/profile" } end RUBY @@ -301,12 +322,17 @@ module ApplicationTests get "/custom" assert_equal "http://www.microsoft.com", last_response.body + get "/mapping" + assert_equal "/profile", last_response.body + app_file "config/routes.rb", <<-RUBY Rails.application.routes.draw do get 'foo', to: 'foo#baz' get 'custom', to: 'foo#custom' + get 'mapping', to: 'foo#mapping' direct(:custom) { "http://www.apple.com" } + direct(class: "User") { "/dashboard" } end RUBY @@ -317,6 +343,9 @@ module ApplicationTests get "/custom" assert_equal expected_url, last_response.body + + get "/mapping" + assert_equal expected_mapping, last_response.body end end @@ -379,7 +408,11 @@ module ApplicationTests end def custom - render text: custom_url + render plain: custom_url + end + + def mapping + render plain: url_for(User.new) end end RUBY @@ -392,6 +425,21 @@ module ApplicationTests end RUBY + app_file "app/models/user.rb", <<-RUBY + class User + extend ActiveModel::Naming + include ActiveModel::Conversion + + def model_name + @_model_name ||= ActiveModel::Name.new(self.class, nil, "User") + end + + def persisted? + false + end + end + RUBY + app_file "config/routes.rb", <<-RUBY Rails.application.routes.draw do get 'foo', to: 'foo#index' @@ -412,6 +460,12 @@ module ApplicationTests Rails.application.routes.draw do get 'foo', to: 'foo#index' get 'bar', to: 'bar#index' + + get 'custom', to: 'foo#custom' + direct(:custom) { 'http://www.apple.com' } + + get 'mapping', to: 'foo#mapping' + direct(class: 'User') { '/profile' } end RUBY @@ -425,6 +479,14 @@ module ApplicationTests assert_equal "bar", last_response.body assert_equal "/bar", Rails.application.routes.url_helpers.bar_path + get "/custom" + assert_equal "http://www.apple.com", last_response.body + assert_equal "http://www.apple.com", Rails.application.routes.url_helpers.custom_url + + get "/mapping" + assert_equal "/profile", last_response.body + assert_equal "/profile", Rails.application.routes.url_helpers.polymorphic_path(User.new) + app_file "config/routes.rb", <<-RUBY Rails.application.routes.draw do get 'foo', to: 'foo#index' @@ -442,6 +504,18 @@ module ApplicationTests assert_raises NoMethodError do assert_equal "/bar", Rails.application.routes.url_helpers.bar_path end + + get "/custom" + assert_equal 404, last_response.status + assert_raises NoMethodError do + assert_equal "http://www.apple.com", Rails.application.routes.url_helpers.custom_url + end + + get "/mapping" + assert_equal 404, last_response.status + assert_raises NoMethodError do + assert_equal "/profile", Rails.application.routes.url_helpers.polymorphic_path(User.new) + end end test "named routes are cleared when reloading" do @@ -463,10 +537,27 @@ module ApplicationTests end RUBY + app_file "app/models/user.rb", <<-RUBY + class User + extend ActiveModel::Naming + include ActiveModel::Conversion + + def model_name + @_model_name ||= ActiveModel::Name.new(self.class, nil, "User") + end + + def persisted? + false + end + end + RUBY + app_file "config/routes.rb", <<-RUBY Rails.application.routes.draw do get ':locale/foo', to: 'foo#index', as: 'foo' + get 'users', to: 'foo#users', as: 'users' direct(:microsoft) { 'http://www.microsoft.com' } + direct(class: 'User') { '/profile' } end RUBY @@ -474,10 +565,12 @@ module ApplicationTests assert_equal "foo", last_response.body assert_equal "/en/foo", Rails.application.routes.url_helpers.foo_path(locale: "en") assert_equal "http://www.microsoft.com", Rails.application.routes.url_helpers.microsoft_url + assert_equal "/profile", Rails.application.routes.url_helpers.polymorphic_path(User.new) app_file "config/routes.rb", <<-RUBY Rails.application.routes.draw do get ':locale/bar', to: 'bar#index', as: 'foo' + get 'users', to: 'foo#users', as: 'users' direct(:apple) { 'http://www.apple.com' } end RUBY @@ -491,6 +584,7 @@ module ApplicationTests assert_equal "bar", last_response.body assert_equal "/en/bar", Rails.application.routes.url_helpers.foo_path(locale: "en") assert_equal "http://www.apple.com", Rails.application.routes.url_helpers.apple_url + assert_equal "/users", Rails.application.routes.url_helpers.polymorphic_path(User.new) assert_raises NoMethodError do assert_equal "http://www.microsoft.com", Rails.application.routes.url_helpers.microsoft_url -- cgit v1.2.3 From c116eaf2217abbc83ad76ac09c4fb89e033e1cdd Mon Sep 17 00:00:00 2001 From: Andrew White Date: Tue, 21 Feb 2017 12:49:25 +0000 Subject: Prefer remove_method over undef_method Using `undef_method` means that when a route is removed any other implementations of that method in the ancestor chain are inaccessible so instead use `remove_method` which restores access to the ancestor. --- railties/test/application/routing_test.rb | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'railties/test') diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index 6c34c72564..1132e7fb55 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -618,5 +618,63 @@ module ApplicationTests get "/yazilar" assert_equal 200, last_response.status end + + test "reloading routes removes methods and doesn't undefine them" do + app_file "config/routes.rb", <<-RUBY + Rails.application.routes.draw do + get '/url', to: 'url#index' + end + RUBY + + app_file "app/models/url_helpers.rb", <<-RUBY + module UrlHelpers + def foo_path + "/foo" + end + end + RUBY + + app_file "app/models/context.rb", <<-RUBY + class Context + include UrlHelpers + include Rails.application.routes.url_helpers + end + RUBY + + controller "url", <<-RUBY + class UrlController < ApplicationController + def index + context = Context.new + render plain: context.foo_path + end + end + RUBY + + get "/url" + assert_equal "/foo", last_response.body + + app_file "config/routes.rb", <<-RUBY + Rails.application.routes.draw do + get '/url', to: 'url#index' + get '/bar', to: 'foo#index', as: 'foo' + end + RUBY + + Rails.application.reload_routes! + + get "/url" + assert_equal "/bar", last_response.body + + app_file "config/routes.rb", <<-RUBY + Rails.application.routes.draw do + get '/url', to: 'url#index' + end + RUBY + + Rails.application.reload_routes! + + get "/url" + assert_equal "/foo", last_response.body + end end end -- cgit v1.2.3 From d7c1e62c2cd2969b991bc4a1150b02b27f6d6e3f Mon Sep 17 00:00:00 2001 From: Andrew White Date: Tue, 21 Feb 2017 15:29:10 +0000 Subject: Split direct method into two Use a separate method called `resolve` for the custom polymorphic mapping to clarify the API. --- railties/test/application/routing_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'railties/test') diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index 1132e7fb55..6742da20cc 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -310,7 +310,7 @@ module ApplicationTests get 'mapping', to: 'foo#mapping' direct(:custom) { "http://www.microsoft.com" } - direct(class: "User") { "/profile" } + resolve("User") { "/profile" } end RUBY @@ -332,7 +332,7 @@ module ApplicationTests get 'mapping', to: 'foo#mapping' direct(:custom) { "http://www.apple.com" } - direct(class: "User") { "/dashboard" } + resolve("User") { "/dashboard" } end RUBY @@ -465,7 +465,7 @@ module ApplicationTests direct(:custom) { 'http://www.apple.com' } get 'mapping', to: 'foo#mapping' - direct(class: 'User') { '/profile' } + resolve('User') { '/profile' } end RUBY @@ -557,7 +557,7 @@ module ApplicationTests get ':locale/foo', to: 'foo#index', as: 'foo' get 'users', to: 'foo#users', as: 'users' direct(:microsoft) { 'http://www.microsoft.com' } - direct(class: 'User') { '/profile' } + resolve('User') { '/profile' } end RUBY -- cgit v1.2.3 From 8e9e94391902b854662ba5eb06cc006cc4e85212 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 21 Feb 2017 17:33:31 +0100 Subject: Revert back to the original of using package.json in the root of the project (#28093) --- railties/test/generators/api_app_generator_test.rb | 2 +- railties/test/generators/app_generator_test.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'railties/test') diff --git a/railties/test/generators/api_app_generator_test.rb b/railties/test/generators/api_app_generator_test.rb index c54d9cc599..bdef1798f8 100644 --- a/railties/test/generators/api_app_generator_test.rb +++ b/railties/test/generators/api_app_generator_test.rb @@ -117,7 +117,7 @@ class ApiAppGeneratorTest < Rails::Generators::TestCase public/apple-touch-icon-precomposed.png public/apple-touch-icon.png public/favicon.icon - vendor/package.json + package.json ) end end diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index bd1b412b36..1ac2b4cde0 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -422,7 +422,7 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_generator_if_skip_yarn_is_given run_generator [destination_root, "--skip-yarn"] - assert_no_file "vendor/package.json" + assert_no_file "package.json" assert_no_file "bin/yarn" end @@ -497,21 +497,21 @@ class AppGeneratorTest < Rails::Generators::TestCase def test_generator_for_yarn run_generator([destination_root]) - assert_file "vendor/package.json", /dependencies/ + assert_file "package.json", /dependencies/ assert_file "config/initializers/assets.rb", /node_modules/ end def test_generator_for_yarn_skipped run_generator([destination_root, "--skip-yarn"]) - assert_no_file "vendor/package.json" + assert_no_file "package.json" assert_file "config/initializers/assets.rb" do |content| assert_no_match(/node_modules/, content) end assert_file ".gitignore" do |content| - assert_no_match(/vendor\/node_modules/, content) - assert_no_match(/vendor\/yarn-error\.log/, content) + assert_no_match(/node_modules/, content) + assert_no_match(/yarn-error\.log/, content) end end -- cgit v1.2.3 From 11660945696155c86a05260795e1a0afce0d291d Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Thu, 23 Feb 2017 15:01:02 +0100 Subject: Add encrypted secrets (#28038) --- railties/test/generators/app_generator_test.rb | 1 + .../generators/encrypted_secrets_generator_test.rb | 42 ++++++++ railties/test/isolation/abstract_unit.rb | 1 + railties/test/secrets_test.rb | 108 +++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 railties/test/generators/encrypted_secrets_generator_test.rb create mode 100644 railties/test/secrets_test.rb (limited to 'railties/test') diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 1ac2b4cde0..986afb6d2a 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -335,6 +335,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end assert_file "config/environments/production.rb" do |content| assert_match(/# config\.action_mailer\.raise_delivery_errors = false/, content) + assert_match(/^ config\.read_encrypted_secrets = true/, content) end end diff --git a/railties/test/generators/encrypted_secrets_generator_test.rb b/railties/test/generators/encrypted_secrets_generator_test.rb new file mode 100644 index 0000000000..747abf19ed --- /dev/null +++ b/railties/test/generators/encrypted_secrets_generator_test.rb @@ -0,0 +1,42 @@ +require "generators/generators_test_helper" +require "rails/generators/rails/encrypted_secrets/encrypted_secrets_generator" + +class EncryptedSecretsGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + + def setup + super + cd destination_root + end + + def test_generates_key_file_and_encrypted_secrets_file + run_generator + + assert_file "config/secrets.yml.key", /[\w\d]+/ + + assert File.exist?("config/secrets.yml.enc") + assert_no_match(/production:\n# external_api_key: [\w\d]+/, IO.binread("config/secrets.yml.enc")) + assert_match(/production:\n# external_api_key: [\w\d]+/, Rails::Secrets.read) + end + + def test_appends_to_gitignore + FileUtils.touch(".gitignore") + + run_generator + + assert_file ".gitignore", /config\/secrets.yml.key/, /(?!config\/secrets.yml.enc)/ + end + + def test_warns_when_ignore_is_missing + assert_match(/Add this to your ignore file/i, run_generator) + end + + def test_doesnt_generate_a_new_key_file_if_already_opted_in_to_encrypted_secrets + FileUtils.mkdir("config") + File.open("config/secrets.yml.enc", "w") { |f| f.puts "already secrety" } + + run_generator + + assert_no_file "config/secrets.yml.key" + end +end diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index 1902eac862..924503a522 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -22,6 +22,7 @@ require "active_support/core_ext/object/blank" require "active_support/testing/isolation" require "active_support/core_ext/kernel/reporting" require "tmpdir" +require "rails/secrets" module TestHelpers module Paths diff --git a/railties/test/secrets_test.rb b/railties/test/secrets_test.rb new file mode 100644 index 0000000000..36e42cf1f9 --- /dev/null +++ b/railties/test/secrets_test.rb @@ -0,0 +1,108 @@ +require "abstract_unit" +require "isolation/abstract_unit" +require "rails/generators" +require "rails/generators/rails/encrypted_secrets/encrypted_secrets_generator" +require "rails/secrets" + +class Rails::SecretsTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + + @old_read_encrypted_secrets, Rails::Secrets.read_encrypted_secrets = + Rails::Secrets.read_encrypted_secrets, true + end + + def teardown + Rails::Secrets.read_encrypted_secrets = @old_read_encrypted_secrets + + teardown_app + end + + test "setting read to false skips parsing" do + Rails::Secrets.read_encrypted_secrets = false + + Dir.chdir(app_path) do + assert_equal Hash.new, Rails::Secrets.parse(%w( config/secrets.yml.enc ), env: "production") + end + end + + test "raises when reading secrets without a key" do + run_secrets_generator do + FileUtils.rm("config/secrets.yml.key") + + assert_raises Rails::Secrets::MissingKeyError do + Rails::Secrets.key + end + end + end + + test "reading with ENV variable" do + run_secrets_generator do + begin + old_key = ENV["RAILS_MASTER_KEY"] + ENV["RAILS_MASTER_KEY"] = IO.binread("config/secrets.yml.key").strip + FileUtils.rm("config/secrets.yml.key") + + assert_match "production:\n# external_api_key", Rails::Secrets.read + ensure + ENV["RAILS_MASTER_KEY"] = old_key + end + end + end + + test "reading from key file" do + run_secrets_generator do + File.binwrite("config/secrets.yml.key", "How do I know you feel it?") + + assert_equal "How do I know you feel it?", Rails::Secrets.key + end + end + + test "editing" do + run_secrets_generator do + decrypted_path = nil + + Rails::Secrets.read_for_editing do |tmp_path| + decrypted_path = tmp_path + + assert_match(/production:\n# external_api_key/, File.read(tmp_path)) + + File.write(tmp_path, "Empty streets, empty nights. The Downtown Lights.") + end + + assert_not File.exist?(decrypted_path) + assert_equal "Empty streets, empty nights. The Downtown Lights.", Rails::Secrets.read + end + end + + test "merging secrets with encrypted precedence" do + run_secrets_generator do + File.write("config/secrets.yml", <<-end_of_secrets) + test: + yeah_yeah: lets-go-walking-down-this-empty-street + end_of_secrets + + Rails::Secrets.write(<<-end_of_secrets) + test: + yeah_yeah: lets-walk-in-the-cool-evening-light + end_of_secrets + + Rails.application.config.root = app_path + Rails.application.instance_variable_set(:@secrets, nil) # Dance around caching 💃🕺 + assert_equal "lets-walk-in-the-cool-evening-light", Rails.application.secrets.yeah_yeah + end + end + + private + def run_secrets_generator + Dir.chdir(app_path) do + capture(:stdout) do + Rails::Generators::EncryptedSecretsGenerator.start + end + + yield + end + end +end -- cgit v1.2.3 From 039380e3eeb24ed17f1824183b94638f0cfff747 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 23 Feb 2017 15:55:15 +0100 Subject: Revert "Add encrypted secrets" (#28127) --- railties/test/generators/app_generator_test.rb | 1 - .../generators/encrypted_secrets_generator_test.rb | 42 -------- railties/test/isolation/abstract_unit.rb | 1 - railties/test/secrets_test.rb | 108 --------------------- 4 files changed, 152 deletions(-) delete mode 100644 railties/test/generators/encrypted_secrets_generator_test.rb delete mode 100644 railties/test/secrets_test.rb (limited to 'railties/test') diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 986afb6d2a..1ac2b4cde0 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -335,7 +335,6 @@ class AppGeneratorTest < Rails::Generators::TestCase end assert_file "config/environments/production.rb" do |content| assert_match(/# config\.action_mailer\.raise_delivery_errors = false/, content) - assert_match(/^ config\.read_encrypted_secrets = true/, content) end end diff --git a/railties/test/generators/encrypted_secrets_generator_test.rb b/railties/test/generators/encrypted_secrets_generator_test.rb deleted file mode 100644 index 747abf19ed..0000000000 --- a/railties/test/generators/encrypted_secrets_generator_test.rb +++ /dev/null @@ -1,42 +0,0 @@ -require "generators/generators_test_helper" -require "rails/generators/rails/encrypted_secrets/encrypted_secrets_generator" - -class EncryptedSecretsGeneratorTest < Rails::Generators::TestCase - include GeneratorsTestHelper - - def setup - super - cd destination_root - end - - def test_generates_key_file_and_encrypted_secrets_file - run_generator - - assert_file "config/secrets.yml.key", /[\w\d]+/ - - assert File.exist?("config/secrets.yml.enc") - assert_no_match(/production:\n# external_api_key: [\w\d]+/, IO.binread("config/secrets.yml.enc")) - assert_match(/production:\n# external_api_key: [\w\d]+/, Rails::Secrets.read) - end - - def test_appends_to_gitignore - FileUtils.touch(".gitignore") - - run_generator - - assert_file ".gitignore", /config\/secrets.yml.key/, /(?!config\/secrets.yml.enc)/ - end - - def test_warns_when_ignore_is_missing - assert_match(/Add this to your ignore file/i, run_generator) - end - - def test_doesnt_generate_a_new_key_file_if_already_opted_in_to_encrypted_secrets - FileUtils.mkdir("config") - File.open("config/secrets.yml.enc", "w") { |f| f.puts "already secrety" } - - run_generator - - assert_no_file "config/secrets.yml.key" - end -end diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index 924503a522..1902eac862 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -22,7 +22,6 @@ require "active_support/core_ext/object/blank" require "active_support/testing/isolation" require "active_support/core_ext/kernel/reporting" require "tmpdir" -require "rails/secrets" module TestHelpers module Paths diff --git a/railties/test/secrets_test.rb b/railties/test/secrets_test.rb deleted file mode 100644 index 36e42cf1f9..0000000000 --- a/railties/test/secrets_test.rb +++ /dev/null @@ -1,108 +0,0 @@ -require "abstract_unit" -require "isolation/abstract_unit" -require "rails/generators" -require "rails/generators/rails/encrypted_secrets/encrypted_secrets_generator" -require "rails/secrets" - -class Rails::SecretsTest < ActiveSupport::TestCase - include ActiveSupport::Testing::Isolation - - def setup - build_app - - @old_read_encrypted_secrets, Rails::Secrets.read_encrypted_secrets = - Rails::Secrets.read_encrypted_secrets, true - end - - def teardown - Rails::Secrets.read_encrypted_secrets = @old_read_encrypted_secrets - - teardown_app - end - - test "setting read to false skips parsing" do - Rails::Secrets.read_encrypted_secrets = false - - Dir.chdir(app_path) do - assert_equal Hash.new, Rails::Secrets.parse(%w( config/secrets.yml.enc ), env: "production") - end - end - - test "raises when reading secrets without a key" do - run_secrets_generator do - FileUtils.rm("config/secrets.yml.key") - - assert_raises Rails::Secrets::MissingKeyError do - Rails::Secrets.key - end - end - end - - test "reading with ENV variable" do - run_secrets_generator do - begin - old_key = ENV["RAILS_MASTER_KEY"] - ENV["RAILS_MASTER_KEY"] = IO.binread("config/secrets.yml.key").strip - FileUtils.rm("config/secrets.yml.key") - - assert_match "production:\n# external_api_key", Rails::Secrets.read - ensure - ENV["RAILS_MASTER_KEY"] = old_key - end - end - end - - test "reading from key file" do - run_secrets_generator do - File.binwrite("config/secrets.yml.key", "How do I know you feel it?") - - assert_equal "How do I know you feel it?", Rails::Secrets.key - end - end - - test "editing" do - run_secrets_generator do - decrypted_path = nil - - Rails::Secrets.read_for_editing do |tmp_path| - decrypted_path = tmp_path - - assert_match(/production:\n# external_api_key/, File.read(tmp_path)) - - File.write(tmp_path, "Empty streets, empty nights. The Downtown Lights.") - end - - assert_not File.exist?(decrypted_path) - assert_equal "Empty streets, empty nights. The Downtown Lights.", Rails::Secrets.read - end - end - - test "merging secrets with encrypted precedence" do - run_secrets_generator do - File.write("config/secrets.yml", <<-end_of_secrets) - test: - yeah_yeah: lets-go-walking-down-this-empty-street - end_of_secrets - - Rails::Secrets.write(<<-end_of_secrets) - test: - yeah_yeah: lets-walk-in-the-cool-evening-light - end_of_secrets - - Rails.application.config.root = app_path - Rails.application.instance_variable_set(:@secrets, nil) # Dance around caching 💃🕺 - assert_equal "lets-walk-in-the-cool-evening-light", Rails.application.secrets.yeah_yeah - end - end - - private - def run_secrets_generator - Dir.chdir(app_path) do - capture(:stdout) do - Rails::Generators::EncryptedSecretsGenerator.start - end - - yield - end - end -end -- cgit v1.2.3 From fbee4e3ce37674eb928298490a35d3dfd1921e67 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Thu, 23 Feb 2017 18:15:28 +0100 Subject: Revert "Revert "Add encrypted secrets"" --- railties/test/generators/app_generator_test.rb | 1 + .../generators/encrypted_secrets_generator_test.rb | 42 ++++++++ railties/test/isolation/abstract_unit.rb | 1 + railties/test/secrets_test.rb | 108 +++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 railties/test/generators/encrypted_secrets_generator_test.rb create mode 100644 railties/test/secrets_test.rb (limited to 'railties/test') diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 1ac2b4cde0..986afb6d2a 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -335,6 +335,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end assert_file "config/environments/production.rb" do |content| assert_match(/# config\.action_mailer\.raise_delivery_errors = false/, content) + assert_match(/^ config\.read_encrypted_secrets = true/, content) end end diff --git a/railties/test/generators/encrypted_secrets_generator_test.rb b/railties/test/generators/encrypted_secrets_generator_test.rb new file mode 100644 index 0000000000..747abf19ed --- /dev/null +++ b/railties/test/generators/encrypted_secrets_generator_test.rb @@ -0,0 +1,42 @@ +require "generators/generators_test_helper" +require "rails/generators/rails/encrypted_secrets/encrypted_secrets_generator" + +class EncryptedSecretsGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + + def setup + super + cd destination_root + end + + def test_generates_key_file_and_encrypted_secrets_file + run_generator + + assert_file "config/secrets.yml.key", /[\w\d]+/ + + assert File.exist?("config/secrets.yml.enc") + assert_no_match(/production:\n# external_api_key: [\w\d]+/, IO.binread("config/secrets.yml.enc")) + assert_match(/production:\n# external_api_key: [\w\d]+/, Rails::Secrets.read) + end + + def test_appends_to_gitignore + FileUtils.touch(".gitignore") + + run_generator + + assert_file ".gitignore", /config\/secrets.yml.key/, /(?!config\/secrets.yml.enc)/ + end + + def test_warns_when_ignore_is_missing + assert_match(/Add this to your ignore file/i, run_generator) + end + + def test_doesnt_generate_a_new_key_file_if_already_opted_in_to_encrypted_secrets + FileUtils.mkdir("config") + File.open("config/secrets.yml.enc", "w") { |f| f.puts "already secrety" } + + run_generator + + assert_no_file "config/secrets.yml.key" + end +end diff --git a/railties/test/isolation/abstract_unit.rb b/railties/test/isolation/abstract_unit.rb index 1902eac862..924503a522 100644 --- a/railties/test/isolation/abstract_unit.rb +++ b/railties/test/isolation/abstract_unit.rb @@ -22,6 +22,7 @@ require "active_support/core_ext/object/blank" require "active_support/testing/isolation" require "active_support/core_ext/kernel/reporting" require "tmpdir" +require "rails/secrets" module TestHelpers module Paths diff --git a/railties/test/secrets_test.rb b/railties/test/secrets_test.rb new file mode 100644 index 0000000000..36e42cf1f9 --- /dev/null +++ b/railties/test/secrets_test.rb @@ -0,0 +1,108 @@ +require "abstract_unit" +require "isolation/abstract_unit" +require "rails/generators" +require "rails/generators/rails/encrypted_secrets/encrypted_secrets_generator" +require "rails/secrets" + +class Rails::SecretsTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + + @old_read_encrypted_secrets, Rails::Secrets.read_encrypted_secrets = + Rails::Secrets.read_encrypted_secrets, true + end + + def teardown + Rails::Secrets.read_encrypted_secrets = @old_read_encrypted_secrets + + teardown_app + end + + test "setting read to false skips parsing" do + Rails::Secrets.read_encrypted_secrets = false + + Dir.chdir(app_path) do + assert_equal Hash.new, Rails::Secrets.parse(%w( config/secrets.yml.enc ), env: "production") + end + end + + test "raises when reading secrets without a key" do + run_secrets_generator do + FileUtils.rm("config/secrets.yml.key") + + assert_raises Rails::Secrets::MissingKeyError do + Rails::Secrets.key + end + end + end + + test "reading with ENV variable" do + run_secrets_generator do + begin + old_key = ENV["RAILS_MASTER_KEY"] + ENV["RAILS_MASTER_KEY"] = IO.binread("config/secrets.yml.key").strip + FileUtils.rm("config/secrets.yml.key") + + assert_match "production:\n# external_api_key", Rails::Secrets.read + ensure + ENV["RAILS_MASTER_KEY"] = old_key + end + end + end + + test "reading from key file" do + run_secrets_generator do + File.binwrite("config/secrets.yml.key", "How do I know you feel it?") + + assert_equal "How do I know you feel it?", Rails::Secrets.key + end + end + + test "editing" do + run_secrets_generator do + decrypted_path = nil + + Rails::Secrets.read_for_editing do |tmp_path| + decrypted_path = tmp_path + + assert_match(/production:\n# external_api_key/, File.read(tmp_path)) + + File.write(tmp_path, "Empty streets, empty nights. The Downtown Lights.") + end + + assert_not File.exist?(decrypted_path) + assert_equal "Empty streets, empty nights. The Downtown Lights.", Rails::Secrets.read + end + end + + test "merging secrets with encrypted precedence" do + run_secrets_generator do + File.write("config/secrets.yml", <<-end_of_secrets) + test: + yeah_yeah: lets-go-walking-down-this-empty-street + end_of_secrets + + Rails::Secrets.write(<<-end_of_secrets) + test: + yeah_yeah: lets-walk-in-the-cool-evening-light + end_of_secrets + + Rails.application.config.root = app_path + Rails.application.instance_variable_set(:@secrets, nil) # Dance around caching 💃🕺 + assert_equal "lets-walk-in-the-cool-evening-light", Rails.application.secrets.yeah_yeah + end + end + + private + def run_secrets_generator + Dir.chdir(app_path) do + capture(:stdout) do + Rails::Generators::EncryptedSecretsGenerator.start + end + + yield + end + end +end -- cgit v1.2.3 From 72fc0b446612134fa7e5cade2c6463a34306ff68 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Thu, 23 Feb 2017 18:38:43 +0100 Subject: Add back tests for test:units and test:functionals. Would have caught that the invoke changes broke rake delegation behavior. And we do ship the behavior so we should test it. --- railties/test/application/test_runner_test.rb | 28 +++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'railties/test') diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index ee03d8b86c..e773b52dbb 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -70,16 +70,18 @@ module ApplicationTests end def test_run_units - skip "we no longer have the concept of unit tests. Just different directories..." create_test_file :models, "foo" create_test_file :helpers, "bar_helper" create_test_file :unit, "baz_unit" create_test_file :controllers, "foobar_controller" - run_test_units_command.tap do |output| - assert_match "FooTest", output - assert_match "BarHelperTest", output - assert_match "BazUnitTest", output - assert_match "3 runs, 3 assertions, 0 failures", output + + Dir.chdir(app_path) do + `bin/rails test:units`.tap do |output| + assert_match "FooTest", output + assert_match "BarHelperTest", output + assert_match "BazUnitTest", output + assert_match "3 runs, 3 assertions, 0 failures", output + end end end @@ -117,16 +119,18 @@ module ApplicationTests end def test_run_functionals - skip "we no longer have the concept of functional tests. Just different directories..." create_test_file :mailers, "foo_mailer" create_test_file :controllers, "bar_controller" create_test_file :functional, "baz_functional" create_test_file :models, "foo" - run_test_functionals_command.tap do |output| - assert_match "FooMailerTest", output - assert_match "BarControllerTest", output - assert_match "BazFunctionalTest", output - assert_match "3 runs, 3 assertions, 0 failures", output + + Dir.chdir(app_path) do + `bin/rails test:functionals`.tap do |output| + assert_match "FooMailerTest", output + assert_match "BarControllerTest", output + assert_match "BazFunctionalTest", output + assert_match "3 runs, 3 assertions, 0 failures", output + end end end -- cgit v1.2.3 From 08d4aaae0c8110e75237d26029b696a2bcd00b1f Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Fri, 24 Feb 2017 09:13:44 +0900 Subject: Make version short-cut alias to work --- railties/test/application/version_test.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 railties/test/application/version_test.rb (limited to 'railties/test') diff --git a/railties/test/application/version_test.rb b/railties/test/application/version_test.rb new file mode 100644 index 0000000000..6b419ae7ae --- /dev/null +++ b/railties/test/application/version_test.rb @@ -0,0 +1,24 @@ +require "isolation/abstract_unit" +require "rails/gem_version" + +class VersionTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + end + + def teardown + teardown_app + end + + test "command works" do + output = Dir.chdir(app_path) { `bin/rails version` } + assert_equal "Rails #{Rails.gem_version}\n", output + end + + test "short-cut alias works" do + output = Dir.chdir(app_path) { `bin/rails -v` } + assert_equal "Rails #{Rails.gem_version}\n", output + end +end -- cgit v1.2.3 From e69a0e34494de8d91f53c0e1647690a8775a9536 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Fri, 24 Feb 2017 09:17:13 +0900 Subject: Make help short-cut alias to work --- railties/test/application/help_test.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 railties/test/application/help_test.rb (limited to 'railties/test') diff --git a/railties/test/application/help_test.rb b/railties/test/application/help_test.rb new file mode 100644 index 0000000000..0c3fe8bfa3 --- /dev/null +++ b/railties/test/application/help_test.rb @@ -0,0 +1,23 @@ +require "isolation/abstract_unit" + +class HelpTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + end + + def teardown + teardown_app + end + + test "command works" do + output = Dir.chdir(app_path) { `bin/rails help` } + assert_match "The most common rails commands are", output + end + + test "short-cut alias works" do + output = Dir.chdir(app_path) { `bin/rails -h` } + assert_match "The most common rails commands are", output + end +end -- cgit v1.2.3 From 35a7a292645ee895e522769f657491e42d2a793a Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Sat, 18 Feb 2017 16:42:48 +0900 Subject: Make adding gemfile entry work even if specify only the plugin name Whether the command was executed within the rails application is checked by whether or not the application's path matches `app_path`. https://github.com/rails/rails/blob/5-0-stable/railties/lib/rails/generators/rails/plugin/plugin_generator.rb#L439..L441 Therefore, if only plugin name is specified in `app_path`, addition to Gemfile is not done. However, in the rails guide an example of specifying only plugin name is given, and it is considered that there are many cases where only plugin name is specified. For that reason, made it work even if only plugin name was specified. --- railties/test/generators/plugin_generator_test.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'railties/test') diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index ddfbc1a698..2ad0ab55d3 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -535,6 +535,21 @@ class PluginGeneratorTest < Rails::Generators::TestCase FileUtils.rm gemfile_path end + def test_creating_plugin_only_specify_plugin_name_in_app_directory_adds_gemfile_entry + # simulate application existence + gemfile_path = "#{Rails.root}/Gemfile" + Object.const_set("APP_PATH", Rails.root) + FileUtils.touch gemfile_path + + FileUtils.cd(destination_root) + run_generator ["bukkits"] + + assert_file gemfile_path, /gem 'bukkits', path: 'bukkits'/ + ensure + Object.send(:remove_const, "APP_PATH") + FileUtils.rm gemfile_path + end + def test_skipping_gemfile_entry # simulate application existence gemfile_path = "#{Rails.root}/Gemfile" -- cgit v1.2.3 From 87a8206dbca1d6992e91eeea9a6c56e7c53e0ef1 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Fri, 24 Feb 2017 23:45:05 +0900 Subject: does not show hidden namespaces in generator's help --- railties/test/application/generators_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'railties/test') diff --git a/railties/test/application/generators_test.rb b/railties/test/application/generators_test.rb index d2ce14f594..ee0d697599 100644 --- a/railties/test/application/generators_test.rb +++ b/railties/test/application/generators_test.rb @@ -184,5 +184,12 @@ module ApplicationTests Rails::Command.send(:remove_const, "APP_PATH") end + + test "help does not show hidden namespaces" do + FileUtils.cd(rails_root) do + output = `bin/rails generate --help` + assert_no_match "active_record:migration", output + end + end end end -- cgit v1.2.3 From 6ac4dabbcfa092ddda24ae9ee4698a55eb495021 Mon Sep 17 00:00:00 2001 From: schneems Date: Thu, 23 Feb 2017 16:22:50 -0600 Subject: [close #24435] Send user_supplied_options to server MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently when Puma gets a `:Port` it doesn't know if it is Rails' default port or if it is one that is specified by a user. Because of this it assumes that the port passed in is always a user defined port and therefor 3000 always "wins" even if you specify `port` inside of the `config/puma.rb` file when booting your server with `rails s`. The fix is to record the options that are explicitly passed in from the user and pass those to the Puma server (or all servers really). Puma then has enough information to know when `:Port` is the default and when it is user defined. I went ahead and did this for all values rails server exposes as server side options for completeness. The hardest thing was converting the input say `-p` or `--port` into the appropriate "name", in this case `Port`. There may be a more straightforward way to do this with Thor, but I'm not an expert here. Move logic for parsing user options to method Better variable name for iteration Explicitly test `--port` user input ✂️ Update array if environment variables are used --- railties/test/commands/server_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'railties/test') diff --git a/railties/test/commands/server_test.rb b/railties/test/commands/server_test.rb index e3dfc3e82b..0f236ff463 100644 --- a/railties/test/commands/server_test.rb +++ b/railties/test/commands/server_test.rb @@ -121,6 +121,14 @@ class Rails::ServerTest < ActiveSupport::TestCase end end + def test_records_user_supplied_options + server_options = parse_arguments(["-p", 3001]) + assert_equal [:Port], server_options[:user_supplied_options] + + server_options = parse_arguments(["--port", 3001]) + assert_equal [:Port], server_options[:user_supplied_options] + end + def test_default_options server = Rails::Server.new old_default_options = server.default_options -- cgit v1.2.3 From 60aeb6a8f9f1fc161a921c2219650c19b6e753e7 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Mon, 27 Feb 2017 17:29:16 +0900 Subject: Set correct host except development environment Currently `localhost` is used for the default host in all environments. But up to Rails 5.0, `0.0.0.0` is used except for development. So fixed to use the same value as 5.0. Fixes #28184 --- railties/test/commands/server_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'railties/test') diff --git a/railties/test/commands/server_test.rb b/railties/test/commands/server_test.rb index 0f236ff463..d21a80982b 100644 --- a/railties/test/commands/server_test.rb +++ b/railties/test/commands/server_test.rb @@ -121,6 +121,24 @@ class Rails::ServerTest < ActiveSupport::TestCase end end + def test_host + with_rails_env "development" do + options = parse_arguments([]) + assert_equal "localhost", options[:Host] + end + + with_rails_env "production" do + options = parse_arguments([]) + assert_equal "0.0.0.0", options[:Host] + end + + with_rails_env "development" do + args = ["-b", "127.0.0.1"] + options = parse_arguments(args) + assert_equal "127.0.0.1", options[:Host] + end + end + def test_records_user_supplied_options server_options = parse_arguments(["-p", 3001]) assert_equal [:Port], server_options[:user_supplied_options] -- cgit v1.2.3 From 82f7dc6178f86e5e2dd82f9e528475a6acee6cd8 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Wed, 1 Mar 2017 20:40:39 +0100 Subject: Tell users how to assign a $EDITOR. In case there's no $EDITOR assigned users would see a cryptic: ``` % EDITOR= bin/rails secrets:edit Waiting for secrets file to be saved. Abort with Ctrl-C. sh: /var/folders/wd/xnncwqp96rj0v1y2nms64mq80000gn/T/secrets.yml.enc: Permission denied New secrets encrypted and saved. ``` That error is misleading, so give a hint in this easily detectable case. Fixes #28143. --- railties/test/commands/secrets_test.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 railties/test/commands/secrets_test.rb (limited to 'railties/test') diff --git a/railties/test/commands/secrets_test.rb b/railties/test/commands/secrets_test.rb new file mode 100644 index 0000000000..13fcf6c8a4 --- /dev/null +++ b/railties/test/commands/secrets_test.rb @@ -0,0 +1,24 @@ +require "isolation/abstract_unit" +require "rails/command" +require "rails/commands/secrets/secrets_command" + +class Rails::Command::SecretsCommandTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + end + + def teardown + teardown_app + end + + test "edit without editor gives hint" do + assert_match "No $EDITOR to open decrypted secrets in", run_edit_command(editor: "") + end + + private + def run_edit_command(editor: "cat") + Dir.chdir(app_path) { `EDITOR="#{editor}" bin/rails secrets:edit` } + end +end -- cgit v1.2.3 From 6aa6f9ae44ed999e972a58f729bdc5b2fcdc127b Mon Sep 17 00:00:00 2001 From: Stephen Touset Date: Thu, 23 Feb 2017 14:41:40 -0800 Subject: Default Secrets to AES-128-GCM, using ActiveSupport::MessageEncryptor Fixes #28135. --- railties/test/secrets_test.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'railties/test') diff --git a/railties/test/secrets_test.rb b/railties/test/secrets_test.rb index 36e42cf1f9..6650a37d2b 100644 --- a/railties/test/secrets_test.rb +++ b/railties/test/secrets_test.rb @@ -54,9 +54,10 @@ class Rails::SecretsTest < ActiveSupport::TestCase test "reading from key file" do run_secrets_generator do - File.binwrite("config/secrets.yml.key", "How do I know you feel it?") + key = "00112233445566778899aabbccddeeff" + File.binwrite("config/secrets.yml.key", key) - assert_equal "How do I know you feel it?", Rails::Secrets.key + assert_equal [key].pack("H*"), Rails::Secrets.key end end -- cgit v1.2.3 From e3b4554f231beb789b981dfb5e32789f5ad4d17b Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Thu, 2 Mar 2017 19:38:01 +0100 Subject: Move key packing into encryptor. --- railties/test/secrets_test.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'railties/test') diff --git a/railties/test/secrets_test.rb b/railties/test/secrets_test.rb index 6650a37d2b..953408f0b4 100644 --- a/railties/test/secrets_test.rb +++ b/railties/test/secrets_test.rb @@ -54,10 +54,9 @@ class Rails::SecretsTest < ActiveSupport::TestCase test "reading from key file" do run_secrets_generator do - key = "00112233445566778899aabbccddeeff" - File.binwrite("config/secrets.yml.key", key) + File.binwrite("config/secrets.yml.key", "00112233445566778899aabbccddeeff") - assert_equal [key].pack("H*"), Rails::Secrets.key + assert_equal "00112233445566778899aabbccddeeff", Rails::Secrets.key end end -- cgit v1.2.3 From 54ee15a203d7463534c2188141c6fb0090c9dc44 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Sun, 26 Feb 2017 21:05:13 +0900 Subject: Show correct commands in help Currently rails' help shows only namespace. However, the secrets command needs to specify command. Therefore, I fixed the command to display in help. --- railties/test/command/base_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 railties/test/command/base_test.rb (limited to 'railties/test') diff --git a/railties/test/command/base_test.rb b/railties/test/command/base_test.rb new file mode 100644 index 0000000000..ebfc4d0ba9 --- /dev/null +++ b/railties/test/command/base_test.rb @@ -0,0 +1,11 @@ +require "abstract_unit" +require "rails/command" +require "rails/commands/generate/generate_command" +require "rails/commands/secrets/secrets_command" + +class Rails::Command::BaseTest < ActiveSupport::TestCase + test "printing commands" do + assert_equal %w(generate), Rails::Command::GenerateCommand.printing_commands + assert_equal %w(secrets:setup secrets:edit), Rails::Command::SecretsCommand.printing_commands + end +end -- cgit v1.2.3 From 5edbdca5c0db00b0724bc0c9202c83194b688ae8 Mon Sep 17 00:00:00 2001 From: Fumiaki MATSUSHIMA Date: Wed, 1 Mar 2017 20:39:29 +0900 Subject: Fix random failure on system test with ajax If application has ajax, browser may begin request after rollback. `teardown_fixtures` will be called after `super` on `after_teardown` so we must call `Capybara.reset_sessions!` before `super` https://github.com/rails/rails/blob/b61a56541aecd7ac685d4f19d943177a3f1b465a/activerecord/lib/active_record/fixtures.rb#L857 --- railties/test/application/test_runner_test.rb | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'railties/test') diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index e773b52dbb..3705448081 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -576,6 +576,34 @@ module ApplicationTests capture(:stderr) { run_test_command("test/models/warnings_test.rb -w") }) end + def test_reset_sessions_before_rollback_on_system_tests + app_file "test/system/reset_session_before_rollback_test.rb", <<-RUBY + require "application_system_test_case" + + class ResetSessionBeforeRollbackTest < ApplicationSystemTestCase + def teardown_fixtures + puts "rollback" + super + end + + Capybara.singleton_class.prepend(Module.new do + def reset_sessions! + puts "reset sessions" + super + end + end) + + test "dummy" do + end + end + RUBY + + run_test_command("test/system/reset_session_before_rollback_test.rb").tap do |output| + assert_match "reset sessions\nrollback", output + assert_match "1 runs, 0 assertions, 0 failures, 0 errors, 0 skips", output + end + end + private def run_test_command(arguments = "test/unit/test_test.rb") Dir.chdir(app_path) { `bin/rails t #{arguments}` } -- cgit v1.2.3 From 4a77213eead0e33a8158e47525bad3d5e1996300 Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Sat, 4 Mar 2017 14:54:03 +0100 Subject: Avoid running system tests by default These tests may be expansive so let's only allow users to run them through `bin/rails test:system` or by passing a path to the `test` command. The same applies for `bin/rake test`. Refs #28109. --- railties/test/application/test_runner_test.rb | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'railties/test') diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index 3705448081..a8e3a7ec5b 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -604,6 +604,52 @@ module ApplicationTests end end + def test_system_tests_are_not_run_with_the_default_test_command + app_file "test/system/dummy_test.rb", <<-RUBY + require "application_system_test_case" + + class DummyTest < ApplicationSystemTestCase + test "something" do + assert true + end + end + RUBY + + run_test_command("").tap do |output| + assert_match "0 runs, 0 assertions, 0 failures, 0 errors, 0 skips", output + end + end + + def test_system_tests_are_not_run_through_rake_test + app_file "test/system/dummy_test.rb", <<-RUBY + require "application_system_test_case" + + class DummyTest < ApplicationSystemTestCase + test "something" do + assert true + end + end + RUBY + + output = Dir.chdir(app_path) { `bin/rake test` } + assert_match "0 runs, 0 assertions, 0 failures, 0 errors, 0 skips", output + end + + def test_system_tests_are_run_through_rake_test_when_given_in_TEST + app_file "test/system/dummy_test.rb", <<-RUBY + require "application_system_test_case" + + class DummyTest < ApplicationSystemTestCase + test "something" do + assert true + end + end + RUBY + + output = Dir.chdir(app_path) { `bin/rake test TEST=test/system/dummy_test.rb` } + assert_match "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips", output + end + private def run_test_command(arguments = "test/unit/test_test.rb") Dir.chdir(app_path) { `bin/rails t #{arguments}` } -- cgit v1.2.3 From 841dddaf26e496c4aaa43dcc2338da193aeffd4b Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Wed, 8 Mar 2017 12:28:00 +0900 Subject: Use appropriate type in generators test This fixes the following thor's warning. ``` Expected string default value for '--generate'; got false (boolean) ``` --- railties/test/generators_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/test') diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb index 68ba435393..c3c16b6f86 100644 --- a/railties/test/generators_test.rb +++ b/railties/test/generators_test.rb @@ -200,7 +200,7 @@ class GeneratorsTest < Rails::Generators::TestCase self.class.class_eval(<<-end_eval, __FILE__, __LINE__ + 1) class WithOptionsGenerator < Rails::Generators::Base - class_option :generate, :default => true + class_option :generate, default: true, type: :boolean end end_eval -- cgit v1.2.3 From 7b4fb2574a099183febe0c8229bf5a928db387a2 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Fri, 10 Mar 2017 09:21:53 +0900 Subject: Add secrets edit test --- railties/test/commands/secrets_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'railties/test') diff --git a/railties/test/commands/secrets_test.rb b/railties/test/commands/secrets_test.rb index 13fcf6c8a4..00b0343397 100644 --- a/railties/test/commands/secrets_test.rb +++ b/railties/test/commands/secrets_test.rb @@ -17,8 +17,21 @@ class Rails::Command::SecretsCommandTest < ActiveSupport::TestCase assert_match "No $EDITOR to open decrypted secrets in", run_edit_command(editor: "") end + test "edit secrets" do + run_setup_command + + # Run twice to ensure encrypted secrets can be reread after first edit pass. + 2.times do + assert_match(/external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289…/, run_edit_command) + end + end + private def run_edit_command(editor: "cat") Dir.chdir(app_path) { `EDITOR="#{editor}" bin/rails secrets:edit` } end + + def run_setup_command + Dir.chdir(app_path) { `bin/rails secrets:setup` } + end end -- cgit v1.2.3 From bc35e63909ef922a0031728350c227ab8e9326fe Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Tue, 14 Mar 2017 16:46:19 +0900 Subject: Make destroy command work within engines Instead of calling methods of Rails.application directly, we need to use a method that is considered for the rails engine. --- .../test/generators/scaffold_generator_test.rb | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'railties/test') diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 436fbd5d73..b9c2e791da 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -558,4 +558,59 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_match(/6 runs, 8 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`) end end + + def test_scaffold_on_invoke_inside_mountable_engine + Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --mountable` } + engine_path = File.join(destination_root, "bukkits") + + Dir.chdir(engine_path) do + quietly { `bin/rails generate scaffold User name:string age:integer` } + + assert File.exist?("app/models/bukkits/user.rb") + assert File.exist?("test/models/bukkits/user_test.rb") + assert File.exist?("test/fixtures/bukkits/users.yml") + + assert File.exist?("app/controllers/bukkits/users_controller.rb") + assert File.exist?("test/controllers/bukkits/users_controller_test.rb") + + assert File.exist?("app/views/bukkits/users/index.html.erb") + assert File.exist?("app/views/bukkits/users/edit.html.erb") + assert File.exist?("app/views/bukkits/users/show.html.erb") + assert File.exist?("app/views/bukkits/users/new.html.erb") + assert File.exist?("app/views/bukkits/users/_form.html.erb") + + assert File.exist?("app/helpers/bukkits/users_helper.rb") + + assert File.exist?("app/assets/javascripts/bukkits/users.js") + assert File.exist?("app/assets/stylesheets/bukkits/users.css") + end + end + + def test_scaffold_on_revoke_inside_mountable_engine + Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --mountable` } + engine_path = File.join(destination_root, "bukkits") + + Dir.chdir(engine_path) do + quietly { `bin/rails generate scaffold User name:string age:integer` } + quietly { `bin/rails destroy scaffold User` } + + assert_not File.exist?("app/models/bukkits/user.rb") + assert_not File.exist?("test/models/bukkits/user_test.rb") + assert_not File.exist?("test/fixtures/bukkits/users.yml") + + assert_not File.exist?("app/controllers/bukkits/users_controller.rb") + assert_not File.exist?("test/controllers/bukkits/users_controller_test.rb") + + assert_not File.exist?("app/views/bukkits/users/index.html.erb") + assert_not File.exist?("app/views/bukkits/users/edit.html.erb") + assert_not File.exist?("app/views/bukkits/users/show.html.erb") + assert_not File.exist?("app/views/bukkits/users/new.html.erb") + assert_not File.exist?("app/views/bukkits/users/_form.html.erb") + + assert_not File.exist?("app/helpers/bukkits/users_helper.rb") + + assert_not File.exist?("app/assets/javascripts/bukkits/users.js") + assert_not File.exist?("app/assets/stylesheets/bukkits/users.css") + end + end end -- cgit v1.2.3 From ad44d7a054f3897154d5c275ddecade4032d0d54 Mon Sep 17 00:00:00 2001 From: schneems Date: Tue, 14 Mar 2017 17:16:32 -0500 Subject: Raise when using a bad symlink There was a case where a dev made a symlink that worked on some machines and not on others. The issue manifested itself on a machine with `RAILS_ENV=staging` as the had their `config/environments/staging.rb` symlinked to another config file. The behavior was very hard to track down. Current behavior: If you use a bad symlink in a file, you get no warnings or failures or anything. If you have a bad symlink it just ignores the file as if it didn't exist (`File.exist?` returns false for a bad symlink). Patch behavior: With this patch when a file is not present we check if a symlink exists. If it does, that indicates there is a bad symlink and we should raise ``` File "config/environments/staging.rb" is a symlink that does not point to a valid file ``` --- railties/test/paths_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'railties/test') diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index 7b2551062a..f3db0a51d2 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -274,3 +274,23 @@ class PathsTest < ActiveSupport::TestCase end end end + +class PathsIntegrationTest < ActiveSupport::TestCase + test "A failed symlink is still a valid file" do + Dir.mktmpdir do |dir| + Dir.chdir(dir) do + FileUtils.mkdir_p("foo") + File.symlink("foo/doesnotexist.rb", "foo/bar.rb") + assert_equal true, File.symlink?("foo/bar.rb") + + root = Rails::Paths::Root.new("foo") + root.add "bar.rb" + + exception = assert_raises(RuntimeError) do + root["bar.rb"].existent + end + assert_match File.expand_path("foo/bar.rb"), exception.message + end + end + end +end -- cgit v1.2.3 From bd5256a168c66a126ab6880b52c0bd8c45aeae29 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Wed, 15 Mar 2017 19:07:36 +0900 Subject: Rename test of `app:update` to more appropriate name `rails:update` was changed to `app:update` in 6fb31638c8b61731103d4963272755b217a2df87. Therefore, I think that it is better that the test name is also `app_update`. --- railties/test/generators/app_generator_test.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'railties/test') diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 986afb6d2a..79fe4e4eb7 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -133,7 +133,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_rails_update_generates_correct_session_key + def test_app_update_generates_correct_session_key app_root = File.join(destination_root, "myapp") run_generator [app_root] @@ -156,7 +156,7 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_no_file "config/initializers/cors.rb" end - def test_rails_update_keep_the_cookie_serializer_if_it_is_already_configured + def test_app_update_keep_the_cookie_serializer_if_it_is_already_configured app_root = File.join(destination_root, "myapp") run_generator [app_root] @@ -168,7 +168,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_rails_update_set_the_cookie_serializer_to_marshal_if_it_is_not_already_configured + def test_app_update_set_the_cookie_serializer_to_marshal_if_it_is_not_already_configured app_root = File.join(destination_root, "myapp") run_generator [app_root] @@ -183,7 +183,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_rails_update_dont_set_file_watcher + def test_app_update_dont_set_file_watcher app_root = File.join(destination_root, "myapp") run_generator [app_root] @@ -197,7 +197,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_rails_update_does_not_create_new_framework_defaults_by_default + def test_app_update_does_not_create_new_framework_defaults_by_default app_root = File.join(destination_root, "myapp") run_generator [app_root] @@ -215,7 +215,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_rails_update_does_not_create_rack_cors + def test_app_update_does_not_create_rack_cors app_root = File.join(destination_root, "myapp") run_generator [app_root] @@ -227,7 +227,7 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_rails_update_does_not_remove_rack_cors_if_already_present + def test_app_update_does_not_remove_rack_cors_if_already_present app_root = File.join(destination_root, "myapp") run_generator [app_root] -- cgit v1.2.3 From 4183ee882055f997fe61804ba43ee4c168b2477c Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Sat, 18 Mar 2017 07:30:30 +1030 Subject: Track the version-compatible config settings inside railties Instead of forcing new applications to carry an initializer that just switches things to what their default "should" be, we can handle it internally. The initializer is then only used by upgraders: it shows what the new default would be (commented out), while their upgraded application continues to operate as it did before. Under this model, a multiply-upgraded application could accumulate several new_framework_defaults_*.rb files, for each release series it has traversed. A given release series only needs to generate the latest, though, because we don't support `rails app:upgrade` while skipping releases. --- railties/test/generators/api_app_generator_test.rb | 9 --------- railties/test/generators/app_generator_test.rb | 21 ++++++++------------- 2 files changed, 8 insertions(+), 22 deletions(-) (limited to 'railties/test') diff --git a/railties/test/generators/api_app_generator_test.rb b/railties/test/generators/api_app_generator_test.rb index bdef1798f8..bb8b29501a 100644 --- a/railties/test/generators/api_app_generator_test.rb +++ b/railties/test/generators/api_app_generator_test.rb @@ -61,15 +61,6 @@ class ApiAppGeneratorTest < Rails::Generators::TestCase end end - def test_generator_skips_per_form_csrf_token_and_origin_check_configs_for_api_apps - run_generator - - assert_file "config/initializers/new_framework_defaults.rb" do |initializer_content| - assert_no_match(/per_form_csrf_tokens/, initializer_content) - assert_no_match(/forgery_protection_origin_check/, initializer_content) - end - end - private def default_files diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 79fe4e4eb7..281376cdee 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -156,6 +156,10 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_no_file "config/initializers/cors.rb" end + def test_new_application_doesnt_need_defaults + assert_no_file "config/initializers/new_framework_defaults_5_1.rb" + end + def test_app_update_keep_the_cookie_serializer_if_it_is_already_configured app_root = File.join(destination_root, "myapp") run_generator [app_root] @@ -197,21 +201,18 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_app_update_does_not_create_new_framework_defaults_by_default + def test_app_update_create_new_framework_defaults app_root = File.join(destination_root, "myapp") run_generator [app_root] - FileUtils.rm("#{app_root}/config/initializers/new_framework_defaults.rb") + assert_no_file "#{app_root}/config/initializers/new_framework_defaults_5_1.rb" stub_rails_application(app_root) do generator = Rails::Generators::AppGenerator.new ["rails"], { update: true }, destination_root: app_root, shell: @shell generator.send(:app_const) quietly { generator.send(:update_config_files) } - assert_file "#{app_root}/config/initializers/new_framework_defaults.rb" do |content| - assert_match(/Rails\.application\.config.active_record\.belongs_to_required_by_default = false/, content) - assert_no_match(/Rails\.application\.config\.ssl_options/, content) - end + assert_file "#{app_root}/config/initializers/new_framework_defaults_5_1.rb" end end @@ -367,10 +368,6 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file "bin/update" do |update_content| assert_no_match(/db:migrate/, update_content) end - - assert_file "config/initializers/new_framework_defaults.rb" do |initializer_content| - assert_no_match(/belongs_to_required_by_default/, initializer_content) - end end def test_generator_if_skip_action_mailer_is_given @@ -415,9 +412,7 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_no_match(/config\.assets\.js_compressor = :uglifier/, content) assert_no_match(/config\.assets\.css_compressor = :sass/, content) end - assert_file "config/initializers/new_framework_defaults.rb" do |content| - assert_no_match(/unknown_asset_fallback/, content) - end + assert_no_file "config/initializers/new_framework_defaults_5_1.rb" end def test_generator_if_skip_yarn_is_given -- cgit v1.2.3 From 53e6fe96b5664e92ee6cd64d08681254fc816f44 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Sun, 19 Mar 2017 17:20:41 +0900 Subject: Correctly check whether key is defined in configuration It can not check correctly with `defined?` ```ruby irb(main):001:0> Rails.application.config.active_record => {:maintain_test_schema=>true, :belongs_to_required_by_default=>true} irb(main):002:0> defined?(Rails.application.config.active_record) => nil ``` Follow up to #28469 --- railties/test/generators/app_generator_test.rb | 12 ++++++++++++ railties/test/railties/mounted_engine_test.rb | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'railties/test') diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 281376cdee..ac43e8d22d 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -160,6 +160,18 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_no_file "config/initializers/new_framework_defaults_5_1.rb" end + def test_new_application_load_defaults + app_root = File.join(destination_root, "myfirstapp") + run_generator [app_root] + output = nil + + Dir.chdir(app_root) do + output = `./bin/rails r "puts Rails.application.config.assets.unknown_asset_fallback"` + end + + assert_equal "false\n", output + end + def test_app_update_keep_the_cookie_serializer_if_it_is_already_configured app_root = File.join(destination_root, "myapp") run_generator [app_root] diff --git a/railties/test/railties/mounted_engine_test.rb b/railties/test/railties/mounted_engine_test.rb index 5838d0d7e7..6639e55382 100644 --- a/railties/test/railties/mounted_engine_test.rb +++ b/railties/test/railties/mounted_engine_test.rb @@ -141,7 +141,7 @@ module ApplicationTests end def engine_asset_path - render inline: "<%= asset_path 'images/foo.png' %>" + render inline: "<%= asset_path 'images/foo.png', skip_pipeline: true %>" end end end -- cgit v1.2.3 From 6dfe25a86c7cad9334cd08833c67f501cc37de83 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Mon, 20 Mar 2017 07:30:45 +0900 Subject: Fix `warning: character class has duplicated range` This fixes the following warnings: ``` railties/test/generators/encrypted_secrets_generator_test.rb:15: warning: character class has duplicated range: /[\w\d]+/ railties/test/generators/encrypted_secrets_generator_test.rb:18: warning: character class has duplicated range: /production:\n# external_api_key: [\w\d]+/ railties/test/generators/encrypted_secrets_generator_test.rb:19: warning: character class has duplicated range: /production:\n# external_api_key: [\w\d]+/ ``` --- railties/test/generators/encrypted_secrets_generator_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'railties/test') diff --git a/railties/test/generators/encrypted_secrets_generator_test.rb b/railties/test/generators/encrypted_secrets_generator_test.rb index 747abf19ed..21fdcab19f 100644 --- a/railties/test/generators/encrypted_secrets_generator_test.rb +++ b/railties/test/generators/encrypted_secrets_generator_test.rb @@ -12,11 +12,11 @@ class EncryptedSecretsGeneratorTest < Rails::Generators::TestCase def test_generates_key_file_and_encrypted_secrets_file run_generator - assert_file "config/secrets.yml.key", /[\w\d]+/ + assert_file "config/secrets.yml.key", /\w+/ assert File.exist?("config/secrets.yml.enc") - assert_no_match(/production:\n# external_api_key: [\w\d]+/, IO.binread("config/secrets.yml.enc")) - assert_match(/production:\n# external_api_key: [\w\d]+/, Rails::Secrets.read) + assert_no_match(/production:\n# external_api_key: \w+/, IO.binread("config/secrets.yml.enc")) + assert_match(/production:\n# external_api_key: \w+/, Rails::Secrets.read) end def test_appends_to_gitignore -- cgit v1.2.3 From a42351acbc5406ab0825befe641144b4cb1ee6bf Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Mon, 16 Jan 2017 22:27:51 +0900 Subject: Add `app:update` task to engines Occasionally we update the file generated by engine. Therefore, I think that there is a task for updating as well as application in the engine, it is convenient for updating. --- railties/test/generators/plugin_generator_test.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'railties/test') diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index 8ec096e5c6..afb37b6a99 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -1,6 +1,7 @@ require "generators/generators_test_helper" require "rails/generators/rails/plugin/plugin_generator" require "generators/shared_generator_tests" +require "rails/engine/updater" DEFAULT_PLUGIN_FILES = %w( .gitignore @@ -731,6 +732,21 @@ class PluginGeneratorTest < Rails::Generators::TestCase end end + def test_app_update_generates_bin_file + run_generator [destination_root, "--mountable"] + + Object.const_set("ENGINE_ROOT", destination_root) + FileUtils.rm("#{destination_root}/bin/rails") + + quietly { Rails::Engine::Updater.run(:create_bin_files) } + + assert_file "#{destination_root}/bin/rails" do |content| + assert_match(%r|APP_PATH = File\.expand_path\('\.\./\.\./test/dummy/config/application', __FILE__\)|, content) + end + ensure + Object.send(:remove_const, "ENGINE_ROOT") + end + private def action(*args, &block) -- cgit v1.2.3 From 8ff7ca5d112a5db5c1449f687e5bdc15e3670579 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Mon, 20 Mar 2017 19:57:56 +0100 Subject: Default to yielding a `form` variable. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit More intention revealing and means `f` can go F itself 😋 --- railties/test/generators/scaffold_generator_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'railties/test') diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index b9c2e791da..0e0ef7d293 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -437,8 +437,8 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end assert_file "app/views/accounts/_form.html.erb" do |content| - assert_match(/^\W{4}<%= f\.text_field :name %>/, content) - assert_match(/^\W{4}<%= f\.text_field :currency_id %>/, content) + assert_match(/^\W{4}<%= form\.text_field :name %>/, content) + assert_match(/^\W{4}<%= form\.text_field :currency_id %>/, content) end end @@ -461,8 +461,8 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end assert_file "app/views/users/_form.html.erb" do |content| - assert_match(/<%= f\.password_field :password %>/, content) - assert_match(/<%= f\.password_field :password_confirmation %>/, content) + assert_match(/<%= form\.password_field :password %>/, content) + assert_match(/<%= form\.password_field :password_confirmation %>/, content) end assert_file "app/views/users/index.html.erb" do |content| -- cgit v1.2.3 From d0accc23b3d01a7d35e73c5dc901014d883ef5f7 Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Tue, 21 Mar 2017 19:07:40 -0400 Subject: CLI arg "host" has precedence over ENV var "host" This is a regression from when the server command switched to its own argument parser, as opposed to Rack's. Rack's argument parser, when provided with a "host" argument, gives that value precedence over environment variables. --- railties/test/commands/server_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'railties/test') diff --git a/railties/test/commands/server_test.rb b/railties/test/commands/server_test.rb index d21a80982b..2d1f071969 100644 --- a/railties/test/commands/server_test.rb +++ b/railties/test/commands/server_test.rb @@ -139,6 +139,14 @@ class Rails::ServerTest < ActiveSupport::TestCase end end + def test_argument_precedence_over_environment_variable + switch_env "HOST", "1.2.3.4" do + args = ["-b", "127.0.0.1"] + options = parse_arguments(args) + assert_equal "127.0.0.1", options[:Host] + end + end + def test_records_user_supplied_options server_options = parse_arguments(["-p", 3001]) assert_equal [:Port], server_options[:user_supplied_options] -- cgit v1.2.3 From 142db6269bf6486440627d9cd884dc0e33179313 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Wed, 22 Mar 2017 21:49:33 +0900 Subject: Don't comment out config.file_watcher during Rails upgrade This is necessary only when updating to Rails 5.0, it is not necessary for updating to 5.1. Related #24243 --- railties/test/generators/app_generator_test.rb | 14 -------------- 1 file changed, 14 deletions(-) (limited to 'railties/test') diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index ac43e8d22d..f6cdbd34fe 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -199,20 +199,6 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_app_update_dont_set_file_watcher - app_root = File.join(destination_root, "myapp") - run_generator [app_root] - - stub_rails_application(app_root) do - generator = Rails::Generators::AppGenerator.new ["rails"], [], destination_root: app_root, shell: @shell - generator.send(:app_const) - quietly { generator.send(:update_config_files) } - assert_file "#{app_root}/config/environments/development.rb" do |content| - assert_match(/# config.file_watcher/, content) - end - end - end - def test_app_update_create_new_framework_defaults app_root = File.join(destination_root, "myapp") run_generator [app_root] -- cgit v1.2.3 From a06a643e0572b8c983738c068ae637d020188c97 Mon Sep 17 00:00:00 2001 From: Robert Thau Date: Tue, 21 Mar 2017 23:07:07 -0400 Subject: Correctly reset ARGV for "rails runner `CODE' arg arg arg..." The code itself should not be in the ARGV vector. Fixes #28515 --- railties/test/application/runner_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'railties/test') diff --git a/railties/test/application/runner_test.rb b/railties/test/application/runner_test.rb index 7d058f6ee6..e8fac442bd 100644 --- a/railties/test/application/runner_test.rb +++ b/railties/test/application/runner_test.rb @@ -35,6 +35,14 @@ module ApplicationTests assert_match "42", Dir.chdir(app_path) { `bin/rails runner "puts User.count"` } end + def test_should_set_argv_when_running_code + output = Dir.chdir(app_path) { + # Both long and short args, at start and end of ARGV + `bin/rails runner "puts ARGV.join(',')" --foo a1 -b a2 a3 --moo` + } + assert_equal "--foo,a1,-b,a2,a3,--moo", output.chomp + end + def test_should_run_file app_file "bin/count_users.rb", <<-SCRIPT puts User.count -- cgit v1.2.3 From 9bc6178e008714828969dcaf1cfde8f62dc9d41f Mon Sep 17 00:00:00 2001 From: Robert Thau Date: Wed, 22 Mar 2017 12:36:27 -0400 Subject: Fixup trailing whitespace, per complaints from CodeClimate. --- railties/test/application/runner_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/test') diff --git a/railties/test/application/runner_test.rb b/railties/test/application/runner_test.rb index e8fac442bd..0c45bc398a 100644 --- a/railties/test/application/runner_test.rb +++ b/railties/test/application/runner_test.rb @@ -36,7 +36,7 @@ module ApplicationTests end def test_should_set_argv_when_running_code - output = Dir.chdir(app_path) { + output = Dir.chdir(app_path) { # Both long and short args, at start and end of ARGV `bin/rails runner "puts ARGV.join(',')" --foo a1 -b a2 a3 --moo` } -- cgit v1.2.3 From 42198064c35ff3b701496309f90df2abc229efbe Mon Sep 17 00:00:00 2001 From: claudiob Date: Thu, 23 Mar 2017 08:07:04 -0700 Subject: Remove -j (--javascript) option from `rails new` The "-j" option was added 5 years ago (https://github.com/rails/rails/commit/d9c39c3a) when we wanted to support prototype-rails and jquery-rails. Prototype is not as popular and jQuery is not a requirement anymore. Still the "-j" option can be used to install *any* gem that ends in "-rails". This "might" open security issues and does not bring great benefits anymore. If you know which "-rails"-ending gem you want to install, you can manually add it to the Gemfile just like any other gem. --- railties/test/generators/app_generator_test.rb | 8 -------- 1 file changed, 8 deletions(-) (limited to 'railties/test') diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index f6cdbd34fe..90aebee144 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -452,14 +452,6 @@ class AppGeneratorTest < Rails::Generators::TestCase end end - def test_inclusion_of_javascript_libraries_if_required - run_generator [destination_root, "-j", "jquery"] - assert_file "app/assets/javascripts/application.js" do |contents| - assert_match %r{^//= require jquery}, contents - end - assert_gem "jquery-rails" - end - def test_javascript_is_skipped_if_required run_generator [destination_root, "--skip-javascript"] -- cgit v1.2.3 From e7c4fe581c6eab18df706875eb57df82e27a1e8f Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Fri, 24 Mar 2017 08:31:30 +0900 Subject: Remove unnecessary files to API-only Applications when `app:task` task executed --- railties/test/generators/api_app_generator_test.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'railties/test') diff --git a/railties/test/generators/api_app_generator_test.rb b/railties/test/generators/api_app_generator_test.rb index bb8b29501a..2edb39c8e8 100644 --- a/railties/test/generators/api_app_generator_test.rb +++ b/railties/test/generators/api_app_generator_test.rb @@ -61,6 +61,28 @@ class ApiAppGeneratorTest < Rails::Generators::TestCase end end + def test_app_update_does_not_generate_unnecessary_config_files + run_generator + + generator = Rails::Generators::AppGenerator.new ["rails"], + { api: true, update: true }, destination_root: destination_root, shell: @shell + quietly { generator.send(:update_config_files) } + + assert_no_file "config/initializers/cookies_serializer.rb" + assert_no_file "config/initializers/assets.rb" + assert_no_file "config/initializers/new_framework_defaults_5_1.rb" + end + + def test_app_update_does_not_generate_unnecessary_bin_files + run_generator + + generator = Rails::Generators::AppGenerator.new ["rails"], + { api: true, update: true }, destination_root: destination_root, shell: @shell + quietly { generator.send(:update_bin_files) } + + assert_no_file "bin/yarn" + end + private def default_files -- cgit v1.2.3 From 27f103fc7e3260efe0b8dde66bf5354f2202ee32 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Sun, 26 Mar 2017 11:42:39 +0200 Subject: add field ids when generating a scaffold form. This is a follow up to a6d065e. When using `form_with` you must supply field ids manually. Since the scaffold generator is using labels we need to make sure that they are linked up properly. --- railties/test/generators/scaffold_generator_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'railties/test') diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 0e0ef7d293..bc76cead18 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -437,8 +437,8 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end assert_file "app/views/accounts/_form.html.erb" do |content| - assert_match(/^\W{4}<%= form\.text_field :name %>/, content) - assert_match(/^\W{4}<%= form\.text_field :currency_id %>/, content) + assert_match(/^\W{4}<%= form\.text_field :name, id: :account_name %>/, content) + assert_match(/^\W{4}<%= form\.text_field :currency_id, id: :account_currency_id %>/, content) end end @@ -461,8 +461,8 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase end assert_file "app/views/users/_form.html.erb" do |content| - assert_match(/<%= form\.password_field :password %>/, content) - assert_match(/<%= form\.password_field :password_confirmation %>/, content) + assert_match(/<%= form\.password_field :password, id: :user_password %>/, content) + assert_match(/<%= form\.password_field :password_confirmation, id: :user_password_confirmation %>/, content) end assert_file "app/views/users/index.html.erb" do |content| -- cgit v1.2.3 From 9a0ad3f5efd34983baf873235356517c680860ee Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Mon, 27 Mar 2017 08:29:20 +0900 Subject: Do not show hidden namespaces in destroy commnad help --- railties/test/application/generators_test.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'railties/test') diff --git a/railties/test/application/generators_test.rb b/railties/test/application/generators_test.rb index ee0d697599..fe581db286 100644 --- a/railties/test/application/generators_test.rb +++ b/railties/test/application/generators_test.rb @@ -189,6 +189,9 @@ module ApplicationTests FileUtils.cd(rails_root) do output = `bin/rails generate --help` assert_no_match "active_record:migration", output + + output = `bin/rails destroy --help` + assert_no_match "active_record:migration", output end end end -- cgit v1.2.3 From 14739b5e27987e1eee080579fa0c80eea07347b8 Mon Sep 17 00:00:00 2001 From: Philippe Guay Date: Sun, 19 Mar 2017 22:39:14 -0400 Subject: Fixes #28359 Add stronger assertions to rake migration tasks to make sure the user is providing a numeric VERSION An empty string was getting converted to version = 0. This would in turn pass the presence check. Address linting warning Add test for rake task and refactor code to meet expectations In particular passing VERSION=0 should not raise an error. Addressed Comments for PR #28485. Trimmed empty lines + change of wording for error message Adjust test for change of wording in error message Change condition to follow rails idioms --- railties/test/application/rake/migrations_test.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'railties/test') diff --git a/railties/test/application/rake/migrations_test.rb b/railties/test/application/rake/migrations_test.rb index 76cb302c62..00f6620188 100644 --- a/railties/test/application/rake/migrations_test.rb +++ b/railties/test/application/rake/migrations_test.rb @@ -37,6 +37,22 @@ module ApplicationTests end end + test "migration with empty version" do + Dir.chdir(app_path) do + output = `bin/rails db:migrate VERSION= 2>&1` + assert_match(/Empty VERSION provided/, output) + + output = `bin/rails db:migrate:redo VERSION= 2>&1` + assert_match(/Empty VERSION provided/, output) + + output = `bin/rails db:migrate:up VERSION= 2>&1` + assert_match(/VERSION is required/, output) + + output = `bin/rails db:migrate:down VERSION= 2>&1` + assert_match(/VERSION is required - To go down one migration, use db:rollback/, output) + end + end + test "model and migration generator with change syntax" do Dir.chdir(app_path) do `bin/rails generate model user username:string password:string; -- cgit v1.2.3 From 72b4265766400a752dae27a3d7d94ccd86df00cb Mon Sep 17 00:00:00 2001 From: ota42y Date: Thu, 23 Mar 2017 13:40:15 +0900 Subject: ignore system test gems on Gemfile when execute with --skip-test option --- railties/test/generators/app_generator_test.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'railties/test') diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index f6cdbd34fe..8e18162df7 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -436,6 +436,30 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file "Gemfile", /^# gem 'redis'/ end + def test_generator_if_skip_test_is_given + run_generator [destination_root, "--skip-test"] + assert_file "Gemfile" do |content| + assert_no_match(/capybara/, content) + assert_no_match(/selenium-webdriver/, content) + end + end + + def test_generator_if_skip_system_test_is_given + run_generator [destination_root, "--skip_system_test"] + assert_file "Gemfile" do |content| + assert_no_match(/capybara/, content) + assert_no_match(/selenium-webdriver/, content) + end + end + + def test_generator_if_api_is_given + run_generator [destination_root, "--api"] + assert_file "Gemfile" do |content| + assert_no_match(/capybara/, content) + assert_no_match(/selenium-webdriver/, content) + end + end + def test_inclusion_of_javascript_runtime run_generator if defined?(JRUBY_VERSION) -- cgit v1.2.3 From 825447130d1a34557d7ba6d8985d2e0715fcbf8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 27 Mar 2017 22:49:24 -0400 Subject: Fix the tests to test what they should be testing With Rack::Test the headers needs to match the `HTTP_` format. The tests were passing before because they are not asserting the response was a cache hit. --- railties/test/application/middleware/cache_test.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'railties/test') diff --git a/railties/test/application/middleware/cache_test.rb b/railties/test/application/middleware/cache_test.rb index dc1d816dc5..93b5263bf7 100644 --- a/railties/test/application/middleware/cache_test.rb +++ b/railties/test/application/middleware/cache_test.rb @@ -117,12 +117,12 @@ module ApplicationTests assert_equal "miss, store", last_response.headers["X-Rack-Cache"] assert_equal "public", last_response.headers["Cache-Control"] - body = last_response.body etag = last_response.headers["ETag"] - get "/expires/expires_etag", {}, "If-None-Match" => etag + get "/expires/expires_etag", {}, "HTTP_IF_NONE_MATCH" => etag assert_equal "stale, valid, store", last_response.headers["X-Rack-Cache"] - assert_equal body, last_response.body + assert_equal 304, last_response.status + assert_equal "", last_response.body end def test_cache_works_with_etags_private @@ -137,7 +137,7 @@ module ApplicationTests body = last_response.body etag = last_response.headers["ETag"] - get "/expires/expires_etag", { private: true }, "If-None-Match" => etag + get "/expires/expires_etag", { private: true }, "HTTP_IF_NONE_MATCH" => etag assert_equal "miss", last_response.headers["X-Rack-Cache"] assert_not_equal body, last_response.body end @@ -151,12 +151,12 @@ module ApplicationTests assert_equal "miss, store", last_response.headers["X-Rack-Cache"] assert_equal "public", last_response.headers["Cache-Control"] - body = last_response.body last = last_response.headers["Last-Modified"] - get "/expires/expires_last_modified", {}, "If-Modified-Since" => last + get "/expires/expires_last_modified", {}, "HTTP_IF_MODIFIED_SINCE" => last assert_equal "stale, valid, store", last_response.headers["X-Rack-Cache"] - assert_equal body, last_response.body + assert_equal 304, last_response.status + assert_equal "", last_response.body end def test_cache_works_with_last_modified_private @@ -171,7 +171,7 @@ module ApplicationTests body = last_response.body last = last_response.headers["Last-Modified"] - get "/expires/expires_last_modified", { private: true }, "If-Modified-Since" => last + get "/expires/expires_last_modified", { private: true }, "HTTP_IF_MODIFIED_SINCE" => last assert_equal "miss", last_response.headers["X-Rack-Cache"] assert_not_equal body, last_response.body end -- cgit v1.2.3