Skip to content

Commit aa56519

Browse files
committed
refactor: revert changes to make path normalization optional
Revert the changes introduced in #64 that allowed the user of this lib to make path normalization optional. The purpose of #64 was to make tests easier to write. The downside was that the library code itself became more complicated. Instead this change removes that and adds mocking for path normalization when it is appropriate to shut it off for tests.
1 parent c8b07f6 commit aa56519

File tree

6 files changed

+70
-110
lines changed

6 files changed

+70
-110
lines changed

lib/ruby_git/repository.rb

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,19 @@ class Repository
2525
# RubyGit::Repository.new('/path/to/repository') #=> #<RubyGit::Repository ...>
2626
#
2727
# @param [String] repository_path the path to the repository
28-
# @param normalize_path [Boolean] if true, path is converted to an absolute path to the root of the working tree
2928
#
3029
# The purpose of this flag is to allow tests to not have to mock the
3130
# normalization of the path. This allows testing that the right git command
3231
# is contructed based on the options passed any particular method.
3332
#
3433
# @raise [ArgumentError] if the path is not a directory
3534
#
36-
def initialize(repository_path, normalize_path: true)
37-
@normalize_path = normalize_path
38-
39-
@path =
40-
if normalize_path?
41-
normalize_path(repository_path)
42-
else
43-
repository_path
44-
end
35+
def initialize(repository_path)
36+
@path = normalize_path(repository_path)
4537
end
4638

4739
private
4840

49-
# true if the path should be expanded and converted to a absolute, real path
50-
# @return [Boolean]
51-
# @api private
52-
def normalize_path? = @normalize_path
53-
5441
# Expand and convert the given path to an absolute, real path
5542
#
5643
# @example Expand the path

lib/ruby_git/worktree.rb

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ class Worktree
3434
#
3535
# @return [RubyGit::Worktree] the working tree whose root is at `path`
3636
#
37-
def self.init(worktree_path, normalize_path: true)
37+
def self.init(worktree_path)
3838
raise RubyGit::Error, "Path '#{worktree_path}' not valid." unless File.directory?(worktree_path)
3939

4040
command = ['init']
4141
options = { chdir: worktree_path, out: StringIO.new, err: StringIO.new }
4242
RubyGit::CommandLine.run(*command, **options)
4343

44-
new(worktree_path, normalize_path:)
44+
new(worktree_path)
4545
end
4646

4747
# Open an existing Git working tree that contains worktree_path
@@ -58,8 +58,8 @@ def self.init(worktree_path, normalize_path: true)
5858
#
5959
# @return [RubyGit::Worktree] the Git working tree that contains `worktree_path`
6060
#
61-
def self.open(worktree_path, normalize_path: true)
62-
new(worktree_path, normalize_path:)
61+
def self.open(worktree_path)
62+
new(worktree_path)
6363
end
6464

6565
# Copy the remote repository and checkout the default branch
@@ -97,12 +97,12 @@ def self.open(worktree_path, normalize_path: true)
9797
#
9898
# @return [RubyGit::Worktree] the Git working tree checked out from the cloned repository
9999
#
100-
def self.clone(repository_url, to_path: nil, normalize_path: true)
100+
def self.clone(repository_url, to_path: nil)
101101
command = ['clone', '--', repository_url]
102102
command << to_path if to_path
103103
options = { out: StringIO.new, err: StringIO.new }
104104
clone_output = RubyGit::CommandLine.run(*command, **options).stderr
105-
new(cloned_to(clone_output), normalize_path:)
105+
new(cloned_to(clone_output))
106106
end
107107

108108
# Show the working tree and index status
@@ -195,21 +195,27 @@ def add(*pathspecs, all: false, force: false, refresh: false, update: false) # r
195195
# @return [RubyGit::Repository] the repository associated with the worktree
196196
#
197197
def repository
198-
@repository ||= begin
199-
command = %w[rev-parse --git-dir]
200-
options = { chdir: path, chomp: true, out: StringIO.new, err: StringIO.new }
201-
# rev-parse path might be relative to the worktree, thus the need to expand it
202-
git_dir = File.realpath(RubyGit::CommandLine.run(*command, **options).stdout, path)
203-
Repository.new(git_dir, normalize_path: normalize_path?)
204-
end
198+
@repository ||= Repository.new(repository_path)
205199
end
206200

207201
private
208202

203+
# The path to the repository associated with this worktree
204+
#
205+
# @return [String]
206+
#
207+
# @api private
208+
#
209+
def repository_path
210+
command = %w[rev-parse --git-dir]
211+
options = { chdir: path, chomp: true, out: StringIO.new, err: StringIO.new }
212+
# rev-parse path might be relative to the worktree, thus the need to expand it
213+
File.realpath(RubyGit::CommandLine.run(*command, **options).stdout, path)
214+
end
215+
209216
# Create a Worktree object
210217
#
211218
# @param worktree_path [String] a path anywhere in the worktree
212-
# @param normalize_path [Boolean] if true, path is converted to an absolute path to the root of the working tree
213219
#
214220
# The purpose of this flag is to allow tests to not have to mock the
215221
# normalization of the path. This allows testing that the right git command
@@ -221,16 +227,8 @@ def repository
221227
# @return [RubyGit::Worktree] the worktree whose root is at `path`
222228
# @api private
223229
#
224-
def initialize(worktree_path, normalize_path: true)
225-
@normalize_path = normalize_path
226-
227-
@path =
228-
if normalize_path?
229-
normalize_worktree_path(worktree_path)
230-
else
231-
worktree_path
232-
end
233-
230+
def initialize(worktree_path)
231+
@path = normalize_path(worktree_path)
234232
RubyGit.logger.debug("Created #{inspect}")
235233
end
236234

@@ -245,17 +243,6 @@ def initialize(worktree_path, normalize_path: true)
245243
clone_output.match(/Cloning into ['"](.+)['"]\.\.\./)[1]
246244
end
247245

248-
# True if the path should be normalized
249-
#
250-
# This means that the path should be expanded and converted to a absolute, real
251-
# path to the working tree root dir.
252-
#
253-
# @return [Boolean]
254-
#
255-
# @api private
256-
#
257-
def normalize_path? = @normalize_path
258-
259246
# Return the absolute path to the root of the working tree containing path
260247
#
261248
# @example Expand the path
@@ -274,7 +261,7 @@ def normalize_path? = @normalize_path
274261
#
275262
# @api private
276263
#
277-
def normalize_worktree_path(path)
264+
def normalize_path(path)
278265
raise ArgumentError, "Directory '#{path}' does not exist." unless File.directory?(path)
279266

280267
begin
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RubyGit::Repository do
4-
let(:repository) { RubyGit::Repository.new(repository_path, normalize_path:) }
4+
let(:repository) { RubyGit::Repository.new(repository_path) }
55
let(:repository_path) { '.' }
66

77
describe '#initialize' do
88
subject { repository }
99

1010
around do |example|
11-
in_temp_dir do |_repository_path|
11+
in_temp_dir do
12+
run %w[git init --initial-branch=main]
1213
example.run
1314
end
1415
end
1516

16-
context 'when normalize_path is true' do
17-
let(:normalize_path) { true }
18-
it 'path should be the absolute path to the repository' do
19-
expected_path = File.realpath(File.expand_path(repository_path))
20-
expect(subject).to have_attributes(path: expected_path)
21-
end
22-
end
23-
24-
context 'when normalize_path is false' do
25-
let(:normalize_path) { false }
26-
it 'path should be as given' do
27-
expect(subject).to have_attributes(path: repository_path)
28-
end
17+
it 'path should be the absolute path to the repository' do
18+
expected_path = File.realpath(File.expand_path(repository_path))
19+
expect(subject).to have_attributes(path: expected_path)
2920
end
3021
end
3122
end

spec/lib/ruby_git/worktree_add_spec.rb

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def untracked_entries
3333
end
3434

3535
describe 'calling the git add command line' do
36-
let(:worktree) { described_class.new(worktree_path, normalize_path: false) }
36+
let(:worktree) { described_class.new(worktree_path) }
3737
let(:worktree_path) { '/some/worktree_path' } # Dummy path for testing
3838

3939
let(:subject_object) { worktree } # for the it_behaves_like 'it runs the git command'
@@ -79,11 +79,7 @@ def untracked_entries
7979
let(:pathspecs) { [] }
8080
let(:options) { { all: 'invalid' } }
8181

82-
it 'should raise an error' do
83-
expect { subject }.to(
84-
raise_error(ArgumentError, %(The 'all:' option must be a Boolean value but was "invalid"))
85-
)
86-
end
82+
it_behaves_like 'it raises an ArgumentError', %(The 'all:' option must be a Boolean value but was "invalid")
8783
end
8884
end
8985

@@ -106,11 +102,10 @@ def untracked_entries
106102
let(:pathspecs) { [] }
107103
let(:options) { { force: 'invalid' } }
108104

109-
it 'should raise an error' do
110-
expect { subject }.to(
111-
raise_error(ArgumentError, %(The 'force:' option must be a Boolean value but was "invalid"))
112-
)
113-
end
105+
it_behaves_like(
106+
'it raises an ArgumentError',
107+
%(The 'force:' option must be a Boolean value but was "invalid")
108+
)
114109
end
115110
end
116111

@@ -133,11 +128,10 @@ def untracked_entries
133128
let(:pathspecs) { [] }
134129
let(:options) { { update: 'invalid' } }
135130

136-
it 'should raise an error' do
137-
expect { subject }.to(
138-
raise_error(ArgumentError, %(The 'update:' option must be a Boolean value but was "invalid"))
139-
)
140-
end
131+
it_behaves_like(
132+
'it raises an ArgumentError',
133+
%(The 'update:' option must be a Boolean value but was "invalid")
134+
)
141135
end
142136
end
143137

@@ -160,11 +154,10 @@ def untracked_entries
160154
let(:pathspecs) { [] }
161155
let(:options) { { refresh: 'invalid' } }
162156

163-
it 'should raise an error' do
164-
expect { subject }.to(
165-
raise_error(ArgumentError, %(The 'refresh:' option must be a Boolean value but was "invalid"))
166-
)
167-
end
157+
it_behaves_like(
158+
'it raises an ArgumentError',
159+
%(The 'refresh:' option must be a Boolean value but was "invalid")
160+
)
168161
end
169162
end
170163
end

spec/lib/ruby_git/worktree_spec.rb

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RubyGit::Worktree do
4-
let(:worktree) { RubyGit::Worktree.open(worktree_path, normalize_path:) }
4+
let(:worktree) { RubyGit::Worktree.open(worktree_path) }
55
let(:worktree_path) { '.' }
6-
let(:normalize_path) { true }
76

87
describe '#initialize' do
98
subject { worktree }
@@ -16,30 +15,19 @@
1615
end
1716
end
1817

19-
context 'when normalize_path is true' do
20-
let(:normalize_path) { true }
21-
context 'when worktree_path is the working tree root directory' do
22-
let(:worktree_path) { '.' }
23-
it 'should have a path to the working tree root directory' do
24-
expected_path = File.realpath(File.expand_path(worktree_path))
25-
expect(subject).to have_attributes(path: expected_path)
26-
end
27-
end
28-
29-
context 'when worktree_path is a subdirectory within the working tree' do
30-
let(:worktree_path) { 'subdir' }
31-
it 'should have a path to the working tree root directory' do
32-
expected_path = File.realpath(File.expand_path('.'))
33-
expect(subject).to have_attributes(path: expected_path)
34-
end
18+
context 'when worktree_path is the working tree root directory' do
19+
let(:worktree_path) { '.' }
20+
it 'should have a path to the working tree root directory' do
21+
expected_path = File.realpath(File.expand_path('.'))
22+
expect(subject).to have_attributes(path: expected_path)
3523
end
3624
end
3725

38-
context 'when normalize_path is false' do
39-
let(:normalize_path) { false }
40-
let(:worktree_path) { 'blah/blah/blah' }
41-
it 'should have the given path' do
42-
expect(subject).to have_attributes(path: worktree_path)
26+
context 'when worktree_path is a subdirectory within the working tree' do
27+
let(:worktree_path) { 'subdir' }
28+
it 'should have a path to the working tree root directory' do
29+
expected_path = File.realpath(File.expand_path('.'))
30+
expect(subject).to have_attributes(path: expected_path)
4331
end
4432
end
4533
end

spec/spec_helper.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ def eol = windows? ? "\r\n" : "\n"
173173
#
174174
RSpec.shared_examples 'it runs the git command' do |command, options = Hash|
175175
it 'should build the correct command' do
176+
allow_any_instance_of(described_class).to(
177+
receive(:normalize_path) { |_, path| path }
178+
)
179+
176180
if options.is_a?(Hash)
177181
# If specific options are given, double splat them into the argument list
178182
# binding.irb
@@ -191,6 +195,16 @@ def eol = windows? ? "\r\n" : "\n"
191195
end
192196
end
193197

198+
RSpec.shared_examples 'it raises an ArgumentError' do |message|
199+
it 'should raise an Argument' do
200+
allow_any_instance_of(described_class).to(
201+
receive(:normalize_path) { |_, path| path }
202+
)
203+
204+
expect { subject }.to(raise_error(ArgumentError, message))
205+
end
206+
end
207+
194208
SimpleCov::RSpec.start(list_uncovered_lines: ci_build?)
195209

196210
require 'ruby_git'

0 commit comments

Comments
 (0)