From 6afbd7a97ffa8f1c05f9d355a120f6ce3adbe907 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:00:56 +0200 Subject: [PATCH 01/59] Add guard for TDD --- Guardfile | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ gems.rb | 1 + 2 files changed, 71 insertions(+) create mode 100644 Guardfile diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..23074cd --- /dev/null +++ b/Guardfile @@ -0,0 +1,70 @@ +# A sample Guardfile +# More info at https://github.com/guard/guard#readme + +## Uncomment and set this to only include directories you want to watch +# directories %w(app lib config test spec features) \ +# .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")} + +## Note: if you are using the `directories` clause above and you are not +## watching the project directory ('.'), then you will want to move +## the Guardfile to a watched dir and symlink it back, e.g. +# +# $ mkdir config +# $ mv Guardfile config/ +# $ ln -s config/Guardfile . +# +# and, you'll have to watch "config/Guardfile" instead of "Guardfile" + +# Note: The cmd option is now required due to the increasing number of ways +# rspec may be run, below are examples of the most common uses. +# * bundler: 'bundle exec rspec' +# * bundler binstubs: 'bin/rspec' +# * spring: 'bin/rspec' (This will use spring if running and you have +# installed the spring binstubs per the docs) +# * zeus: 'zeus rspec' (requires the server to be started separately) +# * 'just' rspec: 'rspec' + +guard :rspec, cmd: "bundle exec rspec" do + require "guard/rspec/dsl" + dsl = Guard::RSpec::Dsl.new(self) + + # Feel free to open issues for suggestions and improvements + + # RSpec files + rspec = dsl.rspec + watch(rspec.spec_helper) { rspec.spec_dir } + watch(rspec.spec_support) { rspec.spec_dir } + watch(rspec.spec_files) + + # Ruby files + ruby = dsl.ruby + dsl.watch_spec_files_for(ruby.lib_files) + + # Rails files + rails = dsl.rails(view_extensions: %w(erb haml slim)) + dsl.watch_spec_files_for(rails.app_files) + dsl.watch_spec_files_for(rails.views) + + watch(rails.controllers) do |m| + [ + rspec.spec.call("routing/#{m[1]}_routing"), + rspec.spec.call("controllers/#{m[1]}_controller"), + rspec.spec.call("acceptance/#{m[1]}") + ] + end + + # Rails config changes + watch(rails.spec_helper) { rspec.spec_dir } + watch(rails.routes) { "#{rspec.spec_dir}/routing" } + watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" } + + # Capybara features specs + watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") } + watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") } + + # Turnip features and steps + watch(%r{^spec/acceptance/(.+)\.feature$}) + watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m| + Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance" + end +end diff --git a/gems.rb b/gems.rb index 4e0b492..d120573 100644 --- a/gems.rb +++ b/gems.rb @@ -16,4 +16,5 @@ group :test do gem "bake-test" gem "bake-test-external" + gem "guard-rspec", "~> 4.7" end From b5aea9371cbe6283e018c4dda9c5dd79b714142a Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:01:57 +0200 Subject: [PATCH 02/59] Remove redundant default guard configuration --- Guardfile | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/Guardfile b/Guardfile index 23074cd..7608976 100644 --- a/Guardfile +++ b/Guardfile @@ -39,32 +39,4 @@ guard :rspec, cmd: "bundle exec rspec" do # Ruby files ruby = dsl.ruby dsl.watch_spec_files_for(ruby.lib_files) - - # Rails files - rails = dsl.rails(view_extensions: %w(erb haml slim)) - dsl.watch_spec_files_for(rails.app_files) - dsl.watch_spec_files_for(rails.views) - - watch(rails.controllers) do |m| - [ - rspec.spec.call("routing/#{m[1]}_routing"), - rspec.spec.call("controllers/#{m[1]}_controller"), - rspec.spec.call("acceptance/#{m[1]}") - ] - end - - # Rails config changes - watch(rails.spec_helper) { rspec.spec_dir } - watch(rails.routes) { "#{rspec.spec_dir}/routing" } - watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" } - - # Capybara features specs - watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") } - watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") } - - # Turnip features and steps - watch(%r{^spec/acceptance/(.+)\.feature$}) - watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m| - Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance" - end end From bd10efbdb516188787d8cf280761aad5f1b74b99 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:04:33 +0200 Subject: [PATCH 03/59] Configure guard-rspec --- Guardfile | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Guardfile b/Guardfile index 7608976..9f9af79 100644 --- a/Guardfile +++ b/Guardfile @@ -15,16 +15,15 @@ # # and, you'll have to watch "config/Guardfile" instead of "Guardfile" -# Note: The cmd option is now required due to the increasing number of ways -# rspec may be run, below are examples of the most common uses. -# * bundler: 'bundle exec rspec' -# * bundler binstubs: 'bin/rspec' -# * spring: 'bin/rspec' (This will use spring if running and you have -# installed the spring binstubs per the docs) -# * zeus: 'zeus rspec' (requires the server to be started separately) -# * 'just' rspec: 'rspec' +RSPEC_CONFIG = { + cmd: 'bundle exec rspec', + all_on_start: true, + all_after_pass: true, + halt_on_fail: true, + results_file: '/tmp/.guard_rspec_results-rspec-memory' +}.freeze -guard :rspec, cmd: "bundle exec rspec" do +guard :rspec, RSPEC_CONFIG do require "guard/rspec/dsl" dsl = Guard::RSpec::Dsl.new(self) From 0d561c9a0bc2a50a2f4546382e7e279395746ad0 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:05:09 +0200 Subject: [PATCH 04/59] Remove redundant comments --- Guardfile | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/Guardfile b/Guardfile index 9f9af79..e426982 100644 --- a/Guardfile +++ b/Guardfile @@ -1,20 +1,3 @@ -# A sample Guardfile -# More info at https://github.com/guard/guard#readme - -## Uncomment and set this to only include directories you want to watch -# directories %w(app lib config test spec features) \ -# .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")} - -## Note: if you are using the `directories` clause above and you are not -## watching the project directory ('.'), then you will want to move -## the Guardfile to a watched dir and symlink it back, e.g. -# -# $ mkdir config -# $ mv Guardfile config/ -# $ ln -s config/Guardfile . -# -# and, you'll have to watch "config/Guardfile" instead of "Guardfile" - RSPEC_CONFIG = { cmd: 'bundle exec rspec', all_on_start: true, From 30b20f32285c690c63974c1014cf69d368ea6624 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:08:45 +0200 Subject: [PATCH 05/59] Add guard-rubocop with default configuration Also store TODOs. --- .rubocop.yml | 1 + .rubocop_todo.yml | 263 ++++++++++++++++++++++++++++++++++++++++++++++ Guardfile | 5 + gems.rb | 1 + 4 files changed, 270 insertions(+) create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..cc32da4 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1 @@ +inherit_from: .rubocop_todo.yml diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..0cab8b0 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,263 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2023-09-16 08:09:26 UTC using RuboCop version 1.56.3. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +# Configuration parameters: EnforcedStyle, Include. +# SupportedStyles: Gemfile, gems.rb +# Include: **/Gemfile, **/gems.rb, **/Gemfile.lock, **/gems.locked +Bundler/GemFilename: + Exclude: + - 'gems.rb' + +# Offense count: 1 +# Configuration parameters: Severity, Include. +# Include: **/*.gemspec +Gemspec/RequiredRubyVersion: + Exclude: + - 'rspec-memory.gemspec' + +# Offense count: 311 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: IndentationWidth, EnforcedStyle. +# SupportedStyles: spaces, tabs +Layout/IndentationStyle: + Exclude: + - 'gems.rb' + - 'lib/rspec/memory.rb' + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'lib/rspec/memory/trace.rb' + - 'lib/rspec/memory/version.rb' + - 'rspec-memory.gemspec' + - 'spec/rspec/memory_spec.rb' + - 'spec/spec_helper.rb' + +# Offense count: 101 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: Width, AllowedPatterns. +Layout/IndentationWidth: + Exclude: + - 'gems.rb' + - 'lib/rspec/memory.rb' + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'lib/rspec/memory/trace.rb' + - 'lib/rspec/memory/version.rb' + - 'rspec-memory.gemspec' + - 'spec/rspec/memory_spec.rb' + - 'spec/spec_helper.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Layout/SpaceAfterComma: + Exclude: + - 'lib/rspec/memory/trace.rb' + +# Offense count: 8 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. +# SupportedStyles: space, no_space +# SupportedStylesForEmptyBraces: space, no_space +Layout/SpaceBeforeBlockBraces: + Exclude: + - 'lib/rspec/memory/trace.rb' + - 'spec/rspec/memory_spec.rb' + +# Offense count: 16 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. +# SupportedStyles: space, no_space +# SupportedStylesForEmptyBraces: space, no_space +Layout/SpaceInsideBlockBraces: + Exclude: + - 'lib/rspec/memory/trace.rb' + - 'spec/rspec/memory_spec.rb' + +# Offense count: 61 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowInHeredoc. +Layout/TrailingWhitespace: + Exclude: + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'lib/rspec/memory/trace.rb' + - 'rspec-memory.gemspec' + - 'spec/rspec/memory_spec.rb' + +# Offense count: 4 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: AllowSafeAssignment. +Lint/AssignmentInCondition: + Exclude: + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'lib/rspec/memory/trace.rb' + +# Offense count: 1 +# Configuration parameters: AllowedMethods. +# AllowedMethods: enums +Lint/ConstantDefinitionInBlock: + Exclude: + - 'lib/rspec/memory/trace.rb' + +# Offense count: 1 +Lint/ShadowingOuterLocalVariable: + Exclude: + - 'lib/rspec/memory/matchers/limit_allocations.rb' + +# Offense count: 2 +Lint/StructNewOverride: + Exclude: + - 'lib/rspec/memory/trace.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods. +Lint/UnusedMethodArgument: + Exclude: + - 'lib/rspec/memory/trace.rb' + +# Offense count: 2 +# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. +Metrics/AbcSize: + Max: 37 + +# Offense count: 2 +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. +# AllowedMethods: refine +Metrics/BlockLength: + Max: 59 + +# Offense count: 1 +# Configuration parameters: AllowedMethods, AllowedPatterns. +Metrics/CyclomaticComplexity: + Max: 9 + +# Offense count: 2 +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. +Metrics/MethodLength: + Max: 27 + +# Offense count: 1 +# Configuration parameters: AllowedMethods, AllowedPatterns. +Metrics/PerceivedComplexity: + Max: 10 + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: AllowModifiersOnSymbols. +# SupportedStyles: inline, group +Style/AccessModifierDeclarations: + EnforcedStyle: inline + +# Offense count: 4 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: separated, grouped +Style/AccessorGrouping: + Exclude: + - 'lib/rspec/memory/trace.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: always, conditionals +Style/AndOr: + Exclude: + - 'lib/rspec/memory/matchers/limit_allocations.rb' + +# Offense count: 4 +# This cop supports safe autocorrection (--autocorrect). +Style/Attr: + Exclude: + - 'lib/rspec/memory/trace.rb' + +# Offense count: 3 +# Configuration parameters: AllowedConstants. +Style/Documentation: + Exclude: + - 'spec/**/*' + - 'test/**/*' + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'lib/rspec/memory/trace.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/EmptyLiteral: + Exclude: + - 'spec/rspec/memory_spec.rb' + +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: always, always_true, never +Style/FrozenStringLiteralComment: + Exclude: + - 'Guardfile' + +# Offense count: 3 +# This cop supports safe autocorrection (--autocorrect). +Style/IfUnlessModifier: + Exclude: + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'lib/rspec/memory/trace.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: require_parentheses, require_no_parentheses, require_no_parentheses_except_multiline +Style/MethodDefParentheses: + Exclude: + - 'lib/rspec/memory/trace.rb' + +# Offense count: 3 +# This cop supports safe autocorrection (--autocorrect). +Style/MultilineIfModifier: + Exclude: + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'spec/rspec/memory_spec.rb' + +# Offense count: 4 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowMultipleReturnValues. +Style/RedundantReturn: + Exclude: + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'lib/rspec/memory/trace.rb' + +# Offense count: 2 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantSelf: + Exclude: + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'lib/rspec/memory/trace.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, AllowInnerSlashes. +# SupportedStyles: slashes, percent_r, mixed +Style/RegexpLiteral: + Exclude: + - 'Guardfile' + +# Offense count: 37 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. +# SupportedStyles: single_quotes, double_quotes +Style/StringLiterals: + Exclude: + - 'Guardfile' + - 'gems.rb' + - 'lib/rspec/memory/trace.rb' + - 'lib/rspec/memory/version.rb' + - 'rspec-memory.gemspec' + - 'spec/rspec/memory_spec.rb' + - 'spec/spec_helper.rb' + +# Offense count: 4 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. +# URISchemes: http, https +Layout/LineLength: + Max: 152 diff --git a/Guardfile b/Guardfile index e426982..a667547 100644 --- a/Guardfile +++ b/Guardfile @@ -22,3 +22,8 @@ guard :rspec, RSPEC_CONFIG do ruby = dsl.ruby dsl.watch_spec_files_for(ruby.lib_files) end + +guard :rubocop do + watch(%r{.+\.rb$}) + watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) } +end diff --git a/gems.rb b/gems.rb index d120573..6376930 100644 --- a/gems.rb +++ b/gems.rb @@ -17,4 +17,5 @@ gem "bake-test" gem "bake-test-external" gem "guard-rspec", "~> 4.7" + gem "guard-rubocop", "~> 1.5" end From d957c620f3ddb727637b87bd323930aaa7f79ab6 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:12:09 +0200 Subject: [PATCH 06/59] Configure RuboCop --- Guardfile | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Guardfile b/Guardfile index a667547..46a46db 100644 --- a/Guardfile +++ b/Guardfile @@ -23,7 +23,14 @@ guard :rspec, RSPEC_CONFIG do dsl.watch_spec_files_for(ruby.lib_files) end -guard :rubocop do - watch(%r{.+\.rb$}) - watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) } +RUBOCOP_CONFIG = { + cli: '--display-cop-names --parallel', + all_on_start: true, + halt_on_fail: true +}.freeze + +guard :rubocop, RUBOCOP_CONFIG do + watch(/.+\.rb$/) + watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| ::File.dirname(m[0]) } + watch('Guardfile') end From 08bb688e45b301a6a9ebe51f82bad42d93dcff91 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:13:28 +0200 Subject: [PATCH 07/59] Use new RuboCop cops and store TODOs --- .rubocop.yml | 3 +++ .rubocop_todo.yml | 32 +++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index cc32da4..2cac09a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1 +1,4 @@ inherit_from: .rubocop_todo.yml + +AllCops: + NewCops: enable diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0cab8b0..6b7d739 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-09-16 08:09:26 UTC using RuboCop version 1.56.3. +# on 2023-09-16 08:13:16 UTC using RuboCop version 1.56.3. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -14,6 +14,22 @@ Bundler/GemFilename: Exclude: - 'gems.rb' +# Offense count: 2 +# Configuration parameters: EnforcedStyle, AllowedGems, Include. +# SupportedStyles: Gemfile, gems.rb, gemspec +# Include: **/*.gemspec, **/Gemfile, **/gems.rb +Gemspec/DevelopmentDependencies: + Exclude: + - 'rspec-memory.gemspec' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: Severity, Include. +# Include: **/*.gemspec +Gemspec/RequireMFA: + Exclude: + - 'rspec-memory.gemspec' + # Offense count: 1 # Configuration parameters: Severity, Include. # Include: **/*.gemspec @@ -218,6 +234,12 @@ Style/MultilineIfModifier: - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'spec/rspec/memory_spec.rb' +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +Style/RedundantConstantBase: + Exclude: + - 'Guardfile' + # Offense count: 4 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowMultipleReturnValues. @@ -233,14 +255,6 @@ Style/RedundantSelf: - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'lib/rspec/memory/trace.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, AllowInnerSlashes. -# SupportedStyles: slashes, percent_r, mixed -Style/RegexpLiteral: - Exclude: - - 'Guardfile' - # Offense count: 37 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. From f80c26e74799c0df2746949b910bccf4912750a1 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:18:22 +0200 Subject: [PATCH 08/59] Use all cops available and store TODOs --- .rubocop.yml | 4 ++ .rubocop_todo.yml | 118 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 115 insertions(+), 7 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 2cac09a..dda706e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -2,3 +2,7 @@ inherit_from: .rubocop_todo.yml AllCops: NewCops: enable + EnabledByDefault: true + +Style/Copyright: + Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6b7d739..33ce0fd 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,11 +1,18 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-09-16 08:13:16 UTC using RuboCop version 1.56.3. +# on 2023-09-16 08:17:38 UTC using RuboCop version 1.56.3. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. +# Offense count: 6 +# Configuration parameters: Include, IgnoredGems, OnlyFor. +# Include: **/*.gemfile, **/Gemfile, **/gems.rb +Bundler/GemComment: + Exclude: + - 'gems.rb' + # Offense count: 1 # Configuration parameters: EnforcedStyle, Include. # SupportedStyles: Gemfile, gems.rb @@ -14,6 +21,22 @@ Bundler/GemFilename: Exclude: - 'gems.rb' +# Offense count: 4 +# Configuration parameters: EnforcedStyle, Include, AllowedGems. +# SupportedStyles: required, forbidden +# Include: **/*.gemfile, **/Gemfile, **/gems.rb +Bundler/GemVersion: + Exclude: + - 'gems.rb' + +# Offense count: 2 +# Configuration parameters: EnforcedStyle, Include, AllowedGems. +# SupportedStyles: required, forbidden +# Include: **/*.gemspec +Gemspec/DependencyVersion: + Exclude: + - 'rspec-memory.gemspec' + # Offense count: 2 # Configuration parameters: EnforcedStyle, AllowedGems, Include. # SupportedStyles: Gemfile, gems.rb, gemspec @@ -37,6 +60,14 @@ Gemspec/RequiredRubyVersion: Exclude: - 'rspec-memory.gemspec' +# Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). +# Configuration parameters: Categories, ExpectedOrder. +# ExpectedOrder: module_inclusion, constants, public_class_methods, initializer, public_methods, protected_methods, private_methods +Layout/ClassStructure: + Exclude: + - 'lib/rspec/memory/matchers/limit_allocations.rb' + # Offense count: 311 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: IndentationWidth, EnforcedStyle. @@ -66,6 +97,22 @@ Layout/IndentationWidth: - 'spec/rspec/memory_spec.rb' - 'spec/spec_helper.rb' +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedTypes: block, case, class, if, kwbegin, module +# SupportedStyles: same_line, new_line +Layout/MultilineAssignmentLayout: + Exclude: + - 'lib/rspec/memory/trace.rb' + +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: InspectBlocks. +Layout/RedundantLineBreak: + Exclude: + - 'Guardfile' + # Offense count: 1 # This cop supports safe autocorrection (--autocorrect). Layout/SpaceAfterComma: @@ -117,6 +164,18 @@ Lint/ConstantDefinitionInBlock: Exclude: - 'lib/rspec/memory/trace.rb' +# Offense count: 66 +# Configuration parameters: Only, Ignore. +Lint/ConstantResolution: + Exclude: + - 'Guardfile' + - 'lib/rspec/memory.rb' + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'lib/rspec/memory/trace.rb' + - 'rspec-memory.gemspec' + - 'spec/rspec/memory_spec.rb' + - 'spec/spec_helper.rb' + # Offense count: 1 Lint/ShadowingOuterLocalVariable: Exclude: @@ -189,6 +248,13 @@ Style/Attr: Exclude: - 'lib/rspec/memory/trace.rb' +# Offense count: 2 +# Configuration parameters: IgnoreModules. +Style/ConstantVisibility: + Exclude: + - 'lib/rspec/memory/trace.rb' + - 'lib/rspec/memory/version.rb' + # Offense count: 3 # Configuration parameters: AllowedConstants. Style/Documentation: @@ -198,6 +264,15 @@ Style/Documentation: - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'lib/rspec/memory/trace.rb' +# Offense count: 14 +# Configuration parameters: RequireForNonPublicMethods. +Style/DocumentationMethod: + Exclude: + - 'spec/**/*' + - 'test/**/*' + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'lib/rspec/memory/trace.rb' + # Offense count: 1 # This cop supports safe autocorrection (--autocorrect). Style/EmptyLiteral: @@ -219,6 +294,24 @@ Style/IfUnlessModifier: - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'lib/rspec/memory/trace.rb' +# Offense count: 26 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: IgnoreMacros, AllowedMethods, AllowedPatterns, IncludedMacros, AllowParenthesesInMultilineCall, AllowParenthesesInChaining, AllowParenthesesInCamelCaseMethod, AllowParenthesesInStringInterpolation, EnforcedStyle. +# SupportedStyles: require_parentheses, omit_parentheses +Style/MethodCallWithArgsParentheses: + Exclude: + - 'lib/rspec/memory.rb' + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'lib/rspec/memory/trace.rb' + - 'rspec-memory.gemspec' + - 'spec/rspec/memory_spec.rb' + - 'spec/spec_helper.rb' + +# Offense count: 14 +Style/MethodCalledOnDoEndBlock: + Exclude: + - 'spec/rspec/memory_spec.rb' + # Offense count: 1 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle. @@ -227,18 +320,21 @@ Style/MethodDefParentheses: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 3 +# Offense count: 4 # This cop supports safe autocorrection (--autocorrect). -Style/MultilineIfModifier: +# Configuration parameters: EnforcedStyle. +# SupportedStyles: if, case, both +Style/MissingElse: Exclude: - 'lib/rspec/memory/matchers/limit_allocations.rb' - - 'spec/rspec/memory_spec.rb' + - 'lib/rspec/memory/trace.rb' -# Offense count: 1 +# Offense count: 3 # This cop supports safe autocorrection (--autocorrect). -Style/RedundantConstantBase: +Style/MultilineIfModifier: Exclude: - - 'Guardfile' + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'spec/rspec/memory_spec.rb' # Offense count: 4 # This cop supports safe autocorrection (--autocorrect). @@ -255,6 +351,14 @@ Style/RedundantSelf: - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'lib/rspec/memory/trace.rb' +# Offense count: 1 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle. +# SupportedStyles: return, return_nil +Style/ReturnNil: + Exclude: + - 'lib/rspec/memory/trace.rb' + # Offense count: 37 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. From 3da3bf0ce67d9156412838e9ee2ad5ee77bfd98f Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:19:13 +0200 Subject: [PATCH 09/59] Suggest extensions in RuboCop for the future --- .rubocop.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.rubocop.yml b/.rubocop.yml index dda706e..88e269c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -2,6 +2,7 @@ inherit_from: .rubocop_todo.yml AllCops: NewCops: enable + SuggestExtensions: true EnabledByDefault: true Style/Copyright: From 066166b10e3f167a672de53997e1014277f611bb Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:20:44 +0200 Subject: [PATCH 10/59] Use recent Ruby version --- .rubocop.yml | 1 + .rubocop_todo.yml | 27 ++++++++++++++++++++++++++- .ruby-version | 1 + gems.rb | 2 ++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 .ruby-version diff --git a/.rubocop.yml b/.rubocop.yml index 88e269c..30dfbb6 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -4,6 +4,7 @@ AllCops: NewCops: enable SuggestExtensions: true EnabledByDefault: true + TargetRubyVersion: 3.2.2 Style/Copyright: Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 33ce0fd..fa834b6 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-09-16 08:17:38 UTC using RuboCop version 1.56.3. +# on 2023-09-16 08:20:23 UTC using RuboCop version 1.56.3. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -219,6 +219,14 @@ Metrics/MethodLength: Metrics/PerceivedComplexity: Max: 10 +# Offense count: 5 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, BlockForwardingName. +# SupportedStyles: anonymous, explicit +Naming/BlockForwarding: + Exclude: + - 'lib/rspec/memory/trace.rb' + # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: AllowModifiersOnSymbols. @@ -242,6 +250,14 @@ Style/AndOr: Exclude: - 'lib/rspec/memory/matchers/limit_allocations.rb' +# Offense count: 4 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: AllowOnlyRestArgument, UseAnonymousForwarding. +Style/ArgumentsForwarding: + Exclude: + - 'lib/rspec/memory/matchers/limit_allocations.rb' + - 'lib/rspec/memory/trace.rb' + # Offense count: 4 # This cop supports safe autocorrection (--autocorrect). Style/Attr: @@ -287,6 +303,15 @@ Style/FrozenStringLiteralComment: Exclude: - 'Guardfile' +# Offense count: 3 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, EnforcedShorthandSyntax, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. +# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys +# SupportedShorthandSyntax: always, never, either, consistent +Style/HashSyntax: + Exclude: + - 'lib/rspec/memory/matchers/limit_allocations.rb' + # Offense count: 3 # This cop supports safe autocorrection (--autocorrect). Style/IfUnlessModifier: diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..9e79f6c --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +ruby-3.2.2 diff --git a/gems.rb b/gems.rb index 6376930..c8e55ba 100644 --- a/gems.rb +++ b/gems.rb @@ -5,6 +5,8 @@ source 'https://rubygems.org' +ruby '3.2.2' + # Specify your gem's dependencies in rspec-memory.gemspec gemspec From c7737c5708f49011dc226b59bcdfb9dfc1571fd3 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:23:28 +0200 Subject: [PATCH 11/59] Use indentation by spaces instead of tabs --- .rubocop_todo.yml | 15 -- gems.rb | 12 +- lib/rspec/memory.rb | 2 +- .../memory/matchers/limit_allocations.rb | 184 +++++++------- lib/rspec/memory/trace.rb | 228 +++++++++--------- lib/rspec/memory/version.rb | 6 +- rspec-memory.gemspec | 36 +-- spec/rspec/memory_spec.rb | 140 +++++------ spec/spec_helper.rb | 14 +- 9 files changed, 311 insertions(+), 326 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index fa834b6..1adcdbb 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -68,21 +68,6 @@ Layout/ClassStructure: Exclude: - 'lib/rspec/memory/matchers/limit_allocations.rb' -# Offense count: 311 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: IndentationWidth, EnforcedStyle. -# SupportedStyles: spaces, tabs -Layout/IndentationStyle: - Exclude: - - 'gems.rb' - - 'lib/rspec/memory.rb' - - 'lib/rspec/memory/matchers/limit_allocations.rb' - - 'lib/rspec/memory/trace.rb' - - 'lib/rspec/memory/version.rb' - - 'rspec-memory.gemspec' - - 'spec/rspec/memory_spec.rb' - - 'spec/spec_helper.rb' - # Offense count: 101 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: Width, AllowedPatterns. diff --git a/gems.rb b/gems.rb index c8e55ba..2221450 100644 --- a/gems.rb +++ b/gems.rb @@ -11,13 +11,13 @@ gemspec group :maintenance, optional: true do - gem "bake-gem" - gem "bake-modernize" + gem "bake-gem" + gem "bake-modernize" end group :test do - gem "bake-test" - gem "bake-test-external" - gem "guard-rspec", "~> 4.7" - gem "guard-rubocop", "~> 1.5" + gem "bake-test" + gem "bake-test-external" + gem "guard-rspec", "~> 4.7" + gem "guard-rubocop", "~> 1.5" end diff --git a/lib/rspec/memory.rb b/lib/rspec/memory.rb index 18f928a..c28bdf9 100644 --- a/lib/rspec/memory.rb +++ b/lib/rspec/memory.rb @@ -6,5 +6,5 @@ require_relative 'memory/matchers/limit_allocations' RSpec.shared_context RSpec::Memory do - include RSpec::Memory::Matchers + include RSpec::Memory::Matchers end diff --git a/lib/rspec/memory/matchers/limit_allocations.rb b/lib/rspec/memory/matchers/limit_allocations.rb index 6eb4a34..5c79160 100644 --- a/lib/rspec/memory/matchers/limit_allocations.rb +++ b/lib/rspec/memory/matchers/limit_allocations.rb @@ -8,96 +8,96 @@ require 'rspec/expectations' module RSpec - module Memory - module Matchers - class LimitAllocations - include RSpec::Matchers::Composable - - def initialize(allocations = {}, count: nil, size: nil) - @count = count - @size = size - - @allocations = {} - @errors = [] - - allocations.each do |klass, count| - self.of(klass, count: count) - end - end - - def supports_block_expectations? - true - end - - def of(klass, **limits) - @allocations[klass] = limits - - return self - end - - private def check(value, limit) - case limit - when Range - unless limit.include? value - yield "expected within #{limit}" - end - when Integer - unless value == limit - yield "expected exactly #{limit}" - end - end - end - - def matches?(given_proc) - return true unless trace = Trace.capture(@allocations.keys, &given_proc) - - if @count or @size - # If the spec specifies a total limit, we have a limit which we can enforce which takes all allocations into account: - total = trace.total - - check(total.count, @count) do |expected| - @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} instances" - end if @count - - check(total.size, @size) do |expected| - @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} bytes" - end if @size - else - # Otherwise unspecified allocations are considered an error: - trace.ignored.each do |klass, allocation| - @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, but it was not specified" - end - end - - trace.allocated.each do |klass, allocation| - next unless acceptable = @allocations[klass] - - check(allocation.count, acceptable[:count]) do |expected| - @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, #{expected} instances" - end - - check(allocation.size, acceptable[:size]) do |expected| - @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, #{expected} bytes" - end - end - - return @errors.empty? - end - - def failure_message - "exceeded allocation limit: #{@errors.join(', ')}" - end - end - - if respond_to?(:ruby2_keywords, true) - def limit_allocations(count: nil, size: nil, **allocations) - LimitAllocations.new(allocations, count: count, size: size) - end - else - def limit_allocations(*arguments) - LimitAllocations.new(*arguments) - end - end - end - end + module Memory + module Matchers + class LimitAllocations + include RSpec::Matchers::Composable + + def initialize(allocations = {}, count: nil, size: nil) + @count = count + @size = size + + @allocations = {} + @errors = [] + + allocations.each do |klass, count| + self.of(klass, count: count) + end + end + + def supports_block_expectations? + true + end + + def of(klass, **limits) + @allocations[klass] = limits + + return self + end + + private def check(value, limit) + case limit + when Range + unless limit.include? value + yield "expected within #{limit}" + end + when Integer + unless value == limit + yield "expected exactly #{limit}" + end + end + end + + def matches?(given_proc) + return true unless trace = Trace.capture(@allocations.keys, &given_proc) + + if @count or @size + # If the spec specifies a total limit, we have a limit which we can enforce which takes all allocations into account: + total = trace.total + + check(total.count, @count) do |expected| + @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} instances" + end if @count + + check(total.size, @size) do |expected| + @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} bytes" + end if @size + else + # Otherwise unspecified allocations are considered an error: + trace.ignored.each do |klass, allocation| + @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, but it was not specified" + end + end + + trace.allocated.each do |klass, allocation| + next unless acceptable = @allocations[klass] + + check(allocation.count, acceptable[:count]) do |expected| + @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, #{expected} instances" + end + + check(allocation.size, acceptable[:size]) do |expected| + @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, #{expected} bytes" + end + end + + return @errors.empty? + end + + def failure_message + "exceeded allocation limit: #{@errors.join(', ')}" + end + end + + if respond_to?(:ruby2_keywords, true) + def limit_allocations(count: nil, size: nil, **allocations) + LimitAllocations.new(allocations, count: count, size: size) + end + else + def limit_allocations(*arguments) + LimitAllocations.new(*arguments) + end + end + end + end end diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index 5ec494f..bd7a09a 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -7,118 +7,118 @@ require 'objspace' module RSpec - module Memory - Allocation = Struct.new(:count, :size) do - SLOT_SIZE = ObjectSpace.memsize_of(Object.new) - - def << object - self.count += 1 - - # We don't want to force specs to take the slot size into account. - self.size += ObjectSpace.memsize_of(object) - SLOT_SIZE - end - - def self.default_hash - Hash.new{|h,k| h[k] = Allocation.new(0, 0)} - end - end - - class Trace - def self.supported? - # There are issues on truffleruby-1.0.0rc9 - return false if RUBY_ENGINE == "truffleruby" - - ObjectSpace.respond_to?(:trace_object_allocations) - end - - if supported? - def self.capture(*args, &block) - self.new(*args).tap do |trace| - trace.capture(&block) - end - end - else - def self.capture(*args, &block) - yield - - return nil - end - end - - def initialize(klasses) - @klasses = klasses - - @allocated = Allocation.default_hash - @retained = Allocation.default_hash - - @ignored = Allocation.default_hash - - @total = Allocation.new(0, 0) - end - - attr :allocated - attr :retained - - attr :ignored - - attr :total - - def current_objects(generation) - allocations = [] - - ObjectSpace.each_object do |object| - if ObjectSpace.allocation_generation(object) == generation - allocations << object - end - end - - return allocations - end - - def find_base(object) - @klasses.find{|klass| object.is_a? klass} - end - - def capture(&block) - GC.start - - begin - GC.disable - - generation = GC.count - ObjectSpace.trace_object_allocations(&block) - - allocated = current_objects(generation) - ensure - GC.enable - end - - GC.start - retained = current_objects(generation) - - # All allocated objects, including those freed in the last GC: - allocated.each do |object| - if klass = find_base(object) - @allocated[klass] << object - else - # If the user specified classes, but we can't pin this allocation to a specific class, we issue a warning. - if @klasses.any? - warn "Ignoring allocation of #{object.class} at #{ObjectSpace.allocation_sourcefile(object)}:#{ObjectSpace.allocation_sourceline(object)}" - end - - @ignored[object.class] << object - end - - @total << object - end - - # Retained objects are still alive after a final GC: - retained.each do |object| - if klass = find_base(object) - @retained[klass] << object - end - end - end - end - end + module Memory + Allocation = Struct.new(:count, :size) do + SLOT_SIZE = ObjectSpace.memsize_of(Object.new) + + def << object + self.count += 1 + + # We don't want to force specs to take the slot size into account. + self.size += ObjectSpace.memsize_of(object) - SLOT_SIZE + end + + def self.default_hash + Hash.new{|h,k| h[k] = Allocation.new(0, 0)} + end + end + + class Trace + def self.supported? + # There are issues on truffleruby-1.0.0rc9 + return false if RUBY_ENGINE == "truffleruby" + + ObjectSpace.respond_to?(:trace_object_allocations) + end + + if supported? + def self.capture(*args, &block) + self.new(*args).tap do |trace| + trace.capture(&block) + end + end + else + def self.capture(*args, &block) + yield + + return nil + end + end + + def initialize(klasses) + @klasses = klasses + + @allocated = Allocation.default_hash + @retained = Allocation.default_hash + + @ignored = Allocation.default_hash + + @total = Allocation.new(0, 0) + end + + attr :allocated + attr :retained + + attr :ignored + + attr :total + + def current_objects(generation) + allocations = [] + + ObjectSpace.each_object do |object| + if ObjectSpace.allocation_generation(object) == generation + allocations << object + end + end + + return allocations + end + + def find_base(object) + @klasses.find{|klass| object.is_a? klass} + end + + def capture(&block) + GC.start + + begin + GC.disable + + generation = GC.count + ObjectSpace.trace_object_allocations(&block) + + allocated = current_objects(generation) + ensure + GC.enable + end + + GC.start + retained = current_objects(generation) + + # All allocated objects, including those freed in the last GC: + allocated.each do |object| + if klass = find_base(object) + @allocated[klass] << object + else + # If the user specified classes, but we can't pin this allocation to a specific class, we issue a warning. + if @klasses.any? + warn "Ignoring allocation of #{object.class} at #{ObjectSpace.allocation_sourcefile(object)}:#{ObjectSpace.allocation_sourceline(object)}" + end + + @ignored[object.class] << object + end + + @total << object + end + + # Retained objects are still alive after a final GC: + retained.each do |object| + if klass = find_base(object) + @retained[klass] << object + end + end + end + end + end end diff --git a/lib/rspec/memory/version.rb b/lib/rspec/memory/version.rb index 0e68176..12f4995 100644 --- a/lib/rspec/memory/version.rb +++ b/lib/rspec/memory/version.rb @@ -4,7 +4,7 @@ # Copyright, 2019-2023, by Samuel Williams. module RSpec - module Memory - VERSION = "1.0.4" - end + module Memory + VERSION = "1.0.4" + end end diff --git a/rspec-memory.gemspec b/rspec-memory.gemspec index bff7dd5..cb5a504 100644 --- a/rspec-memory.gemspec +++ b/rspec-memory.gemspec @@ -3,22 +3,22 @@ require_relative "lib/rspec/memory/version" Gem::Specification.new do |spec| - spec.name = "rspec-memory" - spec.version = RSpec::Memory::VERSION - - spec.summary = "RSpec helpers for checking memory allocations." - spec.authors = ["Samuel Williams", "Olle Jonsson", "Cyril Roelandt", "Daniel Leidert", "Felix Yan"] - spec.license = "MIT" - - spec.cert_chain = ['release.cert'] - spec.signing_key = File.expand_path('~/.gem/release.pem') - - spec.homepage = "https://github.com/socketry/rspec-memory" - - spec.files = Dir.glob(['{lib}/**/*', '*.md'], File::FNM_DOTMATCH, base: __dir__) - - spec.add_dependency "rspec", "~> 3.0" - - spec.add_development_dependency "bundler" - spec.add_development_dependency "covered" + spec.name = "rspec-memory" + spec.version = RSpec::Memory::VERSION + + spec.summary = "RSpec helpers for checking memory allocations." + spec.authors = ["Samuel Williams", "Olle Jonsson", "Cyril Roelandt", "Daniel Leidert", "Felix Yan"] + spec.license = "MIT" + + spec.cert_chain = ['release.cert'] + spec.signing_key = File.expand_path('~/.gem/release.pem') + + spec.homepage = "https://github.com/socketry/rspec-memory" + + spec.files = Dir.glob(['{lib}/**/*', '*.md'], File::FNM_DOTMATCH, base: __dir__) + + spec.add_dependency "rspec", "~> 3.0" + + spec.add_development_dependency "bundler" + spec.add_development_dependency "covered" end diff --git a/spec/rspec/memory_spec.rb b/spec/rspec/memory_spec.rb index e323e4c..b5c83c8 100644 --- a/spec/rspec/memory_spec.rb +++ b/spec/rspec/memory_spec.rb @@ -6,75 +6,75 @@ require 'rspec/memory' RSpec.describe RSpec::Memory do - include_context RSpec::Memory - - it "should execute code in block" do - string = nil - - expect do - string = String.new - end.to limit_allocations(String => 1) - - expect(string).to_not be_nil - end - - context "on supported platform", if: RSpec::Memory::Trace.supported? do - it "should not exceed specified count limit" do - expect do - 2.times{String.new} - end.to limit_allocations(String => 2) - - expect do - 2.times{String.new} - end.to limit_allocations.of(String, count: 2) - end - - it "should fail if there are untracked allocations" do - expect do - expect do - Array.new - end.to limit_allocations - end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /it was not specified/) - end - - it "should exceed specified count limit" do - expect do - expect do - 6.times{String.new} - end.to limit_allocations(String => 4) - end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected exactly 4 instances/) - end if RSpec::Memory::Trace.supported? - - it "should be within specified count range" do - expect do - 2.times{String.new} - end.to limit_allocations(String => 1..3) + include_context RSpec::Memory + + it "should execute code in block" do + string = nil + + expect do + string = String.new + end.to limit_allocations(String => 1) + + expect(string).to_not be_nil + end + + context "on supported platform", if: RSpec::Memory::Trace.supported? do + it "should not exceed specified count limit" do + expect do + 2.times{String.new} + end.to limit_allocations(String => 2) + + expect do + 2.times{String.new} + end.to limit_allocations.of(String, count: 2) + end + + it "should fail if there are untracked allocations" do + expect do + expect do + Array.new + end.to limit_allocations + end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /it was not specified/) + end + + it "should exceed specified count limit" do + expect do + expect do + 6.times{String.new} + end.to limit_allocations(String => 4) + end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected exactly 4 instances/) + end if RSpec::Memory::Trace.supported? + + it "should be within specified count range" do + expect do + 2.times{String.new} + end.to limit_allocations(String => 1..3) - expect do - 2.times{String.new} - end.to limit_allocations.of(String, count: 1..3) - end - - it "should exceed specified count range" do - expect do - expect do - 6.times{String.new} - end.to limit_allocations(String => 1..3) - end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected within 1..3 instances/) - end - - it "should not exceed specified size limit" do - expect do - "a" * 100_000 - end.to limit_allocations.of(String, size: 100_001) - end - - it "should exceed specified size limit" do - expect do - expect do - "a" * 120_000 - end.to limit_allocations(size: 100_000) - end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected exactly 100000 bytes/) - end - end + expect do + 2.times{String.new} + end.to limit_allocations.of(String, count: 1..3) + end + + it "should exceed specified count range" do + expect do + expect do + 6.times{String.new} + end.to limit_allocations(String => 1..3) + end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected within 1..3 instances/) + end + + it "should not exceed specified size limit" do + expect do + "a" * 100_000 + end.to limit_allocations.of(String, size: 100_001) + end + + it "should exceed specified size limit" do + expect do + expect do + "a" * 120_000 + end.to limit_allocations(size: 100_000) + end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected exactly 100000 bytes/) + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 44fcedd..9846848 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -6,13 +6,13 @@ require 'covered/rspec' RSpec.configure do |config| - # Enable flags like --only-failures and --next-failure - config.example_status_persistence_file_path = ".rspec_status" + # Enable flags like --only-failures and --next-failure + config.example_status_persistence_file_path = ".rspec_status" - # Disable RSpec exposing methods globally on `Module` and `main` - config.disable_monkey_patching! + # Disable RSpec exposing methods globally on `Module` and `main` + config.disable_monkey_patching! - config.expect_with :rspec do |c| - c.syntax = :expect - end + config.expect_with :rspec do |c| + c.syntax = :expect + end end From 119312fabc32a490d9c6a101e3c30eb0b9698ffd Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:24:31 +0200 Subject: [PATCH 12/59] Prefer single-quoted strings --- .rubocop_todo.yml | 14 -------------- Guardfile | 2 +- gems.rb | 12 ++++++------ lib/rspec/memory/trace.rb | 2 +- lib/rspec/memory/version.rb | 2 +- rspec-memory.gemspec | 18 +++++++++--------- spec/rspec/memory_spec.rb | 22 +++++++++++----------- spec/spec_helper.rb | 2 +- 8 files changed, 30 insertions(+), 44 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1adcdbb..b23102d 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -369,20 +369,6 @@ Style/ReturnNil: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 37 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. -# SupportedStyles: single_quotes, double_quotes -Style/StringLiterals: - Exclude: - - 'Guardfile' - - 'gems.rb' - - 'lib/rspec/memory/trace.rb' - - 'lib/rspec/memory/version.rb' - - 'rspec-memory.gemspec' - - 'spec/rspec/memory_spec.rb' - - 'spec/spec_helper.rb' - # Offense count: 4 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. diff --git a/Guardfile b/Guardfile index 46a46db..9c52e8a 100644 --- a/Guardfile +++ b/Guardfile @@ -7,7 +7,7 @@ RSPEC_CONFIG = { }.freeze guard :rspec, RSPEC_CONFIG do - require "guard/rspec/dsl" + require 'guard/rspec/dsl' dsl = Guard::RSpec::Dsl.new(self) # Feel free to open issues for suggestions and improvements diff --git a/gems.rb b/gems.rb index 2221450..24253dd 100644 --- a/gems.rb +++ b/gems.rb @@ -11,13 +11,13 @@ gemspec group :maintenance, optional: true do - gem "bake-gem" - gem "bake-modernize" + gem 'bake-gem' + gem 'bake-modernize' end group :test do - gem "bake-test" - gem "bake-test-external" - gem "guard-rspec", "~> 4.7" - gem "guard-rubocop", "~> 1.5" + gem 'bake-test' + gem 'bake-test-external' + gem 'guard-rspec', '~> 4.7' + gem 'guard-rubocop', '~> 1.5' end diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index bd7a09a..5df4201 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -26,7 +26,7 @@ def self.default_hash class Trace def self.supported? # There are issues on truffleruby-1.0.0rc9 - return false if RUBY_ENGINE == "truffleruby" + return false if RUBY_ENGINE == 'truffleruby' ObjectSpace.respond_to?(:trace_object_allocations) end diff --git a/lib/rspec/memory/version.rb b/lib/rspec/memory/version.rb index 12f4995..e18a3d7 100644 --- a/lib/rspec/memory/version.rb +++ b/lib/rspec/memory/version.rb @@ -5,6 +5,6 @@ module RSpec module Memory - VERSION = "1.0.4" + VERSION = '1.0.4' end end diff --git a/rspec-memory.gemspec b/rspec-memory.gemspec index cb5a504..669f58a 100644 --- a/rspec-memory.gemspec +++ b/rspec-memory.gemspec @@ -1,24 +1,24 @@ # frozen_string_literal: true -require_relative "lib/rspec/memory/version" +require_relative 'lib/rspec/memory/version' Gem::Specification.new do |spec| - spec.name = "rspec-memory" + spec.name = 'rspec-memory' spec.version = RSpec::Memory::VERSION - spec.summary = "RSpec helpers for checking memory allocations." - spec.authors = ["Samuel Williams", "Olle Jonsson", "Cyril Roelandt", "Daniel Leidert", "Felix Yan"] - spec.license = "MIT" + spec.summary = 'RSpec helpers for checking memory allocations.' + spec.authors = ['Samuel Williams', 'Olle Jonsson', 'Cyril Roelandt', 'Daniel Leidert', 'Felix Yan'] + spec.license = 'MIT' spec.cert_chain = ['release.cert'] spec.signing_key = File.expand_path('~/.gem/release.pem') - spec.homepage = "https://github.com/socketry/rspec-memory" + spec.homepage = 'https://github.com/socketry/rspec-memory' spec.files = Dir.glob(['{lib}/**/*', '*.md'], File::FNM_DOTMATCH, base: __dir__) - spec.add_dependency "rspec", "~> 3.0" + spec.add_dependency 'rspec', '~> 3.0' - spec.add_development_dependency "bundler" - spec.add_development_dependency "covered" + spec.add_development_dependency 'bundler' + spec.add_development_dependency 'covered' end diff --git a/spec/rspec/memory_spec.rb b/spec/rspec/memory_spec.rb index b5c83c8..9ef933f 100644 --- a/spec/rspec/memory_spec.rb +++ b/spec/rspec/memory_spec.rb @@ -8,7 +8,7 @@ RSpec.describe RSpec::Memory do include_context RSpec::Memory - it "should execute code in block" do + it 'should execute code in block' do string = nil expect do @@ -18,8 +18,8 @@ expect(string).to_not be_nil end - context "on supported platform", if: RSpec::Memory::Trace.supported? do - it "should not exceed specified count limit" do + context 'on supported platform', if: RSpec::Memory::Trace.supported? do + it 'should not exceed specified count limit' do expect do 2.times{String.new} end.to limit_allocations(String => 2) @@ -29,7 +29,7 @@ end.to limit_allocations.of(String, count: 2) end - it "should fail if there are untracked allocations" do + it 'should fail if there are untracked allocations' do expect do expect do Array.new @@ -37,7 +37,7 @@ end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /it was not specified/) end - it "should exceed specified count limit" do + it 'should exceed specified count limit' do expect do expect do 6.times{String.new} @@ -45,7 +45,7 @@ end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected exactly 4 instances/) end if RSpec::Memory::Trace.supported? - it "should be within specified count range" do + it 'should be within specified count range' do expect do 2.times{String.new} end.to limit_allocations(String => 1..3) @@ -55,7 +55,7 @@ end.to limit_allocations.of(String, count: 1..3) end - it "should exceed specified count range" do + it 'should exceed specified count range' do expect do expect do 6.times{String.new} @@ -63,16 +63,16 @@ end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected within 1..3 instances/) end - it "should not exceed specified size limit" do + it 'should not exceed specified size limit' do expect do - "a" * 100_000 + 'a' * 100_000 end.to limit_allocations.of(String, size: 100_001) end - it "should exceed specified size limit" do + it 'should exceed specified size limit' do expect do expect do - "a" * 120_000 + 'a' * 120_000 end.to limit_allocations(size: 100_000) end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected exactly 100000 bytes/) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9846848..c566e75 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,7 +7,7 @@ RSpec.configure do |config| # Enable flags like --only-failures and --next-failure - config.example_status_persistence_file_path = ".rspec_status" + config.example_status_persistence_file_path = '.rspec_status' # Disable RSpec exposing methods globally on `Module` and `main` config.disable_monkey_patching! From 995e4942b67014802b8b555b7a53b4a35562083d Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:25:27 +0200 Subject: [PATCH 13/59] Remove redundant return --- .rubocop_todo.yml | 8 -------- lib/rspec/memory/matchers/limit_allocations.rb | 4 ++-- lib/rspec/memory/trace.rb | 4 ++-- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b23102d..64041fa 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -346,14 +346,6 @@ Style/MultilineIfModifier: - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'spec/rspec/memory_spec.rb' -# Offense count: 4 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowMultipleReturnValues. -Style/RedundantReturn: - Exclude: - - 'lib/rspec/memory/matchers/limit_allocations.rb' - - 'lib/rspec/memory/trace.rb' - # Offense count: 2 # This cop supports safe autocorrection (--autocorrect). Style/RedundantSelf: diff --git a/lib/rspec/memory/matchers/limit_allocations.rb b/lib/rspec/memory/matchers/limit_allocations.rb index 5c79160..f4208db 100644 --- a/lib/rspec/memory/matchers/limit_allocations.rb +++ b/lib/rspec/memory/matchers/limit_allocations.rb @@ -32,7 +32,7 @@ def supports_block_expectations? def of(klass, **limits) @allocations[klass] = limits - return self + self end private def check(value, limit) @@ -81,7 +81,7 @@ def matches?(given_proc) end end - return @errors.empty? + @errors.empty? end def failure_message diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index 5df4201..6754d62 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -41,7 +41,7 @@ def self.capture(*args, &block) def self.capture(*args, &block) yield - return nil + nil end end @@ -72,7 +72,7 @@ def current_objects(generation) end end - return allocations + allocations end def find_base(object) From f385929ffbe563e172ab2f8b89481d2908200425 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:28:05 +0200 Subject: [PATCH 14/59] Regenerate RuboCop TODO after changes --- .rubocop_todo.yml | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 64041fa..9a6e57a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-09-16 08:20:23 UTC using RuboCop version 1.56.3. +# on 2023-09-16 08:27:04 UTC using RuboCop version 1.56.3. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -68,20 +68,6 @@ Layout/ClassStructure: Exclude: - 'lib/rspec/memory/matchers/limit_allocations.rb' -# Offense count: 101 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: Width, AllowedPatterns. -Layout/IndentationWidth: - Exclude: - - 'gems.rb' - - 'lib/rspec/memory.rb' - - 'lib/rspec/memory/matchers/limit_allocations.rb' - - 'lib/rspec/memory/trace.rb' - - 'lib/rspec/memory/version.rb' - - 'rspec-memory.gemspec' - - 'spec/rspec/memory_spec.rb' - - 'spec/spec_helper.rb' - # Offense count: 1 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle. @@ -353,14 +339,6 @@ Style/RedundantSelf: - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'lib/rspec/memory/trace.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: return, return_nil -Style/ReturnNil: - Exclude: - - 'lib/rspec/memory/trace.rb' - # Offense count: 4 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. From 34f617b621a8677fc1edec8c7902c73a0acbc6d3 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:28:37 +0200 Subject: [PATCH 15/59] Remove redundant self --- .rubocop_todo.yml | 7 ------- lib/rspec/memory/matchers/limit_allocations.rb | 2 +- lib/rspec/memory/trace.rb | 2 +- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9a6e57a..39c1dfb 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -332,13 +332,6 @@ Style/MultilineIfModifier: - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'spec/rspec/memory_spec.rb' -# Offense count: 2 -# This cop supports safe autocorrection (--autocorrect). -Style/RedundantSelf: - Exclude: - - 'lib/rspec/memory/matchers/limit_allocations.rb' - - 'lib/rspec/memory/trace.rb' - # Offense count: 4 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. diff --git a/lib/rspec/memory/matchers/limit_allocations.rb b/lib/rspec/memory/matchers/limit_allocations.rb index f4208db..5e6ab8b 100644 --- a/lib/rspec/memory/matchers/limit_allocations.rb +++ b/lib/rspec/memory/matchers/limit_allocations.rb @@ -21,7 +21,7 @@ def initialize(allocations = {}, count: nil, size: nil) @errors = [] allocations.each do |klass, count| - self.of(klass, count: count) + of(klass, count: count) end end diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index 6754d62..1b5441c 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -33,7 +33,7 @@ def self.supported? if supported? def self.capture(*args, &block) - self.new(*args).tap do |trace| + new(*args).tap do |trace| trace.capture(&block) end end From 33f97e5b92ecf46d242fd0e72012ef18206665ca Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:30:07 +0200 Subject: [PATCH 16/59] Favor a normal if-statement Over a modifier clause in a multiline statement. Store more TODOs. --- .rubocop_todo.yml | 14 ++++---------- lib/rspec/memory/matchers/limit_allocations.rb | 16 ++++++++++------ spec/rspec/memory_spec.rb | 14 ++++++++------ 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 39c1dfb..9577266 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-09-16 08:27:04 UTC using RuboCop version 1.56.3. +# on 2023-09-16 08:29:47 UTC using RuboCop version 1.56.3. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -173,7 +173,7 @@ Metrics/AbcSize: # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. # AllowedMethods: refine Metrics/BlockLength: - Max: 59 + Max: 61 # Offense count: 1 # Configuration parameters: AllowedMethods, AllowedPatterns. @@ -183,7 +183,7 @@ Metrics/CyclomaticComplexity: # Offense count: 2 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. Metrics/MethodLength: - Max: 27 + Max: 28 # Offense count: 1 # Configuration parameters: AllowedMethods, AllowedPatterns. @@ -316,7 +316,7 @@ Style/MethodDefParentheses: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 4 +# Offense count: 7 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle. # SupportedStyles: if, case, both @@ -324,12 +324,6 @@ Style/MissingElse: Exclude: - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'lib/rspec/memory/trace.rb' - -# Offense count: 3 -# This cop supports safe autocorrection (--autocorrect). -Style/MultilineIfModifier: - Exclude: - - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'spec/rspec/memory_spec.rb' # Offense count: 4 diff --git a/lib/rspec/memory/matchers/limit_allocations.rb b/lib/rspec/memory/matchers/limit_allocations.rb index 5e6ab8b..ddfbbdf 100644 --- a/lib/rspec/memory/matchers/limit_allocations.rb +++ b/lib/rspec/memory/matchers/limit_allocations.rb @@ -55,13 +55,17 @@ def matches?(given_proc) # If the spec specifies a total limit, we have a limit which we can enforce which takes all allocations into account: total = trace.total - check(total.count, @count) do |expected| - @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} instances" - end if @count + if @count + check(total.count, @count) do |expected| + @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} instances" + end + end - check(total.size, @size) do |expected| - @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} bytes" - end if @size + if @size + check(total.size, @size) do |expected| + @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} bytes" + end + end else # Otherwise unspecified allocations are considered an error: trace.ignored.each do |klass, allocation| diff --git a/spec/rspec/memory_spec.rb b/spec/rspec/memory_spec.rb index 9ef933f..8e5088e 100644 --- a/spec/rspec/memory_spec.rb +++ b/spec/rspec/memory_spec.rb @@ -37,13 +37,15 @@ end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /it was not specified/) end - it 'should exceed specified count limit' do - expect do + if RSpec::Memory::Trace.supported? + it 'should exceed specified count limit' do expect do - 6.times{String.new} - end.to limit_allocations(String => 4) - end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected exactly 4 instances/) - end if RSpec::Memory::Trace.supported? + expect do + 6.times{String.new} + end.to limit_allocations(String => 4) + end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected exactly 4 instances/) + end + end it 'should be within specified count range' do expect do From 41994895243eca2db46bbf176885cc61f9544f7e Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:31:40 +0200 Subject: [PATCH 17/59] Use def with parentheses when there are parameters --- .rubocop_todo.yml | 8 -------- lib/rspec/memory/trace.rb | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9577266..01de0d0 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -308,14 +308,6 @@ Style/MethodCalledOnDoEndBlock: Exclude: - 'spec/rspec/memory_spec.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: require_parentheses, require_no_parentheses, require_no_parentheses_except_multiline -Style/MethodDefParentheses: - Exclude: - - 'lib/rspec/memory/trace.rb' - # Offense count: 7 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle. diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index 1b5441c..e1a9a12 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -11,7 +11,7 @@ module Memory Allocation = Struct.new(:count, :size) do SLOT_SIZE = ObjectSpace.memsize_of(Object.new) - def << object + def <<(object) self.count += 1 # We don't want to force specs to take the slot size into account. From 7d8df2ca0ae2e98c464932f920018bd5a322e2c1 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:34:02 +0200 Subject: [PATCH 18/59] Disable cop as it generates too much visual noise --- .rubocop.yml | 3 +++ .rubocop_todo.yml | 13 ------------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 30dfbb6..b2fcc02 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -8,3 +8,6 @@ AllCops: Style/Copyright: Enabled: false + +Style/MethodCallWithArgsParentheses: + Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 01de0d0..07b6476 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -290,19 +290,6 @@ Style/IfUnlessModifier: - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'lib/rspec/memory/trace.rb' -# Offense count: 26 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: IgnoreMacros, AllowedMethods, AllowedPatterns, IncludedMacros, AllowParenthesesInMultilineCall, AllowParenthesesInChaining, AllowParenthesesInCamelCaseMethod, AllowParenthesesInStringInterpolation, EnforcedStyle. -# SupportedStyles: require_parentheses, omit_parentheses -Style/MethodCallWithArgsParentheses: - Exclude: - - 'lib/rspec/memory.rb' - - 'lib/rspec/memory/matchers/limit_allocations.rb' - - 'lib/rspec/memory/trace.rb' - - 'rspec-memory.gemspec' - - 'spec/rspec/memory_spec.rb' - - 'spec/spec_helper.rb' - # Offense count: 14 Style/MethodCalledOnDoEndBlock: Exclude: From aef9053580fa062cd6ef0befb0da7f18bb07827d Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:34:56 +0200 Subject: [PATCH 19/59] Use newer hash syntax --- .rubocop_todo.yml | 9 --------- lib/rspec/memory/matchers/limit_allocations.rb | 4 ++-- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 07b6476..5d65f8c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -274,15 +274,6 @@ Style/FrozenStringLiteralComment: Exclude: - 'Guardfile' -# Offense count: 3 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, EnforcedShorthandSyntax, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. -# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys -# SupportedShorthandSyntax: always, never, either, consistent -Style/HashSyntax: - Exclude: - - 'lib/rspec/memory/matchers/limit_allocations.rb' - # Offense count: 3 # This cop supports safe autocorrection (--autocorrect). Style/IfUnlessModifier: diff --git a/lib/rspec/memory/matchers/limit_allocations.rb b/lib/rspec/memory/matchers/limit_allocations.rb index ddfbbdf..cd0694b 100644 --- a/lib/rspec/memory/matchers/limit_allocations.rb +++ b/lib/rspec/memory/matchers/limit_allocations.rb @@ -21,7 +21,7 @@ def initialize(allocations = {}, count: nil, size: nil) @errors = [] allocations.each do |klass, count| - of(klass, count: count) + of(klass, count:) end end @@ -95,7 +95,7 @@ def failure_message if respond_to?(:ruby2_keywords, true) def limit_allocations(count: nil, size: nil, **allocations) - LimitAllocations.new(allocations, count: count, size: size) + LimitAllocations.new(allocations, count:, size:) end else def limit_allocations(*arguments) From 589cfdb2f462cb9538eb2744a13e7d07e031972e Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:35:53 +0200 Subject: [PATCH 20/59] Add missing frozen string literal comment --- .rubocop_todo.yml | 8 -------- Guardfile | 2 ++ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5d65f8c..1968cc4 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -266,14 +266,6 @@ Style/EmptyLiteral: Exclude: - 'spec/rspec/memory_spec.rb' -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: always, always_true, never -Style/FrozenStringLiteralComment: - Exclude: - - 'Guardfile' - # Offense count: 3 # This cop supports safe autocorrection (--autocorrect). Style/IfUnlessModifier: diff --git a/Guardfile b/Guardfile index 9c52e8a..f309934 100644 --- a/Guardfile +++ b/Guardfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSPEC_CONFIG = { cmd: 'bundle exec rspec', all_on_start: true, From 370df1fdeb417bb53cda0909b45a59f82d272b08 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:36:59 +0200 Subject: [PATCH 21/59] Use explicit attr_reader --- .rubocop_todo.yml | 6 ------ lib/rspec/memory/trace.rb | 8 ++++---- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1968cc4..fced361 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -229,12 +229,6 @@ Style/ArgumentsForwarding: - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'lib/rspec/memory/trace.rb' -# Offense count: 4 -# This cop supports safe autocorrection (--autocorrect). -Style/Attr: - Exclude: - - 'lib/rspec/memory/trace.rb' - # Offense count: 2 # Configuration parameters: IgnoreModules. Style/ConstantVisibility: diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index e1a9a12..82c6373 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -56,12 +56,12 @@ def initialize(klasses) @total = Allocation.new(0, 0) end - attr :allocated - attr :retained + attr_reader :allocated + attr_reader :retained - attr :ignored + attr_reader :ignored - attr :total + attr_reader :total def current_objects(generation) allocations = [] From 27a1328cff0348cc89636ae838d41fc725a986c2 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:37:40 +0200 Subject: [PATCH 22/59] Use anonymous positional arguments forwarding --- .rubocop_todo.yml | 8 -------- lib/rspec/memory/matchers/limit_allocations.rb | 4 ++-- lib/rspec/memory/trace.rb | 4 ++-- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index fced361..c0fb1a3 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -221,14 +221,6 @@ Style/AndOr: Exclude: - 'lib/rspec/memory/matchers/limit_allocations.rb' -# Offense count: 4 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowOnlyRestArgument, UseAnonymousForwarding. -Style/ArgumentsForwarding: - Exclude: - - 'lib/rspec/memory/matchers/limit_allocations.rb' - - 'lib/rspec/memory/trace.rb' - # Offense count: 2 # Configuration parameters: IgnoreModules. Style/ConstantVisibility: diff --git a/lib/rspec/memory/matchers/limit_allocations.rb b/lib/rspec/memory/matchers/limit_allocations.rb index cd0694b..f3fe490 100644 --- a/lib/rspec/memory/matchers/limit_allocations.rb +++ b/lib/rspec/memory/matchers/limit_allocations.rb @@ -98,8 +98,8 @@ def limit_allocations(count: nil, size: nil, **allocations) LimitAllocations.new(allocations, count:, size:) end else - def limit_allocations(*arguments) - LimitAllocations.new(*arguments) + def limit_allocations(*) + LimitAllocations.new(*) end end end diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index 82c6373..d714342 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -32,8 +32,8 @@ def self.supported? end if supported? - def self.capture(*args, &block) - new(*args).tap do |trace| + def self.capture(*, &block) + new(*).tap do |trace| trace.capture(&block) end end From e192d31be5d57b6686e44c5ebbb01256f4d7fe60 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:38:27 +0200 Subject: [PATCH 23/59] Do not use unsafe 'or' --- .rubocop_todo.yml | 8 -------- lib/rspec/memory/matchers/limit_allocations.rb | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index c0fb1a3..39bb524 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -213,14 +213,6 @@ Style/AccessorGrouping: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: always, conditionals -Style/AndOr: - Exclude: - - 'lib/rspec/memory/matchers/limit_allocations.rb' - # Offense count: 2 # Configuration parameters: IgnoreModules. Style/ConstantVisibility: diff --git a/lib/rspec/memory/matchers/limit_allocations.rb b/lib/rspec/memory/matchers/limit_allocations.rb index f3fe490..de4ab5d 100644 --- a/lib/rspec/memory/matchers/limit_allocations.rb +++ b/lib/rspec/memory/matchers/limit_allocations.rb @@ -51,7 +51,7 @@ def of(klass, **limits) def matches?(given_proc) return true unless trace = Trace.capture(@allocations.keys, &given_proc) - if @count or @size + if @count || @size # If the spec specifies a total limit, we have a limit which we can enforce which takes all allocations into account: total = trace.total From 7d765cb6e33b7c5e85a295e49359b726604c0a74 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:39:55 +0200 Subject: [PATCH 24/59] Use spaces after commas to improve visual distinction --- .rubocop_todo.yml | 6 ------ lib/rspec/memory/trace.rb | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 39bb524..5494b64 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -84,12 +84,6 @@ Layout/RedundantLineBreak: Exclude: - 'Guardfile' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Layout/SpaceAfterComma: - Exclude: - - 'lib/rspec/memory/trace.rb' - # Offense count: 8 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index d714342..f592ada 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -19,7 +19,7 @@ def <<(object) end def self.default_hash - Hash.new{|h,k| h[k] = Allocation.new(0, 0)} + Hash.new{|h, k| h[k] = Allocation.new(0, 0)} end end From 3b6d1768a4dca1e72a9d9839094cee3a5695d9e5 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:41:39 +0200 Subject: [PATCH 25/59] Disable cop causing visual overload in one-liners with hashes --- .rubocop.yml | 3 +++ .rubocop_todo.yml | 7 ------- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index b2fcc02..cf9ea82 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -11,3 +11,6 @@ Style/Copyright: Style/MethodCallWithArgsParentheses: Enabled: false + +Layout/RedundantLineBreak: + Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5494b64..5c7e80e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -77,13 +77,6 @@ Layout/MultilineAssignmentLayout: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: InspectBlocks. -Layout/RedundantLineBreak: - Exclude: - - 'Guardfile' - # Offense count: 8 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. From ed9df4e403288df7db097b94f6f9a9f9c1fe0a85 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:45:45 +0200 Subject: [PATCH 26/59] Trigger the only spec when implementation files change --- Guardfile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Guardfile b/Guardfile index f309934..ba92793 100644 --- a/Guardfile +++ b/Guardfile @@ -23,11 +23,15 @@ guard :rspec, RSPEC_CONFIG do # Ruby files ruby = dsl.ruby dsl.watch_spec_files_for(ruby.lib_files) + + %w[matchers/limit_allocations.rb trace.rb].each do |file_path| + watch("lib/rspec/memory/#{file_path}") { 'spec/rspec/memory_spec.rb' } + end end RUBOCOP_CONFIG = { cli: '--display-cop-names --parallel', - all_on_start: true, + all_on_start: false, halt_on_fail: true }.freeze From 060d12bc4d4198aba683f5b4a4f422992f117df5 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:47:11 +0200 Subject: [PATCH 27/59] Use spaces for better visual distinction --- .rubocop_todo.yml | 10 ---------- lib/rspec/memory/trace.rb | 4 ++-- spec/rspec/memory_spec.rb | 12 ++++++------ 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5c7e80e..a7b6692 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -77,16 +77,6 @@ Layout/MultilineAssignmentLayout: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 8 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces. -# SupportedStyles: space, no_space -# SupportedStylesForEmptyBraces: space, no_space -Layout/SpaceBeforeBlockBraces: - Exclude: - - 'lib/rspec/memory/trace.rb' - - 'spec/rspec/memory_spec.rb' - # Offense count: 16 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index f592ada..590ed90 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -19,7 +19,7 @@ def <<(object) end def self.default_hash - Hash.new{|h, k| h[k] = Allocation.new(0, 0)} + Hash.new {|h, k| h[k] = Allocation.new(0, 0)} end end @@ -76,7 +76,7 @@ def current_objects(generation) end def find_base(object) - @klasses.find{|klass| object.is_a? klass} + @klasses.find {|klass| object.is_a? klass} end def capture(&block) diff --git a/spec/rspec/memory_spec.rb b/spec/rspec/memory_spec.rb index 8e5088e..b2dc3cc 100644 --- a/spec/rspec/memory_spec.rb +++ b/spec/rspec/memory_spec.rb @@ -21,11 +21,11 @@ context 'on supported platform', if: RSpec::Memory::Trace.supported? do it 'should not exceed specified count limit' do expect do - 2.times{String.new} + 2.times {String.new} end.to limit_allocations(String => 2) expect do - 2.times{String.new} + 2.times {String.new} end.to limit_allocations.of(String, count: 2) end @@ -41,7 +41,7 @@ it 'should exceed specified count limit' do expect do expect do - 6.times{String.new} + 6.times {String.new} end.to limit_allocations(String => 4) end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected exactly 4 instances/) end @@ -49,18 +49,18 @@ it 'should be within specified count range' do expect do - 2.times{String.new} + 2.times {String.new} end.to limit_allocations(String => 1..3) expect do - 2.times{String.new} + 2.times {String.new} end.to limit_allocations.of(String, count: 1..3) end it 'should exceed specified count range' do expect do expect do - 6.times{String.new} + 6.times {String.new} end.to limit_allocations(String => 1..3) end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected within 1..3 instances/) end From 3c04befc3ebb1f545a59b66bdd2bd192d57f37a9 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:47:34 +0200 Subject: [PATCH 28/59] Use spaces for better visual distinction --- .rubocop_todo.yml | 10 ---------- lib/rspec/memory/trace.rb | 4 ++-- spec/rspec/memory_spec.rb | 12 ++++++------ 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a7b6692..21a03f4 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -77,16 +77,6 @@ Layout/MultilineAssignmentLayout: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 16 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters. -# SupportedStyles: space, no_space -# SupportedStylesForEmptyBraces: space, no_space -Layout/SpaceInsideBlockBraces: - Exclude: - - 'lib/rspec/memory/trace.rb' - - 'spec/rspec/memory_spec.rb' - # Offense count: 61 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowInHeredoc. diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index 590ed90..91befe3 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -19,7 +19,7 @@ def <<(object) end def self.default_hash - Hash.new {|h, k| h[k] = Allocation.new(0, 0)} + Hash.new { |h, k| h[k] = Allocation.new(0, 0) } end end @@ -76,7 +76,7 @@ def current_objects(generation) end def find_base(object) - @klasses.find {|klass| object.is_a? klass} + @klasses.find { |klass| object.is_a? klass } end def capture(&block) diff --git a/spec/rspec/memory_spec.rb b/spec/rspec/memory_spec.rb index b2dc3cc..2841e17 100644 --- a/spec/rspec/memory_spec.rb +++ b/spec/rspec/memory_spec.rb @@ -21,11 +21,11 @@ context 'on supported platform', if: RSpec::Memory::Trace.supported? do it 'should not exceed specified count limit' do expect do - 2.times {String.new} + 2.times { String.new } end.to limit_allocations(String => 2) expect do - 2.times {String.new} + 2.times { String.new } end.to limit_allocations.of(String, count: 2) end @@ -41,7 +41,7 @@ it 'should exceed specified count limit' do expect do expect do - 6.times {String.new} + 6.times { String.new } end.to limit_allocations(String => 4) end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected exactly 4 instances/) end @@ -49,18 +49,18 @@ it 'should be within specified count range' do expect do - 2.times {String.new} + 2.times { String.new } end.to limit_allocations(String => 1..3) expect do - 2.times {String.new} + 2.times { String.new } end.to limit_allocations.of(String, count: 1..3) end it 'should exceed specified count range' do expect do expect do - 6.times {String.new} + 6.times { String.new } end.to limit_allocations(String => 1..3) end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected within 1..3 instances/) end From 2956aeeee325b40a63e0f7332995019dfda8fefc Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:47:58 +0200 Subject: [PATCH 29/59] Remove trailing whitespaces --- .rubocop_todo.yml | 10 ---- .../memory/matchers/limit_allocations.rb | 34 ++++++------ lib/rspec/memory/trace.rb | 54 +++++++++---------- rspec-memory.gemspec | 12 ++--- spec/rspec/memory_spec.rb | 22 ++++---- 5 files changed, 61 insertions(+), 71 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 21a03f4..dccd75a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -77,16 +77,6 @@ Layout/MultilineAssignmentLayout: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 61 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowInHeredoc. -Layout/TrailingWhitespace: - Exclude: - - 'lib/rspec/memory/matchers/limit_allocations.rb' - - 'lib/rspec/memory/trace.rb' - - 'rspec-memory.gemspec' - - 'spec/rspec/memory_spec.rb' - # Offense count: 4 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: AllowSafeAssignment. diff --git a/lib/rspec/memory/matchers/limit_allocations.rb b/lib/rspec/memory/matchers/limit_allocations.rb index de4ab5d..c5b6754 100644 --- a/lib/rspec/memory/matchers/limit_allocations.rb +++ b/lib/rspec/memory/matchers/limit_allocations.rb @@ -12,29 +12,29 @@ module Memory module Matchers class LimitAllocations include RSpec::Matchers::Composable - + def initialize(allocations = {}, count: nil, size: nil) @count = count @size = size - + @allocations = {} @errors = [] - + allocations.each do |klass, count| of(klass, count:) end end - + def supports_block_expectations? true end - + def of(klass, **limits) @allocations[klass] = limits - + self end - + private def check(value, limit) case limit when Range @@ -47,20 +47,20 @@ def of(klass, **limits) end end end - + def matches?(given_proc) return true unless trace = Trace.capture(@allocations.keys, &given_proc) - + if @count || @size # If the spec specifies a total limit, we have a limit which we can enforce which takes all allocations into account: total = trace.total - + if @count check(total.count, @count) do |expected| @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} instances" end end - + if @size check(total.size, @size) do |expected| @errors << "allocated #{total.count} instances, #{total.size} bytes, #{expected} bytes" @@ -72,27 +72,27 @@ def matches?(given_proc) @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, but it was not specified" end end - + trace.allocated.each do |klass, allocation| next unless acceptable = @allocations[klass] - + check(allocation.count, acceptable[:count]) do |expected| @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, #{expected} instances" end - + check(allocation.size, acceptable[:size]) do |expected| @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, #{expected} bytes" end end - + @errors.empty? end - + def failure_message "exceeded allocation limit: #{@errors.join(', ')}" end end - + if respond_to?(:ruby2_keywords, true) def limit_allocations(count: nil, size: nil, **allocations) LimitAllocations.new(allocations, count:, size:) diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index 91befe3..d76bf1a 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -10,27 +10,27 @@ module RSpec module Memory Allocation = Struct.new(:count, :size) do SLOT_SIZE = ObjectSpace.memsize_of(Object.new) - + def <<(object) self.count += 1 - + # We don't want to force specs to take the slot size into account. self.size += ObjectSpace.memsize_of(object) - SLOT_SIZE end - + def self.default_hash Hash.new { |h, k| h[k] = Allocation.new(0, 0) } end end - + class Trace def self.supported? # There are issues on truffleruby-1.0.0rc9 return false if RUBY_ENGINE == 'truffleruby' - + ObjectSpace.respond_to?(:trace_object_allocations) end - + if supported? def self.capture(*, &block) new(*).tap do |trace| @@ -40,62 +40,62 @@ def self.capture(*, &block) else def self.capture(*args, &block) yield - + nil end end - + def initialize(klasses) @klasses = klasses - + @allocated = Allocation.default_hash @retained = Allocation.default_hash - + @ignored = Allocation.default_hash - + @total = Allocation.new(0, 0) end - + attr_reader :allocated attr_reader :retained - + attr_reader :ignored - + attr_reader :total - + def current_objects(generation) allocations = [] - + ObjectSpace.each_object do |object| if ObjectSpace.allocation_generation(object) == generation allocations << object end end - + allocations end - + def find_base(object) @klasses.find { |klass| object.is_a? klass } end - + def capture(&block) GC.start - + begin GC.disable - + generation = GC.count ObjectSpace.trace_object_allocations(&block) - + allocated = current_objects(generation) ensure GC.enable end - + GC.start retained = current_objects(generation) - + # All allocated objects, including those freed in the last GC: allocated.each do |object| if klass = find_base(object) @@ -105,13 +105,13 @@ def capture(&block) if @klasses.any? warn "Ignoring allocation of #{object.class} at #{ObjectSpace.allocation_sourcefile(object)}:#{ObjectSpace.allocation_sourceline(object)}" end - + @ignored[object.class] << object end - + @total << object end - + # Retained objects are still alive after a final GC: retained.each do |object| if klass = find_base(object) diff --git a/rspec-memory.gemspec b/rspec-memory.gemspec index 669f58a..3d1789b 100644 --- a/rspec-memory.gemspec +++ b/rspec-memory.gemspec @@ -5,20 +5,20 @@ require_relative 'lib/rspec/memory/version' Gem::Specification.new do |spec| spec.name = 'rspec-memory' spec.version = RSpec::Memory::VERSION - + spec.summary = 'RSpec helpers for checking memory allocations.' spec.authors = ['Samuel Williams', 'Olle Jonsson', 'Cyril Roelandt', 'Daniel Leidert', 'Felix Yan'] spec.license = 'MIT' - + spec.cert_chain = ['release.cert'] spec.signing_key = File.expand_path('~/.gem/release.pem') - + spec.homepage = 'https://github.com/socketry/rspec-memory' - + spec.files = Dir.glob(['{lib}/**/*', '*.md'], File::FNM_DOTMATCH, base: __dir__) - + spec.add_dependency 'rspec', '~> 3.0' - + spec.add_development_dependency 'bundler' spec.add_development_dependency 'covered' end diff --git a/spec/rspec/memory_spec.rb b/spec/rspec/memory_spec.rb index 2841e17..ad7cfbc 100644 --- a/spec/rspec/memory_spec.rb +++ b/spec/rspec/memory_spec.rb @@ -7,28 +7,28 @@ RSpec.describe RSpec::Memory do include_context RSpec::Memory - + it 'should execute code in block' do string = nil - + expect do string = String.new end.to limit_allocations(String => 1) - + expect(string).to_not be_nil end - + context 'on supported platform', if: RSpec::Memory::Trace.supported? do it 'should not exceed specified count limit' do expect do 2.times { String.new } end.to limit_allocations(String => 2) - + expect do 2.times { String.new } end.to limit_allocations.of(String, count: 2) end - + it 'should fail if there are untracked allocations' do expect do expect do @@ -36,7 +36,7 @@ end.to limit_allocations end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /it was not specified/) end - + if RSpec::Memory::Trace.supported? it 'should exceed specified count limit' do expect do @@ -46,7 +46,7 @@ end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected exactly 4 instances/) end end - + it 'should be within specified count range' do expect do 2.times { String.new } @@ -56,7 +56,7 @@ 2.times { String.new } end.to limit_allocations.of(String, count: 1..3) end - + it 'should exceed specified count range' do expect do expect do @@ -64,13 +64,13 @@ end.to limit_allocations(String => 1..3) end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected within 1..3 instances/) end - + it 'should not exceed specified size limit' do expect do 'a' * 100_000 end.to limit_allocations.of(String, size: 100_001) end - + it 'should exceed specified size limit' do expect do expect do From e3f25c49195734a3da80e2421d350de333b7cae4 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:49:01 +0200 Subject: [PATCH 30/59] Disable cop that makes no sense in the current codebase --- .rubocop.yml | 3 +++ .rubocop_todo.yml | 10 ---------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index cf9ea82..5fef81b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -14,3 +14,6 @@ Style/MethodCallWithArgsParentheses: Layout/RedundantLineBreak: Enabled: false + +Style/MissingElse: + Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index dccd75a..ed8cbfb 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -213,16 +213,6 @@ Style/MethodCalledOnDoEndBlock: Exclude: - 'spec/rspec/memory_spec.rb' -# Offense count: 7 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: if, case, both -Style/MissingElse: - Exclude: - - 'lib/rspec/memory/matchers/limit_allocations.rb' - - 'lib/rspec/memory/trace.rb' - - 'spec/rspec/memory_spec.rb' - # Offense count: 4 # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. From e807e5ef24f3f5969331ed1d95c14fa16162debe Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:50:18 +0200 Subject: [PATCH 31/59] Use guard clause --- .rubocop_todo.yml | 7 ------- lib/rspec/memory/matchers/limit_allocations.rb | 8 ++------ lib/rspec/memory/trace.rb | 4 +--- 3 files changed, 3 insertions(+), 16 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ed8cbfb..063407c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -201,13 +201,6 @@ Style/EmptyLiteral: Exclude: - 'spec/rspec/memory_spec.rb' -# Offense count: 3 -# This cop supports safe autocorrection (--autocorrect). -Style/IfUnlessModifier: - Exclude: - - 'lib/rspec/memory/matchers/limit_allocations.rb' - - 'lib/rspec/memory/trace.rb' - # Offense count: 14 Style/MethodCalledOnDoEndBlock: Exclude: diff --git a/lib/rspec/memory/matchers/limit_allocations.rb b/lib/rspec/memory/matchers/limit_allocations.rb index c5b6754..7ba7598 100644 --- a/lib/rspec/memory/matchers/limit_allocations.rb +++ b/lib/rspec/memory/matchers/limit_allocations.rb @@ -38,13 +38,9 @@ def of(klass, **limits) private def check(value, limit) case limit when Range - unless limit.include? value - yield "expected within #{limit}" - end + yield "expected within #{limit}" unless limit.include? value when Integer - unless value == limit - yield "expected exactly #{limit}" - end + yield "expected exactly #{limit}" unless value == limit end end diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index d76bf1a..dd56ba7 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -67,9 +67,7 @@ def current_objects(generation) allocations = [] ObjectSpace.each_object do |object| - if ObjectSpace.allocation_generation(object) == generation - allocations << object - end + allocations << object if ObjectSpace.allocation_generation(object) == generation end allocations From dd16ee53e9dbcd2bd10a118504a77a77af202747 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:50:57 +0200 Subject: [PATCH 32/59] Use array literal --- .rubocop_todo.yml | 6 ------ spec/rspec/memory_spec.rb | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 063407c..4263779 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -195,12 +195,6 @@ Style/DocumentationMethod: - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'lib/rspec/memory/trace.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -Style/EmptyLiteral: - Exclude: - - 'spec/rspec/memory_spec.rb' - # Offense count: 14 Style/MethodCalledOnDoEndBlock: Exclude: diff --git a/spec/rspec/memory_spec.rb b/spec/rspec/memory_spec.rb index ad7cfbc..9444a83 100644 --- a/spec/rspec/memory_spec.rb +++ b/spec/rspec/memory_spec.rb @@ -32,7 +32,7 @@ it 'should fail if there are untracked allocations' do expect do expect do - Array.new + [] end.to limit_allocations end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /it was not specified/) end From be77fe84b761fe2294540708d847cb58eeead121 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:52:08 +0200 Subject: [PATCH 33/59] Group accessors --- .rubocop_todo.yml | 8 -------- lib/rspec/memory/trace.rb | 7 +------ 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4263779..b1367c8 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -162,14 +162,6 @@ Naming/BlockForwarding: Style/AccessModifierDeclarations: EnforcedStyle: inline -# Offense count: 4 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: separated, grouped -Style/AccessorGrouping: - Exclude: - - 'lib/rspec/memory/trace.rb' - # Offense count: 2 # Configuration parameters: IgnoreModules. Style/ConstantVisibility: diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index dd56ba7..af134cc 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -56,12 +56,7 @@ def initialize(klasses) @total = Allocation.new(0, 0) end - attr_reader :allocated - attr_reader :retained - - attr_reader :ignored - - attr_reader :total + attr_reader :allocated, :retained, :ignored, :total def current_objects(generation) allocations = [] From 215ca694f6765d4dfcb4a655a27687649161dbba Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:56:18 +0200 Subject: [PATCH 34/59] Properly narrow public interface I.e. make #failure_message private. --- .rubocop_todo.yml | 7 ------- .../memory/matchers/limit_allocations.rb | 20 ++++++++++--------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b1367c8..6c44202 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -155,13 +155,6 @@ Naming/BlockForwarding: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: AllowModifiersOnSymbols. -# SupportedStyles: inline, group -Style/AccessModifierDeclarations: - EnforcedStyle: inline - # Offense count: 2 # Configuration parameters: IgnoreModules. Style/ConstantVisibility: diff --git a/lib/rspec/memory/matchers/limit_allocations.rb b/lib/rspec/memory/matchers/limit_allocations.rb index 7ba7598..925e870 100644 --- a/lib/rspec/memory/matchers/limit_allocations.rb +++ b/lib/rspec/memory/matchers/limit_allocations.rb @@ -35,15 +35,6 @@ def of(klass, **limits) self end - private def check(value, limit) - case limit - when Range - yield "expected within #{limit}" unless limit.include? value - when Integer - yield "expected exactly #{limit}" unless value == limit - end - end - def matches?(given_proc) return true unless trace = Trace.capture(@allocations.keys, &given_proc) @@ -84,6 +75,17 @@ def matches?(given_proc) @errors.empty? end + private + + def check(value, limit) + case limit + when Range + yield "expected within #{limit}" unless limit.include? value + when Integer + yield "expected exactly #{limit}" unless value == limit + end + end + def failure_message "exceeded allocation limit: #{@errors.join(', ')}" end From f735e6b00af6dfe593ddeb555bde432b60831fd7 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:58:02 +0200 Subject: [PATCH 35/59] Use anonymous block forwarding --- .rubocop_todo.yml | 8 -------- lib/rspec/memory/trace.rb | 10 +++++----- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6c44202..af532de 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -147,14 +147,6 @@ Metrics/MethodLength: Metrics/PerceivedComplexity: Max: 10 -# Offense count: 5 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, BlockForwardingName. -# SupportedStyles: anonymous, explicit -Naming/BlockForwarding: - Exclude: - - 'lib/rspec/memory/trace.rb' - # Offense count: 2 # Configuration parameters: IgnoreModules. Style/ConstantVisibility: diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index af134cc..94c0e2b 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -32,13 +32,13 @@ def self.supported? end if supported? - def self.capture(*, &block) + def self.capture(*, &) new(*).tap do |trace| - trace.capture(&block) + trace.capture(&) end end else - def self.capture(*args, &block) + def self.capture(*args, &) yield nil @@ -72,14 +72,14 @@ def find_base(object) @klasses.find { |klass| object.is_a? klass } end - def capture(&block) + def capture(&) GC.start begin GC.disable generation = GC.count - ObjectSpace.trace_object_allocations(&block) + ObjectSpace.trace_object_allocations(&) allocated = current_objects(generation) ensure From d2fd9868f8a26ef4f3e76145baf5caf302bae38d Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 10:58:51 +0200 Subject: [PATCH 36/59] Visually distinct unused method argument --- .rubocop_todo.yml | 7 ------- lib/rspec/memory/trace.rb | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index af532de..a21ff58 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -114,13 +114,6 @@ Lint/StructNewOverride: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 2 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods. -Lint/UnusedMethodArgument: - Exclude: - - 'lib/rspec/memory/trace.rb' - # Offense count: 2 # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. Metrics/AbcSize: diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index 94c0e2b..30564de 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -38,7 +38,7 @@ def self.capture(*, &) end end else - def self.capture(*args, &) + def self.capture(*_args, &) yield nil From e4c20c176f623d4aad7a50ce4f7467737fac06eb Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 11:01:13 +0200 Subject: [PATCH 37/59] Encapsulate assignments in conditionals to distinct them --- .rubocop_todo.yml | 8 -------- lib/rspec/memory/matchers/limit_allocations.rb | 4 ++-- lib/rspec/memory/trace.rb | 4 ++-- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a21ff58..4579cdf 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -77,14 +77,6 @@ Layout/MultilineAssignmentLayout: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 4 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: AllowSafeAssignment. -Lint/AssignmentInCondition: - Exclude: - - 'lib/rspec/memory/matchers/limit_allocations.rb' - - 'lib/rspec/memory/trace.rb' - # Offense count: 1 # Configuration parameters: AllowedMethods. # AllowedMethods: enums diff --git a/lib/rspec/memory/matchers/limit_allocations.rb b/lib/rspec/memory/matchers/limit_allocations.rb index 925e870..8ef52b9 100644 --- a/lib/rspec/memory/matchers/limit_allocations.rb +++ b/lib/rspec/memory/matchers/limit_allocations.rb @@ -36,7 +36,7 @@ def of(klass, **limits) end def matches?(given_proc) - return true unless trace = Trace.capture(@allocations.keys, &given_proc) + return true unless (trace = Trace.capture(@allocations.keys, &given_proc)) if @count || @size # If the spec specifies a total limit, we have a limit which we can enforce which takes all allocations into account: @@ -61,7 +61,7 @@ def matches?(given_proc) end trace.allocated.each do |klass, allocation| - next unless acceptable = @allocations[klass] + next unless (acceptable = @allocations[klass]) check(allocation.count, acceptable[:count]) do |expected| @errors << "allocated #{allocation.count} #{klass} instances, #{allocation.size} bytes, #{expected} instances" diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index 30564de..c6d10d8 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -91,7 +91,7 @@ def capture(&) # All allocated objects, including those freed in the last GC: allocated.each do |object| - if klass = find_base(object) + if (klass = find_base(object)) @allocated[klass] << object else # If the user specified classes, but we can't pin this allocation to a specific class, we issue a warning. @@ -107,7 +107,7 @@ def capture(&) # Retained objects are still alive after a final GC: retained.each do |object| - if klass = find_base(object) + if (klass = find_base(object)) @retained[klass] << object end end From a070a50ad6dc62960f8d8dac6230628f885876c9 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 11:02:15 +0200 Subject: [PATCH 38/59] Right hand side of multi-line assignment was on the same line as the assignment operator --- .rubocop_todo.yml | 9 --------- lib/rspec/memory/trace.rb | 21 +++++++++++---------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4579cdf..319d43c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -68,15 +68,6 @@ Layout/ClassStructure: Exclude: - 'lib/rspec/memory/matchers/limit_allocations.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedTypes: block, case, class, if, kwbegin, module -# SupportedStyles: same_line, new_line -Layout/MultilineAssignmentLayout: - Exclude: - - 'lib/rspec/memory/trace.rb' - # Offense count: 1 # Configuration parameters: AllowedMethods. # AllowedMethods: enums diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index c6d10d8..779d909 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -8,20 +8,21 @@ module RSpec module Memory - Allocation = Struct.new(:count, :size) do - SLOT_SIZE = ObjectSpace.memsize_of(Object.new) + Allocation = + Struct.new(:count, :size) do + SLOT_SIZE = ObjectSpace.memsize_of(Object.new) - def <<(object) - self.count += 1 + def <<(object) + self.count += 1 - # We don't want to force specs to take the slot size into account. - self.size += ObjectSpace.memsize_of(object) - SLOT_SIZE - end + # We don't want to force specs to take the slot size into account. + self.size += ObjectSpace.memsize_of(object) - SLOT_SIZE + end - def self.default_hash - Hash.new { |h, k| h[k] = Allocation.new(0, 0) } + def self.default_hash + Hash.new { |h, k| h[k] = Allocation.new(0, 0) } + end end - end class Trace def self.supported? From dd4588b8103b3fc1891d15032f7e50b69e50f920 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 11:03:09 +0200 Subject: [PATCH 39/59] Remove redundant RuboCop TODO --- .rubocop_todo.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 319d43c..c85574c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -60,14 +60,6 @@ Gemspec/RequiredRubyVersion: Exclude: - 'rspec-memory.gemspec' -# Offense count: 1 -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: Categories, ExpectedOrder. -# ExpectedOrder: module_inclusion, constants, public_class_methods, initializer, public_methods, protected_methods, private_methods -Layout/ClassStructure: - Exclude: - - 'lib/rspec/memory/matchers/limit_allocations.rb' - # Offense count: 1 # Configuration parameters: AllowedMethods. # AllowedMethods: enums From 7fb07130cbb516f4d18a97daaf5a2ff9685d80e9 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 11:03:43 +0200 Subject: [PATCH 40/59] Regenerate RuboCop TODO after last changes --- .rubocop_todo.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index c85574c..f54ebb0 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-09-16 08:29:47 UTC using RuboCop version 1.56.3. +# on 2023-09-16 09:03:18 UTC using RuboCop version 1.56.3. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -67,7 +67,7 @@ Lint/ConstantDefinitionInBlock: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 66 +# Offense count: 65 # Configuration parameters: Only, Ignore. Lint/ConstantResolution: Exclude: @@ -131,7 +131,7 @@ Style/Documentation: - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'lib/rspec/memory/trace.rb' -# Offense count: 14 +# Offense count: 13 # Configuration parameters: RequireForNonPublicMethods. Style/DocumentationMethod: Exclude: From 55205dd8a3899d6042026ea8a41beb02d84d7a67 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 11:08:44 +0200 Subject: [PATCH 41/59] Use shorter line. Improve express intent --- lib/rspec/memory/trace.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index 779d909..fb42b1a 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -97,7 +97,10 @@ def capture(&) else # If the user specified classes, but we can't pin this allocation to a specific class, we issue a warning. if @klasses.any? - warn "Ignoring allocation of #{object.class} at #{ObjectSpace.allocation_sourcefile(object)}:#{ObjectSpace.allocation_sourceline(object)}" + file = ObjectSpace.allocation_sourcefile(object) + line = ObjectSpace.allocation_sourceline(object) + + warn "Ignoring allocation of #{object.class} at #{file}:#{line}" end @ignored[object.class] << object From 798b55f9ccc9d49024c87a71c8a34b7c429b8b61 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 11:09:39 +0200 Subject: [PATCH 42/59] Freeze limits for LineLength and MethodLength --- .rubocop.yml | 6 ++++++ .rubocop_todo.yml | 12 ------------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 5fef81b..1ae8b8b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -17,3 +17,9 @@ Layout/RedundantLineBreak: Style/MissingElse: Enabled: false + +Layout/LineLength: + Max: 129 + +Metrics/MethodLength: + Max: 29 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f54ebb0..3acd9c9 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -105,11 +105,6 @@ Metrics/BlockLength: Metrics/CyclomaticComplexity: Max: 9 -# Offense count: 2 -# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. -Metrics/MethodLength: - Max: 28 - # Offense count: 1 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/PerceivedComplexity: @@ -144,10 +139,3 @@ Style/DocumentationMethod: Style/MethodCalledOnDoEndBlock: Exclude: - 'spec/rspec/memory_spec.rb' - -# Offense count: 4 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. -# URISchemes: http, https -Layout/LineLength: - Max: 152 From 9586ef7a76c11e410e0905d9e067d36a6375fc71 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 11:10:21 +0200 Subject: [PATCH 43/59] Freeze other limits --- .rubocop.yml | 12 ++++++++++++ .rubocop_todo.yml | 21 --------------------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 1ae8b8b..c91cb3a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -23,3 +23,15 @@ Layout/LineLength: Metrics/MethodLength: Max: 29 + +Metrics/AbcSize: + Max: 37 + +Metrics/BlockLength: + Max: 61 + +Metrics/CyclomaticComplexity: + Max: 9 + +Metrics/PerceivedComplexity: + Max: 10 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 3acd9c9..0dd7759 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -89,27 +89,6 @@ Lint/StructNewOverride: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 2 -# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. -Metrics/AbcSize: - Max: 37 - -# Offense count: 2 -# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. -# AllowedMethods: refine -Metrics/BlockLength: - Max: 61 - -# Offense count: 1 -# Configuration parameters: AllowedMethods, AllowedPatterns. -Metrics/CyclomaticComplexity: - Max: 9 - -# Offense count: 1 -# Configuration parameters: AllowedMethods, AllowedPatterns. -Metrics/PerceivedComplexity: - Max: 10 - # Offense count: 2 # Configuration parameters: IgnoreModules. Style/ConstantVisibility: From b8761db5d526cf32f446e56db34536a1f4184ec9 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 11:12:11 +0200 Subject: [PATCH 44/59] Allow for methods on blocks in specs --- .rubocop.yml | 4 ++++ .rubocop_todo.yml | 5 ----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c91cb3a..f4596fc 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -35,3 +35,7 @@ Metrics/CyclomaticComplexity: Metrics/PerceivedComplexity: Max: 10 + +Style/MethodCalledOnDoEndBlock: + Exclude: + - 'spec/**/*_spec.rb' diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0dd7759..790ac23 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -113,8 +113,3 @@ Style/DocumentationMethod: - 'test/**/*' - 'lib/rspec/memory/matchers/limit_allocations.rb' - 'lib/rspec/memory/trace.rb' - -# Offense count: 14 -Style/MethodCalledOnDoEndBlock: - Exclude: - - 'spec/rspec/memory_spec.rb' From 409cb01e34ac64a0e4b0a28c47f4d1caac96b739 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 11:14:29 +0200 Subject: [PATCH 45/59] Explicitly make constants public or private --- .rubocop_todo.yml | 7 ------- lib/rspec/memory/trace.rb | 2 ++ lib/rspec/memory/version.rb | 2 ++ 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 790ac23..3a25b6e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -89,13 +89,6 @@ Lint/StructNewOverride: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 2 -# Configuration parameters: IgnoreModules. -Style/ConstantVisibility: - Exclude: - - 'lib/rspec/memory/trace.rb' - - 'lib/rspec/memory/version.rb' - # Offense count: 3 # Configuration parameters: AllowedConstants. Style/Documentation: diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index fb42b1a..3a1ddbe 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -117,5 +117,7 @@ def capture(&) end end end + + private_constant :Allocation end end diff --git a/lib/rspec/memory/version.rb b/lib/rspec/memory/version.rb index e18a3d7..c6d8df9 100644 --- a/lib/rspec/memory/version.rb +++ b/lib/rspec/memory/version.rb @@ -6,5 +6,7 @@ module RSpec module Memory VERSION = '1.0.4' + + public_constant :VERSION end end From dbdd5680d98f071165d21de5ae17d82e7d683084 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 11:17:00 +0200 Subject: [PATCH 46/59] Do not shadow outer local variable --- .rubocop_todo.yml | 5 ----- lib/rspec/memory/matchers/limit_allocations.rb | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 3a25b6e..80b7c7e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -79,11 +79,6 @@ Lint/ConstantResolution: - 'spec/rspec/memory_spec.rb' - 'spec/spec_helper.rb' -# Offense count: 1 -Lint/ShadowingOuterLocalVariable: - Exclude: - - 'lib/rspec/memory/matchers/limit_allocations.rb' - # Offense count: 2 Lint/StructNewOverride: Exclude: diff --git a/lib/rspec/memory/matchers/limit_allocations.rb b/lib/rspec/memory/matchers/limit_allocations.rb index 8ef52b9..baf7d4c 100644 --- a/lib/rspec/memory/matchers/limit_allocations.rb +++ b/lib/rspec/memory/matchers/limit_allocations.rb @@ -20,8 +20,8 @@ def initialize(allocations = {}, count: nil, size: nil) @allocations = {} @errors = [] - allocations.each do |klass, count| - of(klass, count:) + allocations.each do |klass, cnt| + of(klass, count: cnt) end end From 285462af7cb5e72973d892508cbcbfe00bf14c84 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 11:19:45 +0200 Subject: [PATCH 47/59] Project is small enough to not have conflicts --- .rubocop.yml | 3 +++ .rubocop_todo.yml | 11 ++--------- Guardfile | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index f4596fc..7275c78 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -18,6 +18,9 @@ Layout/RedundantLineBreak: Style/MissingElse: Enabled: false +Lint/ConstantResolution: + Enabled: false + Layout/LineLength: Max: 129 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 80b7c7e..dcbecd1 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -67,17 +67,10 @@ Lint/ConstantDefinitionInBlock: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 65 -# Configuration parameters: Only, Ignore. -Lint/ConstantResolution: +# Offense count: 1 +Lint/ShadowingOuterLocalVariable: Exclude: - - 'Guardfile' - - 'lib/rspec/memory.rb' - 'lib/rspec/memory/matchers/limit_allocations.rb' - - 'lib/rspec/memory/trace.rb' - - 'rspec-memory.gemspec' - - 'spec/rspec/memory_spec.rb' - - 'spec/spec_helper.rb' # Offense count: 2 Lint/StructNewOverride: diff --git a/Guardfile b/Guardfile index ba92793..49c67ef 100644 --- a/Guardfile +++ b/Guardfile @@ -37,6 +37,6 @@ RUBOCOP_CONFIG = { guard :rubocop, RUBOCOP_CONFIG do watch(/.+\.rb$/) - watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| ::File.dirname(m[0]) } + watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) } watch('Guardfile') end From 019368a5c5152dbd1f5d3da45009111c93734a1e Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 11:24:38 +0200 Subject: [PATCH 48/59] Add missing TDD trigger for gemspec --- Guardfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Guardfile b/Guardfile index 49c67ef..75471ab 100644 --- a/Guardfile +++ b/Guardfile @@ -39,4 +39,5 @@ guard :rubocop, RUBOCOP_CONFIG do watch(/.+\.rb$/) watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) } watch('Guardfile') + watch('rspec-memory.gemspec') end From aba5864388cf0da32e86a0ff4510ffbc47d6f238 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sat, 16 Sep 2023 11:25:06 +0200 Subject: [PATCH 49/59] Make gem require recent ruby version --- .rubocop_todo.yml | 7 ------- rspec-memory.gemspec | 2 ++ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index dcbecd1..6857a28 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -53,13 +53,6 @@ Gemspec/RequireMFA: Exclude: - 'rspec-memory.gemspec' -# Offense count: 1 -# Configuration parameters: Severity, Include. -# Include: **/*.gemspec -Gemspec/RequiredRubyVersion: - Exclude: - - 'rspec-memory.gemspec' - # Offense count: 1 # Configuration parameters: AllowedMethods. # AllowedMethods: enums diff --git a/rspec-memory.gemspec b/rspec-memory.gemspec index 3d1789b..393ef14 100644 --- a/rspec-memory.gemspec +++ b/rspec-memory.gemspec @@ -17,6 +17,8 @@ Gem::Specification.new do |spec| spec.files = Dir.glob(['{lib}/**/*', '*.md'], File::FNM_DOTMATCH, base: __dir__) + spec.required_ruby_version = '>= 3.2.2' + spec.add_dependency 'rspec', '~> 3.0' spec.add_development_dependency 'bundler' From 12d870b8b258e2a179d526cd997d32a62a23303b Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sun, 17 Sep 2023 06:53:23 +0200 Subject: [PATCH 50/59] Add rubygems required MFA --- .rubocop_todo.yml | 8 -------- rspec-memory.gemspec | 1 + 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6857a28..fc57f2f 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -45,14 +45,6 @@ Gemspec/DevelopmentDependencies: Exclude: - 'rspec-memory.gemspec' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: Severity, Include. -# Include: **/*.gemspec -Gemspec/RequireMFA: - Exclude: - - 'rspec-memory.gemspec' - # Offense count: 1 # Configuration parameters: AllowedMethods. # AllowedMethods: enums diff --git a/rspec-memory.gemspec b/rspec-memory.gemspec index 393ef14..c4043c0 100644 --- a/rspec-memory.gemspec +++ b/rspec-memory.gemspec @@ -23,4 +23,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'bundler' spec.add_development_dependency 'covered' + spec.metadata['rubygems_mfa_required'] = 'true' end From a8f6eee7b5ea2424e180ca868844a51f32ca6e2a Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sun, 17 Sep 2023 06:58:18 +0200 Subject: [PATCH 51/59] Move development dependencies from gemspec to 'gemfile' --- .rubocop_todo.yml | 8 -------- gems.rb | 4 ++++ rspec-memory.gemspec | 2 -- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index fc57f2f..7a8fea2 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -37,14 +37,6 @@ Gemspec/DependencyVersion: Exclude: - 'rspec-memory.gemspec' -# Offense count: 2 -# Configuration parameters: EnforcedStyle, AllowedGems, Include. -# SupportedStyles: Gemfile, gems.rb, gemspec -# Include: **/*.gemspec, **/Gemfile, **/gems.rb -Gemspec/DevelopmentDependencies: - Exclude: - - 'rspec-memory.gemspec' - # Offense count: 1 # Configuration parameters: AllowedMethods. # AllowedMethods: enums diff --git a/gems.rb b/gems.rb index 24253dd..1bd6c19 100644 --- a/gems.rb +++ b/gems.rb @@ -15,6 +15,10 @@ gem 'bake-modernize' end +group :development do + gem 'covered', '~> 0.25.0', require: false +end + group :test do gem 'bake-test' gem 'bake-test-external' diff --git a/rspec-memory.gemspec b/rspec-memory.gemspec index c4043c0..7bb30d9 100644 --- a/rspec-memory.gemspec +++ b/rspec-memory.gemspec @@ -21,7 +21,5 @@ Gem::Specification.new do |spec| spec.add_dependency 'rspec', '~> 3.0' - spec.add_development_dependency 'bundler' - spec.add_development_dependency 'covered' spec.metadata['rubygems_mfa_required'] = 'true' end From b94856a47c37d8d3ce031d2016ad43d20249db59 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sun, 17 Sep 2023 06:59:17 +0200 Subject: [PATCH 52/59] Remove redundant TODO --- .rubocop_todo.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7a8fea2..9fbf8f2 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -29,14 +29,6 @@ Bundler/GemVersion: Exclude: - 'gems.rb' -# Offense count: 2 -# Configuration parameters: EnforcedStyle, Include, AllowedGems. -# SupportedStyles: required, forbidden -# Include: **/*.gemspec -Gemspec/DependencyVersion: - Exclude: - - 'rspec-memory.gemspec' - # Offense count: 1 # Configuration parameters: AllowedMethods. # AllowedMethods: enums From 0ebda46689f642778e493da4f923aeff83faa34a Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sun, 17 Sep 2023 07:00:44 +0200 Subject: [PATCH 53/59] Regenerate RuboCop TODO after recent changes --- .rubocop_todo.yml | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9fbf8f2..401a0c3 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,12 +1,12 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-09-16 09:03:18 UTC using RuboCop version 1.56.3. +# on 2023-09-17 04:59:27 UTC using RuboCop version 1.56.3. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 6 +# Offense count: 7 # Configuration parameters: Include, IgnoredGems, OnlyFor. # Include: **/*.gemfile, **/Gemfile, **/gems.rb Bundler/GemComment: @@ -36,11 +36,6 @@ Lint/ConstantDefinitionInBlock: Exclude: - 'lib/rspec/memory/trace.rb' -# Offense count: 1 -Lint/ShadowingOuterLocalVariable: - Exclude: - - 'lib/rspec/memory/matchers/limit_allocations.rb' - # Offense count: 2 Lint/StructNewOverride: Exclude: From 087bb726768ee710f9b41dde407efcba0ce98588 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sun, 17 Sep 2023 07:03:00 +0200 Subject: [PATCH 54/59] Freeze gem versions in 'gemfile' --- .rubocop_todo.yml | 8 -------- gems.rb | 8 ++++---- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 401a0c3..24c768f 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -21,14 +21,6 @@ Bundler/GemFilename: Exclude: - 'gems.rb' -# Offense count: 4 -# Configuration parameters: EnforcedStyle, Include, AllowedGems. -# SupportedStyles: required, forbidden -# Include: **/*.gemfile, **/Gemfile, **/gems.rb -Bundler/GemVersion: - Exclude: - - 'gems.rb' - # Offense count: 1 # Configuration parameters: AllowedMethods. # AllowedMethods: enums diff --git a/gems.rb b/gems.rb index 1bd6c19..e7213fb 100644 --- a/gems.rb +++ b/gems.rb @@ -11,8 +11,8 @@ gemspec group :maintenance, optional: true do - gem 'bake-gem' - gem 'bake-modernize' + gem 'bake-gem', '~> 0.4.0' + gem 'bake-modernize', '~> 0.17.8' end group :development do @@ -20,8 +20,8 @@ end group :test do - gem 'bake-test' - gem 'bake-test-external' + gem 'bake-test', '~> 0.2.0' + gem 'bake-test-external', '~> 0.3.3' gem 'guard-rspec', '~> 4.7' gem 'guard-rubocop', '~> 1.5' end From 2f4d3749144175fb332ebfd7e174d49ee20b11af Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sun, 17 Sep 2023 07:03:56 +0200 Subject: [PATCH 55/59] Use gem grouping that better reflect reality --- gems.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gems.rb b/gems.rb index e7213fb..79c73ec 100644 --- a/gems.rb +++ b/gems.rb @@ -15,13 +15,13 @@ gem 'bake-modernize', '~> 0.17.8' end -group :development do +group :development, :test do gem 'covered', '~> 0.25.0', require: false + gem 'guard-rspec', '~> 4.7' + gem 'guard-rubocop', '~> 1.5' end group :test do gem 'bake-test', '~> 0.2.0' gem 'bake-test-external', '~> 0.3.3' - gem 'guard-rspec', '~> 4.7' - gem 'guard-rubocop', '~> 1.5' end From 8840146a19fd48b59bfdc2a4c191b9df1a89bdbe Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sun, 17 Sep 2023 07:08:08 +0200 Subject: [PATCH 56/59] Actually match dots in regexp --- spec/rspec/memory_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/rspec/memory_spec.rb b/spec/rspec/memory_spec.rb index 9444a83..dbdb0de 100644 --- a/spec/rspec/memory_spec.rb +++ b/spec/rspec/memory_spec.rb @@ -62,7 +62,7 @@ expect do 6.times { String.new } end.to limit_allocations(String => 1..3) - end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected within 1..3 instances/) + end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected within 1\.\.3 instances/) end it 'should not exceed specified size limit' do From 95850e0218c8491513cad4c215694e2122900367 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sun, 17 Sep 2023 07:20:44 +0200 Subject: [PATCH 57/59] Exclude specs from block limiting --- .rubocop.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index 7275c78..536c297 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -31,7 +31,9 @@ Metrics/AbcSize: Max: 37 Metrics/BlockLength: - Max: 61 + Max: 12 + Exclude: + - 'spec/**/*_spec.rb' Metrics/CyclomaticComplexity: Max: 9 From 13eaa7a9fcdd2a863f28cd578f1af7bde6c1a208 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sun, 17 Sep 2023 07:35:46 +0200 Subject: [PATCH 58/59] Allow rspec focus --- spec/spec_helper.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c566e75..a1360d1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -15,4 +15,7 @@ config.expect_with :rspec do |c| c.syntax = :expect end + + config.filter_run focus: true + config.run_all_when_everything_filtered = true end From d616aa923a6046c4f98ca1791ccbedd2af37b276 Mon Sep 17 00:00:00 2001 From: Marcin Nowicki Date: Sun, 17 Sep 2023 07:58:53 +0200 Subject: [PATCH 59/59] Allow referencing private constants using strings --- .rubocop.yml | 8 ++++++++ lib/rspec/memory/trace.rb | 2 +- readme.md | 22 ++++++++++++++++++++++ spec/rspec/memory_spec.rb | 24 ++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index 536c297..3afa11b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -44,3 +44,11 @@ Metrics/PerceivedComplexity: Style/MethodCalledOnDoEndBlock: Exclude: - 'spec/**/*_spec.rb' + +Style/ClassEqualityComparison: + Exclude: + - 'lib/rspec/memory/trace.rb' + +Style/StringHashKeys: + Exclude: + - 'spec/rspec/memory_spec.rb' diff --git a/lib/rspec/memory/trace.rb b/lib/rspec/memory/trace.rb index 3a1ddbe..9e3b6f3 100644 --- a/lib/rspec/memory/trace.rb +++ b/lib/rspec/memory/trace.rb @@ -70,7 +70,7 @@ def current_objects(generation) end def find_base(object) - @klasses.find { |klass| object.is_a? klass } + @klasses.find { |klass| (klass.is_a?(String) && object.class.name == klass) || object.is_a?(klass) } end def capture(&) diff --git a/readme.md b/readme.md index 5ef222d..deee777 100644 --- a/readme.md +++ b/readme.md @@ -54,6 +54,28 @@ RSpec.describe "memory allocations" do end ``` +### private constants + +As private constants cannot be referenced we can pride them as strings: + +``` ruby + it "allows using private constants as strings" do + expect do + Timeout.timeout(1) do + String.new + end + end.to limit_allocations( + Thread::Mutex => { count: 1, size: 32 }, + String => { count: 1, size: 0 }, + Timeout::Error => { count: 1, size: 48 }, + Array => { count: 2, size: 0 }, + Proc => { count: 3, size: 120 }, + Hash => { count: 1, size: 128 }, + Thread => { count: 1, size: 360 }, + 'Timeout::Request' => { count: 1, size: 40 } + ) +``` + ## Contributing We welcome contributions to this project. diff --git a/spec/rspec/memory_spec.rb b/spec/rspec/memory_spec.rb index dbdb0de..c62cbe6 100644 --- a/spec/rspec/memory_spec.rb +++ b/spec/rspec/memory_spec.rb @@ -4,6 +4,7 @@ # Copyright, 2019-2023, by Samuel Williams. require 'rspec/memory' +require 'timeout' RSpec.describe RSpec::Memory do include_context RSpec::Memory @@ -78,5 +79,28 @@ end.to limit_allocations(size: 100_000) end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected exactly 100000 bytes/) end + + it 'allows constants as strings' do + expect do + 'a' * 100_000 + end.to limit_allocations.of('String', size: 100_001) + end + + it 'allows using private constants as strings' do + expect do + Timeout.timeout(1) do + String.new + end + end.to limit_allocations( + Thread::Mutex => { count: 1, size: 32 }, + String => { count: 1, size: 0 }, + Timeout::Error => { count: 1, size: 48 }, + Array => { count: 2, size: 0 }, + Proc => { count: 3, size: 120 }, + Hash => { count: 1, size: 128 }, + Thread => { count: 1, size: 360 }, + 'Timeout::Request' => { count: 1, size: 40 } + ) + end end end