<commit_before>require 'spec_helper'
require 'scss_lint/cli'

describe SCSSLint::CLI do
  let(:config_options) do
    {
      'linters' => {
        'FakeTestLinter1' => { 'enabled' => true },
        'FakeTestLinter2' => { 'enabled' => true },
      },
    }
  end

  let(:config) { SCSSLint::Config.new(config_options) }

  class SCSSLint::Linter::FakeTestLinter1 < SCSSLint::Linter; end
  class SCSSLint::Linter::FakeTestLinter2 < SCSSLint::Linter; end

  before do
    # Silence console output
    @output = ''
    STDOUT.stub(:write) { |*args| @output.<<(*args) }

    SCSSLint::Config.stub(:load).and_return(config)
    SCSSLint::LinterRegistry.stub(:linters)
                            .and_return([SCSSLint::Linter::FakeTestLinter1,
                                         SCSSLint::Linter::FakeTestLinter2])
  end

  describe '#parse_arguments' do
    let(:files)   { ['file1.scss', 'file2.scss'] }
    let(:flags) { [] }
    subject       { SCSSLint::CLI.new(flags + files) }

    def safe_parse
      subject.parse_arguments
    rescue SystemExit
      # Keep running tests
    end

    context 'when the config_file flag is set' do
      let(:config_file) { 'my-config-file.yml' }
      let(:flags) { ['-c', config_file] }

      it 'loads that config file' do
        SCSSLint::Config.should_receive(:load).with(config_file)
        safe_parse
      end

      context 'and the config file is invalid' do
        before do
          SCSSLint::Config.should_receive(:load)
                          .with(config_file)
                          .and_raise(SCSSLint::InvalidConfiguration)
        end

        it 'halts with a configuration error code' do
          subject.should_receive(:halt).with(:config)
          safe_parse
        end
      end
    end

    context 'when the excluded files flag is set' do
      let(:flags) { ['-e', 'file1.scss,file3.scss'] }

      it 'sets the :excluded_files option' do
        safe_parse
        subject.options[:excluded_files].should =~ ['file1.scss', 'file3.scss']
      end
    end

    context 'when the include linters flag is set' do
      let(:flags) { %w[-i FakeTestLinter2] }

      it 'enables only the included linters' do
        safe_parse
        subject.config.enabled_linters.should == [SCSSLint::Linter::FakeTestLinter2]
      end

      context 'and the included linter does not exist' do
        let(:flags) { %w[-i NonExistentLinter] }

        it 'halts with a configuration error code' do
          subject.should_receive(:halt).with(:config)
          safe_parse
        end
      end
    end

    context 'when the exclude linters flag is set' do
      let(:flags) { %w[-x FakeTestLinter1] }

      it 'includes all linters except the excluded one' do
        safe_parse
        subject.config.enabled_linters.should == \
          [SCSSLint::Linter::FakeTestLinter2]
      end
    end

    context 'when the require flag is set' do
      let(:flags) { %w[--require uri] }

      it 'requires the specified file and constants are accessible' do
        safe_parse
        proc { subject.instance_eval %{ URI } }.should_not raise_error
      end
    end

    context 'when neither format nor out flag is set' do
      let(:flags) { %w[] }

      it 'sets the default reporter to output to stdout' do
        safe_parse
        subject.options[:reporters].should \
          include([SCSSLint::Reporter::DefaultReporter, :stdout])
      end
    end

    context 'when the out flag is set' do
      context 'and the path is valid' do
        let(:flags) { %w[--out foo.txt] }

        it 'sets the default :output to the given path' do
          safe_parse
          subject.options[:reporters].should \
            include([SCSSLint::Reporter::DefaultReporter, 'foo.txt'])
        end
      end
    end

    context 'when the format flag is set' do
      context 'and the format is valid' do
        let(:flags) { %w[--format XML] }<commit_msg>Switch example to use expect syntax instead of proc<commit_after>require 'spec_helper'
require 'scss_lint/cli'

describe SCSSLint::CLI do
  let(:config_options) do
    {
      'linters' => {
        'FakeTestLinter1' => { 'enabled' => true },
        'FakeTestLinter2' => { 'enabled' => true },
      },
    }
  end

  let(:config) { SCSSLint::Config.new(config_options) }

  class SCSSLint::Linter::FakeTestLinter1 < SCSSLint::Linter; end
  class SCSSLint::Linter::FakeTestLinter2 < SCSSLint::Linter; end

  before do
    # Silence console output
    @output = ''
    STDOUT.stub(:write) { |*args| @output.<<(*args) }

    SCSSLint::Config.stub(:load).and_return(config)
    SCSSLint::LinterRegistry.stub(:linters)
                            .and_return([SCSSLint::Linter::FakeTestLinter1,
                                         SCSSLint::Linter::FakeTestLinter2])
  end

  describe '#parse_arguments' do
    let(:files)   { ['file1.scss', 'file2.scss'] }
    let(:flags) { [] }
    subject       { SCSSLint::CLI.new(flags + files) }

    def safe_parse
      subject.parse_arguments
    rescue SystemExit
      # Keep running tests
    end

    context 'when the config_file flag is set' do
      let(:config_file) { 'my-config-file.yml' }
      let(:flags) { ['-c', config_file] }

      it 'loads that config file' do
        SCSSLint::Config.should_receive(:load).with(config_file)
        safe_parse
      end

      context 'and the config file is invalid' do
        before do
          SCSSLint::Config.should_receive(:load)
                          .with(config_file)
                          .and_raise(SCSSLint::InvalidConfiguration)
        end

        it 'halts with a configuration error code' do
          subject.should_receive(:halt).with(:config)
          safe_parse
        end
      end
    end

    context 'when the excluded files flag is set' do
      let(:flags) { ['-e', 'file1.scss,file3.scss'] }

      it 'sets the :excluded_files option' do
        safe_parse
        subject.options[:excluded_files].should =~ ['file1.scss', 'file3.scss']
      end
    end

    context 'when the include linters flag is set' do
      let(:flags) { %w[-i FakeTestLinter2] }

      it 'enables only the included linters' do
        safe_parse
        subject.config.enabled_linters.should == [SCSSLint::Linter::FakeTestLinter2]
      end

      context 'and the included linter does not exist' do
        let(:flags) { %w[-i NonExistentLinter] }

        it 'halts with a configuration error code' do
          subject.should_receive(:halt).with(:config)
          safe_parse
        end
      end
    end

    context 'when the exclude linters flag is set' do
      let(:flags) { %w[-x FakeTestLinter1] }

      it 'includes all linters except the excluded one' do
        safe_parse
        subject.config.enabled_linters.should == \
          [SCSSLint::Linter::FakeTestLinter2]
      end
    end

    context 'when the require flag is set' do
      let(:flags) { %w[--require uri] }

      it 'requires the specified file and constants are accessible' do
        safe_parse
        expect { subject.instance_eval %{ URI } }.to_not raise_error
      end
    end

    context 'when neither format nor out flag is set' do
      let(:flags) { %w[] }

      it 'sets the default reporter to output to stdout' do
        safe_parse
        subject.options[:reporters].should \
          include([SCSSLint::Reporter::DefaultReporter, :stdout])
      end
    end

    context 'when the out flag is set' do
      context 'and the path is valid' do
        let(:flags) { %w[--out foo.txt] }

        it 'sets the default :output to the given path' do
          safe_parse
          subject.options[:reporters].should \
            include([SCSSLint::Reporter::DefaultReporter, 'foo.txt'])
        end
      end
    end

    context 'when the format flag is set' do
      context 'and the format is valid' do
        let(:flags) { %w[--format XML] }
{'fraction_non_alphanumeric': 0.10649087221095335, 'fraction_numerical': 0.004056795131845842, 'mean_word_length': 2.2775238886580804, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7430556', 'n_tokens_mistral': 2423, 'n_tokens_neox': 2360, 'n_words': 642}