Welcome to mirror list, hosted at ThFree Co, Russian Federation.

diff_note_spec.rb « representation « github_import « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 81722c0eba740caec04d4b5d0d09bed2099b9e1e (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
249
250
251
252
253
254
255
256
257
258
259
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GithubImport::Representation::DiffNote do
  let(:hunk) do
    '@@ -1 +1 @@
    -Hello
    +Hello world'
  end

  let(:created_at) { Time.new(2017, 1, 1, 12, 00) }
  let(:updated_at) { Time.new(2017, 1, 1, 12, 15) }

  shared_examples 'a DiffNote' do
    it 'returns an instance of DiffNote' do
      expect(note).to be_an_instance_of(described_class)
    end

    context 'the returned DiffNote' do
      it 'includes the number of the note' do
        expect(note.noteable_id).to eq(42)
      end

      it 'includes the file path of the diff' do
        expect(note.file_path).to eq('README.md')
      end

      it 'includes the commit ID' do
        expect(note.commit_id).to eq('123abc')
      end

      it 'includes the user details' do
        expect(note.author)
          .to be_an_instance_of(Gitlab::GithubImport::Representation::User)

        expect(note.author.id).to eq(4)
        expect(note.author.login).to eq('alice')
      end

      it 'includes the note body' do
        expect(note.note).to eq('Hello world')
      end

      it 'includes the created timestamp' do
        expect(note.created_at).to eq(created_at)
      end

      it 'includes the updated timestamp' do
        expect(note.updated_at).to eq(updated_at)
      end

      it 'includes the GitHub ID' do
        expect(note.note_id).to eq(1)
      end

      it 'returns the noteable type' do
        expect(note.noteable_type).to eq('MergeRequest')
      end
    end
  end

  describe '.from_api_response' do
    let(:response) do
      double(
        :response,
        html_url: 'https://github.com/foo/bar/pull/42',
        path: 'README.md',
        commit_id: '123abc',
        original_commit_id: 'original123abc',
        diff_hunk: hunk,
        user: double(:user, id: 4, login: 'alice'),
        body: 'Hello world',
        created_at: created_at,
        updated_at: updated_at,
        line: 23,
        start_line: nil,
        id: 1
      )
    end

    it_behaves_like 'a DiffNote' do
      let(:note) { described_class.from_api_response(response) }
    end

    it 'does not set the user if the response did not include a user' do
      allow(response)
        .to receive(:user)
        .and_return(nil)

      note = described_class.from_api_response(response)

      expect(note.author).to be_nil
    end

    it 'formats a suggestion in the note body' do
      allow(response)
        .to receive(:body)
        .and_return <<~BODY
      ```suggestion
      Hello World
      ```
      BODY

      note = described_class.from_api_response(response)

      expect(note.note).to eq <<~BODY
      ```suggestion:-0+0
      Hello World
      ```
      BODY
    end
  end

  describe '.from_json_hash' do
    let(:hash) do
      {
        'noteable_type' => 'MergeRequest',
        'noteable_id' => 42,
        'file_path' => 'README.md',
        'commit_id' => '123abc',
        'original_commit_id' => 'original123abc',
        'diff_hunk' => hunk,
        'author' => { 'id' => 4, 'login' => 'alice' },
        'note' => 'Hello world',
        'created_at' => created_at.to_s,
        'updated_at' => updated_at.to_s,
        'note_id' => 1
      }
    end

    it_behaves_like 'a DiffNote' do
      let(:note) { described_class.from_json_hash(hash) }
    end

    it 'does not convert the author if it was not specified' do
      hash.delete('author')

      note = described_class.from_json_hash(hash)

      expect(note.author).to be_nil
    end

    it 'formats a suggestion in the note body' do
      hash['note'] = <<~BODY
      ```suggestion
      Hello World
      ```
      BODY

      note = described_class.from_json_hash(hash)

      expect(note.note).to eq <<~BODY
      ```suggestion:-0+0
      Hello World
      ```
      BODY
    end
  end

  describe '#line_code' do
    it 'returns a String' do
      note = described_class.new(diff_hunk: hunk, file_path: 'README.md')

      expect(note.line_code).to be_an_instance_of(String)
    end
  end

  describe '#diff_hash' do
    it 'returns a Hash containing the diff details' do
      note = described_class.from_json_hash(
        'noteable_type' => 'MergeRequest',
        'noteable_id' => 42,
        'file_path' => 'README.md',
        'commit_id' => '123abc',
        'original_commit_id' => 'original123abc',
        'diff_hunk' => hunk,
        'author' => { 'id' => 4, 'login' => 'alice' },
        'note' => 'Hello world',
        'created_at' => created_at.to_s,
        'updated_at' => updated_at.to_s,
        'note_id' => 1
      )

      expect(note.diff_hash).to eq(
        diff: hunk,
        new_path: 'README.md',
        old_path: 'README.md',
        a_mode: '100644',
        b_mode: '100644',
        new_file: false
      )
    end
  end

  describe '#github_identifiers' do
    it 'returns a hash with needed identifiers' do
      github_identifiers = {
        noteable_id: 42,
        noteable_type: 'MergeRequest',
        note_id: 1
      }
      other_attributes = { something_else: '_something_else_' }
      note = described_class.new(github_identifiers.merge(other_attributes))

      expect(note.github_identifiers).to eq(github_identifiers)
    end
  end

  describe '#note' do
    it 'returns the given note' do
      hash = {
        'note': 'simple text'
      }

      note = described_class.new(hash)

      expect(note.note).to eq 'simple text'
    end

    it 'returns the suggestion formatted in the note' do
      hash = {
        'note': <<~BODY
        ```suggestion
        Hello World
        ```
        BODY
      }

      note = described_class.new(hash)

      expect(note.note).to eq <<~BODY
      ```suggestion:-0+0
      Hello World
      ```
      BODY
    end

    it 'returns the multi-line suggestion formatted in the note' do
      hash = {
        'start_line': 20,
        'end_line': 23,
        'note': <<~BODY
        ```suggestion
        Hello World
        ```
        BODY
      }

      note = described_class.new(hash)

      expect(note.note).to eq <<~BODY
      ```suggestion:-3+0
      Hello World
      ```
      BODY
    end
  end
end