ronantakizawa/codereview-bench huggingface dataset:
Repository: rails/rails
File: actionpack/test/dispatch/executor_test.rb (Ruby)

Code before:
          ->(env) { [418, {}, ["I'm a teapot"]] },
        ),
        executor,
      )
    )

    env = Rack::MockRequest.env_for("", {})
    ActionDispatch::ExceptionWrapper.with(rescue_responses: { BusinessAsUsual.name => 418 }) do
      assert_no_error_reported do
        response = middleware.call(env)
        assert_equal 418, response[0]
      end
    end
  end

  private
    def call_and_return_body(&block)
      app = block || proc { [200, {}, []] }
      env = Rack::MockRequest.env_for("", {})
      _, _, body = middleware(app).call(env)
      body
    end

    def middleware(inner_app)
      Rack::Lint.new(ActionDispatch::Executor.new(Rack::Lint.new(inner_app), executor))
    end

    def executor
      @executor ||= Class.new(ActiveSupport::Executor)
    end
end


Review comment (suggestion):
This test is insufficient/not to spec (the Rack spec).

```suggestion
    env["rack.response_finished"].first.call({}, 200, {}, nil)
```

Code after:
          ->(env) { [418, {}, ["I'm a teapot"]] },
        ),
        executor,
      )
    )

    env = Rack::MockRequest.env_for("", {})
    ActionDispatch::ExceptionWrapper.with(rescue_responses: { BusinessAsUsual.name => 418 }) do
      assert_no_error_reported do
        response = middleware.call(env)
        assert_equal 418, response[0]
      end
    end
  end

  def test_complete_callbacks_are_called_on_rack_response_finished
    completed = false
    executor.to_complete { completed = true }

    env = { "rack.response_finished" => [] }
    call_and_return_body(env)

    assert_not completed

    assert_equal 1, env["rack.response_finished"].size
    env["rack.response_finished"].first.call

    assert completed
  end

  def test_complete_callbacks_are_called_once_on_rack_response_finished_when_exception_is_raised
    completed_count = 0
    executor.to_complete { completed_count += 1 }

    env = { "rack.response_finished" => [] }

    begin
      call_and_return_body(env) do
        raise "error"
      end
    rescue
    end

    assert_equal 1, env["rack.response_finished"].size
    env["rack.response_finished"].first.call

    assert_equal 1, completed_count
  end

  private
    def call_and_return_body(env = {}, &block)
Repository: rails/rails
File: actionpack/lib/action_dispatch/middleware/executor.rb (Ruby)

Code before:
# frozen_string_literal: true

# :markup: markdown

require "rack/body_proxy"

module ActionDispatch
  class Executor
    def initialize(app, executor)
      @app, @executor = app, executor
    end

    def call(env)
      state = @executor.run!(reset: true)
      begin
        response = @app.call(env)

        if env["action_dispatch.report_exception"]
          error = env["action_dispatch.exception"]
          @executor.error_reporter.report(error, handled: false, source: "application.action_dispatch")
        end

        returned = response << ::Rack::BodyProxy.new(response.pop) { state.complete! }
      rescue Exception => error
        request = ActionDispatch::Request.new env
        backtrace_cleaner = request.get_header("action_dispatch.backtrace_cleaner")
        wrapper = ExceptionWrapper.new(backtrace_cleaner, error)
        @executor.error_reporter.report(wrapper.unwrapped_exception, handled: false, source: "application.action_dispatch")
        raise
      ensure
        state.complete! unless returned
      end
    end
  end
end


Review comment (suggestion):
```suggestion
        response_finished << proc { state.complete! }
```

Code after:
# frozen_string_literal: true

# :markup: markdown

require "rack/body_proxy"

module ActionDispatch
  class Executor
    def initialize(app, executor)
      @app, @executor = app, executor
    end

    def call(env)
      state = @executor.run!(reset: true)
      if response_finished = env["rack.response_finished"]
        response_finished << -> { state.complete! }
      end

      begin
        response = @app.call(env)

        if env["action_dispatch.report_exception"]
          error = env["action_dispatch.exception"]
          @executor.error_reporter.report(error, handled: false, source: "application.action_dispatch")
        end

        unless response_finished
          response << ::Rack::BodyProxy.new(response.pop) { state.complete! }
        end
        returned = true
        response
      rescue Exception => error
        request = ActionDispatch::Request.new env
        backtrace_cleaner = request.get_header("action_dispatch.backtrace_cleaner")
        wrapper = ExceptionWrapper.new(backtrace_cleaner, error)
        @executor.error_reporter.report(wrapper.unwrapped_exception, handled: false, source: "application.action_dispatch")
        raise
      ensure
        if !returned && !response_finished
          state.complete!
        end
Repository: rails/rails
File: activesupport/test/logger_test.rb (Ruby)

Code before:
    ActiveSupport::IsolatedExecutionState.isolation_level = previous_isolation_level
  end

  def test_logger_level_thread_safety
    previous_isolation_level = ActiveSupport::IsolatedExecutionState.isolation_level
    ActiveSupport::IsolatedExecutionState.isolation_level = :thread

    @logger.level = Logger::INFO
    assert_level(Logger::INFO)

    enumerator = Enumerator.new do |yielder|
      @logger.level = Logger::DEBUG
      yielder.yield @logger.level
    end
    assert_equal Logger::DEBUG, enumerator.next
    assert_level(Logger::DEBUG)
  ensure
    ActiveSupport::IsolatedExecutionState.isolation_level = previous_isolation_level
  end

  def test_temporarily_logging_at_a_noisier_level
    @logger.level = Logger::INFO

    @logger.debug "NOT THERE"

    @logger.log_at Logger::DEBUG do
      @logger.debug "THIS IS HERE"
    end

    @logger.debug "NOT THERE"

    assert_not_includes @output.string, "NOT THERE"
    assert_includes @output.string, "THIS IS HERE"
  end

  def test_temporarily_logging_at_a_quieter_level
    @logger.log_at Logger::ERROR do
      @logger.debug "NOT THERE"
      @logger.error "THIS IS HERE"
    end

    assert_not_includes @output.string, "NOT THERE"
    assert_includes @output.string, "THIS IS HERE"
  end

  def test_temporarily_logging_at_a_symbolic_level
    @logger.log_at :error do
      @logger.debug "NOT THERE"
      @logger.error "THIS IS HERE"
    end


Review comment (suggestion):
Without patch:

<img width="2554" height="506" alt="Screenshot 2025-08-08 at 7 33 17 am" src="https://github.com/user-attachments/assets/14b83b8f-4251-4fe4-af14-29e41e5adaa8" />


Code after:
    ActiveSupport::IsolatedExecutionState.isolation_level = previous_isolation_level
  end

  def test_logger_level_thread_safety
    previous_isolation_level = ActiveSupport::IsolatedExecutionState.isolation_level
    ActiveSupport::IsolatedExecutionState.isolation_level = :thread

    @logger.level = Logger::INFO
    assert_level(Logger::INFO)

    enumerator = Enumerator.new do |yielder|
      @logger.level = Logger::DEBUG
      yielder.yield @logger.level
    end
    assert_equal Logger::DEBUG, enumerator.next
    assert_level(Logger::DEBUG)
  ensure
    ActiveSupport::IsolatedExecutionState.isolation_level = previous_isolation_level
  end

  def test_logger_freeze
    logger = @logger.clone
    logger.freeze

    assert_nothing_raised do
      assert_equal Logger::DEBUG, logger.level

      logger.debug "I am frozen 1"
      assert_includes @output.string, "I am frozen 1"

      logger.debug { "I am frozen 2" }
      assert_includes @output.string, "I am frozen 2"

      logger.add Logger::INFO, "I am frozen 3"
      assert_includes @output.string, "I am frozen 3"

      logger.silence do
        logger.debug "I am frozen 4"
      end
      assert_not_includes @output.string, "I am frozen 4"
    end

    assert_raises FrozenError do
      logger.level = Logger::INFO
    end

    assert_raises FrozenError do
      logger.formatter = Logger::Formatter.new
    end
  end
Repository: rails/rails
File: activerecord/test/cases/locking_test.rb (Ruby)

Code before:
            a_wakeup.wait
          end
          t1 = Time.now
        end

        b = Thread.new do
          b_wakeup.wait
          t2 = Time.now
          Person.transaction(&block)
          a_wakeup.set
          t3 = Time.now
        end

        a.join
        b.join

        assert t1 > t0
        assert t2 > t0
        assert t3 > t2
        [t0.to_f..t1.to_f, t2.to_f..t3.to_f]
      end
  end
end

class PessimisticLockingWhilePreventingWritesTest < ActiveRecord::TestCase
  CUSTOM_LOCK = if current_adapter?(:SQLite3Adapter)
    true # no-op
  else
    "FOR SHARE"
  end

  fixtures :people

  def test_lock_when_not_preventing_writes
    person = Person.last!
    assert_nothing_raised do
      person.lock!
    end
  end

  def test_lock_when_preventing_writes
    person = Person.last!
    ActiveRecord::Base.while_preventing_writes do
      assert_raises(ActiveRecord::ReadOnlyError) do
        person.lock!
      end
    end
  end

  def test_lock_when_not_preventing_writes_nested
    person = Person.last!

Review comment (suggestion):
This is what rubocop wants

```suggestion
  CUSTOM_LOCK = current_adapter?(:SQLite3Adapter) || "FOR SHARE"
```

Which is not very nice, so I wrote it differently.

Code after:
            a_wakeup.wait
          end
          t1 = Time.now
        end

        b = Thread.new do
          b_wakeup.wait
          t2 = Time.now
          Person.transaction(&block)
          a_wakeup.set
          t3 = Time.now
        end

        a.join
        b.join

        assert t1 > t0
        assert t2 > t0
        assert t3 > t2
        [t0.to_f..t1.to_f, t2.to_f..t3.to_f]
      end
  end
end

class PessimisticLockingWhilePreventingWritesTest < ActiveRecord::TestCase
  CUSTOM_LOCK = if current_adapter?(:SQLite3Adapter)
    "FOR UPDATE" # no-op
  else
    "FOR SHARE"
  end

  fixtures :people

  def test_lock_when_not_preventing_writes
    person = Person.last!
    assert_nothing_raised do
      person.lock!
    end
  end

  def test_lock_when_preventing_writes
    person = Person.last!
    ActiveRecord::Base.while_preventing_writes do
      assert_raises(ActiveRecord::ReadOnlyError) do
        person.lock!
      end
    end
  end

  def test_lock_when_not_preventing_writes_nested
    person = Person.last!
Repository: rails/rails
File: activerecord/test/cases/associations/belongs_to_associations_test.rb (Ruby)

Code before:
    end
  end

  def test_polymorphic_with_custom_name_counter_cache
    car = CarPolymorphicName.create!
    wheel = WheelPolymorphicName.create!(wheelable_type: "polymorphic_car", wheelable_id: car.id)
    assert_equal 1, car.reload.wheels_count

    wheel.update! wheelable: nil

    assert_equal 0, car.reload.wheels_count
  end

  def test_polymorphic_with_custom_name_touch_old_belongs_to_model
    car = CarPolymorphicName.create!
    wheel = WheelPolymorphicName.create!(wheelable: car)

    touch_time = 1.day.ago.round
    travel_to(touch_time) do
      wheel.update!(wheelable: nil)
    end

    assert_equal touch_time, car.reload.wheels_owned_at
  end

  def test_polymorphic_stale_state_handles_nil_foreign_keys_correctly
      klass = Class.new(ActiveRecord::Base) do
        self.table_name = 'records'

        has_one :attachment, as: :record

        def self.polymorphic_name
          'Blob'
        end
      end

    assert_nothing_raised do
      Attachment.create!(record: klass.build, record_type: 'Document')
    end
  end

  def test_build_with_conditions
    client = companies(:second_client)
    firm   = client.build_bob_firm

    assert_equal "Bob", firm.name
  end

  def test_create_with_conditions
    client = companies(:second_client)
    firm   = client.create_bob_firm

Review comment (suggestion):
@byroot I added a test. It's a bit indirect, but that's exactly the situation in which we encounter the issue in our codebase. Tell me if you have a better idea! 

Code after:
    end
  end

  def test_polymorphic_with_custom_name_counter_cache
    car = CarPolymorphicName.create!
    wheel = WheelPolymorphicName.create!(wheelable_type: "polymorphic_car", wheelable_id: car.id)
    assert_equal 1, car.reload.wheels_count

    wheel.update! wheelable: nil

    assert_equal 0, car.reload.wheels_count
  end

  def test_polymorphic_with_custom_name_touch_old_belongs_to_model
    car = CarPolymorphicName.create!
    wheel = WheelPolymorphicName.create!(wheelable: car)

    touch_time = 1.day.ago.round
    travel_to(touch_time) do
      wheel.update!(wheelable: nil)
    end

    assert_equal touch_time, car.reload.wheels_owned_at
  end

  def test_polymorphic_stale_state_handles_nil_foreign_keys_correctly
    klass = Class.new(ActiveRecord::Base) do
      self.table_name = "records"

      has_one :attachment, as: :record

      def self.polymorphic_name
        "Blob"
      end
    end

    assert_nothing_raised { Attachment.create!(record: klass.build, record_type: "Document") }
  end

  def test_build_with_conditions
    client = companies(:second_client)
    firm   = client.build_bob_firm

    assert_equal "Bob", firm.name
  end

  def test_create_with_conditions
    client = companies(:second_client)
    firm   = client.create_bob_firm

    assert_equal "Bob", firm.name
Repository: rails/rails
File: activesupport/test/parallelization_test.rb (Ruby)

Code before:
# frozen_string_literal: true

class ParallelizationTest < ActiveSupport::TestCase
  def setup
    @original_worker_id = ActiveSupport::TestCase.parallel_worker_id
  end

  def teardown
    ActiveSupport::TestCase.parallel_worker_id = @original_worker_id
  end

  test "parallel_worker_id is accessible as an attribute and method" do
    ActiveSupport::TestCase.parallel_worker_id = nil
    assert_nil ActiveSupport::TestCase.parallel_worker_id
    assert_nil parallel_worker_id
  end

  test "parallel_worker_id is set and accessible from class and instance" do
    ActiveSupport::TestCase.parallel_worker_id = 3

    assert_equal 3, ActiveSupport::TestCase.parallel_worker_id
    assert_equal 3, parallel_worker_id
  end

  test "parallel_worker_id persists across test subclasses" do
    ActiveSupport::TestCase.parallel_worker_id = 5

    subclass = Class.new(ActiveSupport::TestCase)
    assert_equal 5, subclass.parallel_worker_id


Review comment (bug):
Oh the builds is failing because AS isn't defined. You're missing `require_relative "abstract_unit"` above this.

Code after:
# frozen_string_literal: true

require_relative "abstract_unit"

class ParallelizationTest < ActiveSupport::TestCase
  def setup
    @original_worker_id = ActiveSupport::TestCase.parallel_worker_id
  end

  def teardown
    ActiveSupport::TestCase.parallel_worker_id = @original_worker_id
  end

  test "parallel_worker_id is accessible as an attribute and method" do
    ActiveSupport::TestCase.parallel_worker_id = nil
    assert_nil ActiveSupport::TestCase.parallel_worker_id
    assert_nil parallel_worker_id
  end

  test "parallel_worker_id is set and accessible from class and instance" do
    ActiveSupport::TestCase.parallel_worker_id = 3

    assert_equal 3, ActiveSupport::TestCase.parallel_worker_id
    assert_equal 3, parallel_worker_id
  end

  test "parallel_worker_id persists across test subclasses" do
    ActiveSupport::TestCase.parallel_worker_id = 5

    subclass = Class.new(ActiveSupport::TestCase)
Repository: rails/rails
File: railties/test/application/configuration_test.rb (Ruby)

Code before:
      assert_equal :test, Rails.application.config.active_job.queue_adapter
      adapter = ActiveJob::Base.queue_adapter
      assert_instance_of ActiveJob::QueueAdapters::TestAdapter, adapter
    end

    test "Regexp.timeout is set to 1s by default" do
      app "development"
      assert_equal 1, Regexp.timeout
    end

    test "Regexp.timeout can be configured" do
      add_to_config "Regexp.timeout = 5"
      app "development"
      assert_equal 5, Regexp.timeout
    end

    private
      def set_custom_config(contents, config_source = "custom".inspect)
        app_file "config/custom.yml", contents

        add_to_config <<~RUBY
          config.my_custom_config = config_for(#{config_source})
        RUBY
      end
  end
end


Review comment (suggestion):

hi @p8 this should be inverted 
```suggestion
      assert_equal Rails.logger, Rails.application.config.action_controller.logger
```

Code after:
      assert_equal :test, Rails.application.config.active_job.queue_adapter
      adapter = ActiveJob::Base.queue_adapter
      assert_instance_of ActiveJob::QueueAdapters::TestAdapter, adapter
    end

    test "Regexp.timeout is set to 1s by default" do
      app "development"
      assert_equal 1, Regexp.timeout
    end

    test "Regexp.timeout can be configured" do
      add_to_config "Regexp.timeout = 5"
      app "development"
      assert_equal 5, Regexp.timeout
    end

    test "action_controller.logger defaults to Rails.logger" do
      restore_default_config
      add_to_config "config.logger = Logger.new(STDOUT, level: Logger::INFO)"
      app "development"

      output = capture(:stdout) do
        get "/"
      end

      assert_equal Rails.application.config.action_controller.logger, Rails.logger
      assert output.include?("Processing by Rails::WelcomeController#index as HTML")
    end

    test "action_controller.logger can be disabled by assigning nil" do
      add_to_config <<-RUBY
        config.logger = Logger.new(STDOUT, level: Logger::INFO)
        config.action_controller.logger = nil
      RUBY
      app "development"

      output = capture(:stdout) do
        get "/"
      end

      assert_nil Rails.application.config.action_controller.logger
      assert_not output.include?("Processing by Rails::WelcomeController#index as HTML")
    end

    test "action_controller.logger can be disabled by assigning false" do
      add_to_config <<-RUBY
        config.logger = Logger.new(STDOUT, level: Logger::INFO)
        config.action_controller.logger = false
      RUBY

      app "development"
Repository: rails/rails
File: actionview/lib/action_view/helpers/tags/check_box.rb (Ruby)

Code before:
          else
            checkbox
          end
        end

        private
          def checked?(value)
            case value
            when TrueClass, FalseClass
              value == !!@checked_value
            when NilClass
              false
            when String
              value == @checked_value
            else
              if value.respond_to?(:include?)
                value.include?(@checked_value)
              else
                value.to_i == @checked_value.to_i
              end
            end
          end

          def hidden_field_for_checkbox(options)
            if @unchecked_value
              tag_options = options.slice("name", "disabled", "form").merge("type" => "hidden", "value" => @unchecked_value)
              tag_options["autocomplete"] = "off" unless ActionView::Base.remove_hidden_field_autocomplete
              tag("input", tag_options)
            else
              "".html_safe
            end
          end
      end
    end
  end
end


Review comment (suggestion):
```suggestion
              tag_options = options.slice("name", "disabled", "form").merge!("type" => "hidden", "value" => @unchecked_value)
```

Code after:
          else
            checkbox
          end
        end

        private
          def checked?(value)
            case value
            when TrueClass, FalseClass
              value == !!@checked_value
            when NilClass
              false
            when String
              value == @checked_value
            else
              if value.respond_to?(:include?)
                value.include?(@checked_value)
              else
                value.to_i == @checked_value.to_i
              end
            end
          end

          def hidden_field_for_checkbox(options)
            if @unchecked_value
              tag_options = options.slice("name", "disabled", "form").merge!("type" => "hidden", "value" => @unchecked_value)
              tag_options["autocomplete"] = "off" unless ActionView::Base.remove_hidden_field_autocomplete
              tag("input", tag_options)
            else
              "".html_safe
            end
          end
      end
    end
  end
end
Repository: rails/rails
File: actionview/lib/action_view/helpers/tags/file_field.rb (Ruby)

Code before:
# frozen_string_literal: true

module ActionView
  module Helpers
    module Tags # :nodoc:
      class FileField < TextField # :nodoc:
        def render
          include_hidden = @options.delete(:include_hidden)
          options = @options.stringify_keys
          add_default_name_and_id(options)

          if options["multiple"] && include_hidden
            hidden_field_for_multiple_file(options) + super
          else
            super
          end
        end

        private
          def hidden_field_for_multiple_file(options)
            tag_options = { "name" => options["name"], "type" => "hidden", "value" => "" }
            tag_options["autocomplete"] = "off" unless ActionView::Base.remove_hidden_field_autocomplete
            tag("input", **tag_options)
          end
      end
    end
  end
end


Review comment (suggestion):
Is there a reason to splat here?

```suggestion
            tag("input", tag_options)
```

Code after:
# frozen_string_literal: true

module ActionView
  module Helpers
    module Tags # :nodoc:
      class FileField < TextField # :nodoc:
        def render
          include_hidden = @options.delete(:include_hidden)
          options = @options.stringify_keys
          add_default_name_and_id(options)

          if options["multiple"] && include_hidden
            hidden_field_for_multiple_file(options) + super
          else
            super
          end
        end

        private
          def hidden_field_for_multiple_file(options)
            tag_options = { "name" => options["name"], "type" => "hidden", "value" => "" }
            tag_options["autocomplete"] = "off" unless ActionView::Base.remove_hidden_field_autocomplete
            tag("input", tag_options)
          end
      end
    end
  end
end