Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-14806] Update specs #3712

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions spec/mspec/lib/mspec/guards/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,38 @@ def self.wsl?
end
end

# In bits
WORD_SIZE = 1.size * 8
deprecate_constant :WORD_SIZE

# In bits
POINTER_SIZE = begin
require 'rbconfig/sizeof'
RbConfig::SIZEOF["void*"] * 8
rescue LoadError
WORD_SIZE
[0].pack('j').size * 8
end

# In bits
C_LONG_SIZE = if defined?(RbConfig::SIZEOF[])
RbConfig::SIZEOF["long"] * 8
else
[0].pack('l!').size * 8
end

def self.wordsize?(size)
warn "#wordsize? is deprecated, use #c_long_size?"
size == WORD_SIZE
end

def self.pointer_size?(size)
size == POINTER_SIZE
end

def self.c_long_size?(size)
size == C_LONG_SIZE
end

def initialize(*args)
if args.last.is_a?(Hash)
@options, @platforms = args.last, args[0..-2]
Expand All @@ -85,10 +100,13 @@ def match?
case key
when :os
match &&= PlatformGuard.os?(*value)
when :wordsize
match &&= PlatformGuard.wordsize? value
when :pointer_size
match &&= PlatformGuard.pointer_size? value
when :wordsize
warn ":wordsize is deprecated, use :c_long_size"
match &&= PlatformGuard.wordsize? value
when :c_long_size
match &&= PlatformGuard::c_long_size? value
end
end
match
Expand Down
26 changes: 22 additions & 4 deletions spec/mspec/lib/mspec/helpers/numeric.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'mspec/guards/platform'

def nan_value
Expand All @@ -15,11 +16,11 @@ def bignum_value(plus = 0)
end

def max_long
2**(0.size * 8 - 1) - 1
2**(PlatformGuard::C_LONG_SIZE - 1) - 1
end

def min_long
-(2**(0.size * 8 - 1))
-(2**(PlatformGuard::C_LONG_SIZE - 1))
end

# This is a bit hairy, but we need to be able to write specs that cover the
Expand All @@ -28,15 +29,32 @@ def min_long
# specs based on the relationship between values rather than specific
# values.
if PlatformGuard.standard? or PlatformGuard.implementation? :topaz
if PlatformGuard.wordsize? 32
limits_available = begin
require 'rbconfig/sizeof'
defined?(RbConfig::LIMITS.[]) && ['FIXNUM_MAX', 'FIXNUM_MIN'].all? do |key|
Integer === RbConfig::LIMITS[key]
end
rescue LoadError
false
end

if limits_available
def fixnum_max
RbConfig::LIMITS['FIXNUM_MAX']
end

def fixnum_min
RbConfig::LIMITS['FIXNUM_MIN']
end
elsif PlatformGuard.c_long_size? 32
def fixnum_max
(2**30) - 1
end

def fixnum_min
-(2**30)
end
elsif PlatformGuard.wordsize? 64
elsif PlatformGuard.c_long_size? 64
def fixnum_max
(2**62) - 1
end
Expand Down
6 changes: 3 additions & 3 deletions spec/mspec/lib/mspec/runner/actions/leakchecker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ class << self
attr_accessor :count
end

def new(data)
def new(...)
LeakChecker::TempfileCounter.count += 1
super(data)
super
end
}
LeakChecker.const_set(:TempfileCounter, m)

class << Tempfile::Remover
class << Tempfile
prepend LeakChecker::TempfileCounter
end
end
Expand Down
38 changes: 19 additions & 19 deletions spec/mspec/spec/guards/platform_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,44 +81,44 @@
end
end

RSpec.describe Object, "#platform_is :wordsize => SIZE_SPEC" do
RSpec.describe Object, "#platform_is :c_long_size => SIZE_SPEC" do
before :each do
@guard = PlatformGuard.new :darwin, :wordsize => 32
@guard = PlatformGuard.new :darwin, :c_long_size => 32
allow(PlatformGuard).to receive(:os?).and_return(true)
allow(PlatformGuard).to receive(:new).and_return(@guard)
ScratchPad.clear
end

it "yields when #wordsize? returns true" do
allow(PlatformGuard).to receive(:wordsize?).and_return(true)
platform_is(:wordsize => 32) { ScratchPad.record :yield }
it "yields when #c_long_size? returns true" do
allow(PlatformGuard).to receive(:c_long_size?).and_return(true)
platform_is(:c_long_size => 32) { ScratchPad.record :yield }
expect(ScratchPad.recorded).to eq(:yield)
end

it "doesn not yield when #wordsize? returns false" do
allow(PlatformGuard).to receive(:wordsize?).and_return(false)
platform_is(:wordsize => 32) { ScratchPad.record :yield }
it "doesn not yield when #c_long_size? returns false" do
allow(PlatformGuard).to receive(:c_long_size?).and_return(false)
platform_is(:c_long_size => 32) { ScratchPad.record :yield }
expect(ScratchPad.recorded).not_to eq(:yield)
end
end

RSpec.describe Object, "#platform_is_not :wordsize => SIZE_SPEC" do
RSpec.describe Object, "#platform_is_not :c_long_size => SIZE_SPEC" do
before :each do
@guard = PlatformGuard.new :darwin, :wordsize => 32
@guard = PlatformGuard.new :darwin, :c_long_size => 32
allow(PlatformGuard).to receive(:os?).and_return(true)
allow(PlatformGuard).to receive(:new).and_return(@guard)
ScratchPad.clear
end

it "yields when #wordsize? returns false" do
allow(PlatformGuard).to receive(:wordsize?).and_return(false)
platform_is_not(:wordsize => 32) { ScratchPad.record :yield }
it "yields when #c_long_size? returns false" do
allow(PlatformGuard).to receive(:c_long_size?).and_return(false)
platform_is_not(:c_long_size => 32) { ScratchPad.record :yield }
expect(ScratchPad.recorded).to eq(:yield)
end

it "doesn not yield when #wordsize? returns true" do
allow(PlatformGuard).to receive(:wordsize?).and_return(true)
platform_is_not(:wordsize => 32) { ScratchPad.record :yield }
it "doesn not yield when #c_long_size? returns true" do
allow(PlatformGuard).to receive(:c_long_size?).and_return(true)
platform_is_not(:c_long_size => 32) { ScratchPad.record :yield }
expect(ScratchPad.recorded).not_to eq(:yield)
end
end
Expand Down Expand Up @@ -184,13 +184,13 @@
end
end

RSpec.describe PlatformGuard, ".wordsize?" do
RSpec.describe PlatformGuard, ".c_long_size?" do
it "returns true when arg is 32 and 1.size is 4" do
expect(PlatformGuard.wordsize?(32)).to eq(1.size == 4)
expect(PlatformGuard.c_long_size?(32)).to eq(1.size == 4)
end

it "returns true when arg is 64 and 1.size is 8" do
expect(PlatformGuard.wordsize?(64)).to eq(1.size == 8)
expect(PlatformGuard.c_long_size?(64)).to eq(1.size == 8)
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/ruby/.mspec.constants
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Prime
Private
ProcFromMethod
Psych
RactorLocalSingleton
REXML
RUBY_SIGNALS
RbReadline
Expand Down
18 changes: 18 additions & 0 deletions spec/ruby/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ AllCops:
DisplayCopNames: true
Exclude:
- command_line/fixtures/bad_syntax.rb
- core/exception/fixtures/syntax_error.rb
DisabledByDefault: true
NewCops: disable

Layout/IndentationConsistency:
Enabled: true

Layout/TrailingWhitespace:
Enabled: true

Expand Down Expand Up @@ -43,9 +47,23 @@ Lint/InterpolationCheck:
Lint/LiteralAsCondition:
Enabled: false

# Required to support Ruby 3.0
Lint/RedundantRequireStatement:
Exclude:
- core/fiber/**/*.rb
- library/fiber/**/*.rb
- optional/capi/fiber_spec.rb

Lint/RedundantSafeNavigation:
Exclude:
- language/safe_navigator_spec.rb

Lint/RedundantSplatExpansion:
Enabled: false

Lint/RescueException:
Enabled: false

Lint/UnifiedInteger:
Enabled: false

Expand Down
44 changes: 14 additions & 30 deletions spec/ruby/.rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-12-12 22:16:26 +0900 using RuboCop version 0.77.0.
# on 2024-10-12 16:01:45 UTC using RuboCop version 1.66.1.
# 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
Expand All @@ -21,6 +21,7 @@ Lint/DuplicateMethods:
- 'fixtures/class.rb'

# Offense count: 8
# This cop supports safe autocorrection (--autocorrect).
Lint/EnsureReturn:
Exclude:
- 'language/fixtures/ensure.rb'
Expand All @@ -39,6 +40,7 @@ Lint/FloatOutOfRange:
- 'core/string/modulo_spec.rb'

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
Lint/ImplicitStringConcatenation:
Exclude:
- 'language/string_spec.rb'
Expand All @@ -50,8 +52,8 @@ Lint/IneffectiveAccessModifier:
- 'core/module/fixtures/classes.rb'
- 'language/fixtures/private.rb'

# Offense count: 72
# Cop supports --auto-correct.
# Offense count: 71
# This cop supports safe autocorrection (--autocorrect).
Lint/LiteralInInterpolation:
Exclude:
- 'core/module/refine_spec.rb'
Expand All @@ -65,56 +67,37 @@ Lint/LiteralInInterpolation:
- 'language/undef_spec.rb'

# Offense count: 8
# Cop supports --auto-correct.
# This cop supports safe autocorrection (--autocorrect).
Lint/MultipleComparison:
Exclude:
- 'language/precedence_spec.rb'

# Offense count: 9
# This cop supports safe autocorrection (--autocorrect).
Lint/ParenthesesAsGroupedExpression:
Exclude:
- 'core/string/fixtures/freeze_magic_comment.rb'
- 'language/block_spec.rb'
- 'language/fixtures/send.rb'
- 'language/method_spec.rb'

# Offense count: 2
# Cop supports --auto-correct.
# This cop supports safe autocorrection (--autocorrect).
Lint/RedundantStringCoercion:
Exclude:
- 'core/io/print_spec.rb'

# Offense count: 1
# Cop supports --auto-correct.
Lint/RedundantWithIndex:
Exclude:
- 'core/enumerator/with_index_spec.rb'

# Offense count: 22
Lint/RescueException:
Lint/SelfAssignment:
Exclude:
- 'command_line/fixtures/debug_info.rb'
- 'core/dir/fileno_spec.rb'
- 'core/exception/cause_spec.rb'
- 'core/exception/no_method_error_spec.rb'
- 'core/fiber/kill_spec.rb'
- 'core/kernel/fixtures/autoload_frozen.rb'
- 'core/kernel/raise_spec.rb'
- 'core/module/autoload_spec.rb'
- 'core/mutex/sleep_spec.rb'
- 'core/thread/abort_on_exception_spec.rb'
- 'core/thread/shared/exit.rb'
- 'language/rescue_spec.rb'
- 'library/erb/filename_spec.rb'
- 'core/gc/auto_compact_spec.rb'

# Offense count: 4
# Configuration parameters: IgnoreImplicitReferences.
Lint/ShadowedArgument:
Exclude:
- 'language/fixtures/super.rb'

# Offense count: 39
# Configuration parameters: AllowComments.
# Offense count: 45
# Configuration parameters: AllowComments, AllowNil.
Lint/SuppressedException:
Enabled: false

Expand All @@ -127,7 +110,8 @@ Lint/UnderscorePrefixedVariableName:
- 'language/block_spec.rb'

# Offense count: 7
# Configuration parameters: ContextCreatingMethods, MethodCreatingMethods.
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AutoCorrect, ContextCreatingMethods, MethodCreatingMethods.
Lint/UselessAccessModifier:
Exclude:
- 'core/module/define_method_spec.rb'
Expand Down
6 changes: 4 additions & 2 deletions spec/ruby/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ end
platform_is_not :linux, :darwin do # Not Linux and not Darwin
end

platform_is wordsize: 64 do
platform_is pointer_size: 64 do
# 64-bit platform
end

Expand All @@ -179,7 +179,9 @@ In case there is a bug in MRI and the fix will be backported to previous version
If it is not backported or not likely, use `ruby_version_is` instead.
First, file a bug at https://bugs.ruby-lang.org/.
The problem is `ruby_bug` would make non-MRI implementations fail this spec while MRI itself does not pass it, so it should only be used if the bug is/will be fixed and backported.
Otherwise, non-MRI implementations would have to choose between being incompatible with the latest release of MRI to pass the spec or fail the spec, both which make no sense.
Otherwise, non-MRI implementations would have to choose between being incompatible with the latest release of MRI (which has the bug) to pass the spec, or behave the same as the latest release of MRI (which has the bug) and fail the spec, both which make no sense.

IOW, `ruby_bug '#NN', ''...'X.Y' do` is equivalent to `guard_not { RUBY_ENGINE == "ruby" && ruby_version_is ''...'X.Y' } do`. So it skips tests on MRI on specified versions (where a bug is present) and runs tests on alternative implementations only.

```ruby
ruby_bug '#13669', ''...'3.2' do
Expand Down
12 changes: 12 additions & 0 deletions spec/ruby/bin/rubocop
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/inline'

gemfile do
source 'https://rubygems.org'

gem 'rubocop', '1.66.1'
end

exec(Gem.bin_path('rubocop', 'rubocop'), *ARGV)
1 change: 0 additions & 1 deletion spec/ruby/command_line/fixtures/debug_info.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# frozen_string_literal: true
a = 'string'
b = a
c = b
Expand Down
Loading
Loading