<commit_before># encoding: utf-8

require 'spec_helper'

describe RuboCop::Config do
  include FileHelper

  subject(:configuration) { described_class.new(hash, loaded_path) }
  let(:hash) { {} }
  let(:loaded_path) { 'example/.rubocop.yml' }

  describe '#validate', :isolated_environment do
    # TODO: Because Config.load_file now outputs the validation warning,
    #       it is inserting text into the rspec test output here.
    #       The next 2 lines should be removed eventually.
    before(:each) { $stderr = StringIO.new }
    after(:each) { $stderr = STDERR }

    subject(:configuration) do
      RuboCop::ConfigLoader.load_file(configuration_path)
    end

    let(:configuration_path) { '.rubocop.yml' }

    context 'when the configuration includes any unrecognized cop name' do
      before do
        create_file(configuration_path, [
                      'LyneLenth:',
                      '  Enabled: true',
                      '  Max: 100'
                    ])
        $stderr = StringIO.new
      end

      after do
        $stderr = STDERR
      end

      it 'prints a warning message' do
        configuration # ConfigLoader.load_file will validate config
        expect($stderr.string).to match(/unrecognized cop LyneLenth/)
      end
    end

    context 'when the configuration includes an empty section' do
      before do
        create_file(configuration_path, ['Metrics/LineLength:'])
      end

      it 'raises validation error' do
        expect { configuration.validate }
          .to raise_error(described_class::ValidationError,
                          %r{^empty section Metrics/LineLength})
      end
    end

    context 'when the configuration is in the base RuboCop config folder' do
      before do
        create_file(configuration_path, [
                      'InvalidProperty:',
                      '  Enabled: true'
                    ])
        stub_const('RuboCop::ConfigLoader::RUBOCOP_HOME', rubocop_home_path)
      end

      let(:rubocop_home_path) { File.realpath('.') }
      let(:configuration_path) { 'config/.rubocop.yml' }

      it 'is not validated' do
        expect { configuration.validate }.to_not raise_error
      end
    end

    context 'when the configuration includes any unrecognized parameter' do
      before do
        create_file(configuration_path, [
                      'Metrics/LineLength:',
                      '  Enabled: true',
                      '  Min: 10'
                    ])
        $stderr = StringIO.new
      end

      after do
        $stderr = STDERR
      end

      it 'prints a warning message' do
        configuration # ConfigLoader.load_file will validate config
        expect($stderr.string).to match(
          %r{unrecognized parameter Metrics/LineLength:Min})
      end
    end

    context 'when the configuration includes any common parameter' do
      # Common parameters are parameters that are not in the default
      # configuration, but are nonetheless allowed for any cop.
      before do
        create_file(configuration_path, [
                      'Metrics/ModuleLength:',
                      '  Exclude:',
                      '    - lib/file.rb',
                      '  Include:',
                      '    - lib/file.xyz',
                      '  Severity: warning',
                      '  StyleGuide: https://example.com/some-style.html'
                    ])
      end

      it 'does not raise validation error' do
        expect { configuration.validate }.to_not raise_error
      end
    end
  end

  describe '#make_excludes_absolute' do
    context 'when config is in root directory' do
      let(:hash) do
        {
          'AllCops' => {
            'Exclude' => [
              'config/environment',
              'spec'
            ]
          }
        }
      end

      before do
        allow(configuration)
          .to receive(:base_dir_for_path_parameters)
          .and_return('/home/foo/project')
        configuration.make_excludes_absolute
      end

      it 'should generate valid absulute directory' do
        expect(configuration['AllCops']['Exclude'])
          .to eq [
            '/home/foo/project/config/environment',
            '/home/foo/project/spec'
          ]
      end
    end

    context 'when config is in subdirectory' do
      let(:hash) do
        {
          'AllCops' => {
            'Exclude' => [
              '../../config/environment',
              '../../spec'
            ]
          }
        }
      end

      before do
        allow(configuration)
          .to receive(:base_dir_for_path_parameters)
          .and_return('/home/foo/project/config/tools')
        configuration.make_excludes_absolute
      end

      it 'should generate valid absulute directory' do
        expect(configuration['AllCops']['Exclude'])
          .to eq [
            '/home/foo/project/config/environment',
            '/home/foo/project/spec'
          ]
      end
    end
  end

  describe '#file_to_include?' do
    let(:hash) do
      {<commit_msg>Fix typo in config_spec.rb<commit_after># encoding: utf-8

require 'spec_helper'

describe RuboCop::Config do
  include FileHelper

  subject(:configuration) { described_class.new(hash, loaded_path) }
  let(:hash) { {} }
  let(:loaded_path) { 'example/.rubocop.yml' }

  describe '#validate', :isolated_environment do
    # TODO: Because Config.load_file now outputs the validation warning,
    #       it is inserting text into the rspec test output here.
    #       The next 2 lines should be removed eventually.
    before(:each) { $stderr = StringIO.new }
    after(:each) { $stderr = STDERR }

    subject(:configuration) do
      RuboCop::ConfigLoader.load_file(configuration_path)
    end

    let(:configuration_path) { '.rubocop.yml' }

    context 'when the configuration includes any unrecognized cop name' do
      before do
        create_file(configuration_path, [
                      'LyneLenth:',
                      '  Enabled: true',
                      '  Max: 100'
                    ])
        $stderr = StringIO.new
      end

      after do
        $stderr = STDERR
      end

      it 'prints a warning message' do
        configuration # ConfigLoader.load_file will validate config
        expect($stderr.string).to match(/unrecognized cop LyneLenth/)
      end
    end

    context 'when the configuration includes an empty section' do
      before do
        create_file(configuration_path, ['Metrics/LineLength:'])
      end

      it 'raises validation error' do
        expect { configuration.validate }
          .to raise_error(described_class::ValidationError,
                          %r{^empty section Metrics/LineLength})
      end
    end

    context 'when the configuration is in the base RuboCop config folder' do
      before do
        create_file(configuration_path, [
                      'InvalidProperty:',
                      '  Enabled: true'
                    ])
        stub_const('RuboCop::ConfigLoader::RUBOCOP_HOME', rubocop_home_path)
      end

      let(:rubocop_home_path) { File.realpath('.') }
      let(:configuration_path) { 'config/.rubocop.yml' }

      it 'is not validated' do
        expect { configuration.validate }.to_not raise_error
      end
    end

    context 'when the configuration includes any unrecognized parameter' do
      before do
        create_file(configuration_path, [
                      'Metrics/LineLength:',
                      '  Enabled: true',
                      '  Min: 10'
                    ])
        $stderr = StringIO.new
      end

      after do
        $stderr = STDERR
      end

      it 'prints a warning message' do
        configuration # ConfigLoader.load_file will validate config
        expect($stderr.string).to match(
          %r{unrecognized parameter Metrics/LineLength:Min})
      end
    end

    context 'when the configuration includes any common parameter' do
      # Common parameters are parameters that are not in the default
      # configuration, but are nonetheless allowed for any cop.
      before do
        create_file(configuration_path, [
                      'Metrics/ModuleLength:',
                      '  Exclude:',
                      '    - lib/file.rb',
                      '  Include:',
                      '    - lib/file.xyz',
                      '  Severity: warning',
                      '  StyleGuide: https://example.com/some-style.html'
                    ])
      end

      it 'does not raise validation error' do
        expect { configuration.validate }.to_not raise_error
      end
    end
  end

  describe '#make_excludes_absolute' do
    context 'when config is in root directory' do
      let(:hash) do
        {
          'AllCops' => {
            'Exclude' => [
              'config/environment',
              'spec'
            ]
          }
        }
      end

      before do
        allow(configuration)
          .to receive(:base_dir_for_path_parameters)
          .and_return('/home/foo/project')
        configuration.make_excludes_absolute
      end

      it 'should generate valid absulute directory' do
        expect(configuration['AllCops']['Exclude'])
          .to eq [
            '/home/foo/project/config/environment',
            '/home/foo/project/spec'
          ]
      end
    end

    context 'when config is in subdirectory' do
      let(:hash) do
        {
          'AllCops' => {
            'Exclude' => [
              '../../config/environment',
              '../../spec'
            ]
          }
        }
      end

      before do
        allow(configuration)
          .to receive(:base_dir_for_path_parameters)
          .and_return('/home/foo/project/config/tools')
        configuration.make_excludes_absolute
      end

      it 'should generate valid absolute directory' do
        expect(configuration['AllCops']['Exclude'])
          .to eq [
            '/home/foo/project/config/environment',
            '/home/foo/project/spec'
          ]
      end
    end
  end

  describe '#file_to_include?' do
    let(:hash) do
      {
{'fraction_non_alphanumeric': 0.08952719376172145, 'fraction_numerical': 0.0013818971473694602, 'mean_word_length': 1.6953977121574888, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '2856146', 'n_tokens_mistral': 2751, 'n_tokens_neox': 2614, 'n_words': 691}