Skip to content

Commit 079893d

Browse files
committed
Add tests that specificly test read_tree, write_tree, and commit_tree git commands
1 parent 9f737eb commit 079893d

File tree

1 file changed

+129
-7
lines changed

1 file changed

+129
-7
lines changed

tests/units/test_tree_ops.rb

Lines changed: 129 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,134 @@
55
class TestTreeOps < Test::Unit::TestCase
66

77
def test_read_tree
8+
treeish = 'testbranch1'
9+
expected_command_line = ['read-tree', treeish]
10+
git_cmd = :read_tree
11+
git_cmd_args = [treeish]
12+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
13+
end
14+
15+
def test_read_tree_with_prefix
16+
treeish = 'testbranch1'
17+
prefix = 'foo'
18+
expected_command_line = ['read-tree', "--prefix=#{prefix}", treeish]
19+
git_cmd = :read_tree
20+
git_cmd_args = [treeish, prefix: prefix]
21+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
22+
end
23+
24+
def test_write_tree
25+
expected_command_line = ['write-tree']
26+
git_output = 'aa7349e'
27+
git_cmd = :write_tree
28+
git_cmd_args = []
29+
result = assert_command_line(expected_command_line, git_cmd, git_cmd_args, git_output)
30+
# the git output should be returned from Git::Base#write_tree
31+
assert_equal(git_output, result)
32+
end
33+
34+
def test_commit_tree_with_default_message
35+
tree = 'tree-ref'
36+
37+
expected_message = 'commit tree tree-ref'
38+
tempfile_path = 'foo'
39+
mock_tempfile = mock('tempfile')
40+
Tempfile.stubs(:new).returns(mock_tempfile)
41+
mock_tempfile.stubs(:path).returns(tempfile_path)
42+
mock_tempfile.expects(:write).with(expected_message)
43+
mock_tempfile.expects(:close)
44+
45+
redirect_value = windows_platform? ? "< \"#{tempfile_path}\"" : "< '#{tempfile_path}'"
46+
47+
expected_command_line = ['commit-tree', tree, redirect: redirect_value]
48+
git_cmd = :commit_tree
49+
git_cmd_args = [tree]
50+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
51+
end
52+
53+
def test_commit_tree_with_message
54+
tree = 'tree-ref'
55+
message = 'this is my message'
56+
57+
tempfile_path = 'foo'
58+
mock_tempfile = mock('tempfile')
59+
Tempfile.stubs(:new).returns(mock_tempfile)
60+
mock_tempfile.stubs(:path).returns(tempfile_path)
61+
mock_tempfile.expects(:write).with(message)
62+
mock_tempfile.expects(:close)
63+
64+
redirect_value = windows_platform? ? "< \"#{tempfile_path}\"" : "< '#{tempfile_path}'"
65+
66+
expected_command_line = ['commit-tree', tree, redirect: redirect_value]
67+
git_cmd = :commit_tree
68+
git_cmd_args = [tree, message: message]
69+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
70+
end
71+
72+
def test_commit_tree_with_parent
73+
tree = 'tree-ref'
74+
message = 'this is my message'
75+
parent = 'parent-commit'
76+
77+
tempfile_path = 'foo'
78+
mock_tempfile = mock('tempfile')
79+
Tempfile.stubs(:new).returns(mock_tempfile)
80+
mock_tempfile.stubs(:path).returns(tempfile_path)
81+
mock_tempfile.expects(:write).with(message)
82+
mock_tempfile.expects(:close)
83+
84+
redirect_value = windows_platform? ? "< \"#{tempfile_path}\"" : "< '#{tempfile_path}'"
85+
86+
expected_command_line = ['commit-tree', tree, "-p", parent, redirect: redirect_value]
87+
git_cmd = :commit_tree
88+
git_cmd_args = [tree, parent: parent, message: message]
89+
90+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
91+
end
92+
93+
def test_commit_tree_with_parents
94+
tree = 'tree-ref'
95+
message = 'this is my message'
96+
parents = 'commit1'
97+
98+
tempfile_path = 'foo'
99+
mock_tempfile = mock('tempfile')
100+
Tempfile.stubs(:new).returns(mock_tempfile)
101+
mock_tempfile.stubs(:path).returns(tempfile_path)
102+
mock_tempfile.expects(:write).with(message)
103+
mock_tempfile.expects(:close)
104+
105+
redirect_value = windows_platform? ? "< \"#{tempfile_path}\"" : "< '#{tempfile_path}'"
106+
107+
expected_command_line = ['commit-tree', tree, '-p', 'commit1', redirect: redirect_value]
108+
git_cmd = :commit_tree
109+
git_cmd_args = [tree, parents: parents, message: message]
110+
111+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
112+
end
113+
114+
def test_commit_tree_with_multiple_parents
115+
tree = 'tree-ref'
116+
message = 'this is my message'
117+
parents = ['commit1', 'commit2']
118+
119+
tempfile_path = 'foo'
120+
mock_tempfile = mock('tempfile')
121+
Tempfile.stubs(:new).returns(mock_tempfile)
122+
mock_tempfile.stubs(:path).returns(tempfile_path)
123+
mock_tempfile.expects(:write).with(message)
124+
mock_tempfile.expects(:close)
125+
126+
redirect_value = windows_platform? ? "< \"#{tempfile_path}\"" : "< '#{tempfile_path}'"
127+
128+
expected_command_line = ['commit-tree', tree, '-p', 'commit1', '-p', 'commit2', redirect: redirect_value]
129+
git_cmd = :commit_tree
130+
git_cmd_args = [tree, parents: parents, message: message]
131+
132+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
133+
end
134+
135+
def test_tree_ops
8136
in_bare_repo_clone do |g|
9137
g.branch('testbranch1').in_branch('tb commit 1') do
10138
new_file('test-file1', 'blahblahblah2')
@@ -79,12 +207,7 @@ def test_read_tree
79207
assert(c.commit?)
80208
assert_equal('b40f7a9072cdec637725700668f8fdebe39e6d38', c.gtree.sha)
81209

82-
tmp = Tempfile.new('tesxt')
83-
tmppath = tmp.path
84-
tmp.close
85-
tmp.unlink
86-
87-
g.with_index(tmppath) do
210+
g.with_temp_index do
88211
g.read_tree('testbranch1', :prefix => 'b1/')
89212
g.read_tree('testbranch3', :prefix => 'b3/')
90213
index = g.ls_files
@@ -111,7 +234,6 @@ def test_read_tree
111234
g.checkout_index(:all => true)
112235
assert(File.directory?('b1'))
113236
end
114-
115237
end
116238
end
117239
end

0 commit comments

Comments
 (0)