diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index b5fa389..ceac3d2 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -5,7 +5,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby: ['3.2', '3.0', '2.7'] + ruby: ['3.3', '3.0'] steps: - uses: actions/checkout@v3 with: diff --git a/.rubocop.yml b/.rubocop.yml index 1c92c79..faec42a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,8 +1,9 @@ AllCops: NewCops: enable - TargetRubyVersion: 2.7.0 + TargetRubyVersion: 3.0 Exclude: - spec/fixtures/**/* + SuggestExtensions: false Style/RegexpLiteral: EnforcedStyle: slashes diff --git a/Gemfile b/Gemfile index 7f4f5e9..b710dce 100644 --- a/Gemfile +++ b/Gemfile @@ -3,3 +3,13 @@ source 'https://rubygems.org' gemspec + +gem 'bundler' +gem 'pry' +gem 'rake', '~> 13.0' +gem 'rspec', '~> 3.0' +gem 'rubocop' +gem 'simplecov' +gem 'simplecov-html' +gem 'simplecov-lcov', '~> 0.8' +gem 'timecop' diff --git a/lib/undercover/changeset.rb b/lib/undercover/changeset.rb index 78f2309..eac6a95 100644 --- a/lib/undercover/changeset.rb +++ b/lib/undercover/changeset.rb @@ -56,10 +56,10 @@ def each_changed_line end # TODO: refactor to a standalone validator (depending on changeset AND lcov) - # TODO: add specs def validate(lcov_report_path) return :no_changes if files.empty? - return :stale_coverage if last_modified > File.mtime(lcov_report_path) + + :stale_coverage if last_modified > File.mtime(lcov_report_path) end private diff --git a/lib/undercover/formatter.rb b/lib/undercover/formatter.rb index 28b7256..7e011bc 100644 --- a/lib/undercover/formatter.rb +++ b/lib/undercover/formatter.rb @@ -17,21 +17,21 @@ def to_s def formatted_warnings @results.map.with_index(1) do |res, idx| "🚨 #{idx}) node `#{res.node.name}` type: #{res.node.human_name},\n" + - (' ' * pad_size) + "loc: #{res.file_path_with_lines}," \ - " coverage: #{res.coverage_f * 100}%\n" + + (' ' * pad_size) + "loc: #{res.file_path_with_lines}, " \ + "coverage: #{res.coverage_f * 100}%\n" + res.pretty_print end end def success - "#{Rainbow('undercover').bold.green}: ✅ No coverage" \ - ' is missing in latest changes' + "#{Rainbow('undercover').bold.green}: ✅ No coverage " \ + 'is missing in latest changes' end def warnings_header "#{Rainbow('undercover').bold.red}: " \ - '👮‍♂️ some methods have no test coverage! Please add specs for' \ - ' methods listed below' + '👮‍♂️ some methods have no test coverage! Please add specs for ' \ + 'methods listed below' end def pad_size diff --git a/lib/undercover/result.rb b/lib/undercover/result.rb index 3f59bb8..864bf41 100644 --- a/lib/undercover/result.rb +++ b/lib/undercover/result.rb @@ -54,7 +54,7 @@ def coverage_f lines[ln] = 0 end end - return 1.0 if lines.keys.size.zero? + return 1.0 if lines.keys.empty? (lines.values.sum.to_f / lines.keys.size).round(4) end @@ -84,7 +84,7 @@ def pretty_print pad = node.last_line.to_s.length pretty_print_lines.map do |covered, (num, line)| formatted_line = "#{num.to_s.rjust(pad)}: #{line}" - if line.strip.length.zero? + if line.strip.empty? Rainbow(formatted_line).darkgray.dark elsif covered.nil? Rainbow(formatted_line).darkgray.dark + \ @@ -107,8 +107,8 @@ def file_path_with_lines end def inspect - "#" + "#" end alias to_s inspect @@ -119,7 +119,7 @@ def count_covered_branches(line_number) branches = coverage.select { |cov| cov.size == 4 && cov[0] == line_number } count_covered = branches.count { |cov| cov[3].positive? } - return '' if branches.size.zero? + return '' if branches.empty? if count_covered < branches.size Rainbow(' branches: ').italic.darkgray.dark + \ diff --git a/spec/changeset_spec.rb b/spec/changeset_spec.rb index 3fadb9a..5a4c7f3 100644 --- a/spec/changeset_spec.rb +++ b/spec/changeset_spec.rb @@ -47,4 +47,32 @@ changeset = Undercover::Changeset.new('spec/fixtures/test.git', 'master') expect(changeset.last_modified).to eq(Undercover::Changeset::T_ZERO) end + + describe 'validate' do + let(:report_path) { 'spec/fixtures/sample.lcov' } + + it 'returns :no_changes with empty files' do + changeset = Undercover::Changeset.new('spec/fixtures/test.git', 'master') # no update + expect(changeset.validate(report_path)).to eq(:no_changes) + end + + it 'returns :stale_coverage if coverage report is older than last file change' do + changeset = Undercover::Changeset.new('spec/fixtures/test.git', 'master').update + + Timecop.freeze do + file_paths = changeset.file_paths.map { |p| "spec/fixtures/#{p}" } + FileUtils.touch(file_paths, mtime: Time.now) + FileUtils.touch(report_path, mtime: Time.now - 60) + end + + expect(changeset.validate(report_path)).to eq(:stale_coverage) + end + + it 'returns nil with no validation errors' do + changeset = Undercover::Changeset.new('spec/fixtures/test.git', 'master').update + FileUtils.touch(report_path, mtime: Time.now) + + expect(changeset.validate(report_path)).to be_nil + end + end end diff --git a/spec/result_spec.rb b/spec/result_spec.rb index 3de2ff3..74dced9 100644 --- a/spec/result_spec.rb +++ b/spec/result_spec.rb @@ -31,6 +31,13 @@ expect(result.coverage_f).to eq(0.0) end + it 'has a fiendly #inspect' do + node = ast.find_all(with_name('foo')).first + result = described_class.new(node, coverage, 'class.rb') + + expect(result.to_s).to match(/#= 0.1.8' spec.add_dependency 'rainbow', '>= 2.1', '< 4.0' - spec.add_dependency 'rugged', '>= 0.27', '< 1.7' - - spec.add_development_dependency 'bundler' - spec.add_development_dependency 'pry' - spec.add_development_dependency 'rake', '~> 13.0' - spec.add_development_dependency 'rspec', '~> 3.0' - spec.add_development_dependency 'rubocop', '~> 1.28.2' - spec.add_development_dependency 'simplecov' - spec.add_development_dependency 'simplecov-html' - spec.add_development_dependency 'simplecov-lcov', '~> 0.8' - spec.add_development_dependency 'timecop' + spec.add_dependency 'rugged', '>= 0.27', '< 1.8' end -# rubocop:enable Metrics/BlockLength