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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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 = repository_path('bazaar')
  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. Encoding.locale_charmap == "ISO-8859-1")
  40. CHAR_1_UTF8_HEX = "\xc3\x9c".force_encoding('UTF-8')
  41. CHAR_1_LATIN1_HEX = "\xdc".force_encoding('ASCII-8BIT')
  42. def setup
  43. User.current = nil
  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. end
  50. def test_blank_path_to_repository_error_message
  51. set_language_if_valid 'en'
  52. repo = Repository::Bazaar.new(
  53. :project => @project,
  54. :identifier => 'test',
  55. :log_encoding => 'UTF-8'
  56. )
  57. assert !repo.save
  58. assert_include "Path to repository cannot be blank",
  59. repo.errors.full_messages
  60. end
  61. def test_blank_path_to_repository_error_message_fr
  62. set_language_if_valid 'fr'
  63. str = "Chemin du d\xc3\xa9p\xc3\xb4t doit \xc3\xaatre renseign\xc3\xa9(e)".force_encoding('UTF-8')
  64. repo = Repository::Bazaar.new(
  65. :project => @project,
  66. :url => "",
  67. :identifier => 'test',
  68. :log_encoding => 'UTF-8'
  69. )
  70. assert !repo.save
  71. assert_include str, repo.errors.full_messages
  72. end
  73. if File.directory?(REPOSITORY_PATH_TRUNK)
  74. def test_fetch_changesets_from_scratch
  75. assert_equal 0, @repository.changesets.count
  76. @repository.fetch_changesets
  77. @project.reload
  78. assert_equal NUM_REV, @repository.changesets.count
  79. assert_equal 9, @repository.filechanges.count
  80. assert_equal 'Initial import', @repository.changesets.find_by_revision('1').comments
  81. end
  82. def test_fetch_changesets_incremental
  83. assert_equal 0, @repository.changesets.count
  84. @repository.fetch_changesets
  85. @project.reload
  86. assert_equal NUM_REV, @repository.changesets.count
  87. # Remove changesets with revision > 5
  88. @repository.changesets.each {|c| c.destroy if c.revision.to_i > 2}
  89. @project.reload
  90. @repository.reload
  91. assert_equal 2, @repository.changesets.count
  92. @repository.fetch_changesets
  93. @project.reload
  94. assert_equal NUM_REV, @repository.changesets.count
  95. end
  96. def test_entries
  97. entries = @repository.entries
  98. assert_kind_of Redmine::Scm::Adapters::Entries, entries
  99. assert_equal 2, entries.size
  100. assert_equal 'dir', entries[0].kind
  101. assert_equal 'directory', entries[0].name
  102. assert_equal 'directory', entries[0].path
  103. assert_equal 'file', entries[1].kind
  104. assert_equal 'doc-mkdir.txt', entries[1].name
  105. assert_equal 'doc-mkdir.txt', entries[1].path
  106. end
  107. def test_entries_in_subdirectory
  108. entries = @repository.entries('directory')
  109. assert_equal 3, entries.size
  110. assert_equal 'file', entries.last.kind
  111. assert_equal 'edit.png', entries.last.name
  112. assert_equal 'directory/edit.png', entries.last.path
  113. end
  114. def test_previous
  115. assert_equal 0, @repository.changesets.count
  116. @repository.fetch_changesets
  117. @project.reload
  118. assert_equal NUM_REV, @repository.changesets.count
  119. changeset = @repository.find_changeset_by_name('3')
  120. assert_equal @repository.find_changeset_by_name('2'), changeset.previous
  121. end
  122. def test_previous_nil
  123. assert_equal 0, @repository.changesets.count
  124. @repository.fetch_changesets
  125. @project.reload
  126. assert_equal NUM_REV, @repository.changesets.count
  127. changeset = @repository.find_changeset_by_name('1')
  128. assert_nil changeset.previous
  129. end
  130. def test_next
  131. assert_equal 0, @repository.changesets.count
  132. @repository.fetch_changesets
  133. @project.reload
  134. assert_equal NUM_REV, @repository.changesets.count
  135. changeset = @repository.find_changeset_by_name('2')
  136. assert_equal @repository.find_changeset_by_name('3'), changeset.next
  137. end
  138. def test_next_nil
  139. assert_equal 0, @repository.changesets.count
  140. @repository.fetch_changesets
  141. @project.reload
  142. assert_equal NUM_REV, @repository.changesets.count
  143. changeset = @repository.find_changeset_by_name('4')
  144. assert_nil changeset.next
  145. end
  146. if File.directory?(REPOSITORY_PATH_NON_ASCII) && RUN_LATIN1_OUTPUT_TEST
  147. def test_cat_latin1_path
  148. latin1_repo = create_latin1_repo
  149. buf = latin1_repo.cat(
  150. "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-2.txt", 2)
  151. assert buf
  152. lines = buf.split("\n")
  153. assert_equal 2, lines.length
  154. assert_equal 'It is written in Python.', lines[1]
  155. buf = latin1_repo.cat(
  156. "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt", 2)
  157. assert buf
  158. lines = buf.split("\n")
  159. assert_equal 1, lines.length
  160. assert_equal "test-#{CHAR_1_LATIN1_HEX}.txt", lines[0]
  161. end
  162. def test_annotate_latin1_path
  163. latin1_repo = create_latin1_repo
  164. ann1 = latin1_repo.annotate(
  165. "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-2.txt", 2)
  166. assert_equal 2, ann1.lines.size
  167. assert_equal '2', ann1.revisions[0].identifier
  168. assert_equal 'test00@', ann1.revisions[0].author
  169. assert_equal 'It is written in Python.', ann1.lines[1]
  170. ann2 = latin1_repo.annotate(
  171. "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt", 2)
  172. assert_equal 1, ann2.lines.size
  173. assert_equal '2', ann2.revisions[0].identifier
  174. assert_equal 'test00@', ann2.revisions[0].author
  175. assert_equal "test-#{CHAR_1_LATIN1_HEX}.txt", ann2.lines[0]
  176. end
  177. def test_diff_latin1_path
  178. latin1_repo = create_latin1_repo
  179. diff1 = latin1_repo.diff(
  180. "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt", 2, 1)
  181. assert_equal 7, diff1.size
  182. buf = diff1[5].gsub(/\r\n|\r|\n/, "")
  183. assert_equal "+test-#{CHAR_1_LATIN1_HEX}.txt", buf
  184. end
  185. def test_entries_latin1_path
  186. latin1_repo = create_latin1_repo
  187. entries = latin1_repo.entries("test-#{CHAR_1_UTF8_HEX}-dir", 2)
  188. assert_kind_of Redmine::Scm::Adapters::Entries, entries
  189. assert_equal 3, entries.size
  190. assert_equal 'file', entries[1].kind
  191. assert_equal "test-#{CHAR_1_UTF8_HEX}-1.txt", entries[0].name
  192. assert_equal "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt", entries[0].path
  193. end
  194. def test_entry_latin1_path
  195. latin1_repo = create_latin1_repo
  196. ["test-#{CHAR_1_UTF8_HEX}-dir",
  197. "/test-#{CHAR_1_UTF8_HEX}-dir",
  198. "/test-#{CHAR_1_UTF8_HEX}-dir/"
  199. ].each do |path|
  200. entry = latin1_repo.entry(path, 2)
  201. assert_equal "test-#{CHAR_1_UTF8_HEX}-dir", entry.path
  202. assert_equal "dir", entry.kind
  203. end
  204. ["test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt",
  205. "/test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt"
  206. ].each do |path|
  207. entry = latin1_repo.entry(path, 2)
  208. assert_equal "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt",
  209. entry.path
  210. assert_equal "file", entry.kind
  211. end
  212. end
  213. def test_changeset_latin1_path
  214. latin1_repo = create_latin1_repo
  215. assert_equal 0, latin1_repo.changesets.count
  216. latin1_repo.fetch_changesets
  217. @project.reload
  218. assert_equal 3, latin1_repo.changesets.count
  219. cs2 = latin1_repo.changesets.find_by_revision('2')
  220. assert_not_nil cs2
  221. assert_equal "test-#{CHAR_1_UTF8_HEX}", cs2.comments
  222. c2 = cs2.filechanges.sort_by(&:path)
  223. assert_equal 4, c2.size
  224. assert_equal 'A', c2[0].action
  225. assert_equal "/test-#{CHAR_1_UTF8_HEX}-dir/", c2[0].path
  226. assert_equal 'A', c2[1].action
  227. assert_equal "/test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt", c2[1].path
  228. assert_equal 'A', c2[2].action
  229. assert_equal "/test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-2.txt", c2[2].path
  230. assert_equal 'A', c2[3].action
  231. assert_equal "/test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}.txt", c2[3].path
  232. cs3 = latin1_repo.changesets.find_by_revision('3')
  233. assert_not_nil cs3
  234. assert_equal "modify, move and delete #{CHAR_1_UTF8_HEX} files", cs3.comments
  235. c3 = cs3.filechanges.sort_by(&:path)
  236. assert_equal 3, c3.size
  237. assert_equal 'M', c3[0].action
  238. assert_equal "/test-#{CHAR_1_UTF8_HEX}-1.txt", c3[0].path
  239. assert_equal 'D', c3[1].action
  240. assert_equal "/test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-2.txt", c3[1].path
  241. assert_equal 'M', c3[2].action
  242. assert_equal "/test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}.txt", c3[2].path
  243. end
  244. else
  245. msg = "Bazaar non ASCII output test cannot run on this environment.\n"
  246. msg += "Encoding.locale_charmap: " + Encoding.locale_charmap + "\n"
  247. puts msg
  248. end
  249. private
  250. def create_latin1_repo
  251. repo = Repository::Bazaar.create(
  252. :project => @project,
  253. :identifier => 'latin1',
  254. :url => REPOSITORY_PATH_NON_ASCII,
  255. :log_encoding => 'ISO-8859-1'
  256. )
  257. assert repo
  258. repo
  259. end
  260. else
  261. puts "Bazaar test repository NOT FOUND. Skipping unit tests !!!"
  262. def test_fake; assert true end
  263. end
  264. end