-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtests.py
More file actions
715 lines (603 loc) · 23.2 KB
/
Copy pathtests.py
File metadata and controls
715 lines (603 loc) · 23.2 KB
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
import subprocess
import sys
import textwrap
import time
import pytest
import requests.exceptions
import gitlab_trace as gt
@pytest.fixture(autouse=True)
def mock_gitlab(monkeypatch):
monkeypatch.setattr(gt, 'gitlab', FakeGitlabModule())
@pytest.fixture(autouse=True)
def mock_time_sleep(monkeypatch):
monkeypatch.setattr(time, 'sleep', lambda seconds: None)
class FakeGitlabModule:
__version__ = '0.42.frog-knows'
class Gitlab:
def __init__(self):
self.projects = FakeGitlabModule.Projects()
@classmethod
def from_config(cls, name=None):
return cls()
class Projects:
def get(self, project_id):
if project_id == '404':
raise requests.exceptions.HTTPError
return FakeGitlabModule.Project(project_id)
class Project:
def __init__(self, project_id):
self.pipelines = FakeGitlabModule.ProjectPipelines()
self.jobs = FakeGitlabModule.ProjectJobs()
self.web_url = f'https://git.example.com/{project_id}'
class ProjectPipelines:
def list(self, ref=None, iterator=False):
assert iterator
if ref == 'empty':
return
else:
yield from [
FakeGitlabModule.ProjectPipeline(1005),
FakeGitlabModule.ProjectPipeline(997),
]
def get(self, pipeline_id):
return FakeGitlabModule.ProjectPipeline(pipeline_id)
class ProjectPipeline:
def __init__(self, id):
self.id = str(id)
self.jobs = FakeGitlabModule.PipelineJobs(self)
self.attributes = {"type": "pipeline", "json_attributes": "here"}
class PipelineJobs:
def __init__(self, project_pipeline):
self._project_pipeline = project_pipeline
def list(self, all=False):
if self._project_pipeline.id == '1009':
return [
FakeGitlabModule.ProjectJob(3301, 'build', 'success'),
FakeGitlabModule.ProjectJob(3302, 'test', 'failed'),
FakeGitlabModule.ProjectJob(3303, 'test', 'failed'),
FakeGitlabModule.ProjectJob(3304, 'test', 'running'),
]
else:
return [
FakeGitlabModule.ProjectJob(3201, 'build', 'success'),
FakeGitlabModule.ProjectJob(3202, 'test', 'failed',
has_artifacts=True),
]
class ProjectJobs:
def get(self, job_id):
return FakeGitlabModule.ProjectJob(
job_id, 'build', 'success', has_artifacts=(job_id == '3202'))
class ProjectJob:
def __init__(self, id, name, status, has_artifacts=False):
self.id = id
self.name = name
self.status = status
self.created_at = '2020-09-16T06:16:49.180Z'
self.started_at = '2020-09-16T06:16:51.066Z'
self.finished_at = None
self.duration = 42
if has_artifacts:
self.artifacts_file = {
'filename': 'artifacts.zip',
'size': 0,
}
self.attributes = {"type": "job", "json_attributes": "here"}
self._trace = b'Hello, world!\n'
self._refresh = [
{},
{
'_trace': self._trace + b'Bye!\n',
'finished_at': '2020-09-16T06:16:57.452Z',
'status': 'success',
},
]
def artifacts(self, streamed=False, action=None):
pass
def refresh(self):
if self._refresh:
self.__dict__.update(self._refresh.pop(0))
def trace(self):
return self._trace
def test_fatal():
with pytest.raises(SystemExit):
gt.fatal("oh woe is me")
def test_warn():
gt.warn("beware hungry bears")
def test_info():
gt.info("ice cream is yummy")
def test_pipe():
assert gt.pipe('echo hello'.split()) == 'hello'
@pytest.mark.parametrize('url, expected', [
('https://gitlab.com/owner/project', 'owner/project'),
('https://gitlab.com/owner/project.git', 'owner/project'),
('https://gitlab.com:443/owner/project.git', 'owner/project'),
('ssh://git@gitlab.example.com:23/owner/project.git', 'owner/project'),
('git@gitlab.com:owner/project.git', 'owner/project'),
('git@gitlab.example.com:group/subgroup/project.git',
'group/subgroup/project'),
('git@my-gitlab.company.com:owner/project', 'owner/project'),
('git@github.com:owner/project.git', None),
('https://github.com/owner/project', None),
('fridge:git/random.git', None),
])
def test_determine_project(url, expected):
assert gt.determine_project(url) == expected
def test_determine_project_from_git(monkeypatch):
monkeypatch.setattr(
subprocess, 'run',
lambda *a, **kw: subprocess.CompletedProcess(
a, 0, stdout='https://gitlab.com/o/p\n')
)
assert gt.determine_project() == 'o/p'
def test_determine_branch(monkeypatch):
monkeypatch.setattr(
subprocess, 'run',
lambda *a, **kw: subprocess.CompletedProcess(
a, 0, stdout='fix-bugs\n')
)
assert gt.determine_branch() == 'fix-bugs'
@pytest.mark.parametrize('status, expected', [
('success', '\033[32msuccess\033[0m'),
('skipped', 'skipped'),
])
def test_fmt_status(status, expected):
assert gt.fmt_status(status) == expected
@pytest.mark.parametrize('duration, expected', [
(0, '0s'),
(1, '1s'),
(60, '1m'),
(61, '1m 1s'),
(3600, '1h'),
(3601, '1h 1s'),
(3660, '1h 1m'),
(3661, '1h 1m 1s'),
(958.767528, "15m 59s"),
(None, 'n/a'),
])
def test_fmt_duration(duration, expected):
assert gt.fmt_duration(duration) == expected
@pytest.mark.parametrize('size, expected', [
(0, '0 B'),
(1, '1 B'),
(1024, '1 KiB'),
(1124, '1.1 KiB'),
(1024**2, '1 MiB'),
(3.2 * 1024**2, '3.2 MiB'),
(None, 'n/a'),
])
def test_fmt_size(size, expected):
assert gt.fmt_size(size) == expected
@pytest.mark.parametrize("s, n, expected", [
(b'a\nb\nc\nd\n', None, b'a\nb\nc\nd\n'),
(b'a\nb\nc\nd\n', 0, b'a\nb\nc\nd\n'),
(b'a\nb\nc\nd\n', 1, b'd\n'),
(b'a\nb\nc\nd\n', 2, b'c\nd\n'),
(b'a\nb\nc\nd', 1, b'd'),
])
def test_tail(s, n, expected):
assert gt.tail(s, n) == expected
def test_follow_truncation(monkeypatch, capsys):
job = FakeGitlabModule.ProjectJob(42, 'job', 'running')
job._refresh = [
{'_trace': b'Hello, world!\nBye?\n'},
{'_trace': b'Bye, cruel world!\n'},
{'finished_at': '2020-09-16T06:16:57.452Z'},
]
gt.follow(job)
stdout, stderr = capsys.readouterr()
assert stdout == textwrap.dedent("""\
Hello, world!
Bye?
Bye, cruel world!
""")
assert stderr == textwrap.dedent("""\
----- trace was truncated -----
""")
def test_follow_truncation_flushes(monkeypatch, capsys):
job = FakeGitlabModule.ProjectJob(42, 'job', 'running')
job._refresh = [
{'_trace': b'Hello, world!\nBye?\n'},
{'_trace': b'Bye, cruel world!\n'},
{'finished_at': '2020-09-16T06:16:57.452Z'},
]
gt.follow(job, buffer=sys.stderr.buffer)
stdout, stderr = capsys.readouterr()
assert stdout == ''
assert stderr == textwrap.dedent("""\
Hello, world!
Bye?
----- trace was truncated -----
Bye, cruel world!
""")
def test_main_help(monkeypatch):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--help'])
with pytest.raises(SystemExit):
gt.main()
def test_main_warn_extra_args(monkeypatch, capsys):
monkeypatch.setattr(
sys, 'argv', ['gitlab-trace', '--running', '--job=123', '456']
)
monkeypatch.setattr(gt, 'determine_project', lambda: None)
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert (
'Ignoring --running because --job=123 was specified'
in stderr
)
assert (
'Ignoring pipeline (456) because --job=123 was specified'
in stderr
)
def test_main_no_project(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace'])
monkeypatch.setattr(gt, 'determine_project', lambda: None)
with pytest.raises(SystemExit,
match='Could not determine GitLab project ID'):
gt.main()
def test_main_auto_project(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
with pytest.raises(SystemExit):
gt.main()
assert (
'GitLab project: owner/project'
in capsys.readouterr().err
)
def test_main_auto_branch(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
assert (
'Current branch: main'
in capsys.readouterr().err
)
def test_main_no_pipelines(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'empty')
with pytest.raises(SystemExit,
match="Project owner/project doesn't have any pipelines"
" for branch empty"):
gt.main()
def test_main_not_enough_pipelines(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '-5'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit,
match="Project owner/project has only 2 pipelines"
" for branch main"):
gt.main()
def test_main_auto_pipeline(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
assert (
'https://git.example.com/owner/project/pipelines/1005'
in capsys.readouterr().err
)
def test_main_list_jobs(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Current branch: main
https://git.example.com/owner/project/pipelines/1005
""")
assert stdout == textwrap.dedent("""\
Available jobs for pipeline #1005:
--job=3201 - success - build
--job=3202 - failed - test
""")
def test_main_artifacts_no_job_selected(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '-a'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Current branch: main
https://git.example.com/owner/project/pipelines/1005
Ignoring --artifacts because no job was selected.
""")
assert stdout == textwrap.dedent("""\
Available jobs for pipeline #1005:
--job=3201 - success - build
--job=3202 - failed - test
""")
def test_main_no_running_job(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--running', '-a'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Current branch: main
https://git.example.com/owner/project/pipelines/1005
Ignoring --running because no job was running.
Ignoring --artifacts because no job was selected.
""")
assert stdout == textwrap.dedent("""\
Available jobs for pipeline #1005:
--job=3201 - success - build
--job=3202 - failed - test
""")
def test_main_pipeline_debug(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--debug'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Current branch: main
https://git.example.com/owner/project/pipelines/1005
{
"type": "pipeline",
"json_attributes": "here"
}
""")
def test_main_print_url_no_job_specified(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--print-url'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Current branch: main
""")
assert stdout == textwrap.dedent("""\
https://git.example.com/owner/project/pipelines/1005
""")
def test_main_print_url_no_job_selected(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', [
'gitlab-trace', '-1', 'tsets', '--print-url'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Current branch: main
https://git.example.com/owner/project/pipelines/1005
Job tsets not found
Ignoring --print-url because no job was selected.
""")
def test_main_job_by_id(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--job=3202'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
""")
assert stdout == textwrap.dedent("""\
Hello, world!
""")
def test_main_job_by_name(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '1005', 'test'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Job ID: 3202
""")
assert stdout == textwrap.dedent("""\
Hello, world!
""")
def test_main_job_by_name_not_found(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '1005', 'tset'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Job tset not found
""")
assert stdout == textwrap.dedent("""\
Available jobs for pipeline #1005:
--job=3201 - success - build
--job=3202 - failed - test
""")
def test_main_job_by_name_multiple_last(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '1009', 'test'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'refactoring')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Found multiple jobs: 3302 3303 3304
Selecting the last one: 3304
""")
assert stdout == textwrap.dedent("""\
Hello, world!
""")
def test_main_job_by_name_multiple_nth(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '1009', 'test', '2'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'refactoring')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Found multiple jobs: 3302 3303 3304
Selecting #2: 3303
""")
assert stdout == textwrap.dedent("""\
Hello, world!
""")
def test_main_running(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '1009', '--running'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'refactoring')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Automatically selected --job=3304 (test)
""")
assert stdout == textwrap.dedent("""\
Available jobs for pipeline #1009:
--job=3301 - success - build
--job=3302 - failed - test
--job=3303 - failed - test
--job=3304 - running - test
Hello, world!
""")
def test_main_branch_ignored_because_job(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', [
'gitlab-trace', '--job=3202', '--branch=foo'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Ignoring --branch=foo because --job=3202 was specified
""")
def test_main_branch_ignored_because_pipeline(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', [
'gitlab-trace', '1005', '--branch=foo'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Ignoring --branch=foo because pipeline (1005) was specified
""")
def test_main_job_verbose(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--job=3202', '-v'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Job created: 2020-09-16T06:16:49.180Z
Job started: 2020-09-16T06:16:51.066Z
Job finished: not yet
Job duration: 42s
""")
assert stdout == textwrap.dedent("""\
Hello, world!
""")
def test_main_job_follow(monkeypatch, capsys):
monkeypatch.setattr(
sys, "argv", ["gitlab-trace", "--job=3202", "--follow"]
)
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
""")
assert stdout == textwrap.dedent("""\
Hello, world!
Bye!
""")
def test_main_job_debug(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--job=3202', '--debug'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
{
"type": "job",
"json_attributes": "here"
}
""")
assert stdout == textwrap.dedent("""\
Hello, world!
""")
def test_main_print_url(monkeypatch, capsys, tmp_path):
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(sys, 'argv', [
'gitlab-trace', '--job=3202', '--print-url'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
""")
assert stdout == textwrap.dedent("""\
https://git.example.com/owner/project/-/jobs/3202
""")
def test_main_job_artefacts(monkeypatch, capsys, tmp_path):
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--job=3202', '-a'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Artifacts: artifacts.zip (0 B)
""")
assert stdout == textwrap.dedent("""\
Hello, world!
""")
def test_main_job_artefacts_nope(monkeypatch, capsys, tmp_path):
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--job=3201', '-a'])
monkeypatch.setattr(gt, 'determine_project', lambda: 'owner/project')
monkeypatch.setattr(gt, 'determine_branch', lambda: 'main')
with pytest.raises(SystemExit):
gt.main()
stdout, stderr = capsys.readouterr()
assert stderr == textwrap.dedent("""\
GitLab project: owner/project
Job has no artifacts.
""")
assert stdout == textwrap.dedent("""\
Hello, world!
""")
def raise_keyboard_interrupt(*args, **kw):
raise KeyboardInterrupt()
def test_main_suppresses_keyboard_interrupt(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--job=3202'])
monkeypatch.setattr(gt, 'determine_project', raise_keyboard_interrupt)
with pytest.raises(SystemExit):
gt.main()
def test_main_reports_network_issues_without_tracebacks(monkeypatch, capsys):
monkeypatch.setattr(sys, 'argv', ['gitlab-trace', '--project=404'])
with pytest.raises(SystemExit):
gt.main()