Skip to content

Commit b27a15b

Browse files
authored
Add test to ensure that Git.open works to open a submodule (#655)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 5b0e1c8 commit b27a15b

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

tests/test_helper.rb

Lines changed: 18 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,24 @@ 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+
# On Windows, using Dir.mktmpdir with a block sometimes raises an error:
58+
# `Errno::ENOTEMPTY: Directory not empty @ dir_s_rmdir`. I think this might
59+
# be a configuration issue with the Windows CI environment.
60+
#
61+
# This was worked around by using the non-block form of Dir.mktmpdir and
62+
# then removing the directory manually in an ensure block.
63+
#
64+
def in_temp_dir
65+
tmpdir = Dir.mktmpdir
66+
tmpdir_realpath = File.realpath(tmpdir)
67+
Dir.chdir(tmpdir_realpath) do
68+
yield tmpdir_realpath
6469
end
65-
FileUtils.rm_r(tmp_path)
70+
ensure
71+
FileUtils.rm_rf(tmpdir_realpath) if tmpdir_realpath
72+
# raise "Temp dir #{tmpdir} not removed. Remaining files : #{Dir["#{tmpdir}/**/*"]}" if File.exist?(tmpdir)
6673
end
6774

6875
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)