Blob of vendor/grit/test/test_commit.rb (raw blob data)

1 require File.dirname(__FILE__) + '/helper'
2
3 class TestCommit < Test::Unit::TestCase
4 def setup
5 @r = Repo.new(GRIT_REPO)
6 end
7
8 # __bake__
9
10 def test_bake
11 Git.any_instance.expects(:rev_list).returns(fixture('rev_list_single'))
12 @c = Commit.create(@r, :id => '4c8124ffcf4039d292442eeccabdeca5af5c5017')
13 @c.author # bake
14
15 assert_equal "Tom Preston-Werner", @c.author.name
16 assert_equal "tom@mojombo.com", @c.author.email
17 end
18
19 # short_name
20
21 def test_id_abbrev
22 assert_equal '80f136f', @r.commit('80f136f500dfdb8c3e8abf4ae716f875f0a1b57f').id_abbrev
23 end
24
25 # diff
26
27 def test_diff
28 # git diff --full-index 91169e1f5fa4de2eaea3f176461f5dc784796769 > test/fixtures/diff_p
29
30 Git.any_instance.expects(:diff).with({:full_index => true}, 'master').returns(fixture('diff_p'))
31 diffs = Commit.diff(@r, 'master')
32
33 assert_equal 15, diffs.size
34
35 assert_equal '.gitignore', diffs.first.a_path
36 assert_equal '.gitignore', diffs.first.b_path
37 assert_equal '4ebc8aea50e0a67e000ba29a30809d0a7b9b2666', diffs.first.a_commit.id
38 assert_equal '2dd02534615434d88c51307beb0f0092f21fd103', diffs.first.b_commit.id
39 assert_equal '100644', diffs.first.b_mode
40 assert_equal false, diffs.first.new_file
41 assert_equal false, diffs.first.deleted_file
42 assert_equal "--- a/.gitignore\n+++ b/.gitignore\n@@ -1 +1,2 @@\n coverage\n+pkg", diffs.first.diff
43
44 assert_equal 'lib/grit/actor.rb', diffs[5].a_path
45 assert_equal nil, diffs[5].a_commit
46 assert_equal 'f733bce6b57c0e5e353206e692b0e3105c2527f4', diffs[5].b_commit.id
47 assert_equal true, diffs[5].new_file
48 end
49
50 def test_diff_with_two_commits
51 # git diff --full-index 59ddc32 13d27d5 > test/fixtures/diff_2
52 Git.any_instance.expects(:diff).with({:full_index => true}, '59ddc32', '13d27d5').returns(fixture('diff_2'))
53 diffs = Commit.diff(@r, '59ddc32', '13d27d5')
54
55 assert_equal 3, diffs.size
56 assert_equal %w(lib/grit/commit.rb test/fixtures/show_empty_commit test/test_commit.rb), diffs.collect { |d| d.a_path }
57 end
58
59 def test_diff_with_files
60 # git diff --full-index 59ddc32 -- lib > test/fixtures/diff_f
61 Git.any_instance.expects(:diff).with({:full_index => true}, '59ddc32', '--', 'lib').returns(fixture('diff_f'))
62 diffs = Commit.diff(@r, '59ddc32', %w(lib))
63
64 assert_equal 1, diffs.size
65 assert_equal 'lib/grit/diff.rb', diffs.first.a_path
66 end
67
68 def test_diff_with_two_commits_and_files
69 # git diff --full-index 59ddc32 13d27d5 -- lib > test/fixtures/diff_2f
70 Git.any_instance.expects(:diff).with({:full_index => true}, '59ddc32', '13d27d5', '--', 'lib').returns(fixture('diff_2f'))
71 diffs = Commit.diff(@r, '59ddc32', '13d27d5', %w(lib))
72
73 assert_equal 1, diffs.size
74 assert_equal 'lib/grit/commit.rb', diffs.first.a_path
75 end
76
77 # diffs
78 def test_diffs
79 # git diff --full-index 91169e1f5fa4de2eaea3f176461f5dc784796769 > test/fixtures/diff_p
80
81 Git.any_instance.expects(:diff).returns(fixture('diff_p'))
82 @c = Commit.create(@r, :id => '91169e1f5fa4de2eaea3f176461f5dc784796769')
83 diffs = @c.diffs
84
85 assert_equal 15, diffs.size
86
87 assert_equal '.gitignore', diffs.first.a_path
88 assert_equal '.gitignore', diffs.first.b_path
89 assert_equal '4ebc8aea50e0a67e000ba29a30809d0a7b9b2666', diffs.first.a_commit.id
90 assert_equal '2dd02534615434d88c51307beb0f0092f21fd103', diffs.first.b_commit.id
91 assert_equal '100644', diffs.first.b_mode
92 assert_equal false, diffs.first.new_file
93 assert_equal false, diffs.first.deleted_file
94 assert_equal "--- a/.gitignore\n+++ b/.gitignore\n@@ -1 +1,2 @@\n coverage\n+pkg", diffs.first.diff
95
96 assert_equal 'lib/grit/actor.rb', diffs[5].a_path
97 assert_equal nil, diffs[5].a_commit
98 assert_equal 'f733bce6b57c0e5e353206e692b0e3105c2527f4', diffs[5].b_commit.id
99 assert_equal true, diffs[5].new_file
100 end
101
102 def test_diffs_on_initial_import
103 # git show --full-index 634396b2f541a9f2d58b00be1a07f0c358b999b3 > test/fixtures/diff_i
104
105 Git.any_instance.expects(:show).with({:full_index => true, :pretty => 'raw'}, '634396b2f541a9f2d58b00be1a07f0c358b999b3').returns(fixture('diff_i'))
106 @c = Commit.create(@r, :id => '634396b2f541a9f2d58b00be1a07f0c358b999b3')
107 diffs = @c.diffs
108
109 assert_equal 10, diffs.size
110
111 assert_equal 'History.txt', diffs.first.a_path
112 assert_equal 'History.txt', diffs.first.b_path
113 assert_equal nil, diffs.first.a_commit
114 assert_equal nil, diffs.first.b_mode
115 assert_equal '81d2c27608b352814cbe979a6acd678d30219678', diffs.first.b_commit.id
116 assert_equal true, diffs.first.new_file
117 assert_equal false, diffs.first.deleted_file
118 assert_equal "--- /dev/null\n+++ b/History.txt\n@@ -0,0 +1,5 @@\n+== 1.0.0 / 2007-10-09\n+\n+* 1 major enhancement\n+ * Birthday!\n+", diffs.first.diff
119
120
121 assert_equal 'lib/grit.rb', diffs[5].a_path
122 assert_equal nil, diffs[5].a_commit
123 assert_equal '32cec87d1e78946a827ddf6a8776be4d81dcf1d1', diffs[5].b_commit.id
124 assert_equal true, diffs[5].new_file
125 end
126
127 def test_diffs_on_initial_import_with_empty_commit
128 Git.any_instance.expects(:show).with(
129 {:full_index => true, :pretty => 'raw'},
130 '634396b2f541a9f2d58b00be1a07f0c358b999b3'
131 ).returns(fixture('show_empty_commit'))
132
133 @c = Commit.create(@r, :id => '634396b2f541a9f2d58b00be1a07f0c358b999b3')
134 diffs = @c.diffs
135
136 assert_equal [], diffs
137 end
138
139 def test_diffs_with_mode_only_change
140 Git.any_instance.expects(:diff).returns(fixture('diff_mode_only'))
141 @c = Commit.create(@r, :id => '91169e1f5fa4de2eaea3f176461f5dc784796769')
142 diffs = @c.diffs
143
144 assert_equal 23, diffs.size
145 assert_equal '100644', diffs[0].a_mode
146 assert_equal '100755', diffs[0].b_mode
147 end
148
149
150 # stats
151
152 def test_stats
153 Git.any_instance.expects(:diff).with(
154 {:numstat => true},
155 "634396b2f541a9f2d58b00be1a07f0c358b999b3"
156 ).returns(fixture('diff_numstat'))
157 @c = Commit.create(@r, :id => '634396b2f541a9f2d58b00be1a07f0c358b999b3')
158 stats = @c.stats
159 assert_equal ["a.txt", "b.txt"], stats.files.keys.sort
160 end
161
162 # to_s
163
164 def test_to_s
165 @c = Commit.create(@r, :id => 'abc')
166 assert_equal "abc", @c.to_s
167 end
168
169 # inspect
170
171 def test_inspect
172 @c = Commit.create(@r, :id => 'abc')
173 assert_equal %Q{#<Grit::Commit "abc">}, @c.inspect
174 end
175
176 # to_hash
177
178 def test_to_hash
179 @c = Commit.create(@r, :id => '4c8124ffcf4039d292442eeccabdeca5af5c5017')
180
181 expected = {
182 'parents' => ['id' => "634396b2f541a9f2d58b00be1a07f0c358b999b3"],
183 'committed_date' => Time.parse("2007-10-10T00:06:12-07:00").localtime.xmlschema,
184 'tree' => "672eca9b7f9e09c22dcb128c283e8c3c8d7697a4",
185 'authored_date' => Time.parse("2007-10-10T00:06:12-07:00").localtime.xmlschema,
186 'committer' => {'email' => "tom@mojombo.com", 'name' => "Tom Preston-Werner"},
187 'message' => "implement Grit#heads",
188 'author' => {'email' => "tom@mojombo.com", 'name' => "Tom Preston-Werner"},
189 'id' => "4c8124ffcf4039d292442eeccabdeca5af5c5017"
190 }
191
192 generated_hash = @c.to_hash
193 expected.keys.each do |exp_key|
194 assert_equal expected[exp_key], generated_hash[exp_key]
195 end
196 end
197 end