Commit Graph

4131 Commits

Author SHA1 Message Date
Earlopain
c85eca47f0
Filter internal frames in deprecation warnings for Ruby 3.4
Followup to #50923
2024-04-20 15:33:25 +02:00
Jean Boussier
aa84a783ee ruby-head: handle unused block warnings
Ref: https://bugs.ruby-lang.org/issues/15554

This new Ruby 3.4 warning is still being fined tuned to reduce false positives,
so we shouldn't fail builds on it just yet.

It however caught one mistake in the test suite which is valuable.
2024-04-17 12:24:12 +02:00
Jean Boussier
afa019835b Fix ActiveJob::EnqueueAfterTransactionCommit API
Fix: https://github.com/rails/rails/pull/51426#issuecomment-2042611790

`perform_later` is supposed to return the Job instance on success,
and `false` on error.

When the `enqueue` is automatically delayed, it's of course impossible
to predict if the actual queueing will succeed, but for backward compatibility
reasons, it's best to assume it will.

If necessary, you can hold onto the job instance and check for
`#successfully_enqueued?` after the transaction has completed.
2024-04-10 09:20:12 +02:00
Jean Boussier
cc0f0f9c44 Make ActiveSupport::BacktraceCleaner copy filters and silencers on dup and clone
Previously the copy would still share the internal silencers and filters array,
causing state to leak.
2024-03-29 12:13:32 +01:00
Rosa Gutierrez
71a74ad035
Preserve encoding on truncate_bytes (#51313)
String.new with no arguments returns the empty string with ASCII-8BIT
encoding. Then, depending on each grapheme cluster of the string and
on the omission string, the resulting string might keep the ASCII-8BIT
encoding. With this change, we preserve the encoding of the original
string instead.

Note that String.new accepts an `encoding` keyword argument, like
```
String.new(encoding: Encoding::UTF_8)
```
However, instead of using that, we rely on `force_encoding` to set the
original encoding. This is so that String subclasses don't need to
preserve this keyword argument. For example, SafeBuffer doesn't.
Thanks to @jeremy for catching this!
2024-03-12 15:39:21 -07:00
Christian Schmidt
d518457ca3 Rubocop 2024-02-19 13:43:59 +01:00
Christian Schmidt
3aa7d79418 Support filenames in Logger.logger_outputs_to? 2024-02-19 13:39:48 +01:00
Jean Boussier
50daadaa71 Update test suite for compatibility with Ruby 3.4-dev
https://bugs.ruby-lang.org/issues/19117 and https://bugs.ruby-lang.org/issues/16495
slightly change how backtrace are rendered which makes a few tests fail.
2024-02-16 11:55:44 +01:00
Jean Boussier
ca6995a80c Update Method#duplicable? to be consistent with Ruby 3.4
Fix: https://github.com/rails/rails/issues/51075

`Method` and `UnboundMethod` used to raise on `#dup`, but not `#clone`,
this wasn't so much a feature, but a bug.

It was fixed in https://github.com/ruby/ruby/pull/9926.
2024-02-14 09:52:55 +01:00
George Ma
3711c69ab0 Add :: to namespace the module we delegate "to"
Only namespace when the delegation object is a module
2024-01-31 11:35:15 +01:00
Tristan Starck
b0b481a7c4
Fix teardown callbacks (#50915)
* Switch ActiveSupport::TestCase teardown and setup callbacks to run in setup and teardown minitest lifecycle hooks.

Minitest provides `setup` and `teardown` lifecycle hooks to run code in. In general it is best practice to when defining your own test case class, to use `Minitest::TestCase.setup` and `Minitest::TestCase.teardown` instead of `before_setup` and `after_teardown`.

Per Minitest's Documentation on Lifecycle Hooks: https://docs.ruby-lang.org/en/2.1.0/MiniTest/Unit/LifecycleHooks.html
> before_setup()
> Runs before every test, before setup. This hook is meant for libraries to extend minitest. It is not meant to be used by test developers.

> after_teardown()
> Runs after every test, after teardown. This hook is meant for libraries to extend minitest. It is not meant to be used by test developers.

Since the `setup` and `teardown` ActiveSupport::TestCase callbacks are in essence user code, it makes sense to run during their corresponding Minitest Lifecycle hooks.

* Ensure test fixutres are torndown on errors in superclass after_teardown code.

By not adding wrapping the `teardown_fixtures` code, its possible that super raises an error and prevents the existing database transaction from rolling back.
`super` in general should only be calling `Minitest::Testcase.after_teardown` however, if another library were to override `Minitest::Testcase.after_teardown`, like the popular gem [rspec-mocks](https://github.com/rspec/rspec-mocks/blob/main/lib/rspec/mocks/minitest_integration.rb#L23) does, it causes all subsequent tests to retain any changes that were made to the database in the original test that errors.

* Remove unnecessary setup and teardown methods in tests

* update activesupport Changelog

* Fix linter issues in CHANGELOG

* fix tests with improper setup and teardown method definitions

* Fix final CHANGELOG lint

* Revert "Fix final CHANGELOG lint"

This reverts commit f30682eb629780862ccc63e1d3210dfe035e997e.

* Revert "fix tests with improper setup and teardown method definitions"

This reverts commit 1d5b88c8739695a4eed5c46924c9ffc6010353f5.

* Revert "Fix linter issues in CHANGELOG"

This reverts commit 60e89bd189cbcdf50d7e923a90ec5ebe1578a6e9.

* Revert "update activesupport Changelog"

This reverts commit 0f19bc324fec7a793cc34dcfede27017b5a24e46.

* Revert "Remove unnecessary setup and teardown methods in tests"

This reverts commit e5673f179ac01c814ab44017b97e7638aad6e775.

* Revert "Switch ActiveSupport::TestCase teardown and setup callbacks to run in setup and teardown minitest lifecycle hooks."

This reverts commit d08d92d86131d8643a275397d9b0c15995730a14.

* Rescue Minitest::Assertion errors in ActiveSupport::TestCase.teardown callback code to ensure all other after_teardown methods are called.

* Fix name of test class

* remove unused MyError class

* Fix module to not be in global namespace

Co-authored-by: Rafael Mendonça França <rafael@rubyonrails.org>
2024-01-30 19:05:18 -05:00
Yasuo Honda
33c6f0f723 Require 'uri' explicitly to address NameError: uninitialized constant ToQueryTest::URI
This commit addresses CI failure
at https://buildkite.com/rails/rails-nightly/builds/108#018d57ac-4f2a-45f1-86b9-9015a7b0a463/1165-2002

* Error addressed by this commit
```
$ ruby -v
ruby 3.4.0dev (2024-01-30T10:19:23Z master 86547fd69d) [x86_64-linux]
$ rm Gemfile.lock
$ bundle install
$ cd activesupport
$ bin/test test/core_ext/object/to_query_test.rb -n test_hash_not_sorted_lexicographically_for_nested_structure
... snip ...

E

Error:
ToQueryTest#test_hash_not_sorted_lexicographically_for_nested_structure:
NameError: uninitialized constant ToQueryTest::URI
    test/core_ext/object/to_query_test.rb:90:in `test_hash_not_sorted_lexicographically_for_nested_structure'

bin/test test/core_ext/object/to_query_test.rb:79

Finished in 0.000385s, 2597.3082 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
$
```

This behavior has been introduced since Ruby 3.4.0dev vendors URI from top level to `Gem::URI`
via d64d0b5423 .
Here is the RubyGems one.
https://github.com/rubygems/rubygems/pull/7386
2024-01-30 20:29:33 +09:00
Jean Boussier
b54a287f9d Ensure all Cache store have consistent TTL behavior on increment
Make sure they all increment the counter but don't update the TTL.
2024-01-17 15:40:16 +01:00
Jean Boussier
de779f2bf7 Fix ActiveSupport::Notifications.publish_event to preserve units
Ref: https://github.com/rails/rails/pull/43502
Fix: https://github.com/rails/rails/pull/50767
Fix: https://github.com/rails/rails/pull/50493

When republishing a an event into a `start, finish` tuple, we need
to convert the timestamps back into seconds.
2024-01-17 12:53:40 +01:00
Jean Boussier
946e46ebcc Modernize method missing implementations
`...` is both simpler an more correct since the keyword argument
separation.
2024-01-16 13:17:45 +01:00
r-plus
38151711c8 Fix IPAddr prefix information missing when write to cache in msgpack serializer
* Save cache size by omit the prefix if unnecessary

* rename to straightforward naming.

* check the prefix directly instead of inspect

* Remove unused helper method

* add to changelog

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2024-01-15 00:40:44 -06:00
Jonathan Hefner
aa98bc3c71 Prevent CurrentAttributes defaults from leaking
Follow-up to #50677.

Prior to this commit, all `ActiveSupport::CurrentAttributes` subclasses
stored their default values in the same `Hash`, causing default values
to leak between classes.  This commit ensures each subclass maintains a
separate `Hash`.

This commit also simplifies the resolution of default values, replacing
the `merge_defaults!` method with `resolve_defaults`.
2024-01-11 12:40:14 -06:00
Sean Doyle
d1d6b6bce3 Add default: support for ActiveSupport::CurrentAttributes.attribute
Extend the `.attribute` class method to accept a `:default` option for
its list of attributes:

```ruby
class Current < ActiveSupport::CurrentAttributes
  attribute :counter, default: 0
end
```

Internally, `ActiveSupport::CurrentAttributes` will maintain a
`.defaults` class attribute to determine default values during instance
initialization.
2024-01-09 19:09:57 -05:00
Sean Doyle
3c72983dc5 Implement CurrentAttributes#set in terms of Object#with
`CurrentAttributes` supports block-scoped overrides for its attributes
through the `#set` method. The introduction of [CurrentAttributes#set][]
predates the introduction of [Object#with][] by 6 years.

This commit changes the implementation of `#set` to delegate to `#with`.
Through that delegation, the private `#assign_attributes` and
`#compute_attributes` methods are no longer necessary.

[CurrentAttributes#set]: 2d6b02bad6/activesupport/lib/active_support/current_attributes.rb (L210)
[Object#with]: 2d6b02bad6/activesupport/lib/active_support/core_ext/object/with.rb (L26)
2024-01-09 14:34:18 -05:00
Jean Boussier
a3d05309aa Get rid of ForkTracker.check!
Now that we require Ruby 3.1, we can assume `Process._fork` is
defined on MRI, hence we can trust that our decorator will
reliably detect forks so we no longer need to check the if
the pid changed in critical spots.
2024-01-09 11:18:38 +01:00
Sean Doyle
9e64b13d8a Yield instance to Object#with block
The introduction of the block argument means that `Object#with` can now
accept a `Symbol#to_proc` as the block argument:

```ruby
client.with(timeout: 5_000) do |c|
  c.get("/commits")
end
```
2024-01-07 20:56:33 +01:00
Jean Boussier
27140247c2 Cleanup defined? usage
Now that we dropped support for Ruby 2.7, we no longer
need to check if variables are defined before accessing them
to avoid the undefined variable warning.
2024-01-05 15:05:35 +01:00
Rafael Mendonça França
61e2b9a415
Put back the require 2024-01-04 15:18:47 +00:00
Rafael Mendonça França
2bbdb7c3d3
Force skip in test/core_ext/object/json_cherry_pick_test.rb
Those tests should be skipped if the json core_ext is already loaded.
2024-01-04 04:09:41 +00:00
Rafael Mendonça França
391229eddf
Fix force_skip patch 2024-01-04 04:04:05 +00:00
Rafael Mendonça França
ad343c5034
Allow to force skip tests
This is useful for when a test depends on code not being loaded that
might be loaded by another test.
2024-01-04 03:34:07 +00:00
Rafael Mendonça França
e55d59a232
Use a different module
MessageEncryptor loads `json` and we want a test that doesn't do that.
2024-01-04 02:51:02 +00:00
Rafael Mendonça França
8b4e92f4be
Point rubocop to ruby 3.1 2024-01-03 19:02:32 +00:00
Rafael Mendonça França
9d18dc8505
Remove all code to work with Ruby < 3.1 2024-01-03 19:02:31 +00:00
Manish Sharma
94a4adb8a1 [FIX] Fix Activesupport json encode for hash 2023-12-30 20:22:11 +05:30
Mark Oleson
d5c7f7cc06 fix LocalCache#read_multi_entries not namespacing keys before looking them up in the cache 2023-12-11 11:25:05 -06:00
Jean Boussier
be258503ac Module#delegate takes a new private as parameter
This is a continuation of https://github.com/rails/rails/pull/46875

The behavior of looking up the class method when `to: :class` is passed
is a bit error prone because it silently degrades.

By passing the expected owner of the delegated method, we can be more
strict, and also generate a delegator in a module rather than having
to do it at inclusion time.

I made this argument private API because we want it in Rails, but
I'm worried it might be a bit too sharp for public API. I can
be convinced otherwise though.
2023-12-08 15:09:59 +01:00
Aleksei Chernenkov
aedb808829 Fix Time.now/DateTime.now/Date.today to return results in a system timezone after #travel_to
There is a bug in the current implementation of #travel_to:
it remembers a timezone of its argument, and all stubbed methods start
returning results in that remembered timezone. However, the expected
behaviour is to return results in a system timezone.

It can lead to bugs in tests like this one:
https://github.com/faker-ruby/faker/issues/2861
2023-12-02 10:05:31 +01:00
Hartley McGuire
e67b8d0dcc
Fix Active Support test warnings
The namespace ivar needs a definition check to not warn on Ruby 2.7
2023-11-30 11:11:22 -05:00
Hartley McGuire
8f6ee6487f
Fix skips in Active Support
The change to serialization mirrors the test just below it that also
uses a conditional for the assertion instead of a skip. The conditional
is necessary because memcached entries are not strings.

The class_serial test should not run on Ruby 3.2+ because class_serial
was replaced with Object Shapes. The class_serial value in RubyVM.stat
was removed in ruby/ruby@13bd617ea6
2023-11-30 11:11:22 -05:00
Kevin McPhillips
3d60490a9d Add #to_s and pretty print for ActiveSupport::InheritableOptions 2023-11-28 17:49:15 -05:00
Jonathan Hefner
ea46a00a9f
Merge pull request #50192 from sbfaulkner/memory-store-unless-exist
Fix MemoryStore#write with unless_exist and namespace
2023-11-28 12:25:29 -06:00
S. Brent Faulkner
701377c6af
Fix MemoryStore#write with unless_exist and namespace
Updates `MemoryStore#write_entry` to pass a `nil` `namespace` to
`exist?`, which expects a _name_ rather than a an already "normalized"
_key_. This fixes a bug where `unless_exist` would overwrite any
existing entry if a `namespace` was used.
2023-11-28 13:08:55 -05:00
Kevin McPhillips
9b03df5626 Add some behaviour to ActiveSupport::InheritableOptions to make it quack more like a hash 2023-11-28 12:01:10 -05:00
Jean Boussier
2f19782dce ErrorReporter#unexpected to report in production but raise in development
It's a common useful pattern for situation where something isn't
supposed to happen, but if it does we can recover from it.

So in such situation you don't want such issue to be hidden
in development or test, as it's likely a bug, but do not want to
fail a request if it happens in production.

In other words, it behaves like `#record` in development and test
environments, and like `raise` in production.

Fix: https://github.com/rails/rails/pull/49638
Fix: https://github.com/rails/rails/pull/49339

Co-Authored-By: Andrew Novoselac <andrew.novoselac@shopify.com>
Co-Authored-By: Dustin Brown <dbrown9@gmail.com>
2023-11-20 09:42:56 +01:00
Adam Renberg Tamm
2251a4ba07 Adjust instr. for Cache::Store#fetch_multi so writes are after reads 2023-11-17 11:33:07 +01:00
Sander Verdonschot
4cfdcfef8e
Make return values of Cache::Store#write consistent
The return value was not specified before. Now it returns `true` on a
successful write, `nil` if there was an error talking to the cache
backend, and `false` if the write failed for another reason (e.g. the
key already exists and `unless_exist: true` was passed).
2023-11-16 08:54:23 -05:00
Jean Boussier
37b0c603d5 Formally deprecate passing caller to Deprecation#warn
This emitted a warning since 2015, but it's likely most
offenders never saw it.

Ref: 211f55d4fd
2023-11-14 10:33:15 +01:00
Jean Boussier
c2be3ea65c ActiveSupport::Deprecation handle blaming generated code
Fix: #50047

`Backtrace::Location` instance for code generated with eval always
have their `absolute_path` set to `nil`. So if absolute path is nil
we should fallback to checking `#path`.

Co-Authored-By: fatkodima <fatkodima123@gmail.com>
2023-11-14 09:51:41 +01:00
Hartley McGuire
9af99bfffc Fix logged cache key normalization
Previously, the `Cache::Store` instrumentation would call
`normalize_key` when adding a key to the log. However, this resulted in
the logged key not always matching the actual key written/read from the
cache:

```irb
irb(main):004> cache.write("foo", "bar", namespace: "baz")
D, [2023-11-10T12:44:59.286362 #169586] DEBUG -- : Cache write: baz:foo ({:compress=>false, :compress_threshold=>1024, :namespace=>"baz"})
=> true
irb(main):005> cache.delete("foo", namespace: "baz")
D, [2023-11-10T12:45:03.071300 #169586] DEBUG -- : Cache delete: foo
=> true
```

In this example, `#write` would correctly log that the key written to
was `baz:foo` because the `namespace` option would be passed to the
`instrument` method. However, other methods like `#delete` would log
that the `foo` key was deleted because the `namespace` option was _not_
passed to `instrument`.

This commit fixes the issue by making the caller responsible for passing
the correct key to `#instrument`. This allows `normalize_key` to be
removed from the log generation which both prevents the key from being
normalized a second time and removes the need to pass the full options
hash into `#instrument`.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-11-11 12:56:39 -06:00
fatkodima
096201f87d Fix RedisCacheStore#write_multi with :expires_in 2023-11-08 23:27:57 +02:00
Jean Boussier
9ade3f9b56
Merge pull request #49669 from intrip/fix-message-metadata-non-str
Fix decoding data encoded using a non-String purpose
2023-11-01 11:22:36 +01:00
Jean Boussier
db92ea32e0 Simplify attr_internal_define
The `@` prefix is always stripped, so might as well not require it.

For backward compatibility reasons we still handle the prefix for now,
but we eagerly strip it and emit a deprecation.
2023-10-31 13:42:57 +01:00
Jonathan Hefner
1b195b30bc
Merge pull request #48482 from pcreux/improve-assert_changes-error-messages
Improve error messages of `assert_changes` and `assert_no_changes`
2023-10-30 13:43:51 -05:00
Philippe Creux
2b3a002a2a Improve error messages of assert_changes and assert_no_changes 2023-10-30 13:30:33 -05:00