|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +require File.dirname(__FILE__) + '/../test_helper' |
| 4 | + |
| 5 | +class TestGitDir < Test::Unit::TestCase |
| 6 | + def test_index_calculated_from_git_dir |
| 7 | + Dir.mktmpdir do |work_tree| |
| 8 | + Dir.mktmpdir do |git_dir| |
| 9 | + git = Git.open(work_tree, repository: git_dir) |
| 10 | + |
| 11 | + assert_equal(work_tree, git.dir.path) |
| 12 | + assert_equal(git_dir, git.repo.path) |
| 13 | + |
| 14 | + # Since :index was not given in the options to Git#open, index should |
| 15 | + # be defined automatically based on the git_dir. |
| 16 | + # |
| 17 | + index = File.join(git_dir, 'index') |
| 18 | + assert_equal(index, git.index.path) |
| 19 | + end |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + # Test the case where the git-dir is not a subdirectory of work-tree |
| 24 | + # |
| 25 | + def test_git_dir_outside_work_tree |
| 26 | + Dir.mktmpdir do |work_tree| |
| 27 | + Dir.mktmpdir do |git_dir| |
| 28 | + # Setup a bare repository |
| 29 | + # |
| 30 | + source_git_dir = File.expand_path(File.join('tests', 'files', 'working.git')) |
| 31 | + FileUtils.cp_r(Dir["#{source_git_dir}/*"], git_dir, preserve: true) |
| 32 | + git = Git.open(work_tree, repository: git_dir) |
| 33 | + |
| 34 | + assert_equal(work_tree, git.dir.path) |
| 35 | + assert_equal(git_dir, git.repo.path) |
| 36 | + |
| 37 | + # Reconstitute the work tree from the bare repository |
| 38 | + # |
| 39 | + branch = 'master' |
| 40 | + git.checkout(branch, force: true) |
| 41 | + |
| 42 | + # Make sure the work tree contains the expected files |
| 43 | + # |
| 44 | + expected_files = %w[ex_dir example.txt] |
| 45 | + actual_files = Dir[File.join(work_tree, '*')].map { |f| File.basename(f) } |
| 46 | + assert_equal(expected_files, actual_files) |
| 47 | + |
| 48 | + # None of the expected files should have a status that says it has been changed |
| 49 | + # |
| 50 | + expected_files.each do |file| |
| 51 | + assert_equal(false, git.status.changed?(file)) |
| 52 | + end |
| 53 | + |
| 54 | + # Change a file and make sure it's status says it has been changed |
| 55 | + # |
| 56 | + file = 'example.txt' |
| 57 | + File.open(File.join(work_tree, file), "a") { |f| f.write("A new line") } |
| 58 | + assert_equal(true, git.status.changed?(file)) |
| 59 | + |
| 60 | + # Add and commit the file and then check that: |
| 61 | + # * the file is not flagged as changed anymore |
| 62 | + # * the commit was added to the log |
| 63 | + # |
| 64 | + max_log_size = 100 |
| 65 | + assert_equal(64, git.log(max_log_size).size) |
| 66 | + git.add(file) |
| 67 | + git.commit('This is a new commit') |
| 68 | + assert_equal(false, git.status.changed?(file)) |
| 69 | + assert_equal(65, git.log(max_log_size).size) |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | +end |
0 commit comments