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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Ci::Ansi2json, feature_category: :continuous_integration do
subject { described_class }
describe 'lines' do
it 'prints non-ansi as-is' do
expect(convert_json('Hello')).to eq([{ offset: 0, content: [{ text: 'Hello' }] }])
end
context 'new lines' do
it 'adds new line when encountering \n' do
expect(convert_json("Hello\nworld")).to eq(
[
{ offset: 0, content: [{ text: 'Hello' }] },
{ offset: 6, content: [{ text: 'world' }] }
])
end
it 'adds new line when encountering \r\n' do
expect(convert_json("Hello\r\nworld")).to eq(
[
{ offset: 0, content: [{ text: 'Hello' }] },
{ offset: 7, content: [{ text: 'world' }] }
])
end
it 'ignores empty newlines' do
expect(convert_json("Hello\n\nworld")).to eq(
[
{ offset: 0, content: [{ text: 'Hello' }] },
{ offset: 7, content: [{ text: 'world' }] }
])
expect(convert_json("Hello\r\n\r\nworld")).to eq(
[
{ offset: 0, content: [{ text: 'Hello' }] },
{ offset: 9, content: [{ text: 'world' }] }
])
end
it 'replace the current line when encountering \r' do
expect(convert_json("Hello\rworld")).to eq([{ offset: 0, content: [{ text: 'world' }] }])
end
end
it 'recognizes color changing ANSI sequences' do
expect(convert_json("\e[31mHello\e[0m")).to eq(
[
{ offset: 0, content: [{ text: 'Hello', style: 'term-fg-red' }] }
])
end
it 'recognizes color changing ANSI sequences across multiple lines' do
expect(convert_json("\e[31mHello\nWorld\e[0m")).to eq(
[
{ offset: 0, content: [{ text: 'Hello', style: 'term-fg-red' }] },
{ offset: 11, content: [{ text: 'World', style: 'term-fg-red' }] }
])
end
it 'recognizes background and foreground colors' do
expect(convert_json("\e[31;44mHello")).to eq(
[
{ offset: 0, content: [{ text: 'Hello', style: 'term-fg-red term-bg-blue' }] }
])
end
it 'recognizes style changes within the same line' do
expect(convert_json("\e[31;44mHello\e[0m world")).to eq(
[
{ offset: 0, content: [
{ text: 'Hello', style: 'term-fg-red term-bg-blue' },
{ text: ' world' }
] }
])
end
context 'with section markers' do
let(:section_name) { 'prepare-script' }
let(:section_duration) { 63.seconds }
let(:section_start_time) { Time.new(2019, 9, 17).utc }
let(:section_end_time) { section_start_time + section_duration }
let(:section_start) { "section_start:#{section_start_time.to_i}:#{section_name}\r\033[0K" }
let(:section_end) { "section_end:#{section_end_time.to_i}:#{section_name}\r\033[0K" }
it 'marks the first line of the section as header' do
expect(convert_json("Hello#{section_start}world!")).to eq(
[
{
offset: 0,
content: [{ text: 'Hello' }]
},
{
offset: 5,
content: [{ text: 'world!' }],
section: 'prepare-script',
section_header: true
}
])
end
it 'does not marks the other lines of the section as header' do
expect(convert_json("outside section#{section_start}Hello\nworld!")).to eq(
[
{
offset: 0,
content: [{ text: 'outside section' }]
},
{
offset: 15,
content: [{ text: 'Hello' }],
section: 'prepare-script',
section_header: true
},
{
offset: 65,
content: [{ text: 'world!' }],
section: 'prepare-script'
}
])
end
it 'marks the last line of the section as footer' do
expect(convert_json("#{section_start}Good\nmorning\nworld!#{section_end}")).to eq(
[
{
offset: 0,
content: [{ text: 'Good' }],
section: 'prepare-script',
section_header: true
},
{
offset: 49,
content: [{ text: 'morning' }],
section: 'prepare-script'
},
{
offset: 57,
content: [{ text: 'world!' }],
section: 'prepare-script'
},
{
offset: 63,
content: [],
section_duration: '01:03',
section_footer: true,
section: 'prepare-script'
}
])
end
it 'marks the first line as header and footer if is the only line in the section' do
expect(convert_json("#{section_start}Hello world!#{section_end}")).to eq(
[
{
offset: 0,
content: [{ text: 'Hello world!' }],
section: 'prepare-script',
section_header: true
},
{
offset: 56,
content: [],
section: 'prepare-script',
section_duration: '01:03',
section_footer: true
}
])
end
it 'does not add sections attribute to lines after the section is closed' do
expect(convert_json("#{section_start}Hello#{section_end}world")).to eq(
[
{
offset: 0,
content: [{ text: 'Hello' }],
section: 'prepare-script',
section_header: true
},
{
offset: 49,
content: [],
section: 'prepare-script',
section_duration: '01:03',
section_footer: true
},
{
offset: 91,
content: [{ text: 'world' }]
}
])
end
it 'ignores section_end marker if no section_start exists' do
expect(convert_json("Hello #{section_end}world")).to eq(
[
{
offset: 0,
content: [{ text: 'Hello world' }]
}
])
end
context 'when section name contains .-_ and capital letters' do
let(:section_name) { 'a.Legit-SeCtIoN_namE' }
it 'sanitizes the section name' do
expect(convert_json("Hello#{section_start}world!")).to eq(
[
{
offset: 0,
content: [{ text: 'Hello' }]
},
{
offset: 5,
content: [{ text: 'world!' }],
section: 'a-legit-section-name',
section_header: true
}
])
end
end
context 'when section name includes $' do
let(:section_name) { 'my_$ection' }
it 'ignores the section' do
expect(convert_json("#{section_start}hello")).to eq(
[
{
offset: 0,
content: [{ text: 'hello' }]
}
])
end
end
context 'when section name includes <' do
let(:section_name) { '<a_tag>' }
it 'ignores the section' do
expect(convert_json("#{section_start}hello")).to eq(
[
{
offset: 0,
content: [{ text: 'hello' }]
}
])
end
end
it 'prints HTML tags as is' do
trace = "#{section_start}section_end:1:2<div>hello</div>#{section_end}"
expect(convert_json(trace)).to eq(
[
{
offset: 0,
content: [{ text: 'section_end:1:2<div>hello</div>' }],
section: 'prepare-script',
section_header: true
},
{
offset: 75,
content: [],
section: 'prepare-script',
section_duration: '01:03',
section_footer: true
}
])
end
context 'with nested section' do
let(:nested_section_name) { 'prepare-script-nested' }
let(:nested_section_duration) { 2.seconds }
let(:nested_section_start_time) { Time.new(2019, 9, 17).utc }
let(:nested_section_end_time) { nested_section_start_time + nested_section_duration }
let(:nested_section_start) { "section_start:#{nested_section_start_time.to_i}:#{nested_section_name}\r\033[0K" }
let(:nested_section_end) { "section_end:#{nested_section_end_time.to_i}:#{nested_section_name}\r\033[0K" }
it 'adds multiple sections to the lines inside the nested section' do
trace = "Hello#{section_start}foo#{nested_section_start}bar#{nested_section_end}baz#{section_end}world"
expect(convert_json(trace)).to eq(
[
{
offset: 0,
content: [{ text: 'Hello' }]
},
{
offset: 5,
content: [{ text: 'foo' }],
section: 'prepare-script',
section_header: true
},
{
offset: 52,
content: [{ text: 'bar' }],
section: 'prepare-script-nested',
section_header: true
},
{
offset: 106,
content: [],
section: 'prepare-script-nested',
section_duration: '00:02',
section_footer: true
},
{
offset: 155,
content: [{ text: 'baz' }],
section: 'prepare-script'
},
{
offset: 158,
content: [],
section: 'prepare-script',
section_duration: '01:03',
section_footer: true
},
{
offset: 200,
content: [{ text: 'world' }]
}
])
end
it 'adds multiple sections to the lines inside the nested section and closes all sections together' do
trace = "Hello#{section_start}\e[91mfoo\e[0m#{nested_section_start}bar#{nested_section_end}#{section_end}"
expect(convert_json(trace)).to eq(
[
{
offset: 0,
content: [{ text: 'Hello' }]
},
{
offset: 5,
content: [{ text: 'foo', style: 'term-fg-l-red' }],
section: 'prepare-script',
section_header: true
},
{
offset: 61,
content: [{ text: 'bar' }],
section: 'prepare-script-nested',
section_header: true
},
{
offset: 115,
content: [],
section: 'prepare-script-nested',
section_duration: '00:02',
section_footer: true
},
{
offset: 164,
content: [],
section: 'prepare-script',
section_duration: '01:03',
section_footer: true
}
])
end
end
context 'with section options' do
let(:option_section_start) { "section_start:#{section_start_time.to_i}:#{section_name}[collapsed=true,unused_option=123]\r\033[0K" }
it 'provides section options when set' do
trace = "#{option_section_start}hello#{section_end}"
expect(convert_json(trace)).to eq(
[
{
offset: 0,
content: [{ text: 'hello' }],
section: 'prepare-script',
section_header: true,
section_options: {
'collapsed' => 'true',
'unused_option' => '123'
}
},
{
offset: 83,
content: [],
section: 'prepare-script',
section_duration: '01:03',
section_footer: true
}
])
end
end
end
describe 'incremental updates' do
let(:pass1_stream) { StringIO.new(pre_text) }
let(:pass2_stream) { StringIO.new(pre_text + text) }
let(:pass1) { subject.convert(pass1_stream) }
let(:pass2) { subject.convert(pass2_stream, pass1.state) }
context 'with split word' do
let(:pre_text) { "\e[1mHello " }
let(:text) { 'World' }
let(:lines) do
[
{ offset: 0, content: [{ text: 'Hello World', style: 'term-bold' }] }
]
end
it 'returns the full line' do
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_falsey
end
end
context 'with split word on second line' do
let(:pre_text) { "Good\nmorning " }
let(:text) { 'World' }
let(:lines) do
[
{ offset: 5, content: [{ text: 'morning World' }] }
]
end
it 'returns all lines since last partially processed line' do
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_truthy
end
end
context 'with split sequence across multiple lines' do
let(:pre_text) { "\e[1mgood\nmorning\n" }
let(:text) { "\e[3mworld" }
let(:lines) do
[
{ offset: 17, content: [{ text: 'world', style: 'term-bold term-italic' }] }
]
end
it 'returns the line since last partially processed line' do
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_truthy
end
end
context 'with split partial sequence' do
let(:pre_text) { "hello\e" }
let(:text) { "[1m world" }
let(:lines) do
[
{ offset: 0, content: [
{ text: 'hello' },
{ text: ' world', style: 'term-bold' }
] }
]
end
it 'returns the line since last partially processed line' do
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_falsey
end
end
context 'with split new line' do
let(:pre_text) { "hello\r" }
let(:text) { "\nworld" }
let(:lines) do
[
{ offset: 0, content: [{ text: 'hello' }] },
{ offset: 7, content: [{ text: 'world' }] }
]
end
it 'returns a blank line and the next line' do
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_falsey
end
end
context 'with split section' do
let(:section_name) { 'prepare-script' }
let(:section_duration) { 63.seconds }
let(:section_start_time) { Time.new(2019, 9, 17).utc }
let(:section_end_time) { section_start_time + section_duration }
let(:section_start) { "section_start:#{section_start_time.to_i}:#{section_name}\r\033[0K" }
let(:section_end) { "section_end:#{section_end_time.to_i}:#{section_name}\r\033[0K" }
context 'with split section body' do
let(:pre_text) { "#{section_start}this is a header\nand " }
let(:text) { "this\n is a body" }
let(:lines) do
[
{
offset: 61,
content: [{ text: 'and this' }],
section: 'prepare-script'
},
{
offset: 70,
content: [{ text: ' is a body' }],
section: 'prepare-script'
}
]
end
it 'returns the full line' do
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_truthy
end
end
context 'with split section where header is also split' do
let(:pre_text) { "#{section_start}this is " }
let(:text) { "a header\nand body" }
let(:lines) do
[
{
offset: 0,
content: [{ text: 'this is a header' }],
section: 'prepare-script',
section_header: true
},
{
offset: 61,
content: [{ text: 'and body' }],
section: 'prepare-script'
}
]
end
it 'returns the full line' do
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_falsey
end
end
context 'with split section end' do
let(:pre_text) { "#{section_start}this is a header\nthe" }
let(:text) { " body\nthe end#{section_end}" }
let(:lines) do
[
{
offset: 61,
content: [{ text: 'the body' }],
section: 'prepare-script'
},
{
offset: 70,
content: [{ text: 'the end' }],
section: 'prepare-script'
},
{
offset: 77,
content: [],
section: 'prepare-script',
section_duration: '01:03',
section_footer: true
}
]
end
it 'returns the full line' do
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_truthy
end
end
end
end
describe 'trucates' do
let(:text) { 'Hello World' }
let(:stream) { StringIO.new(text) }
let(:subject) { described_class.convert(stream) }
before do
stream.seek(3, IO::SEEK_SET)
end
it 'returns truncated output' do
expect(subject.truncated).to be_truthy
end
it 'does not append output' do
expect(subject.append).to be_falsey
end
end
def convert_json(data)
stream = StringIO.new(data)
subject.convert(stream).lines
end
end
end
|