File: note_diff_file_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (64 lines) | stat: -rw-r--r-- 1,815 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe NoteDiffFile, feature_category: :code_review_workflow do
  let(:diff_note) { create(:diff_note_on_commit) }
  let(:note_diff_file) { diff_note.note_diff_file }

  describe 'associations' do
    it { is_expected.to belong_to(:diff_note) }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:diff_note) }
  end

  describe '.referencing_sha' do
    let(:project) { diff_note.project }

    it 'finds note diff files by project and sha' do
      found = described_class.referencing_sha(diff_note.commit_id, project_id: project.id)

      expect(found).to contain_exactly(note_diff_file)
    end

    it 'excludes note diff files with the wrong project' do
      other_project = create(:project)

      found = described_class.referencing_sha(diff_note.commit_id, project_id: other_project.id)

      expect(found).to be_empty
    end

    it 'excludes note diff files with the wrong sha' do
      found = described_class.referencing_sha(Gitlab::Git::SHA1_BLANK_SHA, project_id: project.id)

      expect(found).to be_empty
    end
  end

  describe '#diff_export' do
    let_it_be(:encoded) { "b\xC3\xA5r" }
    let_it_be(:expected) { "bår" }

    before do
      note_diff_file.update!(diff: encoded)
    end

    context 'when diff can be encoded' do
      it 'force encodes the diff to UTF-8' do
        expect(note_diff_file.diff_export).to eq(expected)
        expect(note_diff_file.diff_export.encoding).to eq(Encoding::UTF_8)
      end
    end

    context 'when diff cannot be encoded' do
      it 'returns the raw diff' do
        allow(note_diff_file).to receive(:force_encode_utf8).with(encoded).and_raise(ArgumentError)

        expect(note_diff_file.diff_export).to eq(encoded)
      end
    end
  end
end