Skip to content

Commit 16670ae

Browse files
dgollahonmbj
authored andcommitted
Add extracted Repository::Diff#touches_path
1 parent 49ccd3e commit 16670ae

File tree

2 files changed

+80
-45
lines changed

2 files changed

+80
-45
lines changed

lib/mutant/repository/diff.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ class Error < RuntimeError; end
2222
# @raise [RepositoryError]
2323
# when git command failed
2424
def touches?(path, line_range)
25-
touched_paths
26-
.from_right { |message| fail Error, message }
27-
.fetch(path) { return false }
25+
touched_path(path) { return false }
2826
.touches?(line_range)
2927
end
3028

29+
def touches_path?(path)
30+
touched_path(path) { return false }
31+
32+
true
33+
end
34+
3135
private
3236

3337
def repository_root
@@ -37,6 +41,10 @@ def repository_root
3741
.fmap(&world.pathname.public_method(:new))
3842
end
3943

44+
def touched_path(path, &block)
45+
touched_paths.from_right { |message| fail Error, message }.fetch(path, &block)
46+
end
47+
4048
def touched_paths
4149
repository_root.bind(&method(:diff_index))
4250
end

spec/unit/mutant/repository/diff_spec.rb

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
11
# frozen_string_literal: true
22

33
describe Mutant::Repository::Diff do
4-
describe '#touches?' do
5-
def apply
6-
subject.touches?(path, line_range)
7-
end
8-
9-
subject { described_class.new(world: world, to: 'to_rev') }
4+
subject { described_class.new(world: world, to: 'to_rev') }
5+
6+
let(:kernel) { class_double(Kernel) }
7+
let(:line_range) { 4..5 }
8+
let(:path) { Pathname.new('/foo/bar.rb') }
9+
let(:pathname) { class_double(Pathname) }
10+
11+
let(:world) do
12+
instance_double(
13+
Mutant::World,
14+
kernel: kernel,
15+
pathname: pathname
16+
)
17+
end
1018

11-
let(:kernel) { class_double(Kernel) }
12-
let(:line_range) { 4..5 }
13-
let(:path) { Pathname.new('/foo/bar.rb') }
14-
let(:pathname) { class_double(Pathname) }
19+
let(:raw_expectations) do
20+
[
21+
{
22+
receiver: world,
23+
selector: :capture_stdout,
24+
arguments: [%w[git rev-parse --show-toplevel]],
25+
reaction: { return: Mutant::Either::Right.new("/foo\n") }
26+
},
27+
{
28+
receiver: world,
29+
selector: :capture_stdout,
30+
arguments: [%w[git diff-index to_rev]],
31+
reaction: { return: Mutant::Either::Right.new(index_stdout) }
32+
},
33+
*file_diff_expectations
34+
]
35+
end
1536

16-
let(:world) do
17-
instance_double(
18-
Mutant::World,
19-
kernel: kernel,
20-
pathname: pathname
21-
)
22-
end
37+
let(:file_diff_expectations) { [] }
2338

24-
let(:allowed_paths) do
25-
%w[/foo bar.rb baz.rb].to_h do |path|
26-
[path, Pathname.new(path)]
27-
end
39+
let(:allowed_paths) do
40+
%w[/foo bar.rb baz.rb].to_h do |path|
41+
[path, Pathname.new(path)]
2842
end
43+
end
2944

30-
let(:file_diff_expectations) { [] }
31-
32-
let(:raw_expectations) do
33-
[
34-
{
35-
receiver: world,
36-
selector: :capture_stdout,
37-
arguments: [%w[git rev-parse --show-toplevel]],
38-
reaction: { return: Mutant::Either::Right.new("/foo\n") }
39-
},
40-
{
41-
receiver: world,
42-
selector: :capture_stdout,
43-
arguments: [%w[git diff-index to_rev]],
44-
reaction: { return: Mutant::Either::Right.new(index_stdout) }
45-
},
46-
*file_diff_expectations
47-
]
48-
end
45+
before do
46+
allow(pathname).to receive(:new, &allowed_paths.public_method(:fetch))
47+
end
4948

50-
before do
51-
allow(pathname).to receive(:new, &allowed_paths.public_method(:fetch))
49+
describe '#touches?' do
50+
def apply
51+
subject.touches?(path, line_range)
5252
end
5353

5454
context 'when file is not touched in the diff' do
@@ -123,4 +123,31 @@ def apply
123123
end
124124
end
125125
end
126+
127+
describe '#touches_path?' do
128+
def apply
129+
subject.touches_path?(path)
130+
end
131+
132+
context 'when file is not touched in the diff' do
133+
let(:index_stdout) { '' }
134+
135+
it 'returns false' do
136+
verify_events { expect(apply).to be(false) }
137+
end
138+
end
139+
140+
context 'when file is touched in the diff' do
141+
let(:index_stdout) do
142+
<<~STR
143+
:000000 000000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 M\tbar.rb
144+
:000000 000000 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 M\tbaz.rb
145+
STR
146+
end
147+
148+
it 'returns true' do
149+
verify_events { expect(apply).to be(true) }
150+
end
151+
end
152+
end
126153
end

0 commit comments

Comments
 (0)