You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repository_bazaar_test.rb 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2014 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class RepositoryBazaarTest < ActiveSupport::TestCase
  19. fixtures :projects
  20. include Redmine::I18n
  21. REPOSITORY_PATH = Rails.root.join('tmp/test/bazaar_repository').to_s
  22. REPOSITORY_PATH_TRUNK = File.join(REPOSITORY_PATH, "trunk")
  23. NUM_REV = 4
  24. REPOSITORY_PATH_NON_ASCII = Rails.root.join(REPOSITORY_PATH + '/' + 'non_ascii').to_s
  25. # Bazaar core does not support xml output such as Subversion and Mercurial.
  26. # "bzr" command output and command line parameter depend on locale.
  27. # So, non ASCII path tests cannot run independent locale.
  28. #
  29. # If you want to run Bazaar non ASCII path tests on Linux *Ruby 1.9*,
  30. # you need to set locale character set "ISO-8859-1".
  31. # E.g. "LANG=en_US.ISO-8859-1".
  32. # On Linux other platforms (e.g. Ruby 1.8, JRuby),
  33. # you need to set "RUN_LATIN1_OUTPUT_TEST = true" manually.
  34. #
  35. # On Windows, because it is too hard to change system locale,
  36. # you cannot run Bazaar non ASCII path tests.
  37. #
  38. RUN_LATIN1_OUTPUT_TEST = (RUBY_PLATFORM != 'java' &&
  39. REPOSITORY_PATH.respond_to?(:force_encoding) &&
  40. Encoding.locale_charmap == "ISO-8859-1")
  41. CHAR_1_UTF8_HEX = "\xc3\x9c"
  42. CHAR_1_LATIN1_HEX = "\xdc"
  43. def setup
  44. @project = Project.find(3)
  45. @repository = Repository::Bazaar.create(
  46. :project => @project, :url => REPOSITORY_PATH_TRUNK,
  47. :log_encoding => 'UTF-8')
  48. assert @repository
  49. @char_1_utf8 = CHAR_1_UTF8_HEX.dup
  50. @char_1_ascii8bit = CHAR_1_LATIN1_HEX.dup
  51. if @char_1_utf8.respond_to?(:force_encoding)
  52. @char_1_utf8.force_encoding('UTF-8')
  53. @char_1_ascii8bit.force_encoding('ASCII-8BIT')
  54. end
  55. end
  56. def test_blank_path_to_repository_error_message
  57. set_language_if_valid 'en'
  58. repo = Repository::Bazaar.new(
  59. :project => @project,
  60. :identifier => 'test',
  61. :log_encoding => 'UTF-8'
  62. )
  63. assert !repo.save
  64. assert_include "Path to repository can't be blank",
  65. repo.errors.full_messages
  66. end
  67. def test_blank_path_to_repository_error_message_fr
  68. set_language_if_valid 'fr'
  69. str = "Chemin du d\xc3\xa9p\xc3\xb4t doit \xc3\xaatre renseign\xc3\xa9(e)"
  70. str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
  71. repo = Repository::Bazaar.new(
  72. :project => @project,
  73. :url => "",
  74. :identifier => 'test',
  75. :log_encoding => 'UTF-8'
  76. )
  77. assert !repo.save
  78. assert_include str, repo.errors.full_messages
  79. end
  80. if File.directory?(REPOSITORY_PATH_TRUNK)
  81. def test_fetch_changesets_from_scratch
  82. assert_equal 0, @repository.changesets.count
  83. @repository.fetch_changesets
  84. @project.reload
  85. assert_equal NUM_REV, @repository.changesets.count
  86. assert_equal 9, @repository.filechanges.count
  87. assert_equal 'Initial import', @repository.changesets.find_by_revision('1').comments
  88. end
  89. def test_fetch_changesets_incremental
  90. assert_equal 0, @repository.changesets.count
  91. @repository.fetch_changesets
  92. @project.reload
  93. assert_equal NUM_REV, @repository.changesets.count
  94. # Remove changesets with revision > 5
  95. @repository.changesets.all.each {|c| c.destroy if c.revision.to_i > 2}
  96. @project.reload
  97. assert_equal 2, @repository.changesets.count
  98. @repository.fetch_changesets
  99. @project.reload
  100. assert_equal NUM_REV, @repository.changesets.count
  101. end
  102. def test_entries
  103. entries = @repository.entries
  104. assert_kind_of Redmine::Scm::Adapters::Entries, entries
  105. assert_equal 2, entries.size
  106. assert_equal 'dir', entries[0].kind
  107. assert_equal 'directory', entries[0].name
  108. assert_equal 'directory', entries[0].path
  109. assert_equal 'file', entries[1].kind
  110. assert_equal 'doc-mkdir.txt', entries[1].name
  111. assert_equal 'doc-mkdir.txt', entries[1].path
  112. end
  113. def test_entries_in_subdirectory
  114. entries = @repository.entries('directory')
  115. assert_equal 3, entries.size
  116. assert_equal 'file', entries.last.kind
  117. assert_equal 'edit.png', entries.last.name
  118. assert_equal 'directory/edit.png', entries.last.path
  119. end
  120. def test_previous
  121. assert_equal 0, @repository.changesets.count
  122. @repository.fetch_changesets
  123. @project.reload
  124. assert_equal NUM_REV, @repository.changesets.count
  125. changeset = @repository.find_changeset_by_name('3')
  126. assert_equal @repository.find_changeset_by_name('2'), changeset.previous
  127. end
  128. def test_previous_nil
  129. assert_equal 0, @repository.changesets.count
  130. @repository.fetch_changesets
  131. @project.reload
  132. assert_equal NUM_REV, @repository.changesets.count
  133. changeset = @repository.find_changeset_by_name('1')
  134. assert_nil changeset.previous
  135. end
  136. def test_next
  137. assert_equal 0, @repository.changesets.count
  138. @repository.fetch_changesets
  139. @project.reload
  140. assert_equal NUM_REV, @repository.changesets.count
  141. changeset = @repository.find_changeset_by_name('2')
  142. assert_equal @repository.find_changeset_by_name('3'), changeset.next
  143. end
  144. def test_next_nil
  145. assert_equal 0, @repository.changesets.count
  146. @repository.fetch_changesets
  147. @project.reload
  148. assert_equal NUM_REV, @repository.changesets.count
  149. changeset = @repository.find_changeset_by_name('4')
  150. assert_nil changeset.next
  151. end
  152. if File.directory?(REPOSITORY_PATH_NON_ASCII) && RUN_LATIN1_OUTPUT_TEST
  153. def test_cat_latin1_path
  154. latin1_repo = create_latin1_repo
  155. buf = latin1_repo.cat(
  156. "test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}-2.txt", 2)
  157. assert buf
  158. lines = buf.split("\n")
  159. assert_equal 2, lines.length
  160. assert_equal 'It is written in Python.', lines[1]
  161. buf = latin1_repo.cat(
  162. "test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}-1.txt", 2)
  163. assert buf
  164. lines = buf.split("\n")
  165. assert_equal 1, lines.length
  166. assert_equal "test-#{@char_1_ascii8bit}.txt", lines[0]
  167. end
  168. def test_annotate_latin1_path
  169. latin1_repo = create_latin1_repo
  170. ann1 = latin1_repo.annotate(
  171. "test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}-2.txt", 2)
  172. assert_equal 2, ann1.lines.size
  173. assert_equal '2', ann1.revisions[0].identifier
  174. assert_equal 'test00@', ann1.revisions[0].author
  175. assert_equal 'It is written in Python.', ann1.lines[1]
  176. ann2 = latin1_repo.annotate(
  177. "test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}-1.txt", 2)
  178. assert_equal 1, ann2.lines.size
  179. assert_equal '2', ann2.revisions[0].identifier
  180. assert_equal 'test00@', ann2.revisions[0].author
  181. assert_equal "test-#{@char_1_ascii8bit}.txt", ann2.lines[0]
  182. end
  183. def test_diff_latin1_path
  184. latin1_repo = create_latin1_repo
  185. diff1 = latin1_repo.diff(
  186. "test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}-1.txt", 2, 1)
  187. assert_equal 7, diff1.size
  188. buf = diff1[5].gsub(/\r\n|\r|\n/, "")
  189. assert_equal "+test-#{@char_1_ascii8bit}.txt", buf
  190. end
  191. def test_entries_latin1_path
  192. latin1_repo = create_latin1_repo
  193. entries = latin1_repo.entries("test-#{@char_1_utf8}-dir", 2)
  194. assert_kind_of Redmine::Scm::Adapters::Entries, entries
  195. assert_equal 3, entries.size
  196. assert_equal 'file', entries[1].kind
  197. assert_equal "test-#{@char_1_utf8}-1.txt", entries[0].name
  198. assert_equal "test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}-1.txt", entries[0].path
  199. end
  200. def test_entry_latin1_path
  201. latin1_repo = create_latin1_repo
  202. ["test-#{@char_1_utf8}-dir",
  203. "/test-#{@char_1_utf8}-dir",
  204. "/test-#{@char_1_utf8}-dir/"
  205. ].each do |path|
  206. entry = latin1_repo.entry(path, 2)
  207. assert_equal "test-#{@char_1_utf8}-dir", entry.path
  208. assert_equal "dir", entry.kind
  209. end
  210. ["test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}-1.txt",
  211. "/test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}-1.txt"
  212. ].each do |path|
  213. entry = latin1_repo.entry(path, 2)
  214. assert_equal "test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}-1.txt",
  215. entry.path
  216. assert_equal "file", entry.kind
  217. end
  218. end
  219. def test_changeset_latin1_path
  220. latin1_repo = create_latin1_repo
  221. assert_equal 0, latin1_repo.changesets.count
  222. latin1_repo.fetch_changesets
  223. @project.reload
  224. assert_equal 3, latin1_repo.changesets.count
  225. cs2 = latin1_repo.changesets.find_by_revision('2')
  226. assert_not_nil cs2
  227. assert_equal "test-#{@char_1_utf8}", cs2.comments
  228. c2 = cs2.filechanges.sort_by(&:path)
  229. assert_equal 4, c2.size
  230. assert_equal 'A', c2[0].action
  231. assert_equal "/test-#{@char_1_utf8}-dir/", c2[0].path
  232. assert_equal 'A', c2[1].action
  233. assert_equal "/test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}-1.txt", c2[1].path
  234. assert_equal 'A', c2[2].action
  235. assert_equal "/test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}-2.txt", c2[2].path
  236. assert_equal 'A', c2[3].action
  237. assert_equal "/test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}.txt", c2[3].path
  238. cs3 = latin1_repo.changesets.find_by_revision('3')
  239. assert_not_nil cs3
  240. assert_equal "modify, move and delete #{@char_1_utf8} files", cs3.comments
  241. c3 = cs3.filechanges.sort_by(&:path)
  242. assert_equal 3, c3.size
  243. assert_equal 'M', c3[0].action
  244. assert_equal "/test-#{@char_1_utf8}-1.txt", c3[0].path
  245. assert_equal 'D', c3[1].action
  246. assert_equal "/test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}-2.txt", c3[1].path
  247. assert_equal 'M', c3[2].action
  248. assert_equal "/test-#{@char_1_utf8}-dir/test-#{@char_1_utf8}.txt", c3[2].path
  249. end
  250. else
  251. msg = "Bazaar non ASCII output test cannot run this environment." + "\n"
  252. if msg.respond_to?(:force_encoding)
  253. msg += "Encoding.locale_charmap: " + Encoding.locale_charmap + "\n"
  254. end
  255. puts msg
  256. end
  257. private
  258. def create_latin1_repo
  259. repo = Repository::Bazaar.create(
  260. :project => @project,
  261. :identifier => 'latin1',
  262. :url => REPOSITORY_PATH_NON_ASCII,
  263. :log_encoding => 'ISO-8859-1'
  264. )
  265. assert repo
  266. repo
  267. end
  268. else
  269. puts "Bazaar test repository NOT FOUND. Skipping unit tests !!!"
  270. def test_fake; assert true end
  271. end
  272. end