1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../test_helper'
class TestGitDir < Test::Unit::TestCase
def test_index_calculated_from_git_dir
Dir.mktmpdir do |work_tree|
Dir.mktmpdir do |git_dir|
git = Git.open(work_tree, repository: git_dir)
assert_equal(work_tree, git.dir.path)
assert_equal(git_dir, git.repo.path)
# Since :index was not given in the options to Git#open, index should
# be defined automatically based on the git_dir.
#
index = File.join(git_dir, 'index')
assert_equal(index, git.index.path)
end
end
end
# Test the case where the git-dir is not a subdirectory of work-tree
#
def test_git_dir_outside_work_tree
Dir.mktmpdir do |work_tree|
Dir.mktmpdir do |git_dir|
# Setup a bare repository
#
source_git_dir = File.expand_path(File.join('tests', 'files', 'working.git'))
FileUtils.cp_r(Dir["#{source_git_dir}/*"], git_dir, preserve: true)
git = Git.open(work_tree, repository: git_dir)
assert_equal(work_tree, git.dir.path)
assert_equal(git_dir, git.repo.path)
# Reconstitute the work tree from the bare repository
#
branch = 'master'
git.checkout(branch, force: true)
# Make sure the work tree contains the expected files
#
expected_files = %w[ex_dir example.txt].sort
actual_files = Dir[File.join(work_tree, '*')].map { |f| File.basename(f) }.sort
assert_equal(expected_files, actual_files)
# None of the expected files should have a status that says it has been changed
#
expected_files.each do |file|
assert_equal(false, git.status.changed?(file))
end
# Change a file and make sure it's status says it has been changed
#
file = 'example.txt'
File.open(File.join(work_tree, file), "a") { |f| f.write("A new line") }
assert_equal(true, git.status.changed?(file))
# Add and commit the file and then check that:
# * the file is not flagged as changed anymore
# * the commit was added to the log
#
max_log_size = 100
assert_equal(64, git.log(max_log_size).size)
git.add(file)
git.commit('This is a new commit')
assert_equal(false, git.status.changed?(file))
assert_equal(65, git.log(max_log_size).size)
end
end
end
# Test that Git::Lib::Diff.to_a works from a linked working tree (not the
# main working tree). See https://git-scm.com/docs/git-worktree for a
# description of 'main' and 'linked' working tree.
#
# This is a real world case where '.git' in the working tree is a file
# instead of a directory and where the value of GIT_INDEX_FILE is relevant.
#
def test_git_diff_to_a
work_tree = Dir.mktmpdir
begin
Dir.chdir(work_tree) do
`git init`
`git commit --allow-empty -m 'init'`
`git worktree add --quiet child`
Dir.chdir('child') do
result = Git.open('.').diff.to_a
assert_equal([], result)
end
end
ensure
FileUtils.rm_rf(work_tree)
end
end
end
|