BigW Consortium Gitlab

file_spec.rb 9.27 KB
Newer Older
1 2 3 4 5 6
require 'spec_helper'

describe Gitlab::Conflict::File, lib: true do
  let(:project) { create(:project) }
  let(:repository) { project.repository }
  let(:rugged) { repository.rugged }
7 8 9
  let(:their_commit) { rugged.branches['conflict-start'].target }
  let(:our_commit) { rugged.branches['conflict-resolvable'].target }
  let(:merge_request) { create(:merge_request, source_branch: 'conflict-resolvable', target_branch: 'conflict-start', source_project: project) }
10 11
  let(:index) { rugged.merge_commits(our_commit, their_commit) }
  let(:conflict) { index.conflicts.last }
12
  let(:merge_file_result) { index.merge_file('files/ruby/regex.rb') }
13
  let(:conflict_file) { Gitlab::Conflict::File.new(merge_file_result, conflict, merge_request: merge_request) }
14

15 16 17 18
  describe '#resolve_lines' do
    let(:section_keys) { conflict_file.sections.map { |section| section[:id] }.compact }

    context 'when resolving everything to the same side' do
19
      let(:resolution_hash) { section_keys.map { |key| [key, 'head'] }.to_h }
20 21 22 23 24 25 26 27 28 29 30 31 32 33
      let(:resolved_lines) { conflict_file.resolve_lines(resolution_hash) }
      let(:expected_lines) { conflict_file.lines.reject { |line| line.type == 'old' } }

      it 'has the correct number of lines' do
        expect(resolved_lines.length).to eq(expected_lines.length)
      end

      it 'has content matching the chosen lines' do
        expect(resolved_lines.map(&:text)).to eq(expected_lines.map(&:text))
      end
    end

    context 'with mixed resolutions' do
      let(:resolution_hash) do
34
        section_keys.map.with_index { |key, i| [key, i.even? ? 'head' : 'origin'] }.to_h
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
      end

      let(:resolved_lines) { conflict_file.resolve_lines(resolution_hash) }

      it 'has the correct number of lines' do
        file_lines = conflict_file.lines.reject { |line| line.type == 'new' }

        expect(resolved_lines.length).to eq(file_lines.length)
      end

      it 'returns a file containing only the chosen parts of the resolved sections' do
        expect(resolved_lines.chunk { |line| line.type || 'both' }.map(&:first)).
          to eq(['both', 'new', 'both', 'old', 'both', 'new', 'both'])
      end
    end

    it 'raises MissingResolution when passed a hash without resolutions for all sections' do
      empty_hash = section_keys.map { |key| [key, nil] }.to_h
      invalid_hash = section_keys.map { |key| [key, 'invalid'] }.to_h

      expect { conflict_file.resolve_lines({}) }.
        to raise_error(Gitlab::Conflict::File::MissingResolution)

      expect { conflict_file.resolve_lines(empty_hash) }.
        to raise_error(Gitlab::Conflict::File::MissingResolution)

      expect { conflict_file.resolve_lines(invalid_hash) }.
        to raise_error(Gitlab::Conflict::File::MissingResolution)
    end
  end

66
  describe '#highlight_lines!' do
67
    def html_to_text(html)
68
      CGI.unescapeHTML(ActionView::Base.full_sanitizer.sanitize(html)).delete("\n")
69 70
    end

71 72
    it 'modifies the existing lines' do
      expect { conflict_file.highlight_lines! }.to change { conflict_file.lines.map(&:instance_variables) }
73 74
    end

75 76 77 78 79 80 81 82
    it 'is called implicitly when rich_text is accessed on a line' do
      expect(conflict_file).to receive(:highlight_lines!).once.and_call_original

      conflict_file.lines.each(&:rich_text)
    end

    it 'sets the rich_text of the lines matching the text content' do
      conflict_file.lines.each do |line|
83 84 85 86 87 88
        expect(line.text).to eq(html_to_text(line.rich_text))
      end
    end
  end

  describe '#sections' do
89 90 91 92
    it 'only inserts match lines when there is a gap between sections' do
      conflict_file.sections.each_with_index do |section, i|
        previous_line_number = 0
        current_line_number = section[:lines].map(&:old_line).compact.min
93

94 95 96
        if i > 0
          previous_line_number = conflict_file.sections[i - 1][:lines].map(&:old_line).compact.last
        end
97

98 99 100 101 102
        if current_line_number == previous_line_number + 1
          expect(section[:lines].first.type).not_to eq('match')
        else
          expect(section[:lines].first.type).to eq('match')
          expect(section[:lines].first.text).to match(/\A@@ -#{current_line_number},\d+ \+\d+,\d+ @@ module Gitlab\Z/)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        end
      end
    end

    it 'sets conflict to false for sections with only unchanged lines' do
      conflict_file.sections.reject { |section| section[:conflict] }.each do |section|
        without_match = section[:lines].reject { |line| line.type == 'match' }

        expect(without_match).to all(have_attributes(type: nil))
      end
    end

    it 'only includes a maximum of CONTEXT_LINES (plus an optional match line) in context sections' do
      conflict_file.sections.reject { |section| section[:conflict] }.each do |section|
        without_match = section[:lines].reject { |line| line.type == 'match' }

        expect(without_match.length).to be <= Gitlab::Conflict::File::CONTEXT_LINES * 2
      end
    end

    it 'sets conflict to true for sections with only changed lines' do
      conflict_file.sections.select { |section| section[:conflict] }.each do |section|
        section[:lines].each do |line|
          expect(line.type).to be_in(['new', 'old'])
        end
      end
    end
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

    it 'adds unique IDs to conflict sections, and not to other sections' do
      section_ids = []

      conflict_file.sections.each do |section|
        if section[:conflict]
          expect(section).to have_key(:id)
          section_ids << section[:id]
        else
          expect(section).not_to have_key(:id)
        end
      end

      expect(section_ids.uniq).to eq(section_ids)
    end
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

    context 'with an example file' do
      let(:file) do
        <<FILE
  # Ensure there is no match line header here
  def username_regexp
    default_regexp
  end

<<<<<<< files/ruby/regex.rb
def project_name_regexp
  /\A[a-zA-Z0-9][a-zA-Z0-9_\-\. ]*\z/
end

def name_regexp
  /\A[a-zA-Z0-9_\-\. ]*\z/
=======
def project_name_regex
  %r{\A[a-zA-Z0-9][a-zA-Z0-9_\-\. ]*\z}
end

def name_regex
  %r{\A[a-zA-Z0-9_\-\. ]*\z}
>>>>>>> files/ruby/regex.rb
end

# Some extra lines
# To force a match line
# To be created

def path_regexp
  default_regexp
end

<<<<<<< files/ruby/regex.rb
def archive_formats_regexp
  /(zip|tar|7z|tar\.gz|tgz|gz|tar\.bz2|tbz|tbz2|tb2|bz2)/
=======
def archive_formats_regex
  %r{(zip|tar|7z|tar\.gz|tgz|gz|tar\.bz2|tbz|tbz2|tb2|bz2)}
>>>>>>> files/ruby/regex.rb
end

def git_reference_regexp
  # Valid git ref regexp, see:
  # https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
  %r{
    (?!
       (?# doesn't begins with)
       \/|                    (?# rule #6)
       (?# doesn't contain)
       .*(?:
          [\/.]\.|            (?# rule #1,3)
          \/\/|               (?# rule #6)
          @\{|                (?# rule #8)
          \\                  (?# rule #9)
       )
    )
    [^\000-\040\177~^:?*\[]+  (?# rule #4-5)
    (?# doesn't end with)
    (?<!\.lock)               (?# rule #1)
    (?<![\/.])                (?# rule #6-7)
  }x
end

protected

<<<<<<< files/ruby/regex.rb
def default_regexp
  /\A[.?]?[a-zA-Z0-9][a-zA-Z0-9_\-\.]*(?<!\.git)\z/
=======
def default_regex
  %r{\A[.?]?[a-zA-Z0-9][a-zA-Z0-9_\-\.]*(?<!\.git)\z}
>>>>>>> files/ruby/regex.rb
end
FILE
      end

      let(:conflict_file) { Gitlab::Conflict::File.new({ data: file }, conflict, merge_request: merge_request) }
      let(:sections) { conflict_file.sections }

      it 'sets the correct match line headers' do
        expect(sections[0][:lines].first).to have_attributes(type: 'match', text: '@@ -3,14 +3,14 @@')
        expect(sections[3][:lines].first).to have_attributes(type: 'match', text: '@@ -19,26 +19,26 @@ def path_regexp')
        expect(sections[6][:lines].first).to have_attributes(type: 'match', text: '@@ -47,52 +47,52 @@ end')
      end

      it 'does not add match lines where they are not needed' do
        expect(sections[1][:lines].first.type).not_to eq('match')
        expect(sections[2][:lines].first.type).not_to eq('match')
        expect(sections[4][:lines].first.type).not_to eq('match')
        expect(sections[5][:lines].first.type).not_to eq('match')
        expect(sections[7][:lines].first.type).not_to eq('match')
      end

      it 'creates context sections of the correct length' do
        expect(sections[0][:lines].reject(&:type).length).to eq(3)
        expect(sections[2][:lines].reject(&:type).length).to eq(3)
        expect(sections[3][:lines].reject(&:type).length).to eq(3)
        expect(sections[5][:lines].reject(&:type).length).to eq(3)
        expect(sections[6][:lines].reject(&:type).length).to eq(3)
        expect(sections[8][:lines].reject(&:type).length).to eq(1)
      end
    end
249
  end
250 251 252 253 254 255

  describe '#as_json' do
    it 'includes the blob path for the file' do
      expect(conflict_file.as_json[:blob_path]).
        to eq("/#{project.namespace.to_param}/#{merge_request.project.to_param}/blob/#{our_commit.oid}/files/ruby/regex.rb")
    end
256 257 258 259

    it 'includes the blob icon for the file' do
      expect(conflict_file.as_json[:blob_icon]).to eq('file-text-o')
    end
260 261 262 263 264 265 266 267 268 269 270

    context 'with the full_content option passed' do
      it 'includes the full content of the conflict' do
        expect(conflict_file.as_json(full_content: true)).to have_key(:content)
      end

      it 'includes the detected language of the conflict file' do
        expect(conflict_file.as_json(full_content: true)[:blob_ace_mode]).
          to eq('ruby')
      end
    end
271
  end
272
end