Commit Graph

14917 Commits

Author SHA1 Message Date
Jonathan Hefner
4986cfb26e
Merge pull request #49141 from ghiculescu/declarative-specs-2
Fix more bugs in declarative specs filter
2023-09-06 21:47:59 -05:00
Alex
0a8e5374b9 Simplify normalization of regexp test filters
Follow-up to #47942.

This commit simplifies the normalization of regexp test filters.
Instead of modifying a given regexp to match a union of whitespace and
underscores, the regexp is unioned with the underscore-normalized
version of itself.  This allows filters that include escaped spaces,
such as `/foo\ bar/`.

This commit also fixes `Rails::LineFiltering#run` such that
normalization isn't reapplied for every test suite.  Thus
`normalize_declarative_test_filter` is no longer required to be
idempotent.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-09-06 21:30:51 -05:00
Akhil G Krishnan
59854da1bf
Removed Webpacker files 2023-09-06 17:11:58 +00:00
Javier Aranda
ea351f0f49
Do not generate configuration for sprockets if propshaft is used 2023-09-05 10:11:45 +02:00
Sean Doyle
e85a3ec624
Change has_secure_token default to on: :initialize
Follow-up to [#47420][]

With the changes made in [#47420][], `has_secure_token` declarations can
be configured to execute in an `after_initialize` callback. This commit
proposed a new Rails 7.1 default: generate all `has_secure_token` values
when their corresponding models are initialized.

To preserve pre-7.1 behavior, applications can set
`config.active_record.generate_secure_token_on = :create`.

By default, generate the value when the model is initialized:

```ruby
class User < ApplicationRecord
  has_secure_token
end

record = User.new
record.token # => "fwZcXX6SkJBJRogzMdciS7wf"
```

With `config.active_record.generate_secure_token_on = :create`, generate
the value when the model is created:

```ruby
 # config/application.rb
config.active_record.generate_secure_token_on = :create

 # app/models/user.rb
class User < ApplicationRecord
  has_secure_token on: :create
end

record = User.new
record.token # => nil
record.save!
record.token # => "fwZcXX6SkJBJRogzMdciS7wf"
```

[#47420]: https://github.com/rails/rails/pull/47420

Co-authored-by: Hartley McGuire <skipkayhil@gmail.com>
2023-09-01 20:17:22 +00:00
Jean Boussier
a7fdc1cdb4 Optimize ActiveRecord::LogSubscriber#query_source_location
`Kernel#caller` has linear performance based on how deep the
stack is. While this is a development only feature, it can end
up being quite slow.

Ruby 3.2 introduced `Thread.each_caller_location`, which lazily
yield `Backtrace::Location` objects.

Ref: https://bugs.ruby-lang.org/issues/16663

This is perfect for this use case as we are searching for the
closest frame that matches a pattern, saving us from collecting
the entire backtrace.
2023-08-31 11:56:55 +02:00
fatkodima
14d9b3e07e Add ability to show slow tests to the test runner 2023-08-30 00:08:56 +03:00
Wojciech Wnętrzak
b872b36e0c
Rename MRSK to Kamal
https://github.com/basecamp/kamal/releases/tag/v0.16.0

[ci skip]
2023-08-28 09:42:15 +02:00
Xavier Noria
b16adcd4e4 Generate config.autoload_lib(...) for new apps 2023-08-25 09:22:25 +02:00
Akhil G Krishnan
0f7f9e3d5e [skip ci] Moved sh to bash and added missing $ 2023-08-24 18:48:20 +05:30
Aaron Patterson
3338621efa
Use a temporary file for storing unencrypted files while editing
When we're editing the contents of encrypted files, we should use the
`Tempfile` class because it creates temporary files with restrictive
permissions.  This prevents other users on the same system from reading
the contents of those files while the user is editing them.

[CVE-2023-38037]
2023-08-22 10:23:15 -07:00
Shouichi Kamiya
b8decf79d8 Add an option to start rails console in sandbox mode by default
To avoid accidental writing to the production database, I always start
rails console in sandbox mode. I only start rails console in non-sandbox
mode when I'm sure I want to write to the production database.

`sandbox_by_default` option is added to start rails console in sandbox
mode by default. With this option turned on, `--no-sandbox` must be
specified to start rails in non-sandbox mode.

Note that this option is ignored when rails environment is development
or test.
2023-08-22 09:23:52 +09:00
Guillermo Iguaran
4ec3a986d5
Merge pull request #48959 from skipkayhil/hm-clean-filters-requires
Remove uneeded requires of core_ext/string/filters
2023-08-18 16:03:44 -07:00
Hartley McGuire
2d61a206f4
Fix served_url printed when using Puma & Rack 3
This `#use_puma?` check was [introduced][1] because Rails could
potentially display the wrong server URL in development if another host
or port is configured in `config/puma.rb`.

However, in Rack 3 the the name of the server class changed from
`Rack::Handler::Puma` to `Rackup::Handler::Puma`, which means that the
served_url is being logged again.

This commit fixes the check to accept either version of the class.
Additionally, puma was updated in Gemfile.lock because Puma 6.0.x prints
a deprecation warning about Rack::Handler that's fixed in 6.1.0.

[1]: 29648ff60e66c674b3cc49151df15dc100ffb4f4
2023-08-17 20:01:19 -04:00
Jonathan Hefner
92dcc9a09f Fix list item numbering on API docs main page [ci-skip]
Follow-up to #47178.

RDoc's Markdown parser requires multi-line list items to use a hanging
indent of at least four spaces.  Otherwise, trailing lines will be
rendered as an independent paragraph, and the next list item will start
a new list.
2023-08-17 16:22:16 -05:00
Hartley McGuire
92b716204e
Do not suggest adding non-existent gems to Gemfile
While these are valid Rack Handler names, their handler name is not the
same as their gem name.

| handler | gem        |
| ------- | ---------- |
| fastcgi | fcgi       |
| lsws    | ruby-lsapi |
2023-08-16 22:05:13 -04:00
Hartley McGuire
ff6e885d59
Remove uneeded requires of core_ext/string/filters
`actionpack/lib/action_dispatch/routing.rb`
- added: 013745151be062aa4d0fc1f2a008a7303fdb6e04
- removed: 93034ad7fea7e00562103a7cd0acfab19bbfadf9

`activejob/lib/active_job/log_subscriber.rb`
- added: b314ab555e0d85e6efb41be94fb5f3a157bb12fe
- removed: 5ab2034730feacfc2caee418f8c0b55191d27427

`activemodel/lib/active_model/errors.rb`
- added: cf7fac7e29bb2816412c949fdaed3d61a923eb23
- removed: 9de6457ab0767ebab7f2c8bc583420fda072e2bd

`activerecord/lib/active_record/core.rb`
- added: b3bfa361c503e107aff4dee5edf79bd7fd3d3725
- removed: e1066f450d1a99c9a0b4d786b202e2ca82a4c3b3

`activesupport/lib/active_support/core_ext/module/introspection.rb`
- added: 358ac36edf1695fcbec0aa21f126a3d8b83d4b5a
- removed: 167b4153cac0069a21e0bb9689cb16f34f6abbaa

`activesupport/lib/active_support/duration.rb`
- added: 75924c4517c8f87712d3f59c11f10152ed57b9d8
- removed: a91ea1d51048342d13fc73f9b09ce4cfd086bb34

`railties/lib/rails/commands/server/server_command.rb`
- added: f2173648938b418d120f5a68d8f3862d8ae9dace
- removed: 553b86fc751c751db504bcbe2d033eb2bb5b6a0b

`railties/lib/rails/command/base.rb`
- added: 6813edc7d926965e5644cd8befaf229a35b9d8ca
- removed: b617a561d865a65cfc140caa0e3c4af4350bfcef
2023-08-16 17:39:25 -04:00
Akhil G Krishnan
8ae0b4ac7a [skip ci] Testing by line range documentation added in guide
Review changes updated

Update railties/lib/rails/commands/test/USAGE

Co-authored-by: Yasuo Honda <yasuo.honda@gmail.com>

Update guides/source/testing.md

Co-authored-by: Yasuo Honda <yasuo.honda@gmail.com>
2023-08-15 07:40:17 +05:30
Eileen M. Uchitelle
22fa76c125
Merge pull request #48911 from jhbabon/fix/schema-cache-settings
Ensure schema_cache_dump settings are propagated to SchemaReflection class
2023-08-10 10:57:57 -04:00
Eileen M. Uchitelle
030652eeb9
Merge pull request #48916 from vinistock/vs/fix_engine_rake_task_on_windows
Use Pathnames for comparisons in the engine rake tasks
2023-08-10 10:54:07 -04:00
Vinicius Stock
3db3bd10af
Use Pathnames for comparisons in the engine rake tasks
We cannot compare paths directly to strings or that might fail on
different operating system. In this case, comparing to `/` fails on
Windows and we end up in endless recursion. We need to use
`Pathname#root?` to check if we reached the root of the folder structure
2023-08-10 09:37:41 -04:00
Juan Hernández Babón
94274e6c80 Fix rubocop offenses in railties/test/application/configuration_test.rb 2023-08-09 15:42:12 +02:00
Juan Hernández Babón
d5984f4d80 Ensure schema_cache_dump settings are propagated to SchemaReflection class 2023-08-09 14:11:44 +02:00
r7kamura
7f52979f0a Remove unnecessary isolation_level setting from load_defaults 7.0
The default value of `ActiveSupport::IsolatedExecutionState.isolation_level` is set to `:thread`, regardless of whether `load_defaults 7.0` is used or not.

- https://github.com/rails/rails/blob/v7.0.4/activesupport/lib/active_support/isolated_execution_state.rb

If so, there is no need to set this in `load_defaults 7.0`. This Pull Request has been created to remove this unnecessary setting
2023-08-09 14:39:38 +09:00
Rafael Mendonça França
c1fb418007
Merge pull request #48888 from seanpdoyle/action-mailer-preview-empty-states
Action Mailer Preview empty states
2023-08-04 17:18:57 -04:00
Sean Doyle
c4b67ec56b Action Mailer Preview empty states
When an application defines mailers without any corresponding previews,
requests to `GET /rails/mailers` return a page with a blank `<body>`
element.

This entirely empty page can be confusing, since it's difficult to
distinguish the "success" state with an empty list and a "failure" state
with swallowed errors.

Similarly, when an `ActionMailer::Preview` subclass is defined, but
doesn't declare any actions, the response contains a mostly empty page.

This commit renders empty-state messaging for both scenarios, and links
to the [Action Mailer Basics][] guides.

To effectively cover that behavior, this commit also expands the Mailer
Preview test coverage to utilize [rails-dom-testing][] assertions.

[Action Mailer Basics]: https://guides.rubyonrails.org/action_mailer_basics.html#previewing-emails
[rails-dom-testing]: http://github.com/rails/rails-dom-testing
2023-08-04 13:47:21 -04:00
zzak
9511a86794
Remove overflow:hidden from welcome logo
This was originally added in #47781:

> ...rails logo hover on welcome page is being triggered on square instead of round image.

However, on Safari the border is being cut-off.

I'm not sure there is another workaround, but I am willing to accept a larger (square) clickable area to have a consistent drop shadow.
2023-08-04 18:49:59 +09:00
Mike Dalessio
bfeb580264
Introduce config.dom_testing_default_html_version
which controls the HTML parser used by rails-dom-testing assertions.

The config parameter is set to :html5 in Rails 7.1 if the Nokogiri
HTML5 parser is supported.
2023-08-03 11:16:52 -04:00
Sean Doyle
425f100f08 Add RUBY_VERSION conditional 2023-08-02 11:19:37 -04:00
Sean Doyle
9a53234695 Omit webdrivers gem from Gemfile template
As of Selenium 4.6, [the Selenium Manager is capable of managing Chrome
Driver installations and integrations][readme]. As of Selenium 4.11, the
Selenium Manager is capable of [capable of resolving the Chrome for
Testing installation][] path.

By omitting the `gem` declaration from the `Gemfile.tt`, newly generated
applications and applications updating their `Gemfile` in lockstep with
newer Rails versions can shed the dependency and avoid test failures
introduced by newly released Chrome versions (like, for example,
[titusfortner/webdrivers#247][]).

[readme]: 43f8ac436c (update-selenium-manager)
[titusfortner/webdrivers#247]: https://github.com/titusfortner/webdrivers/issues/247
[capable of resolving the Chrome for Testing installation]: https://github.com/rails/rails/pull/48847#issuecomment-1656756862

Co-authored-by: Titus Fortner <titusfortner@users.noreply.github.com>
2023-08-01 09:22:08 -04:00
Jean Boussier
35a614c227
Merge pull request #48793 from Shopify/define-attributes-initializer
Never connect to the database in define_attribute_methods initializer
2023-08-01 08:45:24 +02:00
Jean Boussier
741cd18c45
Merge pull request #48807 from shouichi/rails-test-line-range
Support filtering tests by line ranges
2023-08-01 08:40:50 +02:00
Akhil G Krishnan
29c7fa014a Removed unnecessary --no-skip-* app generator test 2023-07-28 22:39:28 +05:30
Guillermo Iguaran
5b45803984 Use constants defined in ActionDispatch::Constants instead of conditional assignments 2023-07-27 15:21:18 -07:00
Guillermo Iguaran
034cae80a9
Merge pull request #48818 from skipkayhil/hm-rack-list-permissions-policy
Add Rack::Lint to PermissionsPolicy tests
2023-07-27 13:56:03 -07:00
Akhil G Krishnan
5cabd9edbe Added test for --no-skip-docker command line argument
Update railties/test/generators/app_generator_test.rb

Co-authored-by: zzak <zzakscott@gmail.com>

removed unwanted spacing

Added a newline
2023-07-27 10:28:11 +05:30
Hartley McGuire
4991525abb
Add Rack::Lint to PermissionsPolicy tests
This adds additional test coverage to PermissionsPolicy::Middleware to
validate that it conforms to the Rack SPEC.

The only changes necessary were to use the appropriate header casing for
Content-Type and Feature-Policy. Since this was the only usage of the
CONTENT_TYPE constant, I opted to remove it, but I can replace it with a
DeprecatedConstantProxy if that's more desirable.
2023-07-26 13:34:14 -04:00
Jonathan Hefner
3efb84486e Support replacing cache compressor
This commit adds support for replacing the compressor used for
serialized cache entries.  Custom compressors must respond to `deflate`
and `inflate`.  For example:

  ```ruby
  module MyCompressor
    def self.deflate(string)
      # compression logic...
    end

    def self.inflate(compressed)
      # decompression logic...
    end
  end

  config.cache_store = :redis_cache_store, { compressor: MyCompressor }
  ```

As part of this work, cache stores now also support a `:serializer`
option.  Similar to the `:coder` option, serializers must respond to
`dump` and `load`. However, serializers are only responsible for
serializing a cached value, whereas coders are responsible for
serializing the entire `ActiveSupport::Cache::Entry` instance.
Additionally, the output from serializers can be automatically
compressed, whereas coders are responsible for their own compression.

Specifying a serializer instead of a coder also enables performance
optimizations, including the bare string optimization introduced by cache
format version 7.1.
2023-07-26 11:59:09 -05:00
Shouichi Kamiya
5931a68158 Support filtering tests by line ranges
The new syntax allows you to filter tests by line ranges. For example, the
following command runs tests between line 10 to 20.

```bash
$ rails test test/models/user_test.rb:10-20
```

Co-authored-by: Seonggi Yang <seonggi.yang@gmail.com>
Co-authored-by: Ryohei UEDA <ueda@anipos.co.jp>
Co-authored-by: oljfte <oljfte@gmail.com>
2023-07-26 15:00:08 +09:00
Jean Boussier
de765e37c1 Never connect to the database in define_attribute_methods initializer
Followup: https://github.com/rails/rails/pull/48743

After careful consideration, unless users have a schema cache dump loaded
and `check_schema_cache_dump_version = false`, we have no choice but
to arbitrate between resiliency and performance.

If we define attribute methods during boot, we allow them to be shared
between forked workers, and prevent the first requests to be slower.

However, by doing so we'd trigger a connection to the datase, which
if it's unresponsive could lead to workers not being able to restart
triggering a cascading failure.

Previously Rails used to be in some sort of middle-ground where
it would define attribute methods during boot if for some reason
it was already connected to the database at this point.

But this is now deemed undesirable, as an application initializer
inadvertantly establishing the database connection woudl lead to
a readically different behavior.
2023-07-24 16:37:50 +02:00
Guillermo Iguaran
463ce660db
Merge pull request #48755 from ioquatix/rack-3.1
Update tests to support changes in Rack 3.1 (head).
2023-07-19 11:13:29 -07:00
Jean Boussier
ff344f61a4
Merge pull request #48715 from jcoyne/patch-2
Clarify comment about debug log level.
2023-07-19 19:34:07 +02:00
Samuel Williams
9a2894d8bf
Remove unused dependency on deprecated rack/file.rb. 2023-07-19 15:36:57 +12:00
Jean Boussier
7e07fde9a3 Cleanup define_attribute_methods initializer
Followup: https://github.com/rails/rails/pull/48716

`model.connection_pool.schema_reflection` is never falsy, so that `if`
was pointless.

Instead we more properly check if the schema cache contains that table.

I also added some more comments to explain why the initializer tries
so hard not to touch the database.
2023-07-17 16:45:22 +02:00
Matthew Draper
b36f9186b0 Refactor Active Record Schema Cache to not hold a connection
A big problem with the current `SchemaCache` implementation is that it holds
a reference to a connection, and uses it to lazily query the schema when the
information is missing.

This cause issues in multi-threaded contexts because all the connections
of a pool share the same schema, so they are constantly setting themselves
as the connection the schema cache should use, and you can end up in
situation where Thread#1 query the schema cache, which end up using the
connection currently attributed to Thread#2.

For a very long time this worked more or less by accident, because all
connection adapters would have an internal Monitor.

However in https://github.com/rails/rails/pull/46519 we got rid of this
synchronization, revealing the pre-existing issue. Previously it would
work™,  but still wasn't ideal because of unexpected the connection
sharing between threads.

The idea here is to refactor the schema cache so that it doesn't hold a
reference onto any connection.

And instead of a `SchemaCache`, connections now have access to a
`SchemaReflection`. Now the connection that needs a schema information
is always provided as an argument, so that in case of a miss, it can be
used to populate the cache.

That should fix the database thread safety issues that were witnessed.

However note that at this stage, the underlying `SchemaCache` isn't
synchronized, so in case of a race condition, the same schema query
can be performed more than once. It could make sense to add synchronization
in a followup.

This refactoring also open the door to query the cache without a connection,
making it easier to eagerly define model attribute methods on boot without
establishing a connection to the database.

Co-Authored-By: Jean Boussier <jean.boussier@gmail.com>
Co-Authored-By: zzak <zzakscott@gmail.com>
2023-07-13 12:34:13 +02:00
Justin Coyne
68ba7582fb
Clarify comment about debug log level.
I believe the previous phrasing could be interpreted ambiguously. See discussion in #48713
2023-07-11 10:26:58 -05:00
Jean Boussier
5fbaa524b9 Active Record commit transaction on return, break and throw
Fix: https://github.com/rails/rails/issues/45017
Ref: https://github.com/rails/rails/pull/29333
Ref: https://github.com/ruby/timeout/pull/30

Historically only raised errors would trigger a rollback, but in Ruby `2.3`, the `timeout` library
started using `throw` to interupt execution which had the adverse effect of committing open transactions.

To solve this, in Active Record 6.1 the behavior was changed to instead rollback the transaction as it was safer
than to potentially commit an incomplete transaction.

Using `return`, `break` or `throw` inside a `transaction` block was essentially deprecated from Rails 6.1 onwards.

However with the release of `timeout 0.4.0`, `Timeout.timeout` now raises an error again, and Active Record is able
to return to its original, less surprising, behavior.
2023-07-10 10:37:42 +02:00
Eileen M. Uchitelle
190a43808a
Merge pull request #48560 from eileencodes/fix-parallel-testing-48547
Fix parallel testing (issue #48547)
2023-07-05 11:06:52 -04:00
eileencodes
c933cc4a41
Silence unnecessary warning about sqlite3
This test uses sqlite3 in production mode which results in a warning in
the test output. This change sets the config option for silencing this
warning so we don't see it in test output.
2023-07-05 11:01:33 -04:00
eileencodes
5cf5a2af13
Fix parallel testing (issue #48547)
Fixes 2 bugs in parallel testing.

The first bug was related to changes made in #45450 which meant that we were
no longer replacing the connection in parallel testing because the
config object is equal (we simply merge a new db name but the object id
of the config stays the same). This bug only manifested in mysql and
sqlite3 interestingly. It would fail on the internal metadata tables
because they were missing in the schema version check.

To fix this I introduced a `clobber: true` kwarg onto the connection
handler that allows us to bypass the functionality that won't make a new
connection if the config is the same. This is an easy way to fall back
to the old behavior from before this change. I only added `clobber`
to the `reconstruct_from_schema` call because we need to actually
replace the connection for these. It's not safe to add everywhere since
we don't always want to replace the connection.

After implementing this fix I was still seeing failures in the mysql
demo app I made due to the fact that `purge` was not re-establishing the
connection to a config that had a database defined. Neither sqlite3 or
postgresql were missing this.

I added a test for mysql2 so we don't have regressions in the future. I
think this was missed because sqlite3 only demonstrates the bug if it
was never successful on that worker and postgresql was fine.

Fixes #48547
2023-07-05 10:50:54 -04:00