Skip to content

Commit 3ca721e

Browse files
Remove Rugged::SubmoduleTestCase.
Submodules are now setup automatically.
1 parent 40baeaa commit 3ca721e

File tree

2 files changed

+76
-73
lines changed

2 files changed

+76
-73
lines changed

test/submodule_test.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
require 'test_helper'
22

3-
class SubmoduleTest < Rugged::SubmoduleTestCase
3+
class SubmoduleTest < Rugged::TestCase
44
def setup
5-
@repo = setup_submodule
5+
@repo = FixtureRepo.from_libgit2('submod2').tap do |repo|
6+
Dir.chdir(repo.workdir) do
7+
File.rename(
8+
File.join('not-submodule', '.gitted'),
9+
File.join('not-submodule', '.git')
10+
)
11+
12+
File.rename(
13+
File.join('not', '.gitted'),
14+
File.join('not', '.git')
15+
)
16+
end
17+
end
618
end
719

820
class TestException < StandardError

test/test_helper.rb

Lines changed: 62 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ module FixtureRepo
1616
# Create a new, empty repository.
1717
def self.empty(*args)
1818
path = Dir.mktmpdir("rugged-empty")
19-
with_cleanup(Rugged::Repository.init_at(path, *args), path)
19+
Rugged::Repository.init_at(path, *args).tap do |repo|
20+
schedule_cleanup(repo, path)
21+
end
2022
rescue
2123
FileUtils.remove_entry_secure(path)
2224
raise
@@ -30,7 +32,10 @@ def self.from_rugged(name, *args)
3032

3133
prepare(path)
3234

33-
with_cleanup(Rugged::Repository.new(path, *args), path)
35+
Rugged::Repository.new(path, *args).tap do |repo|
36+
schedule_cleanup(repo, path)
37+
rewrite_gitmodules(repo) unless repo.bare?
38+
end
3439
rescue
3540
FileUtils.remove_entry_secure(path)
3641
raise
@@ -44,7 +49,10 @@ def self.from_libgit2(name, *args)
4449

4550
prepare(path)
4651

47-
with_cleanup(Rugged::Repository.new(path, *args), path)
52+
Rugged::Repository.new(path, *args).tap do |repo|
53+
schedule_cleanup(repo, path)
54+
rewrite_gitmodules(repo) unless repo.bare?
55+
end
4856
rescue
4957
FileUtils.remove_entry_secure(path)
5058
raise
@@ -56,7 +64,9 @@ def self.clone(repository)
5664

5765
`git clone --quiet -- #{repository.path} #{path}`
5866

59-
with_cleanup(Rugged::Repository.new(path), path)
67+
Rugged::Repository.new(path).tap do |repo|
68+
schedule_cleanup(repo, path)
69+
end
6070
rescue
6171
FileUtils.remove_entry_secure(path)
6272
raise
@@ -70,6 +80,53 @@ def self.prepare(path)
7080
end
7181
end
7282

83+
# Rugged reuses libgit2 fixtures and needs the same setup code.
84+
#
85+
# This should be the same as the libgit2 fixture
86+
# setup in vendor/libgit2/tests/submodule/submodule_helpers.c
87+
def self.rewrite_gitmodules(repo)
88+
workdir = repo.workdir
89+
90+
return unless File.exist?(File.join(workdir, 'gitmodules'))
91+
92+
input_path = File.join(workdir, 'gitmodules')
93+
output_path = File.join(workdir, '.gitmodules')
94+
submodules = []
95+
96+
File.open(input_path, 'r') do |input|
97+
File.open(output_path, 'w') do |output|
98+
input.each_line do |line|
99+
if %r{path = (?<submodule>.+$)} =~ line
100+
submodules << submodule.strip
101+
elsif %r{url = \.\.\/(?<url>.+$)} =~ line
102+
# Copy repositories pointed to by relative urls
103+
# and replace the relative url by the absolute path to the
104+
# copied repo.
105+
url.strip!
106+
path = Dir.mktmpdir(url)
107+
FileUtils.cp_r(File.join(TestCase::LIBGIT2_FIXTURE_DIR, url, "."), path)
108+
schedule_cleanup(repo, path)
109+
110+
line = "url = #{path}\n"
111+
end
112+
output.write(line)
113+
end
114+
end
115+
end
116+
117+
FileUtils.remove_entry_secure(input_path)
118+
119+
# rename .gitted -> .git in submodule dirs
120+
submodules.each do |submodule|
121+
submodule_path = File.join(workdir, submodule)
122+
if File.exist?(File.join(submodule_path, '.gitted'))
123+
Dir.chdir(submodule_path) do
124+
File.rename('.gitted', '.git')
125+
end
126+
end
127+
end
128+
end
129+
73130
def self.finalize_cleanup(path)
74131
proc { FileUtils.remove_entry_secure(path) if File.exist?(path) }
75132
end
@@ -88,10 +145,9 @@ def self.directories
88145
# Schedule the given +path+ to be deleted, either when
89146
# +FixtureRepo.eager_teardown+ is called or when the given +repo+
90147
# gets gc'ed.
91-
def self.with_cleanup(repo, path)
148+
def self.schedule_cleanup(repo, path)
92149
ObjectSpace.define_finalizer(repo, finalize_cleanup(path))
93150
self.directories << path
94-
repo
95151
end
96152
end
97153

@@ -134,69 +190,4 @@ def ssh_key_credential_from_agent
134190
})
135191
end
136192
end
137-
138-
# Rugged reuses libgit2 fixtures and needs the same setup code.
139-
#
140-
# This should be the same as the libgit2 fixture
141-
# setup in vendor/libgit2/tests/submodule/submodule_helpers.c
142-
class SubmoduleTestCase < Rugged::TestCase
143-
def setup_submodule
144-
repository = FixtureRepo.from_libgit2('submod2')
145-
146-
rewrite_gitmodules(repository)
147-
148-
Dir.chdir(repository.workdir) do
149-
File.rename(
150-
File.join('not-submodule', '.gitted'),
151-
File.join('not-submodule', '.git')
152-
)
153-
154-
File.rename(
155-
File.join('not', '.gitted'),
156-
File.join('not', '.git')
157-
)
158-
end
159-
160-
repository
161-
end
162-
163-
def rewrite_gitmodules(repo)
164-
workdir = repo.workdir
165-
166-
input_path = File.join(workdir, 'gitmodules')
167-
output_path = File.join(workdir, '.gitmodules')
168-
submodules = []
169-
170-
File.open(input_path, 'r') do |input|
171-
File.open(output_path, 'w') do |output|
172-
input.each_line do |line|
173-
if %r{path = (?<submodule>.+$)} =~ line
174-
submodules << submodule.strip
175-
elsif %r{url = \.\.\/(?<url>.+$)} =~ line
176-
# Copy repositories pointed to by relative urls
177-
# and replace the relative url by the absolute path to the
178-
# copied repo.
179-
path = Dir.mktmpdir(url)
180-
FileUtils.cp_r(File.join(TestCase::LIBGIT2_FIXTURE_DIR, url, "."), path)
181-
ObjectSpace.define_finalizer(repo, FixtureRepo.finalize_cleanup(path) )
182-
line = "url = #{path}\n"
183-
end
184-
output.write(line)
185-
end
186-
end
187-
end
188-
189-
FileUtils.remove_entry_secure(input_path)
190-
191-
# rename .gitted -> .git in submodule dirs
192-
submodules.each do |submodule|
193-
submodule_path = File.join(workdir, submodule)
194-
if File.exist?(File.join(submodule_path, '.gitted'))
195-
Dir.chdir(submodule_path) do
196-
File.rename('.gitted', '.git')
197-
end
198-
end
199-
end
200-
end
201-
end
202193
end

0 commit comments

Comments
 (0)