From 16670ae69ac9732f9d4731be5bea5f845e4ffda0 Mon Sep 17 00:00:00 2001 From: Daniel Gollahon Date: Sun, 21 May 2023 16:58:47 +0000 Subject: [PATCH] Add extracted Repository::Diff#touches_path --- lib/mutant/repository/diff.rb | 14 ++- spec/unit/mutant/repository/diff_spec.rb | 111 ++++++++++++++--------- 2 files changed, 80 insertions(+), 45 deletions(-) diff --git a/lib/mutant/repository/diff.rb b/lib/mutant/repository/diff.rb index cdbf47c6b..c85332755 100644 --- a/lib/mutant/repository/diff.rb +++ b/lib/mutant/repository/diff.rb @@ -22,12 +22,16 @@ class Error < RuntimeError; end # @raise [RepositoryError] # when git command failed def touches?(path, line_range) - touched_paths - .from_right { |message| fail Error, message } - .fetch(path) { return false } + touched_path(path) { return false } .touches?(line_range) end + def touches_path?(path) + touched_path(path) { return false } + + true + end + private def repository_root @@ -37,6 +41,10 @@ def repository_root .fmap(&world.pathname.public_method(:new)) end + def touched_path(path, &block) + touched_paths.from_right { |message| fail Error, message }.fetch(path, &block) + end + def touched_paths repository_root.bind(&method(:diff_index)) end diff --git a/spec/unit/mutant/repository/diff_spec.rb b/spec/unit/mutant/repository/diff_spec.rb index b164ab313..79114936a 100644 --- a/spec/unit/mutant/repository/diff_spec.rb +++ b/spec/unit/mutant/repository/diff_spec.rb @@ -1,54 +1,54 @@ # frozen_string_literal: true describe Mutant::Repository::Diff do - describe '#touches?' do - def apply - subject.touches?(path, line_range) - end - - subject { described_class.new(world: world, to: 'to_rev') } + subject { described_class.new(world: world, to: 'to_rev') } + + let(:kernel) { class_double(Kernel) } + let(:line_range) { 4..5 } + let(:path) { Pathname.new('/foo/bar.rb') } + let(:pathname) { class_double(Pathname) } + + let(:world) do + instance_double( + Mutant::World, + kernel: kernel, + pathname: pathname + ) + end - let(:kernel) { class_double(Kernel) } - let(:line_range) { 4..5 } - let(:path) { Pathname.new('/foo/bar.rb') } - let(:pathname) { class_double(Pathname) } + let(:raw_expectations) do + [ + { + receiver: world, + selector: :capture_stdout, + arguments: [%w[git rev-parse --show-toplevel]], + reaction: { return: Mutant::Either::Right.new("/foo\n") } + }, + { + receiver: world, + selector: :capture_stdout, + arguments: [%w[git diff-index to_rev]], + reaction: { return: Mutant::Either::Right.new(index_stdout) } + }, + *file_diff_expectations + ] + end - let(:world) do - instance_double( - Mutant::World, - kernel: kernel, - pathname: pathname - ) - end + let(:file_diff_expectations) { [] } - let(:allowed_paths) do - %w[/foo bar.rb baz.rb].to_h do |path| - [path, Pathname.new(path)] - end + let(:allowed_paths) do + %w[/foo bar.rb baz.rb].to_h do |path| + [path, Pathname.new(path)] end + end - let(:file_diff_expectations) { [] } - - let(:raw_expectations) do - [ - { - receiver: world, - selector: :capture_stdout, - arguments: [%w[git rev-parse --show-toplevel]], - reaction: { return: Mutant::Either::Right.new("/foo\n") } - }, - { - receiver: world, - selector: :capture_stdout, - arguments: [%w[git diff-index to_rev]], - reaction: { return: Mutant::Either::Right.new(index_stdout) } - }, - *file_diff_expectations - ] - end + before do + allow(pathname).to receive(:new, &allowed_paths.public_method(:fetch)) + end - before do - allow(pathname).to receive(:new, &allowed_paths.public_method(:fetch)) + describe '#touches?' do + def apply + subject.touches?(path, line_range) end context 'when file is not touched in the diff' do @@ -123,4 +123,31 @@ def apply end end end + + describe '#touches_path?' do + def apply + subject.touches_path?(path) + end + + context 'when file is not touched in the diff' do + let(:index_stdout) { '' } + + it 'returns false' do + verify_events { expect(apply).to be(false) } + end + end + + context 'when file is touched in the diff' do + let(:index_stdout) do + <<~STR + :000000 000000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 M\tbar.rb + :000000 000000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 M\tbaz.rb + STR + end + + it 'returns true' do + verify_events { expect(apply).to be(true) } + end + end + end end