From 2f9ff01b01cf7953e51e86185a4360aef7366a19 Mon Sep 17 00:00:00 2001 From: Jan Grodowski Date: Mon, 8 Jan 2024 21:05:27 +0100 Subject: [PATCH] Improve specs based on undercover --- lib/undercover/changeset.rb | 1 - spec/changeset_spec.rb | 28 ++++++++++++++++++++++++++++ spec/result_spec.rb | 7 +++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/undercover/changeset.rb b/lib/undercover/changeset.rb index c7b9316..eac6a95 100644 --- a/lib/undercover/changeset.rb +++ b/lib/undercover/changeset.rb @@ -56,7 +56,6 @@ 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? 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 74bb44b..85914f0 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 eq('#') + end + context 'for an empty module def' do let(:ast) { Imagen.from_local('spec/fixtures/empty_class_def.rb') } let(:lcov) do