Skip to content

Commit 5cae54d

Browse files
committed
Refactoring towards Diff, step 7
1 parent 81e9cc3 commit 5cae54d

File tree

4 files changed

+37
-63
lines changed

4 files changed

+37
-63
lines changed

lib/files_in_my_diff/commit.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ def initialize(folder:, revision:, file_strategy: TmpDir::FileStrategy, git_stra
1212

1313
def call
1414
validate_folder!
15-
validate_revision!
16-
sha = @git_strategy.object.sha
17-
@git_strategy.diff
18-
dir = @file_strategy.create_tmp_dir(sha)
19-
{ dir:, sha: }
15+
diff = @git_strategy.diff(@revision)
16+
diff.validate!
17+
diff.changes
18+
dir = @file_strategy.create_tmp_dir(diff.sha)
19+
{ dir:, sha: diff.sha }
2020
end
2121

2222
private

lib/files_in_my_diff/git.rb

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,24 @@ def changes
2626
end
2727

2828
class Adapter
29-
def object(revision = nil)
30-
@object ||= if revision.is_a?(::Git::Object::AbstractObject)
31-
@repo.object(revision.sha)
32-
else
33-
@repo.object(revision)
34-
end
35-
rescue ::Git::FailedError
36-
nil
37-
end
38-
3929
def initialize(folder:, repo: ::Git.open(folder))
4030
@repo = repo
4131
end
4232

43-
def revision_exists?(revision)
44-
@object = object(revision)
45-
@object && true
46-
rescue ::Git::FailedError
47-
false
48-
end
49-
50-
def diff2(revision)
33+
def diff(revision)
5134
Diff.new(object: object(revision), revision:)
5235
end
5336

54-
def diff
55-
@object.diff_parent.map do |change|
56-
{ path: change.path, type: change.type }
37+
private
38+
39+
def object(revision)
40+
if revision.is_a?(::Git::Object::AbstractObject)
41+
@repo.object(revision.sha)
42+
else
43+
@repo.object(revision)
5744
end
58-
rescue ::Git::FailedError => e
59-
raise DiffError, "Failed to get diff for #{@object.sha}: #{e.message}"
45+
rescue ::Git::FailedError
46+
nil
6047
end
6148
end
6249
end

test/commit/main_test.rb

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ def test_that_main_validates_that_revision_exists
2323
end
2424
end
2525

26-
def test_that_main_accepts_a_git_object_as_a_revision
27-
refute_nil subject(revision: GitObjectStub.new).call
28-
end
29-
30-
def test_that_main_accepts_an_integer_version_as_a_revision
31-
refute_nil subject(revision: 32).call
32-
end
33-
34-
def test_that_main_accepts_a_relative_revision
35-
refute_nil subject(revision: 'HEAD~1').call
36-
end
37-
3826
def test_that_error_on_tmp_dir_resolution_is_propagated
3927
file_strategy = file_strategy_stub(create_success: false)
4028
assert_raises(TmpDir::DirectoryError) do
@@ -59,17 +47,8 @@ def file_strategy_stub(dir_exists: true, create_success: true)
5947
FileStrategyStub.new(dir_exists, create_success)
6048
end
6149

62-
def git_strategy_stub(revision_exists: true, object: GitObjectStub.new, diff_success: true)
63-
GitStrategyStub.new(revision_exists, object, diff_success)
64-
end
65-
66-
class GitObjectStub < ::Git::Object::AbstractObject
67-
attr_reader :sha
68-
69-
def initialize(sha: 'abc')
70-
super(nil, nil)
71-
@sha = sha
72-
end
50+
def git_strategy_stub(revision_exists: true, diff_success: true)
51+
GitStrategyStub.new(revision_exists, diff_success)
7352
end
7453

7554
class FileStrategyStub
@@ -90,22 +69,30 @@ def create_tmp_dir(_sha)
9069
end
9170

9271
class GitStrategyStub
93-
attr_reader :object
94-
95-
def initialize(revision_exists, object, diff_success)
72+
def initialize(revision_exists, diff_success)
9673
@revision_exists = revision_exists
97-
@object = object
9874
@diff_success = diff_success
9975
end
10076

101-
def revision_exists?(_revision)
102-
@revision_exists
77+
def diff(_revision)
78+
return InvalidDiffStub unless @revision_exists
79+
raise Git::DiffError unless @diff_success
80+
81+
ValidDiffStub
10382
end
83+
end
10484

105-
def diff
106-
raise Git::DiffError unless @diff_success
85+
module InvalidDiffStub
86+
def self.validate!
87+
raise ValidationError
88+
end
89+
end
10790

108-
{ 'file_1' => :changed, 'file_2' => :deleted, 'file_3' => :added }
91+
module ValidDiffStub
92+
class << self
93+
def validate! = true
94+
def sha = 'abcd1234'
95+
def changes = []
10996
end
11097
end
11198
end

test/git/adapter_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ module FilesInMyDiff
66
module Git
77
class AdapterTest < Minitest::Test
88
def test_it_returns_a_diff_instance
9-
assert_instance_of Diff, subject.diff2('x')
9+
assert_instance_of Diff, subject.diff('x')
1010
end
1111

1212
def test_that_diff_is_evaluated_by_sha_for_git_objects
1313
mock = GitRepoMock.new
1414
sha = 'y'
15-
subject(repo: mock).diff2(GitObjectStub.new(sha:))
15+
subject(repo: mock).diff(GitObjectStub.new(sha:))
1616

1717
assert_equal mock.object_called_with, sha
1818
end
1919

2020
def test_that_diff_is_evaluated_by_revision_for_strings
2121
mock = GitRepoMock.new
2222
revision = 'HEAD~1'
23-
subject(repo: mock).diff2(revision)
23+
subject(repo: mock).diff(revision)
2424

2525
assert_equal mock.object_called_with, revision
2626
end

0 commit comments

Comments
 (0)