Skip to content

Commit

Permalink
Use RSpec output matcher and do not redefine Kernel#warn in specs
Browse files Browse the repository at this point in the history
References mikel#1378
  • Loading branch information
eregon authored and jeremy committed Feb 14, 2020
1 parent 904fc32 commit d87de15
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
7 changes: 5 additions & 2 deletions spec/mail/field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

describe 'parsing' do
it "parses full header fields" do
expect($stderr).to_not receive(:puts)
field = Mail::Field.parse('To: Mikel')
field = nil
expect {
field = Mail::Field.parse('To: Mikel')
}.to_not output.to_stderr

expect(field.name).to eq 'To'
expect(field.value).to eq 'Mikel'
if field.value.respond_to?(:encoding)
Expand Down
30 changes: 18 additions & 12 deletions spec/mail/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ def create_mail_with_splat_args
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)
read_fixture('emails', 'plain_emails', 'raw_email_string_in_date_field.eml')
expect {
read_fixture('emails', 'plain_emails', 'raw_email_string_in_date_field.eml')
}.to_not output.to_stderr
end

it "should raise a warning (and keep parsing) on having an incorrectly formatted header" do
Expand Down Expand Up @@ -1325,8 +1326,7 @@ def message_headers_should_match(message, other)
body = "This is plain text US-ASCII"
mail = Mail.new
mail.body = body
expect($stderr).not_to receive(:puts)
mail.to_s
expect { mail.to_s }.to_not output.to_stderr
end

it "should set the content type to text/plain; charset=us-ascii" do
Expand Down Expand Up @@ -1357,8 +1357,12 @@ def message_headers_should_match(message, other)
mail = Mail.new
mail.body = body
mail.content_transfer_encoding = "8bit"
expect($stderr).to receive(:puts).with(/Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect./m)
expect(mail.to_s).to match(%r{|Content-Type: text/plain; charset=UTF-8|})

result = nil
expect {
result = mail.to_s
}.to output(/Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect./).to_stderr
expect(result).to match(%r{|Content-Type: text/plain; charset=UTF-8|})
end

it "should raise a warning if there is no charset parameter and there is non ascii chars and default to text/plain, UTF-8" do
Expand All @@ -1367,8 +1371,12 @@ def message_headers_should_match(message, other)
mail.body = body
mail.content_type = "text/plain"
mail.content_transfer_encoding = "8bit"
expect($stderr).to receive(:puts).with(/Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect./m)
expect(mail.to_s).to match(%r{|Content-Type: text/plain; charset=UTF-8|})

result = nil
expect {
result = mail.to_s
}.to output(/Non US-ASCII detected and no charset defined.\nDefaulting to UTF-8, set your own if this is incorrect./).to_stderr
expect(result).to match(%r{|Content-Type: text/plain; charset=UTF-8|})
end

it "should not raise a warning if there is no charset parameter and the content-type is not text" do
Expand All @@ -1377,8 +1385,7 @@ def message_headers_should_match(message, other)
mail.body = body
mail.content_type = "image/png"
mail.content_transfer_encoding = "8bit"
expect($stderr).to_not receive(:puts)
mail.to_s
expect { mail.to_s }.to_not output.to_stderr
end

it "should not raise a warning if there is a charset defined and there is non ascii chars" do
Expand All @@ -1387,8 +1394,7 @@ def message_headers_should_match(message, other)
mail.body = body
mail.content_transfer_encoding = "8bit"
mail.content_type = "text/plain; charset=UTF-8"
expect($stderr).not_to receive(:puts)
mail.to_s
expect { mail.to_s }.to_not output.to_stderr
end

it "should be able to set a content type with an array and hash" do
Expand Down
3 changes: 1 addition & 2 deletions spec/mail/mime_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,7 @@
mail.body = body
mail.charset = 'UTF-8'
mail.add_file fixture_path('attachments', 'test.png')
expect($stderr).not_to receive(:puts)
mail.to_s
expect { mail.to_s }.to_not output.to_stderr
end


Expand Down
5 changes: 3 additions & 2 deletions spec/mail/part_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@

describe "parts that have a missing header" do
it "should not try to init a header if there is none" do
expect($stderr).not_to receive(:puts)
Mail::Part.new(Mail::Utilities.to_crlf(<<PARTEND))
expect {
Mail::Part.new(Mail::Utilities.to_crlf(<<PARTEND))
The original message was received at Mon, 24 Dec 2007 10:03:47 +1100
from 60-0-0-146.static.tttttt.com.au [60.0.0.146]
Expand All @@ -107,6 +107,7 @@
dangerous content by MailScanner, and is
believed to be clean.
PARTEND
}.to_not output.to_stderr
end
end

Expand Down
6 changes: 0 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,3 @@ def self.new(*args)
MockIMAP.new
end
end

module Kernel
def warn(m)
$stderr.puts(m)
end
end

0 comments on commit d87de15

Please sign in to comment.