diff --git a/lib/polish_geeks/dev_tools/commands/rubocop_rspec.rb b/lib/polish_geeks/dev_tools/commands/rubocop_rspec.rb index c14020f..e53d5c3 100644 --- a/lib/polish_geeks/dev_tools/commands/rubocop_rspec.rb +++ b/lib/polish_geeks/dev_tools/commands/rubocop_rspec.rb @@ -1,15 +1,20 @@ module PolishGeeks module DevTools module Commands - # Command wrapper for Rubocop Rspec validation - # It informs us if rspec specs are formatted in a proper way - class RubocopRspec < Rubocop + # Command wrapper for Rubocop validation + # It informs us if code is formatted in a proper way + class RubocopRspec < Base self.config_name = '.rubocop.yml' self.type = :validator self.validators = [ Validators::Rubocop ] + # Regexp used to get number of inspected files + FILES_REGEXP = /(\d+) files inspected/ + # Regexp used to get number of offenses detected + OFFENSES_REGEXP = /(\d+) offense.* detected/ + # Executes this command # @return [String] command output def execute @@ -19,10 +24,27 @@ def execute @output = Shell.new.execute(cmd.join(' ')) end + # @return [Boolean] true if there were no Rubocop offenses detected + def valid? + offenses_count == 0 + end + # @return [String] default label for this command def label "RubocopRspec (#{files_count} files, #{offenses_count} offenses)" end + + private + + # @return [Integer] number of files inspected + def files_count + output.scan(FILES_REGEXP).flatten.first.to_i + end + + # @return [Integer] number of offences found + def offenses_count + output.scan(OFFENSES_REGEXP).flatten.first.to_i + end end end end diff --git a/spec/lib/polish_geeks/dev_tools/commands/rubocop_rspec_spec.rb b/spec/lib/polish_geeks/dev_tools/commands/rubocop_rspec_spec.rb index ebce6ec..f9ba0c6 100644 --- a/spec/lib/polish_geeks/dev_tools/commands/rubocop_rspec_spec.rb +++ b/spec/lib/polish_geeks/dev_tools/commands/rubocop_rspec_spec.rb @@ -49,6 +49,32 @@ end end + describe '#valid?' do + context 'when offenses count is equal 0' do + before do + expect(subject) + .to receive(:offenses_count) + .and_return(0) + end + + it 'returns true' do + expect(subject.valid?).to eq true + end + end + + context 'when offenses count is different from 0' do + before do + expect(subject) + .to receive(:offenses_count) + .and_return(100) + end + + it 'returns false' do + expect(subject.valid?).to eq false + end + end + end + describe '#label' do context 'when we run rubocop' do before do @@ -65,6 +91,30 @@ end end + describe '#files_count' do + context 'when we count files' do + before do + subject.instance_variable_set(:@output, '10 files inspected') + end + + it 'returns a proper value' do + expect(subject.send(:files_count)).to eq 10 + end + end + end + + describe '#offenses_count' do + context 'when we count offenses' do + before do + subject.instance_variable_set(:@output, '5 offenses detected') + end + + it 'returns a proper value' do + expect(subject.send(:offenses_count)).to eq 5 + end + end + end + describe '.generator?' do it { expect(described_class.generator?).to eq false } end