Skip to content

Commit

Permalink
Actualize ThreadSafety::NewThread cop specs
Browse files Browse the repository at this point in the history
  • Loading branch information
viralpraxis committed Jan 23, 2025
1 parent 72b8b4f commit bb6cc37
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions spec/rubocop/cop/thread_safety/new_thread_spec.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::ThreadSafety::NewThread, :config do
let(:msg) { 'Avoid starting new threads.' }

it 'registers an offense for starting a new thread' do
expect_offense(<<~RUBY)
Thread.new { do_work }
^^^^^^^^^^ #{msg}
RUBY
end

it 'registers an offense for starting a new thread with positional arguments' do
expect_offense(<<~RUBY)
Thread.new(1) { do_work }
^^^^^^^^^^^^^ #{msg}
RUBY
end

it 'registers an offense for starting a new thread with keyword arguments' do
expect_offense(<<~RUBY)
Thread.new(a: 42) { do_work }
^^^^^^^^^^^^^^^^^ #{msg}
RUBY
end

it 'registers an offense for starting a new thread with `fork` method' do
expect_offense(<<~RUBY)
Thread.fork { do_work }
^^^^^^^^^^^ #{msg}
RUBY
end

it 'registers an offense for starting a new thread with `start` method' do
expect_offense(<<~RUBY)
Thread.start { do_work }
^^^^^^^^^^^^ #{msg}
RUBY
end

it 'registers an offense for starting a new thread with top-level constant' do
expect_offense(<<~RUBY)
::Thread.new { do_work }
^^^^^^^^^^^^ #{msg}
RUBY
end

it 'registers an offense for starting a new thread with safe navigation' do
expect_offense(<<~RUBY)
Thread&.new { do_work }
^^^^^^^^^^^ #{msg}
RUBY
end

it 'does not register an offense for calling new on other classes' do
expect_no_offenses('Other.new { do_work }')
%w[new fork start].each do |method_name|
it "registers an offense for `#{Thread}.#{method_name}`" do
expect_offense(<<~RUBY, method_name: method_name)
Thread.%{method_name} { do_work }
^^^^^^^^{method_name} Avoid starting new threads.
RUBY
end

it "registers an offense for `#{Thread}.#{method_name}` with positional arguments" do
expect_offense(<<~RUBY, method_name: method_name)
Thread.%{method_name}(1) { do_work }
^^^^^^^^{method_name}^^^ Avoid starting new threads.
RUBY
end

it "registers an offense for `#{Thread}.#{method_name}` with keyword arguments" do
expect_offense(<<~RUBY, method_name: method_name)
Thread.%{method_name}(a: 42) { do_work }
^^^^^^^^{method_name}^^^^^^^ Avoid starting new threads.
RUBY
end

it "registers an offense for `#{Thread}.#{method_name}` with fully qualified constant name" do
expect_offense(<<~RUBY, method_name: method_name)
::Thread.%{method_name}(a: 42) { do_work }
^^^^^^^^^^{method_name}^^^^^^^ Avoid starting new threads.
RUBY
end

it "registers an offense for `#{Thread}.#{method_name}` with safe navigation" do
expect_offense(<<~RUBY, method_name: method_name)
Thread&.%{method_name}(a: 42) { do_work }
^^^^^^^^^{method_name}^^^^^^^ Avoid starting new threads.
RUBY
end

it "registers an offense for `#{Thread}.#{method_name}` with block argument" do
expect_offense(<<~RUBY, method_name: method_name)
Thread&.%{method_name}(&block)
^^^^^^^^^{method_name}^^^^^^^^ Avoid starting new threads.
RUBY
end

it "registers an offense for `#{Thread}.#{method_name}` with block and other arguments" do
expect_offense(<<~RUBY, method_name: method_name)
Thread&.%{method_name}(1, a: 42, &block)
^^^^^^^^^{method_name}^^^^^^^^^^^^^^^^^^ Avoid starting new threads.
RUBY
end

it 'does not register an offense for unrelated receiver' do
expect_no_offenses("Other.#{method_name} { do_work }")
end
end
end

0 comments on commit bb6cc37

Please sign in to comment.