<NME> use_multipart_alternative_as_content_type_of_email_review_spec.rb
<BEF> # frozen_string_literal: true

require 'spec_helper'
  let(:runner) { RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::UseMultipartAlternativeAsContentTypeOfEmailReview.new) }

  context "rails2" do
    it "should use mulipart/alternative as content_type of email" do
      content =<<-EOF
      class ProjectMailer < ActionMailer::Base
      end

      before do
        content = <<~EOF
          GEM
            remote: https://rubygems.org
        end
      end
      EOF
      runner.review('app/mailers/project_mailer.rb', content)
      runner.should have(1).errors
      runner.errors[0].to_s.should == "app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email"
                bundler (~> 1.0)
                railties (= 3.2.13)
        EOF
        runner.prepare('Gemfile.lock', content)
      end

      context 'project_mailer' do
        let(:content) do
          <<-EOF
          class ProjectMailer < ActionMailer::Base
            def send_email(email)
        end
      end
      EOF
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.erb").and_return(true)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.erb").and_return(true)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.erb").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.erb").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.haml").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.haml").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.haml").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.haml").and_return(false)
      runner.review('app/mailers/project_mailer.rb', content)
      runner.should have(0).errors
    end
            )
          end

          it 'does not use multiple/alternative as content_type of email when only plain text' do
            mock_email_files(['send_email.text.erb'])
            runner.review('app/mailers/project_mailer.rb', content)
            expect(runner.errors.size).to eq(0)
          end

          it 'does not use multipart/alternative as content_type of email' do
        end
      end
      EOF
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.erb").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.erb").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.erb").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.erb").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.haml").and_return(true)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.haml").and_return(true)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.haml").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.haml").and_return(false)
      runner.review('app/mailers/project_mailer.rb', content)
      runner.should have(0).errors
    end
          runner.review('app/mailers/project_mailer.rb', content)
          expect(runner.errors.size).to eq(0)
        end
      end
    end
  end
end
        end
      end
      EOF
      runner.review('app/mailers/project_mailer.rb', content)
      runner.should have(1).errors
      runner.errors[0].to_s.should == "app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email"
        end
      end
      EOF
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.erb").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.erb").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.erb").and_return(true)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.erb").and_return(true)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.haml").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.haml").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.haml").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.haml").and_return(false)
      runner.review('app/mailers/project_mailer.rb', content)
      runner.should have(0).errors
    end
        end
      end
      EOF
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.erb").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.erb").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.erb").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.erb").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.haml").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.haml").and_return(false)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.haml").and_return(true)
      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.haml").and_return(true)
      runner.review('app/mailers/project_mailer.rb', content)
      runner.should have(0).errors
    end

<MSG> more accurate check for use_multipart_alternative_as_content_type_of_email

<DFF> @@ -4,6 +4,10 @@ describe RailsBestPractices::Reviews::UseMultipartAlternativeAsContentTypeOfEmai
   let(:runner) { RailsBestPractices::Core::Runner.new(:reviews => RailsBestPractices::Reviews::UseMultipartAlternativeAsContentTypeOfEmailReview.new) }
 
   context "rails2" do
+    before :each do
+      RailsBestPractices::Core::Runner.stub!(:base_path).and_return(".")
+    end
+
     it "should use mulipart/alternative as content_type of email" do
       content =<<-EOF
       class ProjectMailer < ActionMailer::Base
@@ -16,6 +20,7 @@ describe RailsBestPractices::Reviews::UseMultipartAlternativeAsContentTypeOfEmai
         end
       end
       EOF
+      Dir.stub!(:entries).with("./app/views/project_mailer").and_return(["send_email.html.erb"])
       runner.review('app/mailers/project_mailer.rb', content)
       runner.should have(1).errors
       runner.errors[0].to_s.should == "app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email"
@@ -33,14 +38,17 @@ describe RailsBestPractices::Reviews::UseMultipartAlternativeAsContentTypeOfEmai
         end
       end
       EOF
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.erb").and_return(true)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.erb").and_return(true)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.erb").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.erb").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.haml").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.haml").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.haml").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.haml").and_return(false)
+      Dir.stub!(:entries).with("./app/views/project_mailer").and_return(["send_email.html.erb"])
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.erb").and_return(true)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.erb").and_return(true)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.html.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.html.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.rhtml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.rhtml").and_return(false)
       runner.review('app/mailers/project_mailer.rb', content)
       runner.should have(0).errors
     end
@@ -57,14 +65,56 @@ describe RailsBestPractices::Reviews::UseMultipartAlternativeAsContentTypeOfEmai
         end
       end
       EOF
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.erb").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.erb").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.erb").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.erb").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.haml").and_return(true)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.haml").and_return(true)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.haml").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.haml").and_return(false)
+      Dir.stub!(:entries).with("./app/views/project_mailer").and_return(["send_email.html.erb"])
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.html.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.haml").and_return(true)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.haml").and_return(true)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.html.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.rhtml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.rhtml").and_return(false)
+      runner.review('app/mailers/project_mailer.rb', content)
+      runner.should have(0).errors
+    end
+
+    it "should not use mulipart/alternative as content_type of email by rhtml" do
+      content =<<-EOF
+      class ProjectMailer < ActionMailer::Base
+        def send_email(email)
+          subject       email.subject
+          from          email.from
+          recipients    email.recipients
+          sent_on       Time.now
+          body          :email => email
+        end
+      end
+      EOF
+      Dir.stub!(:entries).with("./app/views/project_mailer").and_return(["send_email.html.erb"])
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.html.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.html.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.rhtml").and_return(true)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.rhtml").and_return(true)
+      runner.review('app/mailers/project_mailer.rb', content)
+      runner.should have(0).errors
+    end
+
+    it "should not use mulipart/alternative as content_type of email for non deliver method" do
+      content =<<-EOF
+      class ProjectMailer < ActionMailer::Base
+        def no_deliver
+        end
+      end
+      EOF
+      Dir.stub!(:entries).with("./app/views/project_mailer").and_return(["send_email.html.erb"])
       runner.review('app/mailers/project_mailer.rb', content)
       runner.should have(0).errors
     end
@@ -83,6 +133,7 @@ describe RailsBestPractices::Reviews::UseMultipartAlternativeAsContentTypeOfEmai
         end
       end
       EOF
+      Dir.stub!(:entries).with("./app/views/project_mailer").and_return(["send_email.html.erb"])
       runner.review('app/mailers/project_mailer.rb', content)
       runner.should have(1).errors
       runner.errors[0].to_s.should == "app/mailers/project_mailer.rb:2 - use multipart/alternative as content_type of email"
@@ -100,14 +151,17 @@ describe RailsBestPractices::Reviews::UseMultipartAlternativeAsContentTypeOfEmai
         end
       end
       EOF
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.erb").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.erb").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.erb").and_return(true)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.erb").and_return(true)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.haml").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.haml").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.haml").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.haml").and_return(false)
+      Dir.stub!(:entries).with("./app/views/project_mailer").and_return(["send_email.html.erb"])
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.erb").and_return(true)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.html.erb").and_return(true)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.html.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.rhtml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.rhtml").and_return(false)
       runner.review('app/mailers/project_mailer.rb', content)
       runner.should have(0).errors
     end
@@ -124,14 +178,17 @@ describe RailsBestPractices::Reviews::UseMultipartAlternativeAsContentTypeOfEmai
         end
       end
       EOF
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.erb").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.erb").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.erb").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.erb").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.plain.haml").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.html.haml").and_return(false)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.text.haml").and_return(true)
-      File.stub!(:exist?).with("app/views/project_mailer/send_email.html.haml").and_return(true)
+      Dir.stub!(:entries).with("./app/views/project_mailer").and_return(["send_email.html.erb"])
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.html.erb").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.haml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.haml").and_return(true)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.html.haml").and_return(true)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.plain.rhtml").and_return(false)
+      File.stub!(:exist?).with("./app/views/project_mailer/send_email.text.html.rhtml").and_return(false)
       runner.review('app/mailers/project_mailer.rb', content)
       runner.should have(0).errors
     end

{'addition_count': 89, 'commit_subject': 'more accurate check for use_multipart_alternative_as_content_type_of_email', 'deletion_count': 32, 'file_extension': '.rb', 'lang': 'rb', 'license': 'mit', 'repo_name': 'flyerhzm/rails_best_practices'}