Skip to content

Commit 604550d

Browse files
committed
Add test to ensure that Git.open works to open a submodule
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 5b0e1c8 commit 604550d

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

tests/test_helper.rb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'fileutils'
33
require 'minitar'
44
require 'test/unit'
5+
require 'tmpdir'
56

67
require "git"
78

@@ -51,18 +52,23 @@ def create_temp_repo(clone_name)
5152
tmp_path
5253
end
5354

54-
def in_temp_dir # :yields: the temporary dir's path
55-
tmp_path = nil
56-
while tmp_path.nil? || File.directory?(tmp_path)
57-
filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
58-
tmp_path = File.join(Dir.tmpdir, filename)
59-
end
60-
FileUtils.mkdir(tmp_path)
61-
tmp_path = File.realpath(tmp_path)
62-
FileUtils.cd tmp_path do
63-
yield tmp_path
55+
# Creates a temp directory and yields that path to the passed block
56+
#
57+
# Using the for of Dir.mktmpdir which takes a block sometimes raises an error:
58+
# `Errno::ENOTEMPTY: Directory not empty @ dir_s_rmdir`.
59+
#
60+
# This was worked around by using the non-block form of Dir.mktmpdir and
61+
# then removing the directory manually in an ensure block.
62+
#
63+
def in_temp_dir
64+
tmpdir = Dir.mktmpdir
65+
tmpdir_realpath = File.realpath(tmpdir)
66+
Dir.chdir(tmpdir_realpath) do
67+
yield tmpdir_realpath
6468
end
65-
FileUtils.rm_r(tmp_path)
69+
ensure
70+
FileUtils.rm_rf(tmpdir_realpath) if tmpdir_realpath
71+
raise "Temp dir #{tmpdir} not removed" if File.exist?(tmpdir)
6672
end
6773

6874
def create_file(path, content)

tests/units/test_submodule.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'test_helper'
4+
5+
class TestSubmodule < Test::Unit::TestCase
6+
test 'Git.open should be able to open a submodule' do
7+
in_temp_dir do
8+
submodule = Git.init('submodule', initial_branch: 'main')
9+
File.write('submodule/README.md', '# Submodule')
10+
submodule.add('README.md')
11+
submodule.commit('Add README.md')
12+
13+
repo = Git.init('repo', initial_branch: 'main')
14+
File.write('repo/README.md', '# Main Repository')
15+
repo.add('README.md')
16+
repo.commit('Add README.md')
17+
18+
Dir.chdir('repo') do
19+
assert_child_process_success { `git -c protocol.file.allow=always submodule add ../submodule submodule 2>&1` }
20+
assert_child_process_success { `git commit -am "Add submodule" 2>&1` }
21+
end
22+
23+
submodule_repo = assert_nothing_raised { Git.open('repo/submodule') }
24+
25+
assert_equal(submodule.object('HEAD').sha, submodule_repo.object('HEAD').sha)
26+
end
27+
end
28+
29+
test 'Git.open should be able to open a submodule from a subdirectory within the submodule' do
30+
in_temp_dir do
31+
submodule = Git.init('submodule', initial_branch: 'main')
32+
Dir.mkdir('submodule/subdir')
33+
File.write('submodule/subdir/README.md', '# Submodule')
34+
submodule.add('subdir/README.md')
35+
submodule.commit('Add README.md')
36+
37+
repo = Git.init('repo', initial_branch: 'main')
38+
File.write('repo/README.md', '# Main Repository')
39+
repo.add('README.md')
40+
repo.commit('Add README.md')
41+
42+
Dir.chdir('repo') do
43+
assert_child_process_success { `git -c protocol.file.allow=always submodule add ../submodule submodule 2>&1` }
44+
assert_child_process_success { `git commit -am "Add submodule" 2>&1` }
45+
end
46+
47+
submodule_repo = assert_nothing_raised { Git.open('repo/submodule/subdir') }
48+
49+
repo_files = submodule_repo.ls_files
50+
assert(repo_files.include?('subdir/README.md'))
51+
end
52+
end
53+
end

0 commit comments

Comments
 (0)