PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/source_control_test.rb

https://github.com/thiyagarajan/cruisecontrol.rb
Ruby | 175 lines | 147 code | 28 blank | 0 comment | 0 complexity | 2c2096ceccff4909aee8b87ad73229ae MD5 | raw file
  1. require 'test_helper'
  2. class SourceControlTest < Test::Unit::TestCase
  3. include FileSandbox
  4. def test_create_should_require_presence_of_url_in_options
  5. in_sandbox do
  6. assert_raise(ArgumentError, "options should include repository") do
  7. scm = SourceControl.create({:repository => nil})
  8. end
  9. end
  10. end
  11. def test_create_should_default_to_git
  12. in_sandbox do
  13. SourceControl::Git.expects(:new).with(:repository => "http://my_repo").returns(:foo)
  14. assert_equal :foo, SourceControl.create(:repository => "http://my_repo")
  15. end
  16. end
  17. def test_create_should_return_git_adapter_for_git_url
  18. in_sandbox do
  19. SourceControl::Git.expects(:new).with(:repository => "git://my_repo").returns(:foo)
  20. assert_equal :foo, SourceControl.create(:repository => "git://my_repo")
  21. end
  22. end
  23. def test_create_should_return_svn_adapter_for_svn_url
  24. in_sandbox do
  25. SourceControl::Subversion.expects(:new).with(:repository => "svn://my_repo").returns(:foo)
  26. assert_equal :foo, SourceControl.create(:repository => "svn://my_repo")
  27. end
  28. end
  29. def test_create_should_return_svn_adapter_for_svn_ssh_url
  30. in_sandbox do
  31. SourceControl::Subversion.expects(:new).with(:repository => "svn+ssh://my_repo").returns(:foo)
  32. assert_equal :foo, SourceControl.create(:repository => "svn+ssh://my_repo")
  33. end
  34. end
  35. def test_create_should_return_a_git_instance_if_asked_to_do_so
  36. in_sandbox do
  37. SourceControl::Git.expects(:new).with(:repository => "http://my_repo").returns(:foo)
  38. assert_equal :foo, SourceControl.create(:repository => "http://my_repo", :source_control => 'git')
  39. end
  40. end
  41. def test_create_should_return_a_subversion_instance_if_asked_to_do_so
  42. in_sandbox do
  43. SourceControl::Subversion.expects(:new).with(:repository => "http://my_repo").returns(:foo)
  44. assert_equal :foo, SourceControl.create(:repository => "http://my_repo", :source_control => 'subversion')
  45. end
  46. end
  47. def test_create_should_return_a_subversion_instance_if_asked_to_do_so_in_abbreviated_way
  48. in_sandbox do
  49. SourceControl::Subversion.expects(:new).with(:repository => "http://my_repo").returns(:foo)
  50. assert_equal :foo, SourceControl.create(:repository => "http://my_repo", :source_control => 'svn')
  51. end
  52. end
  53. def test_create_should_return_mercurial_adapter_if_asked_to_do_so
  54. in_sandbox do
  55. SourceControl::Mercurial.expects(:new).with(:repository => "http://my_repo").returns(:foo)
  56. assert_equal :foo, SourceControl.create(:repository => "http://my_repo", :source_control => 'mercurial')
  57. end
  58. end
  59. def test_create_should_return_mercurial_adapter_if_asked_to_do_so_in_abbreviated_way
  60. in_sandbox do
  61. SourceControl::Mercurial.expects(:new).with(:repository => "http://my_repo").returns(:foo)
  62. assert_equal :foo, SourceControl.create(:repository => "http://my_repo", :source_control => 'hg')
  63. end
  64. end
  65. def test_create_should_blow_up_if_given_a_non_recognized_source_control_string
  66. in_sandbox do
  67. assert_raise RuntimeError do
  68. SourceControl.create(:repository => "http://my_repo", :source_control => 'not_a_scm')
  69. end
  70. end
  71. end
  72. def test_create_should_blow_up_if_given_class_that_can_be_constantized_but_is_not_a_scm_adapter
  73. in_sandbox do
  74. assert_raise RuntimeError do
  75. SourceControl.create(:repository => "http://my_repo", :source_control => "String")
  76. end
  77. end
  78. end
  79. def test_create_should_return_adapter_according_to_scm_type_when_it_contradicts_url
  80. in_sandbox do
  81. SourceControl::Subversion.expects(:new).with(:repository => "git://my_repo").returns(:foo)
  82. assert_equal :foo, SourceControl.create(:repository => "git://my_repo", :source_control => 'svn')
  83. end
  84. end
  85. def test_detect_should_identify_git_repository_by_presence_of_dotgit_directory
  86. in_sandbox do
  87. File.expects(:directory?).with(File.join('./Proj1/work', '.git')).returns(true)
  88. File.expects(:directory?).with(File.join('./Proj1/work', '.svn')).returns(false)
  89. File.expects(:directory?).with(File.join('./Proj1/work', '.hg')).returns(false)
  90. File.expects(:directory?).with(File.join('./Proj1/work', '.bzr')).returns(false)
  91. SourceControl::Git.expects(:new).with(:path => './Proj1/work').returns(:git_instance)
  92. assert_equal :git_instance, SourceControl.detect('./Proj1/work')
  93. end
  94. end
  95. def test_detect_should_identify_mercurial_repository_by_presence_of_dothg_directory
  96. in_sandbox do
  97. File.expects(:directory?).with(File.join('./Proj1/work', '.git')).returns(false)
  98. File.expects(:directory?).with(File.join('./Proj1/work', '.svn')).returns(false)
  99. File.expects(:directory?).with(File.join('./Proj1/work', '.hg')).returns(true)
  100. File.expects(:directory?).with(File.join('./Proj1/work', '.bzr')).returns(false)
  101. SourceControl::Mercurial.expects(:new).with(:path => './Proj1/work').returns(:hg_instance)
  102. assert_equal :hg_instance, SourceControl.detect('./Proj1/work')
  103. end
  104. end
  105. def test_detect_should_identify_subversion_repository_by_presence_of_dotsvn_directory
  106. in_sandbox do
  107. File.expects(:directory?).with(File.join('./Proj1/work', '.git')).returns(false)
  108. File.expects(:directory?).with(File.join('./Proj1/work', '.svn')).returns(true)
  109. File.expects(:directory?).with(File.join('./Proj1/work', '.hg')).returns(false)
  110. File.expects(:directory?).with(File.join('./Proj1/work', '.bzr')).returns(false)
  111. SourceControl::Subversion.expects(:new).with(:path => './Proj1/work').returns(:svn_instance)
  112. assert_equal :svn_instance, SourceControl.detect('./Proj1/work')
  113. end
  114. end
  115. def test_detect_should_identify_bazaar_repository_by_presence_of_dotbzr_directory
  116. in_sandbox do
  117. File.expects(:directory?).with(File.join('./Proj1/work', '.git')).returns(false)
  118. File.expects(:directory?).with(File.join('./Proj1/work', '.svn')).returns(false)
  119. File.expects(:directory?).with(File.join('./Proj1/work', '.hg')).returns(false)
  120. File.expects(:directory?).with(File.join('./Proj1/work', '.bzr')).returns(true)
  121. SourceControl::Bazaar.expects(:new).with(:path => './Proj1/work').returns(:bzr_instance)
  122. assert_equal :bzr_instance, SourceControl.detect('./Proj1/work')
  123. end
  124. end
  125. def test_detect_should_blow_up_if_there_is_neither_subversion_nor_git
  126. in_sandbox do
  127. File.expects(:directory?).with(File.join('./Proj1/work', '.git')).returns(false)
  128. File.expects(:directory?).with(File.join('./Proj1/work', '.svn')).returns(false)
  129. File.expects(:directory?).with(File.join('./Proj1/work', '.hg')).returns(false)
  130. File.expects(:directory?).with(File.join('./Proj1/work', '.bzr')).returns(false)
  131. assert_raise RuntimeError, "Could not detect the type of source control in ./Proj1/work" do
  132. SourceControl.detect('./Proj1/work')
  133. end
  134. end
  135. end
  136. def test_detect_should_blow_up_if_there_is_both_subversion_and_git
  137. in_sandbox do
  138. File.expects(:directory?).with(File.join('./Proj1/work', '.git')).returns(true)
  139. File.expects(:directory?).with(File.join('./Proj1/work', '.svn')).returns(true)
  140. File.expects(:directory?).with(File.join('./Proj1/work', '.hg')).returns(false)
  141. File.expects(:directory?).with(File.join('./Proj1/work', '.bzr')).returns(false)
  142. assert_raise RuntimeError, "More than one type of source control was detected in ./Proj1/work" do
  143. SourceControl.detect('./Proj1/work')
  144. end
  145. end
  146. end
  147. end