[NEW] Initial Revision
[obnox/ohloh/ohloh_scm.git] / test / unit / svn_commits_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 module Scm::Adapters
4         class SvnCommitsTest < Scm::Test
5
6                 def test_commits
7                         with_svn_repository('svn') do |svn|
8                                 assert_equal 5, svn.commit_count
9                                 assert_equal 3, svn.commit_count(2)
10                                 assert_equal 0, svn.commit_count(1000)
11
12                                 assert_equal [1,2,3,4,5], svn.commit_tokens
13                                 assert_equal [3,4,5], svn.commit_tokens(2)
14                                 assert_equal [], svn.commit_tokens(1000)
15
16                                 assert_equal [1,2,3,4,5], svn.commits.collect { |c| c.token }
17                                 assert_equal [3,4,5], svn.commits(2).collect { |c| c.token }
18                                 assert_equal [], svn.commits(1000)
19                         end
20                 end
21
22                 # Confirms that the sha1 matches those created by git exactly
23                 def test_sha1
24                         with_svn_repository('svn') do |svn|
25                                 assert_equal '0000000000000000000000000000000000000000', svn.try_get_sha1('/trunk/file_not_found')
26                                 assert_equal 'f6adcae4447809b651c787c078d255b2b4e963c5', svn.try_get_sha1('/trunk/helloworld.c')
27                         end
28                 end
29
30                 # Given a commit with diffs, fill in all of the SHA1 values.
31                 def test_populate_sha1
32                         with_svn_repository('svn') do |svn|
33                                 c = Scm::Commit.new(:token => 3)
34                                 c.diffs = [Scm::Diff.new(:path => "/trunk/helloworld.c", :action => "M")]
35                                 svn.populate_sha1s!(c)
36                                 assert_equal 'f6adcae4447809b651c787c078d255b2b4e963c5', c.diffs.first.sha1
37                                 assert_equal '4c734ad53b272c9b3d719f214372ac497ff6c068', c.diffs.first.parent_sha1
38                         end
39                 end
40
41                 def test_strip_commit_branch
42                         svn = SvnAdapter.new(:branch_name => "/trunk")
43                         commit = Scm::Commit.new
44
45                         # nil diffs before => nil diffs after
46                         assert !svn.strip_commit_branch(commit).diffs
47
48                         # [] diffs before => [] diffs after
49                         commit.diffs = []
50                         assert_equal [], svn.strip_commit_branch(commit).diffs
51
52                         commit.diffs = [
53                                 Scm::Diff.new(:path => "/trunk"),
54                                 Scm::Diff.new(:path => "/trunk/helloworld.c"),
55                                 Scm::Diff.new(:path => "/branches/a")
56                         ]
57                         assert_equal ['', '/helloworld.c'], svn.strip_commit_branch(commit).diffs.collect { |d| d.path }.sort
58                 end
59
60                 def test_strip_diff_branch
61                         svn = SvnAdapter.new(:branch_name => "/trunk")
62                         assert !svn.strip_diff_branch(Scm::Diff.new)
63                         assert !svn.strip_diff_branch(Scm::Diff.new(:path => "/branches/b"))
64                         assert_equal '', svn.strip_diff_branch(Scm::Diff.new(:path => "/trunk")).path
65                         assert_equal '/helloworld.c', svn.strip_diff_branch(Scm::Diff.new(:path => "/trunk/helloworld.c")).path
66                 end
67
68                 def test_strip_path_branch
69                         # Returns nil for any path outside of SvnAdapter::branch_name
70                         assert !SvnAdapter.new.strip_path_branch(nil)
71                         assert !SvnAdapter.new(:branch_name => "/trunk").strip_path_branch("/branches/foo")
72                         assert !SvnAdapter.new(:branch_name => "/trunk").strip_path_branch("/t")
73
74                         # If branch_name is empty or root, returns path unchanged
75                         assert_equal '', SvnAdapter.new.strip_path_branch('')
76                         assert_equal '/trunk', SvnAdapter.new.strip_path_branch('/trunk')
77
78                         # If path is equal to or is a subdirectory of branch_name, returns subdirectory portion only.
79                         assert_equal '', SvnAdapter.new(:branch_name => "/trunk").strip_path_branch('/trunk')
80                         assert_equal '/foo', SvnAdapter.new(:branch_name => "/trunk").strip_path_branch('/trunk/foo')
81                 end
82
83                 def test_strip_path_branch_with_special_chars
84                         assert_equal '/foo', SvnAdapter.new(:branch_name => '/trunk/hamcrest-c++').strip_path_branch('/trunk/hamcrest-c++/foo')
85                 end
86
87
88                 def test_deep_commits
89                         with_svn_repository('deep_svn') do |svn|
90
91                                 # The full repository contains 4 revisions...
92                                 assert_equal 4, svn.commit_count
93
94                                 # ...however, the current trunk contains only revisions 3 and 4.
95                                 # That's because the branch was moved to replace the trunk at revision 3.
96                                 #
97                                 # Even though there was a different trunk directory present in
98                                 # revisions 1 and 2, it is not visible to Ohloh.
99
100                                 trunk = SvnAdapter.new(:url => File.join(svn.url,'trunk'), :branch_name => '/trunk').normalize
101                                 assert_equal 2, trunk.commit_count
102                                 assert_equal [3,4], trunk.commit_tokens
103
104
105                                 deep_commits = []
106                                 trunk.each_commit { |c| deep_commits << c }
107
108                                 # When the branch is moved to replace the trunk in revision 3,
109                                 # the Subversion log shows
110                                 #
111                                 #   D /branches/b
112                                 #   A /trunk (from /branches/b:2)
113                                 #
114                                 # However, there are files in those directories. Make sure the commits
115                                 # that we generate include all of those files not shown by the log.
116                                 #
117                                 # Also, our commits do not include diffs for the actual directories;
118                                 # only the files within those directories.
119                                 #
120                                 # Also, since we are only tracking the /trunk and not /branches/b, then
121                                 # there should not be anything referring to activity in /branches/b.
122
123                                 assert_equal 3, deep_commits.first.token # Make sure this is the right revision
124                                 assert_equal 2, deep_commits.first.diffs.size # Two files seen
125
126                                 assert_equal 'A', deep_commits.first.diffs[0].action
127                                 assert_equal '/subdir/bar.rb', deep_commits.first.diffs[0].path
128                                 assert_equal 'A', deep_commits.first.diffs[1].action
129                                 assert_equal '/subdir/foo.rb', deep_commits.first.diffs[1].path
130
131                                 # In Revision 4, a directory is renamed. This shows in the Subversion log as
132                                 #
133                                 #   A /trunk/newdir (from /trunk/subdir:3)
134                                 #   D /trunk/subdir
135                                 #
136                                 # Again, there are files in this directory, so make sure our commit includes
137                                 # both delete and add events for all of the files in this directory, but does
138                                 # not actually refer to the directories themselves.
139
140                                 assert_equal 4, deep_commits.last.token # Make sure we're checking the right revision
141
142                                 # There should be 2 files removed and two files added
143                                 assert_equal 4, deep_commits.last.diffs.size
144
145                                 assert_equal 'A', deep_commits.last.diffs[0].action
146                                 assert_equal '/newdir/bar.rb', deep_commits.last.diffs[0].path
147                                 assert_equal 'A', deep_commits.last.diffs[1].action
148                                 assert_equal '/newdir/foo.rb', deep_commits.last.diffs[1].path
149
150                                 assert_equal 'D', deep_commits.last.diffs[2].action
151                                 assert_equal '/subdir/bar.rb', deep_commits.last.diffs[2].path
152                                 assert_equal 'D', deep_commits.last.diffs[3].action
153                                 assert_equal '/subdir/foo.rb', deep_commits.last.diffs[3].path
154                         end
155                 end
156
157                 # A mini-integration test.
158                 # Check that SHA1 values are populated, directories are recursed, and outside branches are ignored.
159                 def test_each_commit
160                         commits = []
161                         with_svn_repository('svn') do |svn|
162                                 svn.each_commit do |e|
163                                         commits << e
164                                         assert e.token
165                                         assert e.committer_name
166                                         assert e.committer_date
167                                         assert e.message
168                                         assert e.diffs
169                                         e.diffs.each do |d|
170                                                 assert d.action.length == 1
171                                                 assert d.path.length > 0
172                                         end
173                                 end
174                         end
175
176                         assert_equal [1, 2, 3, 4, 5], commits.collect { |c| c.token }
177                         assert_equal ['robin','robin','robin','jason','jason'], commits.collect { |c| c.committer_name }
178
179                         assert commits[0].committer_date - Time.utc(2006,6,11,18,28,0) < 1 # commits include milliseconds
180                         assert commits[1].committer_date - Time.utc(2006,6,11,18,32,13) < 1
181                         assert commits[2].committer_date - Time.utc(2006,6,11,18,34,17) < 1
182                         assert commits[3].committer_date - Time.utc(2006,7,14,22,17,8) < 1
183                         assert commits[4].committer_date - Time.utc(2006,7,14,23,7,15) < 1
184
185                         assert_equal "Initial Checkin\n", commits[0].message
186                         assert_equal "added makefile", commits[1].message
187                         assert_equal "added some documentation and licensing info", commits[2].message
188                         assert_equal "added bs COPYING to catch global licenses", commits[3].message
189                         assert_equal "moving COPYING", commits[4].message
190
191                         assert_equal 1, commits[0].diffs.size
192                         assert_equal 'A', commits[0].diffs[0].action
193                         assert_equal '/trunk/helloworld.c', commits[0].diffs[0].path
194                         assert_equal '4c734ad53b272c9b3d719f214372ac497ff6c068', commits[0].diffs[0].sha1
195                         assert_equal '0000000000000000000000000000000000000000', commits[0].diffs[0].parent_sha1
196
197                         assert_equal 1, commits[1].diffs.size
198                         assert_equal 'A', commits[1].diffs[0].action
199                         assert_equal '/trunk/makefile', commits[1].diffs[0].path
200                         assert_equal 'af2dfd5070b01a19b672861e595de98c101c49cc', commits[1].diffs[0].sha1
201                         assert_equal '0000000000000000000000000000000000000000', commits[1].diffs[0].parent_sha1
202
203                         assert_equal 2, commits[2].diffs.size
204                         assert_equal 'A', commits[2].diffs[0].action
205                         assert_equal '/trunk/README', commits[2].diffs[0].path
206                         assert_equal 'f0547ce063095e66be74618bc410989df226d2d2', commits[2].diffs[0].sha1
207                         assert_equal '0000000000000000000000000000000000000000', commits[2].diffs[0].parent_sha1
208                         assert_equal 'M', commits[2].diffs[1].action
209                         assert_equal '/trunk/helloworld.c', commits[2].diffs[1].path
210                         assert_equal 'f6adcae4447809b651c787c078d255b2b4e963c5', commits[2].diffs[1].sha1
211                         assert_equal '4c734ad53b272c9b3d719f214372ac497ff6c068', commits[2].diffs[1].parent_sha1
212
213                         assert_equal 1, commits[3].diffs.size
214                         assert_equal 'A', commits[3].diffs[0].action
215                         assert_equal '/COPYING', commits[3].diffs[0].path
216                         assert_equal '6ff87c4664981e4397625791c8ea3bbb5f2279a3', commits[3].diffs[0].sha1
217                         assert_equal '0000000000000000000000000000000000000000', commits[3].diffs[0].parent_sha1
218
219                         assert_equal 2, commits[4].diffs.size
220                         assert_equal 'D', commits[4].diffs[0].action
221                         assert_equal '/COPYING', commits[4].diffs[0].path
222                         assert_equal '0000000000000000000000000000000000000000', commits[4].diffs[0].sha1
223                         assert_equal '6ff87c4664981e4397625791c8ea3bbb5f2279a3', commits[4].diffs[0].parent_sha1
224                         assert_equal 'A', commits[4].diffs[1].action
225                         assert_equal '/trunk/COPYING', commits[4].diffs[1].path
226                         assert_equal '6ff87c4664981e4397625791c8ea3bbb5f2279a3', commits[4].diffs[1].sha1
227                         assert_equal '0000000000000000000000000000000000000000', commits[4].diffs[1].parent_sha1
228                 end
229
230
231         end
232 end