Repository: rails/rails
File: activestorage/test/analyzer/image_analyzer/image_magick_test.rb (Ruby)

Code before:
  test "analyzing with ruby-vips unavailable" do
    stub_const(Object, :Vips, Module.new) do
      analyze_with_image_magick do
        blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg")
        metadata = extract_metadata_from(blob)

        assert_equal 4104, metadata[:width]
        assert_equal 2736, metadata[:height]
      end
    end
  end

  test "instrumenting analysis" do
    blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg")

    assert_notifications_count("analyze.active_storage", 1) do
      assert_notification("analyze.active_storage", analyzer: "mini_magick") do
        analyze_with_image_magick do
          blob.analyze
        end
      end
    end
  end

  test "when image_magick is not installed" do
    old_value = ActiveStorage.const_get(:MINIMAGICK_AVAILABLE)
    ActiveStorage.send(:remove_const, :MINIMAGICK_AVAILABLE)
    ActiveStorage.const_set(:MINIMAGICK_AVAILABLE, false)

    blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg")

    output = StringIO.new
    logger = ActiveSupport::Logger.new(output)

    ActiveStorage.with(logger: logger) do
      analyze_with_image_magick do
        blob.analyze
      end
    end

    assert_includes output.string, "Skipping image analysis because the mini_magick gem isn't installed"
  ensure
    ActiveStorage.send(:remove_const, :MINIMAGICK_AVAILABLE)
    ActiveStorage.const_set(:MINIMAGICK_AVAILABLE, old_value)
  end

  private
    def analyze_with_image_magick
      previous_processor, ActiveStorage.variant_processor = ActiveStorage.variant_processor, :mini_magick

      yield

Review comment (suggestion):
You can use [stub_const](https://api.rubyonrails.org/classes/ActiveSupport/Testing/ConstantStubbing.html#method-i-stub_const) for this

Code after:
  test "analyzing with ruby-vips unavailable" do
    stub_const(Object, :Vips, Module.new) do
      analyze_with_image_magick do
        blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg")
        metadata = extract_metadata_from(blob)

        assert_equal 4104, metadata[:width]
        assert_equal 2736, metadata[:height]
      end
    end
  end

  test "instrumenting analysis" do
    blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg")

    assert_notifications_count("analyze.active_storage", 1) do
      assert_notification("analyze.active_storage", analyzer: "mini_magick") do
        analyze_with_image_magick do
          blob.analyze
        end
      end
    end
  end

  test "when image_magick is not installed" do
    stub_const(ActiveStorage, :MINIMAGICK_AVAILABLE, false) do
      blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg")

      output = StringIO.new
      logger = ActiveSupport::Logger.new(output)

      ActiveStorage.with(logger: logger) do
        analyze_with_image_magick do
          blob.analyze
        end
      end

      assert_includes output.string, "Skipping image analysis because the mini_magick gem isn't installed"
    end
  end

  private
    def analyze_with_image_magick
      previous_processor, ActiveStorage.variant_processor = ActiveStorage.variant_processor, :mini_magick

      yield
    rescue LoadError
      ENV["BUILDKITE"] ? raise : skip("Variant processor image_magick is not installed")
    ensure
      ActiveStorage.variant_processor = previous_processor
    end