<commit_before># encoding: utf-8
require 'spec_helper'

describe Mail::Message do

  def basic_email
    "To: mikel\r\nFrom: bob\r\nSubject: Hello!\r\n\r\nemail message\r\n"
  end

  describe "initialization" do

    it "should instantiate empty" do
      expect(Mail::Message.new.class).to eq Mail::Message
    end

    it "should return a basic email" do
      mail = Mail.new
      mail = Mail.new(mail.to_s)
      expect(mail.date).not_to be_blank
      expect(mail.message_id).not_to be_blank
      expect(mail.mime_version).to eq "1.0"
      expect(mail.content_type).to eq "text/plain"
      expect(mail.content_transfer_encoding).to eq "7bit"
      expect(mail.subject).to be_blank
      expect(mail.body).to be_blank
    end

    it "should instantiate with a string" do
      expect(Mail::Message.new(basic_email).class).to eq Mail::Message
    end

    it "should allow us to pass it a block" do
      mail = Mail::Message.new do
        from '<EMAIL>'
        to '<EMAIL>'
      end
      expect(mail.from).to eq ['<EMAIL>']
      expect(mail.to).to eq ['<EMAIL>']
    end

    it "should initialize a body and header class even if called with nothing to begin with" do
      mail = Mail::Message.new
      expect(mail.header.class).to eq Mail::Header
      expect(mail.body.class).to eq Mail::Body
    end

    it "should not report basic emails as bounced" do
      expect(Mail::Message.new).not_to be_bounced
    end

    it "should be able to parse a basic email" do
      expect { Mail.read(fixture('emails', 'plain_emails', 'basic_email.eml')) }.not_to raise_error
    end

    it "should be able to parse an email with @ in display name" do
      message = Mail.read(fixture('emails', 'plain_emails', 'raw_email_with_at_display_name.eml'))
      expect(message.to).to eq ["<EMAIL>", "<EMAIL>", "<EMAIL>"]
    end

    it "should be able to parse an email with only blank lines as body" do
      expect { Mail.read(fixture('emails', 'error_emails', 'missing_body.eml')) }.not_to raise_error
    end

    it "should be able to parse an email with a funky date header" do
      # TODO: This spec should actually do something
       expect { Mail.read(fixture('emails', 'error_emails', 'bad_date_header2.eml')) }
    end

    it 'should be able to invoke subject on a funky subject header' do
      Mail.read(fixture('emails', 'error_emails', 'bad_subject.eml')).subject
    end

    it 'should use default charset' do
      begin
        Mail::Message.default_charset, old = 'iso-8859-1', Mail::Message.default_charset
        expect(Mail::Message.new.charset).to eq 'iso-8859-1'
      ensure
        Mail::Message.default_charset = old
      end
    end

    it 'should be able to parse an email missing an encoding' do
      Mail.read(fixture('emails', 'error_emails', 'must_supply_encoding.eml'))
    end

    it "should be able to parse every email example we have without raising an exception" do
      emails = Dir.glob( fixture('emails/**/*') ).delete_if { |f| File.directory?(f) }

      allow(STDERR).to receive(:puts) # Don't want to get noisy about any warnings
      errors = false
      expected_failures = []
      emails.each do |email|
        begin
          Mail.read(email)
        rescue => e
          unless expected_failures.include?(email)
            puts "Failed on email #{email}"
            puts "Failure was:\n#{e}\n\n"
            errors = true
          end
        end
      end
      expect(errors).to be_falsey
    end

    it "should be able to parse a large email without raising an exception" do
      m = Mail.new
      m.add_file(:filename => "attachment.data", :content => "a" * (8 * 1024 * 1024))
      raw_email = "From <EMAIL> Mon May  2 16:07:05 2005\r\n#{m.to_s}"

      expect { Mail::Message.new(raw_email) }.not_to raise_error
    end

    it "should not raise a warning on having non US-ASCII characters in the header (should just handle it)" do
      expect(STDERR).not_to receive(:puts)
      Mail.read(fixture('emails', 'plain_emails', 'raw_email_string_in_date_field.eml'))
    end

    it "should raise a warning (and keep parsing) on having an incorrectly formatted header" do
      expect(STDERR).to receive(:puts).with("WARNING: Could not parse (and so ignoring) 'quite Delivered-To: xxx@xxx.xxx'")
      Mail.read(fixture('emails', 'plain_emails', 'raw_email_incorrect_header.eml')).to_s
    end

    it "should read in an email message and basically parse it" do
      mail = Mail.read(fixture('emails', 'plain_emails', 'basic_email.eml'))
      expect(mail.to).to eq ["<EMAIL>"]
    end

    it "should not fail parsing message with caps in content_type" do
      mail = Mail.read(fixture('emails', 'plain_emails', 'mix_caps_content_type.eml'))
      expect(mail.content_type).to eq 'text/plain; charset=iso-8859-1'
      expect(mail.main_type).to eq 'text'
      expect(mail.sub_type).to eq 'plain'
    end

    it "should be able to pass an empty reply-to header" do
      mail = Mail.read(fixture('emails', 'error_emails', 'empty_in_reply_to.eml'))
      expect(mail.in_reply_to).to be_blank
    end

    describe "YAML serialization" do
      before(:each) do
        @yaml_mail = Mail::Message.new(:to => '<EMAIL>',
                                  :cc => '<EMAIL>',
                                  :bcc => '<EMAIL>',
                                  :body => 'body',
                                  :subject => 'subject')
        @smtp_settings = { :address=>"smtp.somewhere.net",
          :port=>"587", :domain=>"somewhere.net", :user_name=>"<EMAIL>",
          :password=>"password", :authentication=>:plain, :enable_starttls_auto => true,
          :openssl_verify_mode => nil, :ssl=>nil, :tls=>nil }
        @yaml_mail.delivery_method :smtp, @smtp_settings
      end

      it "should serialize the basic information to YAML" do
        yaml = @yaml_mail.to_yaml
        yaml_output = YAML.load(yaml)
        expect(yaml_output['headers']['To']).to       eq "<EMAIL>"
        expect(yaml_output['headers']['Cc']).to       eq "<EMAIL>"
        expect(yaml_output['headers']['Subject']).to  eq "subject"
        expect(yaml_output['headers']['Bcc']).to      eq "<EMAIL>"
        expect(yaml_output['@body_raw']).to           eq "body"
        expect(yaml_output['@delivery_method']).not_to be_blank
      end
<commit_msg>fix more random errors<commit_after># encoding: utf-8
require 'spec_helper'

describe Mail::Message do

  def basic_email
    "To: mikel\r\nFrom: bob\r\nSubject: Hello!\r\n\r\nemail message\r\n"
  end

  describe "initialization" do

    it "should instantiate empty" do
      expect(Mail::Message.new.class).to eq Mail::Message
    end

    it "should return a basic email" do
      mail = Mail.new
      mail = Mail.new(mail.to_s)
      expect(mail.date).not_to be_blank
      expect(mail.message_id).not_to be_blank
      expect(mail.mime_version).to eq "1.0"
      expect(mail.content_type).to eq "text/plain"
      expect(mail.content_transfer_encoding).to eq "7bit"
      expect(mail.subject).to be_blank
      expect(mail.body).to be_blank
    end

    it "should instantiate with a string" do
      expect(Mail::Message.new(basic_email).class).to eq Mail::Message
    end

    it "should allow us to pass it a block" do
      mail = Mail::Message.new do
        from '<EMAIL>'
        to '<EMAIL>'
      end
      expect(mail.from).to eq ['<EMAIL>']
      expect(mail.to).to eq ['<EMAIL>']
    end

    it "should initialize a body and header class even if called with nothing to begin with" do
      mail = Mail::Message.new
      expect(mail.header.class).to eq Mail::Header
      expect(mail.body.class).to eq Mail::Body
    end

    it "should not report basic emails as bounced" do
      expect(Mail::Message.new).not_to be_bounced
    end

    it "should be able to parse a basic email" do
      expect { Mail.read(fixture('emails', 'plain_emails', 'basic_email.eml')) }.not_to raise_error
    end

    it "should be able to parse an email with @ in display name" do
      message = Mail.read(fixture('emails', 'plain_emails', 'raw_email_with_at_display_name.eml'))
      expect(message.to).to eq ["<EMAIL>", "<EMAIL>", "<EMAIL>"]
    end

    it "should be able to parse an email with only blank lines as body" do
      expect { Mail.read(fixture('emails', 'error_emails', 'missing_body.eml')) }.not_to raise_error
    end

    it "should be able to parse an email with a funky date header" do
      # TODO: This spec should actually do something
       expect { Mail.read(fixture('emails', 'error_emails', 'bad_date_header2.eml')) }
    end

    it 'should be able to invoke subject on a funky subject header' do
      Mail.read(fixture('emails', 'error_emails', 'bad_subject.eml')).subject
    end

    it 'should use default charset' do
      begin
        Mail::Message.default_charset, old = 'iso-8859-1', Mail::Message.default_charset
        expect(Mail::Message.new.charset).to eq 'iso-8859-1'
      ensure
        Mail::Message.default_charset = old
      end
    end

    it 'should be able to parse an email missing an encoding' do
      Mail.read(fixture('emails', 'error_emails', 'must_supply_encoding.eml'))
    end

    it "should be able to parse every email example we have without raising an exception" do
      emails = Dir.glob( fixture('emails/**/*') ).delete_if { |f| File.directory?(f) }

      allow(STDERR).to receive(:puts) # Don't want to get noisy about any warnings
      errors = false
      expected_failures = []
      emails.each do |email|
        begin
          Mail.read(email)
        rescue => e
          unless expected_failures.include?(email)
            puts "Failed on email #{email}"
            puts "Failure was:\n#{e}\n\n"
            errors = true
          end
        end
      end
      expect(errors).to be_falsey
    end

    it "should be able to parse a large email without raising an exception" do
      m = Mail.new
      m.add_file(:filename => "attachment.data", :content => "a" * (8 * 1024 * 1024))
      raw_email = "From <EMAIL> Mon May  2 16:07:05 2005\r\n#{m.to_s}"

      expect { Mail::Message.new(raw_email) }.not_to raise_error
    end

    it "should not raise a warning on having non US-ASCII characters in the header (should just handle it)" do
      expect(STDERR).not_to receive(:puts)
      Mail.read(fixture('emails', 'plain_emails', 'raw_email_string_in_date_field.eml'))
    end

    it "should raise a warning (and keep parsing) on having an incorrectly formatted header" do
      expect(STDERR).to receive(:puts).with("WARNING: Could not parse (and so ignoring) 'quite Delivered-To: <EMAIL>'")
      Mail.read(fixture('emails', 'plain_emails', 'raw_email_incorrect_header.eml')).to_s
    end

    it "should read in an email message and basically parse it" do
      mail = Mail.read(fixture('emails', 'plain_emails', 'basic_email.eml'))
      expect(mail.to).to eq ["<EMAIL>"]
    end

    it "should not fail parsing message with caps in content_type" do
      mail = Mail.read(fixture('emails', 'plain_emails', 'mix_caps_content_type.eml'))
      expect(mail.content_type).to eq 'text/plain; charset=iso-8859-1'
      expect(mail.main_type).to eq 'text'
      expect(mail.sub_type).to eq 'plain'
    end

    it "should be able to pass an empty reply-to header" do
      mail = Mail.read(fixture('emails', 'error_emails', 'empty_in_reply_to.eml'))
      expect(mail.in_reply_to).to be_blank
    end

    describe "YAML serialization" do
      before(:each) do
        # Ensure specs don't randomly fail due to messages being generated 1 second apart
        time = DateTime.now
        allow(DateTime).to receive(:now).and_return(time)

        @yaml_mail = Mail::Message.new(:to => '<EMAIL>',
                                  :cc => '<EMAIL>',
                                  :bcc => '<EMAIL>',
                                  :body => 'body',
                                  :subject => 'subject')
        @smtp_settings = { :address=>"smtp.somewhere.net",
          :port=>"587", :domain=>"somewhere.net", :user_name=>"<EMAIL>",
          :password=>"password", :authentication=>:plain, :enable_starttls_auto => true,
          :openssl_verify_mode => nil, :ssl=>nil, :tls=>nil }
        @yaml_mail.delivery_method :smtp, @smtp_settings
      end

      it "should serialize the basic information to YAML" do
        yaml = @yaml_mail.to_yaml
        yaml_output = YAML.load(yaml)
        expect(yaml_output['headers']['To']).to       eq "some<EMAIL>"
        expect(yaml_output['headers']['Cc']).to       eq "<EMAIL>"
        expect(yaml_output['headers']['Subject']).to  eq "subject"
        expect(yaml_output['headers']['Bcc']).to      eq "<EMAIL>"
        expect(yaml_output['@body_raw']).to           eq "body"
        expect(yaml_output['@delivery_method']).not_to be_blank
      end

{'fraction_non_alphanumeric': 0.12246331564158601, 'fraction_numerical': 0.006790508897908211, 'mean_word_length': 2.722545031958164, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7556949', 'n_tokens_mistral': 4136, 'n_tokens_neox': 3981, 'n_words': 1122}