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

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