From bb6cc37f41eecbeac4d2acfdd64d02bdbf422342 Mon Sep 17 00:00:00 2001 From: viralpraxis Date: Thu, 23 Jan 2025 17:03:11 +0300 Subject: [PATCH] Actualize `ThreadSafety::NewThread` cop specs --- .../cop/thread_safety/new_thread_spec.rb | 106 +++++++++--------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/spec/rubocop/cop/thread_safety/new_thread_spec.rb b/spec/rubocop/cop/thread_safety/new_thread_spec.rb index be2072a..24b6bd6 100644 --- a/spec/rubocop/cop/thread_safety/new_thread_spec.rb +++ b/spec/rubocop/cop/thread_safety/new_thread_spec.rb @@ -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