Skip to content

Commit

Permalink
Merge pull request #61 from viralpraxis/increase-coverage
Browse files Browse the repository at this point in the history
Increase tests coverage
  • Loading branch information
viralpraxis authored Oct 12, 2024
2 parents 696e9c0 + 9d8d81e commit 3965383
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
1 change: 0 additions & 1 deletion lib/rubocop/cop/thread_safety/class_instance_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def in_def_class_methods_dsl?(node)
node.ancestors.any? do |ancestor|
break if new_lexical_scope?(ancestor)
next unless ancestor.block_type?
next unless ancestor.children.first.is_a? AST::SendNode

ancestor.children.first.command? :class_methods
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def on_ivasgn(node)
end

def on_or_asgn(node)
return unless node.assignment_node&.ivasgn_type?
return unless node.assignment_node.ivasgn_type?
return unless in_class?(node)

on_assignment(node.expression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ class << self
RUBY
end

it 'registers no offense for `attr_accessor` within procs' do
expect_no_offenses(<<~RUBY)
module Test
DEFINE_ACCESSOR = ->{ attr_accessor :foobar }
end
RUBY
end

context 'when in a singleton class method' do
it 'registers no offense for `attr_accessor`' do
expect_no_offenses(<<~RUBY)
Expand Down
31 changes: 31 additions & 0 deletions spec/rubocop/cop/thread_safety/class_instance_variable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ def some_method(params)
RUBY
end

# rubocop:disable RSpec/ExampleLength
it 'registers an offense for assigning an ivar in class_methods within lambda', :with_legacy_lambda_node do
expect_offense(<<~RUBY)
module Test
class_methods do
->() {
def some_method(params)
@params = params
^^^^^^^ #{msg}
end
}
end
end
RUBY
end
# rubocop:enable RSpec/ExampleLength

it 'registers an offense for assigning an ivar in a class singleton method' do
expect_offense(<<~RUBY)
class Test
Expand Down Expand Up @@ -266,6 +283,20 @@ def some_method(params)
RUBY
end

it 'does not register an offenses for synchronized ivar_set in a method marked by module_function' do
expect_no_offenses(<<~RUBY)
module Test
def some_method(params)
$mutex.synchronize do
instance_variable_set(:@params, params)
end
end
module_function :some_method
end
RUBY
end

it 'registers an offense for assigning an ivar in a method below module_function directive' do
expect_offense(<<~RUBY)
module Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def surround(code)
@var ||= #{o}.freeze
RUBY
end

it 'registers no offense for `or-asgn` node without ivar' do
expect_no_offenses(surround(<<~RUBY))
var ||= %{o}
RUBY
end
end
end

Expand Down Expand Up @@ -346,6 +352,12 @@ def some_method
@a, @b, @c = 'foo'.freeze, [2].freeze, 3
RUBY
end

it 'registers no offenses for multiple mutable objects without class' do
expect_no_offenses(<<~RUBY)
@a, @b, @c = 'foo', [2], 3
RUBY
end
end

it 'freezes a heredoc' do
Expand Down Expand Up @@ -677,6 +689,12 @@ def assignment?
@a, @b, @c = 'foo'.freeze, [2].freeze, 3
RUBY
end

it 'registers no offenses for single assignment' do
expect_no_offenses(surround(<<~RUBY))
@a, @b = 1
RUBY
end
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'rubocop-thread_safety'
require 'rubocop-ast'

require 'rubocop/rspec/support'
require_relative 'shared_contexts'
Expand All @@ -40,6 +41,15 @@
config.fail_fast = ENV.key? 'RSPEC_FAIL_FAST'
end

config.around :each, :with_legacy_lambda_node do |example|
initial_value = RuboCop::AST::Builder.emit_lambda
RuboCop::AST::Builder.emit_lambda = true

example.call
ensure
RuboCop::AST::Builder.emit_lambda = initial_value
end

config.filter_run_excluding unsupported_on: :prism if ENV['PARSER_ENGINE'] == 'parser_prism'

config.disable_monkey_patching!
Expand Down

0 comments on commit 3965383

Please sign in to comment.