<commit_before>require 'spec_helper'
require 'tmpdir'

module RailsBestPractices
  describe Analyzer do
    subject { Analyzer.new(".") }

    describe '::new' do
      it 'should expand a relative path to an absolute' do
        subject.path.should eq File.expand_path('.')
      end
    end

    describe "expand_dirs_to_files" do
      it "should expand all files in spec directory" do
        dir = File.dirname(__FILE__)
        subject.expand_dirs_to_files(dir).should be_include(dir + '/analyzer_spec.rb')
      end
    end

    describe "file_sort" do
      it "should get models first, mailers, helpers and then others" do
        files = ["app/controllers/users_controller.rb", "app/mailers/user_mailer.rb", "app/helpers/users_helper.rb", "app/models/user.rb", "app/views/users/index.html.haml", "app/views/users/show.html.slim", "lib/user.rb"]
        subject.file_sort(files).should == ["app/models/user.rb", "app/mailers/user_mailer.rb", "app/helpers/users_helper.rb", "app/controllers/users_controller.rb", "app/views/users/index.html.haml", "app/views/users/show.html.slim", "lib/user.rb"]
      end
    end

    describe "file_ignore" do
      it "should ignore lib" do
        files = ["app/controllers/users_controller.rb", "app/mailers/user_mailer.rb", "app/models/user.rb", "app/views/users/index.html.haml", "app/views/users/show.html.slim", "lib/user.rb"]
        subject.file_ignore(files, 'lib/').should == ["app/controllers/users_controller.rb", "app/mailers/user_mailer.rb", "app/models/user.rb", "app/views/users/index.html.haml", "app/views/users/show.html.slim"]
      end
    end

    describe "output_terminal_errors" do
      it "should output errors in terminal" do
        check1 = Reviews::LawOfDemeterReview.new
        check2 = Reviews::UseQueryAttributeReview.new
        runner = Core::Runner.new(reviews: [check1, check2])
        check1.add_error "law of demeter", "app/models/user.rb", 10
        check2.add_error "use query attribute", "app/models/post.rb", 100
        subject.runner = runner
        subject.instance_variable_set("@options", {"without-color" => false})

        $origin_stdout = $stdout
        $stdout = StringIO.new
        subject.output_terminal_errors
        result = $stdout.string
        $stdout = $origin_stdout
        result.should == ["app/models/user.rb:10 - law of demeter".red, "app/models/post.rb:100 - use query attribute".red, "\nPlease go to http://rails-bestpractices.com to see more useful Rails Best Practices.".green, "\nFound 2 warnings.".red].join("\n") + "\n"
      end
    end

    describe 'parse_files' do

      it 'should not filter out all files when the path contains "vendor"' do
        Dir.mktmpdir { |random_dir|
          Dir.mkdir(File.join(random_dir, 'vendor'))
          Dir.mkdir(File.join(random_dir, 'vendor', 'my_project'))
          IO.write(File.join(random_dir, 'vendor', 'my_project', 'my_file.rb'), 'woot')
          analyzer = Analyzer.new(File.join(random_dir, 'vendor', 'my_project'))
          analyzer.parse_files.should be_include File.join(random_dir, 'vendor', 'my_project', 'my_file.rb')
        }
      end

      it 'should not filter out all files when the path contains "spec"' do
        Dir.mktmpdir { |random_dir|
          Dir.mkdir(File.join(random_dir, 'spec'))
          Dir.mkdir(File.join(random_dir, 'spec', 'my_project'))
          IO.write(File.join(random_dir, 'spec', 'my_project', 'my_file.rb'), 'woot')
          analyzer = Analyzer.new(File.join(random_dir, 'spec', 'my_project'))
          analyzer.parse_files.should be_include File.join(random_dir, 'spec', 'my_project', 'my_file.rb')
        }
      end

      it 'should not filter out all files when the path contains "test"' do
        Dir.mktmpdir { |random_dir|
          Dir.mkdir(File.join(random_dir, 'test'))
          Dir.mkdir(File.join(random_dir, 'test', 'my_project'))
          IO.write(File.join(random_dir, 'test', 'my_project', 'my_file.rb'), 'woot')
          analyzer = Analyzer.new(File.join(random_dir, 'test', 'my_project'))
          analyzer.parse_files.should be_include File.join(random_dir, 'test', 'my_project', 'my_file.rb')
        }
      end

      it 'should not filter out all files when the path contains "features"' do
        Dir.mktmpdir { |random_dir|
          Dir.mkdir(File.join(random_dir, 'test'))
          Dir.mkdir(File.join(random_dir, 'test', 'my_project'))
          IO.write(File.join(random_dir, 'test', 'my_project', 'my_file.rb'), 'woot')
          analyzer = Analyzer.new(File.join(random_dir, 'test', 'my_project'))
          analyzer.parse_files.should be_include File.join(random_dir, 'test', 'my_project', 'my_file.rb')
        }
      end

      it 'should not filter out all files when the path contains "tmp"' do
        Dir.mktmpdir { |random_dir|
          Dir.mkdir(File.join(random_dir, 'tmp'))
          Dir.mkdir(File.join(random_dir, 'tmp', 'my_project'))
          IO.write(File.join(random_dir, 'tmp', 'my_project', 'my_file.rb'), 'woot')
          analyzer = Analyzer.new(File.join(random_dir, 'tmp', 'my_project'))
          analyzer.parse_files.should be_include File.join(random_dir, 'tmp', 'my_project', 'my_file.rb')
        }
      end

    end

  end
end<commit_msg>IO.write does not work on ruby 1.9.2<commit_after>require 'spec_helper'
require 'tmpdir'

module RailsBestPractices
  describe Analyzer do
    subject { Analyzer.new(".") }

    describe '::new' do
      it 'should expand a relative path to an absolute' do
        subject.path.should eq File.expand_path('.')
      end
    end

    describe "expand_dirs_to_files" do
      it "should expand all files in spec directory" do
        dir = File.dirname(__FILE__)
        subject.expand_dirs_to_files(dir).should be_include(dir + '/analyzer_spec.rb')
      end
    end

    describe "file_sort" do
      it "should get models first, mailers, helpers and then others" do
        files = ["app/controllers/users_controller.rb", "app/mailers/user_mailer.rb", "app/helpers/users_helper.rb", "app/models/user.rb", "app/views/users/index.html.haml", "app/views/users/show.html.slim", "lib/user.rb"]
        subject.file_sort(files).should == ["app/models/user.rb", "app/mailers/user_mailer.rb", "app/helpers/users_helper.rb", "app/controllers/users_controller.rb", "app/views/users/index.html.haml", "app/views/users/show.html.slim", "lib/user.rb"]
      end
    end

    describe "file_ignore" do
      it "should ignore lib" do
        files = ["app/controllers/users_controller.rb", "app/mailers/user_mailer.rb", "app/models/user.rb", "app/views/users/index.html.haml", "app/views/users/show.html.slim", "lib/user.rb"]
        subject.file_ignore(files, 'lib/').should == ["app/controllers/users_controller.rb", "app/mailers/user_mailer.rb", "app/models/user.rb", "app/views/users/index.html.haml", "app/views/users/show.html.slim"]
      end
    end

    describe "output_terminal_errors" do
      it "should output errors in terminal" do
        check1 = Reviews::LawOfDemeterReview.new
        check2 = Reviews::UseQueryAttributeReview.new
        runner = Core::Runner.new(reviews: [check1, check2])
        check1.add_error "law of demeter", "app/models/user.rb", 10
        check2.add_error "use query attribute", "app/models/post.rb", 100
        subject.runner = runner
        subject.instance_variable_set("@options", {"without-color" => false})

        $origin_stdout = $stdout
        $stdout = StringIO.new
        subject.output_terminal_errors
        result = $stdout.string
        $stdout = $origin_stdout
        result.should == ["app/models/user.rb:10 - law of demeter".red, "app/models/post.rb:100 - use query attribute".red, "\nPlease go to http://rails-bestpractices.com to see more useful Rails Best Practices.".green, "\nFound 2 warnings.".red].join("\n") + "\n"
      end
    end

    describe 'parse_files' do

      it 'should not filter out all files when the path contains "vendor"' do
        Dir.mktmpdir { |random_dir|
          Dir.mkdir(File.join(random_dir, 'vendor'))
          Dir.mkdir(File.join(random_dir, 'vendor', 'my_project'))
          File.open(File.join(random_dir, 'vendor', 'my_project', 'my_file.rb'), "w") { |file| file << 'woot' }
          analyzer = Analyzer.new(File.join(random_dir, 'vendor', 'my_project'))
          analyzer.parse_files.should be_include File.join(random_dir, 'vendor', 'my_project', 'my_file.rb')
        }
      end

      it 'should not filter out all files when the path contains "spec"' do
        Dir.mktmpdir { |random_dir|
          Dir.mkdir(File.join(random_dir, 'spec'))
          Dir.mkdir(File.join(random_dir, 'spec', 'my_project'))
          File.open(File.join(random_dir, 'spec', 'my_project', 'my_file.rb'), "w") { |file| file << 'woot' }
          analyzer = Analyzer.new(File.join(random_dir, 'spec', 'my_project'))
          analyzer.parse_files.should be_include File.join(random_dir, 'spec', 'my_project', 'my_file.rb')
        }
      end

      it 'should not filter out all files when the path contains "test"' do
        Dir.mktmpdir { |random_dir|
          Dir.mkdir(File.join(random_dir, 'test'))
          Dir.mkdir(File.join(random_dir, 'test', 'my_project'))
          File.open(File.join(random_dir, 'test', 'my_project', 'my_file.rb'), "w") { |file| file << 'woot' }
          analyzer = Analyzer.new(File.join(random_dir, 'test', 'my_project'))
          analyzer.parse_files.should be_include File.join(random_dir, 'test', 'my_project', 'my_file.rb')
        }
      end

      it 'should not filter out all files when the path contains "features"' do
        Dir.mktmpdir { |random_dir|
          Dir.mkdir(File.join(random_dir, 'test'))
          Dir.mkdir(File.join(random_dir, 'test', 'my_project'))
          File.open(File.join(random_dir, 'test', 'my_project', 'my_file.rb'), "w") { |file| file << 'woot' }
          analyzer = Analyzer.new(File.join(random_dir, 'test', 'my_project'))
          analyzer.parse_files.should be_include File.join(random_dir, 'test', 'my_project', 'my_file.rb')
        }
      end

      it 'should not filter out all files when the path contains "tmp"' do
        Dir.mktmpdir { |random_dir|
          Dir.mkdir(File.join(random_dir, 'tmp'))
          Dir.mkdir(File.join(random_dir, 'tmp', 'my_project'))
          File.open(File.join(random_dir, 'tmp', 'my_project', 'my_file.rb'), "w") { |file| file << 'woot' }
          analyzer = Analyzer.new(File.join(random_dir, 'tmp', 'my_project'))
          analyzer.parse_files.should be_include File.join(random_dir, 'tmp', 'my_project', 'my_file.rb')
        }
      end

    end

  end
end
{'fraction_non_alphanumeric': 0.14514156711504092, 'fraction_numerical': 0.0034803875458564574, 'mean_word_length': 3.054919908466819, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, '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': '6428016', 'n_tokens_mistral': 3571, 'n_tokens_neox': 3472, 'n_words': 740}