From 40e5acc63251759433951b268fd6a7f80e0f20b1 Mon Sep 17 00:00:00 2001 From: James Couball Date: Thu, 9 Mar 2023 09:38:15 -0800 Subject: [PATCH] Add test to ensure that `Git.open` works to open a submodule Signed-off-by: James Couball --- tests/test_helper.rb | 29 +++++++++++-------- tests/units/test_submodule.rb | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 tests/units/test_submodule.rb diff --git a/tests/test_helper.rb b/tests/test_helper.rb index 4b7111de..79c06387 100644 --- a/tests/test_helper.rb +++ b/tests/test_helper.rb @@ -2,6 +2,7 @@ require 'fileutils' require 'minitar' require 'test/unit' +require 'tmpdir' require "git" @@ -51,18 +52,24 @@ def create_temp_repo(clone_name) tmp_path end - def in_temp_dir # :yields: the temporary dir's path - tmp_path = nil - while tmp_path.nil? || File.directory?(tmp_path) - filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0') - tmp_path = File.join(Dir.tmpdir, filename) - end - FileUtils.mkdir(tmp_path) - tmp_path = File.realpath(tmp_path) - FileUtils.cd tmp_path do - yield tmp_path + # Creates a temp directory and yields that path to the passed block + # + # On Windows, using Dir.mktmpdir with a block sometimes raises an error: + # `Errno::ENOTEMPTY: Directory not empty @ dir_s_rmdir`. I think this might + # be a configuration issue with the Windows CI environment. + # + # This was worked around by using the non-block form of Dir.mktmpdir and + # then removing the directory manually in an ensure block. + # + def in_temp_dir + tmpdir = Dir.mktmpdir + tmpdir_realpath = File.realpath(tmpdir) + Dir.chdir(tmpdir_realpath) do + yield tmpdir_realpath end - FileUtils.rm_r(tmp_path) + ensure + FileUtils.rm_rf(tmpdir_realpath) if tmpdir_realpath + # raise "Temp dir #{tmpdir} not removed. Remaining files : #{Dir["#{tmpdir}/**/*"]}" if File.exist?(tmpdir) end def create_file(path, content) diff --git a/tests/units/test_submodule.rb b/tests/units/test_submodule.rb new file mode 100644 index 00000000..009127f2 --- /dev/null +++ b/tests/units/test_submodule.rb @@ -0,0 +1,53 @@ +#!/usr/bin/env ruby + +require 'test_helper' + +class TestSubmodule < Test::Unit::TestCase + test 'Git.open should be able to open a submodule' do + in_temp_dir do + submodule = Git.init('submodule', initial_branch: 'main') + File.write('submodule/README.md', '# Submodule') + submodule.add('README.md') + submodule.commit('Add README.md') + + repo = Git.init('repo', initial_branch: 'main') + File.write('repo/README.md', '# Main Repository') + repo.add('README.md') + repo.commit('Add README.md') + + Dir.chdir('repo') do + assert_child_process_success { `git -c protocol.file.allow=always submodule add ../submodule submodule 2>&1` } + assert_child_process_success { `git commit -am "Add submodule" 2>&1` } + end + + submodule_repo = assert_nothing_raised { Git.open('repo/submodule') } + + assert_equal(submodule.object('HEAD').sha, submodule_repo.object('HEAD').sha) + end + end + + test 'Git.open should be able to open a submodule from a subdirectory within the submodule' do + in_temp_dir do + submodule = Git.init('submodule', initial_branch: 'main') + Dir.mkdir('submodule/subdir') + File.write('submodule/subdir/README.md', '# Submodule') + submodule.add('subdir/README.md') + submodule.commit('Add README.md') + + repo = Git.init('repo', initial_branch: 'main') + File.write('repo/README.md', '# Main Repository') + repo.add('README.md') + repo.commit('Add README.md') + + Dir.chdir('repo') do + assert_child_process_success { `git -c protocol.file.allow=always submodule add ../submodule submodule 2>&1` } + assert_child_process_success { `git commit -am "Add submodule" 2>&1` } + end + + submodule_repo = assert_nothing_raised { Git.open('repo/submodule/subdir') } + + repo_files = submodule_repo.ls_files + assert(repo_files.include?('subdir/README.md')) + end + end +end