<commit_before>require 'spec_helper'
require 'rspec/core/formatters/deprecation_formatter'
require 'tempfile'

module RSpec::Core::Formatters
  describe DeprecationFormatter do
    describe "#deprecation" do
      let(:deprecation_stream) { StringIO.new }
      let(:summary_stream)     { StringIO.new }
      let(:formatter) { DeprecationFormatter.new deprecation_stream, summary_stream }

      it "includes the method" do
        formatter.deprecation(:deprecated => "i_am_deprecated")
        deprecation_stream.rewind
        expect(deprecation_stream.read).to match(/i_am_deprecated is deprecated/)
      end

      it "includes the replacement" do
        formatter.deprecation(:replacement => "use_me")
        deprecation_stream.rewind
        expect(deprecation_stream.read).to match(/Use use_me instead/)
      end

      it "includes the call site if provided" do
        formatter.deprecation(:call_site => "somewhere")
        deprecation_stream.rewind
        expect(deprecation_stream.read).to match(/Called from somewhere/)
      end

      it "prints a message if provided, ignoring other data" do
        formatter.deprecation(:message => "this message", :deprecated => "x", :replacement => "y", :call_site => "z")
        deprecation_stream.rewind
        expect(deprecation_stream.read).to eq "this message"
      end

      it "limits the deprecation warnings after 3 calls" do
        5.times { formatter.deprecation(:deprecated => 'i_am_deprecated') }
        deprecation_stream.rewind
        expected = <<-EOS.gsub(/^ {10}/, '')
          DEPRECATION: i_am_deprecated is deprecated.
          DEPRECATION: i_am_deprecated is deprecated.
          DEPRECATION: i_am_deprecated is deprecated.
          DEPRECATION: Too many uses of deprecated 'i_am_deprecated'. Set config.deprecation_stream to a File for full output
        EOS
        expect(deprecation_stream.read).to eq expected
      end
    end

    describe "#deprecation_summary" do
      it "is printed when deprecations go to a file" do
        file = File.open("#{Dir.tmpdir}/deprecation_summary_example_output", "w")
        summary_stream = StringIO.new
        formatter = DeprecationFormatter.new file, summary_stream
        formatter.deprecation(:deprecated => 'i_am_deprecated')
        formatter.deprecation_summary
        summary_stream.rewind
        expect(summary_stream.read).to match(/1 deprecation logged to .*deprecation_summary_example_output/)
      end

      it "pluralizes for more than one deprecation" do
        file = File.open("#{Dir.tmpdir}/deprecation_summary_example_output", "w")
        summary_stream = StringIO.new
        formatter = DeprecationFormatter.new file, summary_stream
        formatter.deprecation(:deprecated => 'i_am_deprecated')
        formatter.deprecation(:deprecated => 'i_am_deprecated_also')
        formatter.deprecation_summary
        summary_stream.rewind
        expect(summary_stream.read).to match(/2 deprecations/)
      end

      it "is not printed when there are no deprecations" do<commit_msg>Add spec for File deprecation_stream output<commit_after>require 'spec_helper'
require 'rspec/core/formatters/deprecation_formatter'
require 'tempfile'

module RSpec::Core::Formatters
  describe DeprecationFormatter do
    describe "#deprecation" do
      let(:deprecation_stream) { StringIO.new }
      let(:summary_stream)     { StringIO.new }
      let(:formatter) { DeprecationFormatter.new deprecation_stream, summary_stream }

      it "includes the method" do
        formatter.deprecation(:deprecated => "i_am_deprecated")
        deprecation_stream.rewind
        expect(deprecation_stream.read).to match(/i_am_deprecated is deprecated/)
      end

      it "includes the replacement" do
        formatter.deprecation(:replacement => "use_me")
        deprecation_stream.rewind
        expect(deprecation_stream.read).to match(/Use use_me instead/)
      end

      it "includes the call site if provided" do
        formatter.deprecation(:call_site => "somewhere")
        deprecation_stream.rewind
        expect(deprecation_stream.read).to match(/Called from somewhere/)
      end

      it "prints a message if provided, ignoring other data" do
        formatter.deprecation(:message => "this message", :deprecated => "x", :replacement => "y", :call_site => "z")
        deprecation_stream.rewind
        expect(deprecation_stream.read).to eq "this message"
      end

      it "limits the deprecation warnings after 3 calls" do
        5.times { formatter.deprecation(:deprecated => 'i_am_deprecated') }
        deprecation_stream.rewind
        expected = <<-EOS.gsub(/^ {10}/, '')
          DEPRECATION: i_am_deprecated is deprecated.
          DEPRECATION: i_am_deprecated is deprecated.
          DEPRECATION: i_am_deprecated is deprecated.
          DEPRECATION: Too many uses of deprecated 'i_am_deprecated'. Set config.deprecation_stream to a File for full output
        EOS
        expect(deprecation_stream.read).to eq expected
      end

      context "with a File deprecation stream" do
        let(:deprecation_stream) { File.open("#{Dir.tmpdir}/deprecation_summary_example_output", "w+") }

        it "continues printing all deprecation warnings after the first 3" do
          5.times { formatter.deprecation(:deprecated => 'i_am_deprecated') }
          deprecation_stream.rewind
          expected = Array.new(5) { "i_am_deprecated is deprecated.\n" }.join
          expect(deprecation_stream.read).to eq expected
        end
      end
    end

    describe "#deprecation_summary" do
      it "is printed when deprecations go to a file" do
        file = File.open("#{Dir.tmpdir}/deprecation_summary_example_output", "w")
        summary_stream = StringIO.new
        formatter = DeprecationFormatter.new file, summary_stream
        formatter.deprecation(:deprecated => 'i_am_deprecated')
        formatter.deprecation_summary
        summary_stream.rewind
        expect(summary_stream.read).to match(/1 deprecation logged to .*deprecation_summary_example_output/)
      end

      it "pluralizes for more than one deprecation" do
        file = File.open("#{Dir.tmpdir}/deprecation_summary_example_output", "w")
        summary_stream = StringIO.new
        formatter = DeprecationFormatter.new file, summary_stream
        formatter.deprecation(:deprecated => 'i_am_deprecated')
        formatter.deprecation(:deprecated => 'i_am_deprecated_also')
        formatter.deprecation_summary
        summary_stream.rewind
        expect(summary_stream.read).to match(/2 deprecations/)
      end

      it "is not printed when there are no deprecations" do
{'fraction_non_alphanumeric': 0.08833283087126922, 'fraction_numerical': 0.0022610792885137173, 'mean_word_length': 3.133956386292835, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4329579', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1883, 'n_words': 456}