repository_bazaar_test.rb 11.1 KB
Newer Older
1
# frozen_string_literal: true
2

3
# Redmine - project management software
jplang's avatar
jplang committed
4
# Copyright (C) 2006-2017  Jean-Philippe Lang
5 6 7 8 9
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
10
#
11 12 13 14
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
15
#
16 17 18 19
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

20
require File.expand_path('../../test_helper', __FILE__)
21

22
class RepositoryBazaarTest < ActiveSupport::TestCase
23
  fixtures :projects
24

25 26
  include Redmine::I18n

27
  REPOSITORY_PATH = repository_path('bazaar')
28
  REPOSITORY_PATH_TRUNK = File.join(REPOSITORY_PATH, "trunk")
29
  NUM_REV = 4
30

31 32 33 34
  REPOSITORY_PATH_NON_ASCII = Rails.root.join(REPOSITORY_PATH + '/' + 'non_ascii').to_s

  # Bazaar core does not support xml output such as Subversion and Mercurial.
  # "bzr" command output and command line parameter depend on locale.
35
  # So, non ASCII path tests cannot run independent locale.
36
  #
37
  # If you want to run Bazaar non ASCII path tests on Linux *Ruby 1.9*,
38 39
  # you need to set locale character set "ISO-8859-1".
  # E.g. "LANG=en_US.ISO-8859-1".
40
  # On Linux other platforms (e.g. Ruby 1.8, JRuby),
41 42 43 44 45 46 47 48
  # you need to set "RUN_LATIN1_OUTPUT_TEST = true" manually.
  #
  # On Windows, because it is too hard to change system locale,
  # you cannot run Bazaar non ASCII path tests.
  #
  RUN_LATIN1_OUTPUT_TEST = (RUBY_PLATFORM != 'java' &&
                             Encoding.locale_charmap == "ISO-8859-1")

49
  CHAR_1_UTF8_HEX   = 'Ü'
50
  CHAR_1_LATIN1_HEX = "\xdc".b
51

52
  def setup
53
    User.current = nil
54
    @project = Project.find(3)
55
    @repository = Repository::Bazaar.create(
56
              :project => @project, :url => REPOSITORY_PATH_TRUNK,
57 58
              :log_encoding => 'UTF-8')
    assert @repository
59
  end
60

61 62 63 64 65 66 67 68
  def test_blank_path_to_repository_error_message
    set_language_if_valid 'en'
    repo = Repository::Bazaar.new(
                          :project      => @project,
                          :identifier   => 'test',
                          :log_encoding => 'UTF-8'
                        )
    assert !repo.save
69
    assert_include "Path to repository cannot be blank",
70 71 72 73 74 75 76 77 78 79 80 81
                   repo.errors.full_messages
  end

  def test_blank_path_to_repository_error_message_fr
    set_language_if_valid 'fr'
    repo = Repository::Bazaar.new(
                          :project      => @project,
                          :url          => "",
                          :identifier   => 'test',
                          :log_encoding => 'UTF-8'
                        )
    assert !repo.save
82
    assert_include 'Chemin du dépôt doit être renseigné(e)', repo.errors.full_messages
83 84
  end

85
  if File.directory?(REPOSITORY_PATH_TRUNK)
86
    def test_fetch_changesets_from_scratch
87
      assert_equal 0, @repository.changesets.count
88
      @repository.fetch_changesets
89
      @project.reload
90

91
      assert_equal NUM_REV, @repository.changesets.count
92
      assert_equal 9, @repository.filechanges.count
93
      assert_equal 'Initial import', @repository.changesets.find_by_revision('1').comments
94
    end
95

96
    def test_fetch_changesets_incremental
97
      assert_equal 0, @repository.changesets.count
98
      @repository.fetch_changesets
99 100
      @project.reload
      assert_equal NUM_REV, @repository.changesets.count
101
      # Remove changesets with revision > 5
102
      @repository.changesets.each {|c| c.destroy if c.revision.to_i > 2}
103
      @project.reload
104
      @repository.reload
105
      assert_equal 2, @repository.changesets.count
106

107
      @repository.fetch_changesets
108 109
      @project.reload
      assert_equal NUM_REV, @repository.changesets.count
110
    end
111

112 113
    def test_entries
      entries = @repository.entries
114
      assert_kind_of Redmine::Scm::Adapters::Entries, entries
115
      assert_equal 2, entries.size
116

117 118
      assert_equal 'dir', entries[0].kind
      assert_equal 'directory', entries[0].name
119
      assert_equal 'directory', entries[0].path
120

121 122
      assert_equal 'file', entries[1].kind
      assert_equal 'doc-mkdir.txt', entries[1].name
123
      assert_equal 'doc-mkdir.txt', entries[1].path
124
    end
125

126 127 128 129 130 131
    def test_entries_in_subdirectory
      entries = @repository.entries('directory')
      assert_equal 3, entries.size

      assert_equal 'file', entries.last.kind
      assert_equal 'edit.png', entries.last.name
132
      assert_equal 'directory/edit.png', entries.last.path
133
    end
134 135

    def test_previous
136
      assert_equal 0, @repository.changesets.count
137
      @repository.fetch_changesets
138 139
      @project.reload
      assert_equal NUM_REV, @repository.changesets.count
140 141 142 143 144
      changeset = @repository.find_changeset_by_name('3')
      assert_equal @repository.find_changeset_by_name('2'), changeset.previous
    end

    def test_previous_nil
145
      assert_equal 0, @repository.changesets.count
146
      @repository.fetch_changesets
147 148
      @project.reload
      assert_equal NUM_REV, @repository.changesets.count
149 150 151 152 153
      changeset = @repository.find_changeset_by_name('1')
      assert_nil changeset.previous
    end

    def test_next
154
      assert_equal 0, @repository.changesets.count
155
      @repository.fetch_changesets
156 157
      @project.reload
      assert_equal NUM_REV, @repository.changesets.count
158 159 160 161 162
      changeset = @repository.find_changeset_by_name('2')
      assert_equal @repository.find_changeset_by_name('3'), changeset.next
    end

    def test_next_nil
163
      assert_equal 0, @repository.changesets.count
164
      @repository.fetch_changesets
165 166
      @project.reload
      assert_equal NUM_REV, @repository.changesets.count
167 168 169
      changeset = @repository.find_changeset_by_name('4')
      assert_nil changeset.next
    end
170 171 172 173 174

    if File.directory?(REPOSITORY_PATH_NON_ASCII) && RUN_LATIN1_OUTPUT_TEST
      def test_cat_latin1_path
        latin1_repo = create_latin1_repo
        buf = latin1_repo.cat(
175
                 "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-2.txt", 2)
176 177 178 179 180 181
        assert buf
        lines = buf.split("\n")
        assert_equal 2, lines.length
        assert_equal 'It is written in Python.', lines[1]

        buf = latin1_repo.cat(
182
                 "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt", 2)
183 184 185
        assert buf
        lines = buf.split("\n")
        assert_equal 1, lines.length
186
        assert_equal "test-#{CHAR_1_LATIN1_HEX}.txt", lines[0]
187 188 189 190 191
      end

      def test_annotate_latin1_path
        latin1_repo = create_latin1_repo
        ann1 = latin1_repo.annotate(
192
                   "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-2.txt", 2)
193 194 195 196 197
        assert_equal 2, ann1.lines.size
        assert_equal '2', ann1.revisions[0].identifier
        assert_equal 'test00@', ann1.revisions[0].author
        assert_equal 'It is written in Python.', ann1.lines[1]
        ann2 = latin1_repo.annotate(
198
                   "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt", 2)
199 200 201
        assert_equal 1, ann2.lines.size
        assert_equal '2', ann2.revisions[0].identifier
        assert_equal 'test00@', ann2.revisions[0].author
202
        assert_equal "test-#{CHAR_1_LATIN1_HEX}.txt", ann2.lines[0]
203 204 205 206 207
      end

      def test_diff_latin1_path
        latin1_repo = create_latin1_repo
        diff1 = latin1_repo.diff(
208
                  "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt", 2, 1)
209 210
        assert_equal 7, diff1.size
        buf =  diff1[5].gsub(/\r\n|\r|\n/, "")
211
        assert_equal "+test-#{CHAR_1_LATIN1_HEX}.txt", buf
212 213 214 215
      end

      def test_entries_latin1_path
        latin1_repo = create_latin1_repo
216
        entries = latin1_repo.entries("test-#{CHAR_1_UTF8_HEX}-dir", 2)
217 218 219
        assert_kind_of Redmine::Scm::Adapters::Entries, entries
        assert_equal 3, entries.size
        assert_equal 'file', entries[1].kind
220 221
        assert_equal "test-#{CHAR_1_UTF8_HEX}-1.txt", entries[0].name
        assert_equal "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt", entries[0].path
222 223 224 225
      end

      def test_entry_latin1_path
        latin1_repo = create_latin1_repo
226 227 228
        ["test-#{CHAR_1_UTF8_HEX}-dir",
          "/test-#{CHAR_1_UTF8_HEX}-dir",
          "/test-#{CHAR_1_UTF8_HEX}-dir/"
229 230
        ].each do |path|
          entry = latin1_repo.entry(path, 2)
231
          assert_equal "test-#{CHAR_1_UTF8_HEX}-dir", entry.path
232 233
          assert_equal "dir", entry.kind
        end
234 235
        ["test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt",
          "/test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt"
236 237
        ].each do |path|
          entry = latin1_repo.entry(path, 2)
238
          assert_equal "test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt",
239 240 241 242 243 244 245 246 247 248 249 250 251 252
                       entry.path
          assert_equal "file", entry.kind
        end
      end

      def test_changeset_latin1_path
        latin1_repo = create_latin1_repo
        assert_equal 0, latin1_repo.changesets.count
        latin1_repo.fetch_changesets
        @project.reload
        assert_equal 3, latin1_repo.changesets.count

        cs2 = latin1_repo.changesets.find_by_revision('2')
        assert_not_nil cs2
253
        assert_equal "test-#{CHAR_1_UTF8_HEX}", cs2.comments
254 255 256
        c2  = cs2.filechanges.sort_by(&:path)
        assert_equal 4, c2.size
        assert_equal 'A', c2[0].action
257
        assert_equal "/test-#{CHAR_1_UTF8_HEX}-dir/", c2[0].path
258
        assert_equal 'A', c2[1].action
259
        assert_equal "/test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-1.txt", c2[1].path
260
        assert_equal 'A', c2[2].action
261
        assert_equal "/test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-2.txt", c2[2].path
262
        assert_equal 'A', c2[3].action
263
        assert_equal "/test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}.txt", c2[3].path
264 265 266

        cs3 = latin1_repo.changesets.find_by_revision('3')
        assert_not_nil cs3
267
        assert_equal "modify, move and delete #{CHAR_1_UTF8_HEX} files", cs3.comments
268 269 270
        c3  = cs3.filechanges.sort_by(&:path)
        assert_equal 3, c3.size
        assert_equal 'M', c3[0].action
271
        assert_equal "/test-#{CHAR_1_UTF8_HEX}-1.txt", c3[0].path
272
        assert_equal 'D', c3[1].action
273
        assert_equal "/test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}-2.txt", c3[1].path
274
        assert_equal 'M', c3[2].action
275
        assert_equal "/test-#{CHAR_1_UTF8_HEX}-dir/test-#{CHAR_1_UTF8_HEX}.txt", c3[2].path
276 277
      end
    else
278
      msg = "Bazaar non ASCII output test cannot run on this environment.\n"
279
      msg += "Encoding.locale_charmap: " + Encoding.locale_charmap + "\n"
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
      puts msg
    end

    private

    def create_latin1_repo
      repo = Repository::Bazaar.create(
                            :project      => @project,
                            :identifier   => 'latin1',
                            :url => REPOSITORY_PATH_NON_ASCII,
                            :log_encoding => 'ISO-8859-1'
                        )
      assert repo
      repo
    end
295
  else
296
    puts "Bazaar test repository NOT FOUND. Skipping unit tests !!!"
297 298 299
    def test_fake; assert true end
  end
end