-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_runner.py
645 lines (497 loc) · 20.6 KB
/
test_runner.py
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
# -*- coding: utf-8 -*-
import os
import pytest
from xml.etree import ElementTree as ET
from therapist.config import Config
from therapist.runner import Runner
from therapist.runner.action import Action, ActionCollection
from therapist.runner.result import Result, ResultCollection
from . import Project
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
def equivalent_xml(xmlstring, fixture_path):
eta = ET.fromstring(xmlstring)
etb = ET.parse(fixture_path)
xmlsa = ET.tostring(eta, encoding="utf-8", method="xml")
xmlsb = ET.tostring(etb.getroot(), encoding="utf-8", method="xml")
return xmlsa == xmlsb
class TestAction(object):
def test_str(self):
a = Action("flake8")
assert str(a) == "flake8"
a.description = "Flake 8"
assert str(a) == "Flake 8"
def test_repr(self):
a = Action("flake8")
assert a.__repr__() == "<Action flake8>"
def test_include(self):
a = Action("flake8")
a.include = "*.py"
assert a.include == ["*.py"]
a.include = ["a.py", "b.py"]
assert a.include == ["a.py", "b.py"]
def test_exclude(self):
a = Action("flake8")
a.exclude = "ignore.py"
assert a.exclude == ["ignore.py"]
a.exclude = ["a.py", "b.py"]
assert a.exclude == ["a.py", "b.py"]
class TestActionCollection(object):
def test_get(self):
action = Action("flake8")
actions = ActionCollection([action])
assert actions.get("flake8") == action
with pytest.raises(actions.DoesNotExist):
actions.get("not_an_action")
class TestResult(object):
def test_str(self):
a = Action("flake8")
r = Result(a, status=Result.SUCCESS)
assert str(r) == "SUCCESS"
r.status = Result.FAILURE
assert str(r) == "FAILURE"
r.status = None
assert str(r) == "SKIP"
r.status = Result.ERROR
assert str(r) == "ERROR"
def test_action_validation(self):
with pytest.raises(TypeError):
Result("something")
class TestResultCollection(object):
def test_count(self):
r1 = Result(Action("flake8"), status=Result.SUCCESS)
r2 = Result(Action("eslint"))
rs = ResultCollection([r1, r2])
assert rs.count() == 2
assert rs.count(status=None) == 1
assert rs.count(status=Result.FAILURE) == 0
def test_has_success(self):
r = Result(Action("flake8"), status=Result.SUCCESS)
rs = ResultCollection()
assert not rs.has_success
rs.append(r)
assert rs.has_success
def test_has_failure(self):
r = Result(Action("flake8"), status=Result.FAILURE)
rs = ResultCollection()
assert not rs.has_failure
rs.append(r)
assert rs.has_failure
def test_has_skip(self):
r = Result(Action("flake8"))
rs = ResultCollection()
assert not rs.has_skip
rs.append(r)
assert rs.has_skip
def test_has_error(self):
r = Result(Action("flake8"), status=Result.ERROR)
rs = ResultCollection()
assert not rs.has_error
rs.append(r)
assert rs.has_error
def test_dump_colors(self):
r = Result(Action("flake8"), status=Result.FAILURE)
r.mark_complete(output="Failed!")
rs = ResultCollection([r])
assert rs.dump() == (
"\n#{red}#{bright}"
"===============================================================================\n"
"FAILED: flake8\n"
"===============================================================================\n"
"#{reset_all}Failed!\n"
)
def test_dump_unicode(self):
r = Result(Action("flake8"), status=Result.FAILURE)
r.mark_complete(output="✖")
rs = ResultCollection([r])
assert rs.dump() == (
"\n#{red}#{bright}"
"===============================================================================\n"
"FAILED: flake8\n"
"===============================================================================\n"
"#{reset_all}✖\n"
)
def test_dump_success(self):
r = Result(Action("flake8"), status=Result.SUCCESS)
rs = ResultCollection([r])
assert rs.dump() == ""
def test_dump_failure(self):
r = Result(Action("flake8"), status=Result.FAILURE)
r.mark_complete(output="Failed!")
rs = ResultCollection([r])
assert rs.dump() == (
"\n#{red}#{bright}"
"===============================================================================\n"
"FAILED: flake8\n"
"===============================================================================\n"
"#{reset_all}Failed!\n"
)
r = Result(Action("flake8"), status=Result.FAILURE)
r.mark_complete(error="ERR!", output="Failed!")
rs = ResultCollection([r])
assert rs.dump() == (
"\n#{red}#{bright}"
"===============================================================================\n"
"FAILED: flake8\n"
"===============================================================================\n"
"#{reset_all}Failed!\n"
"-------------------------------------------------------------------------------\n"
"Additional error output:\n"
"-------------------------------------------------------------------------------\n"
"ERR!\n"
)
def test_dump_skip(self):
r = Result(Action("flake8"))
rs = ResultCollection([r])
assert rs.dump() == ""
def test_dump_error(self):
r = Result(Action("flake8"), status=Result.ERROR)
r.mark_complete(output="OH NOES!")
rs = ResultCollection([r])
assert rs.dump() == (
"\n#{red}#{bright}"
"===============================================================================\n"
"ERROR: flake8\n"
"===============================================================================\n"
"#{reset_all}OH NOES!\n"
)
r = Result(Action("flake8"), status=Result.ERROR)
r.mark_complete(error="Yikes!")
rs = ResultCollection([r])
assert rs.dump() == (
"\n#{red}#{bright}"
"===============================================================================\n"
"ERROR: flake8\n"
"===============================================================================\n"
"#{reset_all}Yikes!\n"
)
r = Result(Action("flake8"), status=Result.ERROR)
r.mark_complete(error="ERR!", output="Failed!")
rs = ResultCollection([r])
assert rs.dump() == (
"\n#{red}#{bright}"
"===============================================================================\n"
"ERROR: flake8\n"
"===============================================================================\n"
"#{reset_all}Failed!\n"
"-------------------------------------------------------------------------------\n"
"Additional error output:\n"
"-------------------------------------------------------------------------------\n"
"ERR!\n"
)
def test_dump_junit_success(self):
a = Action("flake8", run="flake8 {files}")
r = Result(a, status=Result.SUCCESS)
r.end_time = r.start_time + 1
rs = ResultCollection([r])
assert equivalent_xml(
rs.dump_junit(), os.path.join(BASE_DIR, "fixtures/xml/junit_success.xml")
)
def test_dump_junit_failure(self):
a = Action("flake8", run="flake8 {files}")
r = Result(a, status=Result.FAILURE)
r.mark_complete(output="Failed!")
r.end_time = r.start_time + 1
rs = ResultCollection([r])
assert equivalent_xml(
rs.dump_junit(), os.path.join(BASE_DIR, "fixtures/xml/junit_failure_1.xml")
)
r = Result(a, status=Result.FAILURE)
r.mark_complete(error="ERR!", output="Failed!")
r.end_time = r.start_time + 1
rs = ResultCollection([r])
assert equivalent_xml(
rs.dump_junit(), os.path.join(BASE_DIR, "fixtures/xml/junit_failure_2.xml")
)
def test_dump_junit_skip(self):
a = Action("flake8", run="flake8 {files}")
r = Result(a)
rs = ResultCollection([r])
assert equivalent_xml(
rs.dump_junit(), os.path.join(BASE_DIR, "fixtures/xml/junit_skip.xml")
)
def test_dump_junit_error(self):
a = Action("flake8", run="flake8 {files}")
r = Result(a, status=Result.ERROR)
r.mark_complete(output="Error!")
r.end_time = r.start_time + 1
rs = ResultCollection([r])
assert equivalent_xml(
rs.dump_junit(), os.path.join(BASE_DIR, "fixtures/xml/junit_error_1.xml")
)
r = Result(a, status=Result.ERROR)
r.mark_complete(error="ERR!", output="Error!")
r.end_time = r.start_time + 1
rs = ResultCollection([r])
assert equivalent_xml(
rs.dump_junit(), os.path.join(BASE_DIR, "fixtures/xml/junit_error_2.xml")
)
class TestRunner(object):
def test_unstaged_changes(self, project):
project.write("pass.txt", "FAIL")
project.git.add(".")
project.write("pass.txt", "x")
out, err, code = project.git.status(porcelain=True)
assert "AM pass.txt" in out
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
result, message = r.run_process(c.actions.get("lint"))
assert result.is_failure
assert project.read("pass.txt") == "x"
out, err, code = project.git.status(porcelain=True)
assert "AM pass.txt" in out
def test_include_untracked(self, project):
project.write("fail.txt")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True, include_untracked=True)
result, message = r.run_process(c.actions.get("lint"))
assert result.is_failure
assert "fail.txt" in r.files
assert message == (
"#{bright}Linting ............................................................. "
"#{red}[FAILURE]"
)
def test_include_unstaged(self, project):
project.write("fail.txt")
project.git.add(".")
project.git.commit(m="Add file.", date="Wed Jul 27 15:13:46 2016 -0400")
project.write("fail.txt", "x")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True, include_unstaged=True)
result, message = r.run_process(c.actions.get("lint"))
assert result.is_failure
assert "fail.txt" in r.files
assert message == (
"#{bright}Linting ............................................................. "
"#{red}[FAILURE]"
)
def test_include_unstaged_changes(self, project):
project.write("pass.txt", "FAIL")
project.git.add(".")
project.write("pass.txt", "x")
out, err, code = project.git.status(porcelain=True)
assert "AM pass.txt" in out
c = Config(project.path)
r = Runner(c.cwd, enable_git=True, include_unstaged_changes=True)
result, message = r.run_process(c.actions.get("lint"))
assert result.is_success
assert project.read("pass.txt") == "x"
out, err, code = project.git.status(porcelain=True)
assert "AM pass.txt" in out
def test_run_action_does_not_exist(self, project):
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
with pytest.raises(c.actions.DoesNotExist):
r.run_process(c.actions.get("notanaction"))
def test_run_process_success(self, project):
project.write("pass.py")
c = Config(project.path)
r = Runner(c.cwd, files=["pass.py"])
result, message = r.run_process(c.actions.get("lint"))
assert result.is_success
assert "pass.py" in r.files
assert message == (
"#{bright}Linting ............................................................. "
"#{green}[SUCCESS]"
)
def test_run_process_failure(self, project):
project.write("fail.txt")
c = Config(project.path)
r = Runner(c.cwd, files=["fail.txt"])
result, message = r.run_process(c.actions.get("lint"))
assert result.is_failure
assert "fail.txt" in r.files
assert message == (
"#{bright}Linting ............................................................. "
"#{red}[FAILURE]"
)
def test_run_process_skipped(self, project):
project.write(".ignore.pass.py")
c = Config(project.path)
r = Runner(c.cwd, files=[".ignore.pass.py"])
result, message = r.run_process(c.actions.get("lint"))
assert ".ignore.pass.py" in r.files
assert message == (
"#{bright}Linting ............................................................. "
"#{cyan}[SKIPPED]"
)
def test_run_process_error(self, project):
for i in range(10000):
project.write("pass{}.txt".format(str(i).ljust(200, "_")))
project.git.add(".")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
result, message = r.run_process(c.actions.get("lint"))
assert result.is_error
assert len(r.files) == 10000
assert message == (
"#{bright}Linting ............................................................. "
"#{red}[ERROR!!]"
)
def test_run_process_skips_deleted(self, project):
project.write("pass.py")
project.git.add(".")
project.git.commit(m="Add file.")
project.git.rm("pass.py")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
result, message = r.run_process(c.actions.get("lint"))
assert result.is_skip
assert len(r.files) == 0
def test_action_no_run(self, tmpdir):
config_data = {"actions": {"norun": {"description": "Skips"}}}
project = Project(tmpdir.strpath, config_data=config_data)
project.write("pass.py")
project.git.add(".")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
result, message = r.run_process(c.actions.get("norun"))
assert "pass.py" in r.files
assert message == (
"#{bright}Skips ............................................................... "
"#{cyan}[SKIPPED]"
)
def test_action_run_issue(self, tmpdir):
config_data = {
"actions": {
"runissue": {"description": "Should fail", "run": "not-a-real-command {files}"}
}
}
project = Project(tmpdir.strpath, config_data=config_data)
project.write("pass.py")
project.git.add(".")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
result, message = r.run_process(c.actions.get("runissue"))
assert result.is_failure
assert "pass.py" in r.files
assert message == (
"#{bright}Should fail ......................................................... "
"#{red}[FAILURE]"
)
def test_action_filter_include(self, project):
project.write("pass.py")
project.write("fail.js")
project.git.add(".")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
result, message = r.run_process(c.actions.get("lint"))
assert "fail.js" in r.files
assert message == (
"#{bright}Linting ............................................................. "
"#{green}[SUCCESS]"
)
def test_action_filter_exclude(self, project):
project.write("pass.py")
project.write(".ignore.fail.txt")
project.git.add(".")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
result, message = r.run_process(c.actions.get("lint"))
assert ".ignore.fail.txt" in r.files
assert message == (
"#{bright}Linting ............................................................. "
"#{green}[SUCCESS]"
)
def test_unstash_on_error(self, project, monkeypatch):
project.write("pass.py")
project.git.add(".")
project.write("pass.py", "changed")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
def raise_exc():
raise Exception
monkeypatch.setattr("time.time", raise_exc)
with pytest.raises(Exception):
r.run_process(c.actions.get("lint"))
assert project.read("pass.py") == "changed"
def test_output_encoded(self, project):
project.write("fail.txt")
project.git.add(".")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
result, message = r.run_process(c.actions.get("lint"))
assert result.is_failure
assert "fail.txt" in r.files
assert isinstance(message, str)
assert 'b"' not in message
assert "b'" not in message
def test_run_process_no_settings(self, tmpdir):
config_data = {"actions": {"no-settings": None}}
project = Project(tmpdir.strpath, config_data=config_data)
project.write("pass.py")
project.git.add(".")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
result, message = r.run_process(c.actions.get("no-settings"))
assert "pass.py" in r.files
assert message == (
"#{bright}no-settings ......................................................... "
"#{cyan}[SKIPPED]"
)
def test_user_stashed_files_are_not_unstashed(self, project):
project.write("test.txt", "unchanged")
project.git.add(".")
project.git.commit(m="first commit")
project.write("test.txt", "changed")
project.git.stash()
out, err, code = project.git.stash.list()
assert project.read("test.txt") == "unchanged"
assert out.startswith("stash@{0}")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
result, message = r.run_process(c.actions.get("lint"))
out, err, code = project.git.stash.list()
assert result.is_skip
assert out.startswith("stash@{0}")
def test_files_are_not_deleted_when_stash_fails(self, tmpdir):
project = Project(tmpdir.strpath, blank=True)
config = {"actions": {"lint": {"run": "false"}}}
project.set_config_data(config, commit=False)
project.write("fail.txt")
project.git.add(".")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
result, message = r.run_process(c.actions.get("lint"))
assert result.is_failure
assert project.exists("fail.txt")
def test_action_working_dir(self, project):
project.makedirs("working")
project.copy("scripts", "working/scripts")
project.git.add(".")
config = {
"actions": {"lint": {"run": "./scripts/lint.py {files}", "working_dir": "working"}}
}
project.set_config_data(config, commit=False)
project.write("fail.txt")
project.git.add(".")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
result, message = r.run_process(c.actions.get("lint"))
assert result.is_failure
assert project.exists("fail.txt")
def test_action_files_root(self, project):
project.copy("scripts", "working/scripts")
project.git.add(".")
config = {
"actions": {"lint": {"run": "./scripts/lint.py {files}", "files_root": "working"}}
}
project.set_config_data(config, commit=True)
project.makedirs("working")
project.write("working/pass.txt")
project.write("fail.txt")
project.git.add(".")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True)
result, message = r.run_process(c.actions.get("lint"))
assert result.is_success
assert project.exists("fail.txt")
def test_skips_staged_file_deleted_in_working_tree_with_include_unstaged(self, project):
project.write("pass.txt")
project.git.add("pass.txt")
project.remove("pass.txt")
c = Config(project.path)
r = Runner(c.cwd, enable_git=True, include_unstaged=True)
result, message = r.run_process(c.actions.get("lint"))
assert len(r.files) == 0
assert result.is_skip
assert not project.exists("pass.txt")