|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | 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 |
10 | 18 |
|
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 |
15 | 36 |
|
16 | | - let(:world) do |
17 | | - instance_double( |
18 | | - Mutant::World, |
19 | | - kernel: kernel, |
20 | | - pathname: pathname |
21 | | - ) |
22 | | - end |
| 37 | + let(:file_diff_expectations) { [] } |
23 | 38 |
|
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)] |
28 | 42 | end |
| 43 | + end |
29 | 44 |
|
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 |
49 | 48 |
|
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) |
52 | 52 | end |
53 | 53 |
|
54 | 54 | context 'when file is not touched in the diff' do |
@@ -123,4 +123,31 @@ def apply |
123 | 123 | end |
124 | 124 | end |
125 | 125 | 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 |
126 | 153 | end |
0 commit comments