ohloh_scm / branches / master / test / unit / svn_chain_commits_test.rb

history
require File.dirname(__FILE__) + '/../test_helper'

module Scm::Parsers
	class SvnChainTest < Scm::Test

		def test_chained_commit_tokens
			with_svn_chain_repository('svn_with_branching', '/trunk') do |svn|
				assert_equal [1,2,4,5,8,9], svn.commit_tokens
				assert_equal [2,4,5,8,9], svn.commit_tokens(1)
				assert_equal [4,5,8,9], svn.commit_tokens(2)
				assert_equal [4,5,8,9], svn.commit_tokens(3)
				assert_equal [5,8,9], svn.commit_tokens(4)
				assert_equal [8,9], svn.commit_tokens(5)
				assert_equal [8,9], svn.commit_tokens(6)
				assert_equal [8,9], svn.commit_tokens(7)
				assert_equal [9], svn.commit_tokens(8)
				assert_equal [], svn.commit_tokens(9)
				assert_equal [], svn.commit_tokens(10)
			end
		end

		def test_chained_commit_count
			with_svn_chain_repository('svn_with_branching', '/trunk') do |svn|
				assert_equal 6, svn.commit_count
				assert_equal 5, svn.commit_count(1)
				assert_equal 4, svn.commit_count(2)
				assert_equal 4, svn.commit_count(3)
				assert_equal 3, svn.commit_count(4)
				assert_equal 2, svn.commit_count(5)
				assert_equal 2, svn.commit_count(6)
				assert_equal 2, svn.commit_count(7)
				assert_equal 1, svn.commit_count(8)
				assert_equal 0, svn.commit_count(9)
			end
		end

		def test_chained_commits
			with_svn_chain_repository('svn_with_branching', '/trunk') do |svn|
				assert_equal [1,2,4,5,8,9], svn.commits.collect { |c| c.token }
				assert_equal [2,4,5,8,9], svn.commits(1).collect { |c| c.token }
				assert_equal [4,5,8,9], svn.commits(2).collect { |c| c.token }
				assert_equal [4,5,8,9], svn.commits(3).collect { |c| c.token }
				assert_equal [5,8,9], svn.commits(4).collect { |c| c.token }
				assert_equal [8,9], svn.commits(5).collect { |c| c.token }
				assert_equal [8,9], svn.commits(6).collect { |c| c.token }
				assert_equal [8,9], svn.commits(7).collect { |c| c.token }
				assert_equal [9], svn.commits(8).collect { |c| c.token }
				assert_equal [], svn.commits(9).collect { |c| c.token }
			end
		end

		# This test is primarly concerned with the checking the diffs
		# of commits. Specifically, when an entire branch is moved
		# to a new name, we should not see any diffs. From our
		# point of view the code is unchanged; only the base directory
		# has moved.
		def test_chained_each_commit
			commits = []
			with_svn_chain_repository('svn_with_branching', '/trunk') do |svn|
				svn.each_commit do |c|
					assert c.scm # To support checkout of chained commits, the
					             # commit must include a link to its containing adapter.
					commits << c
				end
			end

			assert_equal [1,2,4,5,8,9], commits.collect { |c| c.token }

			# This repository spends a lot of energy moving directories around.
			# File edits actually occur in just 3 commits.

			# Revision 1: /trunk directory created, but it is empty
			assert_equal 0, commits[0].diffs.size

			# Revision 2: /trunk/helloworld.c is added
			assert_equal 1, commits[1].diffs.size
			assert_equal 'A', commits[1].diffs.first.action
			assert_equal '/helloworld.c', commits[1].diffs.first.path

			# Revision 3: /trunk is deleted. We can't see this revision.

			# Revision 4: /trunk is re-created by copying it from revision 2.
			# From our point of view, there has been no change at all, and thus no diffs.
			assert_equal 0, commits[2].diffs.size

			# Revision 5: /branches/development is created by copying /trunk.
			# From our point of view, the contents of the repository are unchanged, so
			# no diffs result from the copy.
			# However, /branches/development/goodbyeworld.c is also created, so we should
			# have a diff for that.
			assert_equal 1, commits[3].diffs.size
			assert_equal 'A', commits[3].diffs.first.action
			assert_equal '/goodbyeworld.c', commits[3].diffs.first.path

			# Revision 6: /trunk/goodbyeworld.c is created, but we only see activity
			# on /branches/development, so no commit reported.

			# Revision 7: /trunk is deleted, but again we don't see it.

			# Revision 8: /branches/development is moved to become the new /trunk.
			# The directory contents are unchanged, so no diffs result.
			assert_equal 0, commits[4].diffs.size

			# Revision 9: an edit to /trunk/helloworld.c
			assert_equal 1, commits[5].diffs.size
			assert_equal 'M', commits[5].diffs.first.action
			assert_equal '/helloworld.c', commits[5].diffs.first.path
		end

		# Specifically tests this case:
		# Suppose we're importing /myproject/trunk, and the log
		# contains the following:
		#
		#   A /myproject (from /all/myproject:1)
		#   D /all/myproject
		#
		# We need to make sure we detect the move here, even though
		# "/myproject" is not an exact match for "/myproject/trunk".
		def test_tree_move
			with_svn_chain_repository('svn_with_tree_move', '/myproject/trunk') do |svn|
				assert_equal svn.url, svn.root + '/myproject/trunk'
				assert_equal svn.branch_name, '/myproject/trunk'

				p = svn.parent_svn
				assert_equal p.url, svn.root + '/all/myproject/trunk'
				assert_equal p.branch_name, '/all/myproject/trunk'
				assert_equal p.final_token, 1

				assert_equal [1, 2], svn.commit_tokens
			end
		end

		def test_verbose_commit_with_chaining
			with_svn_chain_repository('svn_with_branching','/trunk') do |svn|

				c = svn.verbose_commit(9)
				assert_equal 'modified helloworld.c', c.message
				assert_equal ['/helloworld.c'], c.diffs.collect { |d| d.path }
				assert_equal '/trunk', c.scm.branch_name

				c = svn.verbose_commit(8)
				assert_equal [], c.diffs
				assert_equal '/trunk', c.scm.branch_name

				# Reaching these commits requires chaining
				c = svn.verbose_commit(5)
				assert_equal 'add a new branch, with goodbyeworld.c', c.message
				assert_equal ['/goodbyeworld.c'], c.diffs.collect { |d| d.path }
				assert_equal '/branches/development', c.scm.branch_name

				# Reaching these commits requires chaining twice
				c = svn.verbose_commit(4)
				assert_equal [], c.diffs
				assert_equal '/trunk', c.scm.branch_name

				# And now a fourth chain (to skip over /trunk deletion in rev 3)
				c = svn.verbose_commit(2)
				assert_equal 'Added helloworld.c to trunk', c.message
				assert_equal ['/helloworld.c'], c.diffs.collect { |d| d.path }
				assert_equal '/trunk', c.scm.branch_name

				c = svn.verbose_commit(1)
				assert_equal [], c.diffs
				assert_equal '/trunk', c.scm.branch_name
			end
		end
	end
end