Skip to content

Commit

Permalink
Add extracted Repository::Diff#touches_path
Browse files Browse the repository at this point in the history
  • Loading branch information
dgollahon authored and mbj committed May 21, 2023
1 parent 49ccd3e commit 16670ae
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 45 deletions.
14 changes: 11 additions & 3 deletions lib/mutant/repository/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
111 changes: 69 additions & 42 deletions spec/unit/mutant/repository/diff_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit 16670ae

Please sign in to comment.