- Following the Rails guide which state that a logger needs to include
the `ActiveSupport::LoggerSilence` as well as
`ActiveSupport::LoggerThreadSafe` modules isn't enough and won't
work.
Here is a test cases with 3 tests that all fails
https://gist.github.com/Edouard-chin/4a72930c2b1eafbbd72a80c66f102010
The problems are the following:
1) The logger needs to call `after_initialize` in order to setup
some instance variables.
2) The silence doesn't actually work because the bare ruby Logger
`add` method checks for the instance variable `@logger`. We need to
override the `add` (like we used to in the ActiveSupport::Logger
class).
3) Calling `debug?` `info?` etc... doesn't work as the bare ruby
methods will check for the instance variable. Again we need to
override this methods (like we used to in the ActiveSupport::Logger
class)
The LoggerSilence won't work without LoggerThreadSafe, but the later
is not public API, the user shouldn't have to include it so I
modified to include it automatically.
Same for the `after_initialize` method. I find unuintitive to have
to call it directly. I modified to instance the variables when the
module get included.
- I found this weird that the LoggerSilence wasn't using the
`ActiveSupport` namespace (AFAIK all other classes have it).
This PR deprecate the use of `LoggerSilence` for
`ActiveSupport::LoggerSilence` instead.
Since Rails 6.0 will support Ruby 2.4.1 or higher
`# frozen_string_literal: true` magic comment is enough to make string object frozen.
This magic comment is enabled by `Style/FrozenStringLiteralComment` cop.
* Exclude these files not to auto correct false positive `Regexp#freeze`
- 'actionpack/lib/action_dispatch/journey/router/utils.rb'
- 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb'
It has been fixed by https://github.com/rubocop-hq/rubocop/pull/6333
Once the newer version of RuboCop released and available at Code Climate these exclude entries should be removed.
* Replace `String#freeze` with `String#-@` manually if explicit frozen string objects are required
- 'actionpack/test/controller/test_case_test.rb'
- 'activemodel/test/cases/type/string_test.rb'
- 'activesupport/lib/active_support/core_ext/string/strip.rb'
- 'activesupport/test/core_ext/string_ext_test.rb'
- 'railties/test/generators/actions_test.rb'
The `merged_options` method is private and the output is never mutated.
Using this info we can get rid of the `dup` behavior. We can also
eliminate the `merge` call in the case where all the options being merged are the same.
Returned results are frozen as an extra layer of protection against
mutation.
Before
```
Total allocated: 741749 bytes (6642 objects)
```
After
```
Total allocated: 734039 bytes (6648 objects)
```
Diff
```
(741749 - 734039) / 741749.0 => ~ 1.0 %
```
If you don't feel comfortable modifying this method, we could rename it
and only use it internally.
If you are using the "in cache versioning" also known as "recyclable cache keys" the cache store must be aware of this scheme, otherwise you will generate cache entries that never invalidate.
This PR adds a check to the initialization process to ensure that if recyclable cache keys are being used via
```
config.active_record.cache_versioning = true
```
Then the cache store needs to show that it supports this versioning scheme. Cache stores can let Rails know that they support this scheme by adding a method `supports_in_cache_versioning?` and returning true.
The name of the minitest library is spelled that way: regular font, and
lowercase. Lowercase is used even at the beginning of sentences, see
http://docs.seattlerb.org/minitest/
I double-checked this with @zenspider too (thanks!).
Memory before 1826584.8 memory after: 1797795.6 difference: 1.58% memory (speed) savings.
When the key is not longer than the limit we can avoid allocating two strings and an array.
This commit moves the `run_cleanup` hook into an `ensure` block so we
make sure to cleanup the databases even if an exception is raised while
running the parallel tests.
Following up on #33747, this takes things a step further by pulling out
the method name from the arguments array, letting us skip an allocation
in the case where there are no arguments -- notably, this also no longer
*requires* the splat to be an array, allowing us to benefit from
optimizations in Jruby (and maybe MRI in the future) of skipping the
array allocation entirely.
Benchmark results:
```
Warming up --------------------------------------
old 179.987k i/100ms
new 199.201k i/100ms
Calculating -------------------------------------
old 3.029M (± 1.6%) i/s - 15.299M in 5.052417s
new 3.657M (± 1.2%) i/s - 18.326M in 5.012648s
Comparison:
new: 3656620.7 i/s
old: 3028848.3 i/s - 1.21x slower
```
* Update the comments for TimeWithZone subtraction
While reading the description/documentation comments for the subtraction method, it was confusing. While in practical usage, the returned values make sense. It seems as though the explanation could be a bit clearer.
* Removed erroneous closing parenthesis
[ci skip]
[Rob Race + Ryuta Kamizono]
Avoid allocating the second array by using `Array#reject!` instead of
`Enumerable#partition` in `Array#extract!`.
There are benchmarks in order to ensure that the changes speed up the method:
```
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update
your Bundler"
raise e
end
class Array
def extract_v1!(&block)
unless block_given?
to_enum(:extract!) { size }
else
extracted_elements, other_elements = partition(&block)
replace(other_elements)
extracted_elements
end
end
def extract_v2!
return to_enum(:extract!) { size } unless block_given?
extracted_elements = []
reject! do |element|
extracted_elements << element if yield(element)
end
extracted_elements
end
end
gemfile(true) do
source "https://rubygems.org"
gem "benchmark-ips"
end
arrays_for_partition = Array.new(1000) { (0..10000).to_a }
arrays_for_extract_v1 = Array.new(1000) { (0..10000).to_a }
arrays_for_extract_v2 = Array.new(1000) { (0..10000).to_a }
Benchmark.ips do |x|
x.report("Array#partition") do
arrays_for_partition.each do |numbers|
odd_numbers, numbers = numbers.partition { |number| number.odd? }
numbers
end
end
x.report("Array#extract_v1!") do
arrays_for_extract_v1.each do |numbers|
odd_numbers = numbers.extract_v1! { |number| number.odd? }
numbers
end
end
x.report("Array#extract_v2!") do
arrays_for_extract_v2.each do |numbers|
odd_numbers = numbers.extract_v2! { |number| number.odd? }
numbers
end
end
x.compare!
end
```
The result of the benchmarks:
```
ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
```
```
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Using benchmark-ips 2.7.2
Using bundler 1.16.1
Warming up --------------------------------------
Array#partition 1.000 i/100ms
Array#extract_v1! 1.000 i/100ms
Array#extract_v2! 1.000 i/100ms
Calculating -------------------------------------
Array#partition 1.390 (± 0.0%) i/s - 7.000 in 5.044843s
Array#extract_v1! 2.781 (± 0.0%) i/s - 14.000 in 5.050589s
Array#extract_v2! 3.151 (± 0.0%) i/s - 16.000 in 5.080608s
Comparison:
Array#extract_v2!: 3.2 i/s
Array#extract_v1!: 2.8 i/s - 1.13x slower
Array#partition: 1.4 i/s - 2.27x slower
```
Avoid `unless`/`else` in favour of an early return.
The double-negative of that `else` can be confusing,
even though the code layout is nearly the same.
Also using of early return would improve `git diff`
if we needed to change this method.
The method removes and returns the elements for which the block returns a true value.
If no block is given, an Enumerator is returned instead.
```
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
odd_numbers = numbers.extract! { |number| number.odd? } # => [1, 3, 5, 7, 9]
numbers # => [0, 2, 4, 6, 8]
```
- ActiveSupport::Testing::MethodCallAssertions#assert_called
- Ensure that the method stubbed by `assert_called` returns correct value after
- ActiveSupport::Testing::MethodCallAssertions#assert_called_with
- Ensure that `#assert_called_with` stubs the method to return a specific value
- Ensure that the method stubbed by `assert_called_with` returns correct value after
Six Mocha calls prove quite resistant to Minitestification. For example,
if we replace
```
ActiveRecord::Associations::HasManyAssociation
.any_instance
.expects(:reader)
.never
```
with `assert_not_called`, Minitest wisely raises
```
NameError: undefined method `reader' for class `ActiveRecord::Associations::HasManyAssociation'
```
as `:reader` comes from a deeply embedded abstract class,
`ActiveRecord::Associations::CollectionAssociation`.
This patch tackles this difficulty by adding
`ActiveSupport::Testing::MethodCallAsserts#assert_called_on_instance_of`
which injects a stubbed method into `klass`, and verifies the number of
times it is called, similar to `assert_called`. It also adds a convenience
method, `assert_not_called_on_instance_of`, mirroring
`assert_not_called`.
It uses the new method_call_assertions to replace the remaining Mocha
calls in `ActiveRecord` tests.
[utilum + bogdanvlviv + kspath]
We always add new entries on the top of changelog file.
This commit moves the entry added in 47018a82 up in order to
preserve the chronology.
Follows up 3e2629eb7f
* test case for fetch cache miss with skip_nil
* abondon nil cache if skip_nil specified
* ensure not cache key for skip nil
* add document with skip_nil for Store#fetch
* add a new change log entry for #25437
- Refactoring alias_chain to Module#prepend broke the possibility to deprecate class methods since the module
generated was prepended to the target's instance.
A suggestion to fix this was to use `AS#redefine_method` which would solve the
problem but with the cost of redefining directly the method.
Decided to go with the same alias_chain implementation as before instead.
- Fixes#33253
According to #33449 and #33468, cpu_time and allocations are 0 when
JRuby is used.
```ruby
$ ruby -v
jruby 9.2.1.0-SNAPSHOT (2.5.0) 2018-07-27 13b2df5 Java HotSpot(TM) 64-Bit Server VM 25.181-b13 on 1.8.0_181-b13 [linux-x86_64]
$ bundle exec ruby -w -Itest test/log_subscriber_test.rb -n test_event_attributes
Run options: -n test_event_attributes --seed 6231
F
Failure:
SyncLogSubscriberTest#test_event_attributes [test/log_subscriber_test.rb:84]:
Expected 0 to be > 0.
rails test test/log_subscriber_test.rb:78
Finished in 0.018983s, 52.6791 runs/s, 105.3582 assertions/s.
1 runs, 2 assertions, 1 failures, 0 errors, 0 skips
```
Rubocop warns about "Use String#end_with? instead of a regex match anchored to the end of the string",
it doesn't seem aware of the $` special variable like Performance/RegexpMatch
```
Offenses:
activesupport/lib/active_support/subscriber.rb:91:17:
C: Layout/SpaceAroundOperators: Operator = should be surrounded by a single space.
event = event_stack.pop
```
We don't need to have a special subscribe method for objects. The
regular `subscribe` method is more expensive than a specialized method,
but `subscribe` should not be called frequently. If that turns out to
be a hotspot, we can introduce a specialized method. :)
Fanout notifier can send event objects to subscribers now. Also moved
`end` lower in the `finish!` method to guarantee that CPU time is
shorter than real time.
* Use process clock instead of Time.now
This fixes any issues with the system clock changing and also eliminates
2 object allocations per event.
* Add start! and finish! methods to the event object so we can record
more information
* Adds cpu time, idle time, and allocation count for a particular event.
Co-authored-by: Aaron Patterson <aaron.patterson@gmail.com>
Use attr_reader/attr_writer instead of methods
method is 12% slower
Use flat_map over map.flatten(1)
flatten is 66% slower
Use hash[]= instead of hash.merge! with single arguments
merge! is 166% slower
See https://github.com/rails/rails/pull/32337 for more conversation
- There was an issue inside controller tests where order params were not respected, the reason
was because we were calling `Hash#to_query` which sorts the results lexicographically.
1e4e1b62 fixed that issue by not using `to_query` but instead a utility function provided by rack.
- However with the fix came another issue where it's now no longer possible to do this
```
post :foo, params: { user: User.first }
# Prior to the patch the controller will receive { "user" => "1" }
# Whereas now you get { "user": "#<User: ...>" }
```
The fix in this PR is to modify `Hash#to_query` to sort only when it
doesn't contain an array structure that looks something like "bar[]"
Ref https://github.com/rails/rails/pull/33341#issuecomment-404039396
As discussed in #33203 rails command already looks for, and runs,
bin/rails if it is present.
We were mixing recommendations within guides and USAGE guidelines,
in some files we recommended using rails, in others bin/rails and
in some cases we even had both options mixed together.
- When lazy load hooks were triggered we were using
`Object.instance_eval` which evaluates the block in the context of
the class being passed. Most of the time that class was a
`Class`. If one wants to define a instance method on the class then
it wasn't possible.
```ruby
class A; end;
A.instance_eval do
def foo
puts 'bar'
end
end
A.new.foo #> NoMethodError: undefined method `foo`
A.foo #> bar
```
- This PR checks what object is passed when triggering the hooks and
either call `class_eval` or `instance_eval`. My rational and assumptions being
that if an instance of a class is passed, then the blocks needs to
evaluate in the context of that instance (i.e. defining a method
should only define it on that instance).
On the other hand, if a Class or Module is passed when triggering
hooks, then defining a method should define it on the class itself
- #32776 Pushed me to introduce this change
Firstly, increment and decrement shouldn't care about the particulars of
key expiry. They should only know that they have to pass that responsibility
on to somewhere else.
Secondly, it moves the key normalization back inside the instrumentation like
it was originally. I think that matches the original design intention or at
the very least it lets users catch haywire key truncation.
Thirdly, it moves the changelog entry to the top of the file, where new entries
go. I couldn't understand what the entry was saying so I tried to rewrite it.
When multiplying or dividing a duration by a scalar, it's tempting to
operate directly on the duration's value in seconds and recompute the
parts from the result. However this loses information, as there are
multiple combinations of parts that map to any given number of seconds
(e.g. `2.weeks` or `336.hours`). This is especially problematic when
dealing with durations on the scale of months or years, as converting an
exact number of seconds to one of those intervals and then using the
resulting duration to modify a date will give the wrong result.
- `activesupport/multibyte_normalization_conformance_test.rb`
`fileutils`, `tmpdir`, and `open-uri` are unused since
c245ca30f2,
c245ca30f2, and
7d7c2d13ba in accordance.
- `activesupport/test/multibyte_conformance_test.rb`
`tmpdir`, and `open-uri` are unused since
c245ca30f2, and
7d7c2d13ba in accordance.
Remove using of `fileutils` since
c245ca30f2.
- `activesupport/test/multibyte_grapheme_break_conformance_test.rb`
`fileutils`, `tmpdir`, and `open-uri` are unused since
c245ca30f2,
c245ca30f2, and
7d7c2d13ba in accordance.
ruby/ruby@989e07c features switching `Range#===` to use internal `r_cover_p`
instead of rubyland `include?`. This breaks expected behavior of
`ActiveSupport::CoreExt::Range` documented since at least 8b67a02.
This patch adds overrides on `Range#cover?` and `Range#===` and places all
three in a single module, `CompareWithRange`.
*Requiring core_ext/range/include_range now causes a deprecation warnning*
In the app I'm working on I've wished that index_by had a buddy that would
assign the hash value instead of the key multiple times.
Enter index_with. Useful when building a hash from a static list of
symbols. Before you'd do:
```ruby
POST_ATTRIBUTES.map { |attr_name| [ attr_name, public_send(attr_name) ] }.to_h
```
But now that's a little clearer and faster with:
````ruby
POST_ATTRIBUTES.index_with { |attr_name| public_send(attr_name) }
```
It's also useful when you have an enumerable that should be converted to a hash,
but you don't want to muddle the code up with the overhead that it takes to create
that hash. So before, that's:
```ruby
WEEKDAYS.each_with_object(Hash.new) do |day, intervals|
intervals[day] = [ Interval.all_day ]
end
```
And now it's just:
```ruby
WEEKDAYS.index_with([ Interval.all_day ])
```
It's also nice to quickly get a hash with either nil, [], or {} as the value.
When `to:` is passed to `assert_changes`, it now prints the well-known `"Expected: x\n Actual: y"` message.
Before, the message only contained the actual value.
See #29632 for details. In short, it's possible to enter `LoadError#is_missing?` when `LoadError#path` returns `nil`, leading to `path.sub` throwing an none-to-helpful `NoMethodError`.
This tiniest of patch inserts `#to_s` before the `sub` call to make sure it succeeds. Affected surface area should be just as tiny since something has already gone wrong to get us into `#is_missing?` and the current behavior when `#path` returns `nil` seems clearly not intended.
[Gannon McGibbon + Neil Souza]
- In #32472 I introduced a fix in order for all `after_teardown` method provided by libraries and Rails to run, even if the application's `teardown` method raised an error (That's the default minitest behavior). However this change wasn't enough and doesn't take in consideration the ancestors chain.
If a library's module containing an `after_teardown` method get included after the `SetupAndTeardown` module (one example is the [ActiveRecord::TestFixtures module](7d2400ab61/activerecord/lib/active_record/fixtures.rb (L855-L856)), then the ancestors of the test class would look something like
```ruby
class MyTest < ActiveSupport::TestCase
end
puts MyTest.ancestors # [MyTest, ActiveSupport::TestCase, ActiveRecord::TestFixtures, ActiveSupport::Testing::SetupAndTeardown]
```
Any class/module in the ancestors chain that are **before** the `ActiveSupport::Testing::SetupAndTeardown` will behave incorrectly:
- Their `before_setup` method will get called **after** all regular setup method
- Their `after_teardown` method won't even get called in case an exception is raised inside a regular's test `teardown`
A simple reproduction script of the problem here https://gist.github.com/Edouard-chin/70705542a59a8593f619b02e1c0a188c
- One solution to this problem is to have the `AS::SetupAndTeardown` module be the very first in the ancestors chain. By doing that we ensure that no `before_setup` / `after_teardown` get executed prior to running the teardown callbacks
tags_text method creates 3 Ruby objects per each logger call when no custom tags are given
(which is the default setting, and so presumably the majority use case).
This patch reduces two temporary object creations in this case.
require 'allocation_tracer'
ObjectSpace::AllocationTracer.setup(%i{type})
tags = ['a']
pp before: ObjectSpace::AllocationTracer.trace {
tags.collect { |tag| "[#{tag}] " }.join
}
pp after: ObjectSpace::AllocationTracer.trace {
"[#{tags[0]}] "
}
{:before=>{[:T_ARRAY]=>[1, 0, 0, 0, 0, 0], [:T_STRING]=>[2, 0, 0, 0, 0, 0]}}
{:after=>{[:T_STRING]=>[1, 0, 0, 0, 0, 0]}}
- The "/" was removed in 40bdbce191 during
refactoring. It may cause regression since looks like was added
intentionaly because it is possible that a name of any another gem
can start with /rails/, so slash was added to ensure that it is "rails"
gem.
I would like to backport this to `5-2-stable` too.
- Use `__dir__` instead of `__FILE__`. Follow up #29176.
This commit fix pattern of filenames for `CustomCops/AssertNot` and
`CustomCops/RefuteNot`.
rubocop should check every file under `test/`.
Related to #32441, #32605