summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/diff/custom_diff_spec.rb
blob: 77d2a6cbcd6a8d11f87d8fe053b9419d40abf13b (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Diff::CustomDiff do
  include RepoHelpers

  let(:project) { create(:project, :repository) }
  let(:repository) { project.repository }
  let(:ipynb_blob) { repository.blob_at('f6b7a707', 'files/ipython/markdown-table.ipynb') }
  let(:blob) { repository.blob_at('HEAD', 'files/ruby/regex.rb') }

  describe '#preprocess_before_diff' do
    context 'for ipynb files' do
      it 'transforms the diff' do
        expect(described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)).not_to include('cells')
      end

      it 'adds the blob to the list of transformed blobs' do
        described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)

        expect(described_class.transformed_for_diff?(ipynb_blob)).to be_truthy
      end
    end

    context 'for other files' do
      it 'returns nil' do
        expect(described_class.preprocess_before_diff(blob.path, nil, blob)).to be_nil
      end

      it 'does not add the blob to the list of transformed blobs' do
        described_class.preprocess_before_diff(blob.path, nil, blob)

        expect(described_class.transformed_for_diff?(blob)).to be_falsey
      end
    end

    context 'timeout' do
      subject { described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob) }

      it 'falls back to nil on timeout' do
        allow(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception)
        expect(Timeout).to receive(:timeout).and_raise(Timeout::Error)

        expect(subject).to be_nil
      end

      context 'when in foreground' do
        it 'utilizes timeout for web' do
          expect(Timeout).to receive(:timeout).with(described_class::RENDERED_TIMEOUT_FOREGROUND).and_call_original

          expect(subject).not_to include('cells')
        end

        it 'increments metrics' do
          counter = Gitlab::Metrics.counter(:ipynb_semantic_diff_timeouts_total, 'desc')

          expect(Timeout).to receive(:timeout).and_raise(Timeout::Error)
          expect { subject }.to change { counter.get(source: described_class::FOREGROUND_EXECUTION) }.by(1)
        end
      end

      context 'when in background' do
        before do
          allow(Gitlab::Runtime).to receive(:sidekiq?).and_return(true)
        end

        it 'utilizes longer timeout for sidekiq' do
          expect(Timeout).to receive(:timeout).with(described_class::RENDERED_TIMEOUT_BACKGROUND).and_call_original

          expect(subject).not_to include('cells')
        end

        it 'increments metrics' do
          counter = Gitlab::Metrics.counter(:ipynb_semantic_diff_timeouts_total, 'desc')

          expect(Timeout).to receive(:timeout).and_raise(Timeout::Error)
          expect { subject }.to change { counter.get(source: described_class::BACKGROUND_EXECUTION) }.by(1)
        end
      end
    end

    context 'when invalid ipynb' do
      it 'returns nil' do
        expect(ipynb_blob).to receive(:data).and_return('invalid ipynb')

        expect(described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)).to be_nil
      end
    end
  end

  describe '#transformed_blob_data' do
    it 'transforms blob data if file was processed' do
      described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)

      expect(described_class.transformed_blob_data(ipynb_blob)).not_to include('cells')
    end

    it 'does not transform blob data if file was not processed' do
      expect(described_class.transformed_blob_data(ipynb_blob)).to be_nil
    end
  end

  describe '#transformed_blob_language' do
    it 'is md when file was preprocessed' do
      described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)

      expect(described_class.transformed_blob_language(ipynb_blob)).to eq('md')
    end

    it 'is nil for a .ipynb blob that was not preprocessed' do
      expect(described_class.transformed_blob_language(ipynb_blob)).to be_nil
    end
  end
end