-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_cli.py
601 lines (520 loc) · 17.3 KB
/
test_cli.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
import os
import pkgutil
from pathlib import Path
import pytest
from click.testing import CliRunner
from slotscheck.cli import root as cli
from .conftest import EXAMPLES_DIR
@pytest.fixture()
def runner():
return CliRunner()
@pytest.fixture(autouse=True)
def set_cwd(request):
os.chdir(EXAMPLES_DIR)
yield
os.chdir(request.config.invocation_dir)
def test_no_inputs(runner: CliRunner):
result = runner.invoke(cli, [])
assert result.exit_code == 0
assert result.output == "No files or modules given. Nothing to do!\n"
def test_module_doesnt_exist(runner: CliRunner):
result = runner.invoke(cli, ["-m", "foo"])
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert result.output == (
"ERROR: Module 'foo' not found.\n\n"
"See slotscheck.rtfd.io/en/latest/discovery.html\n"
"for help resolving common import problems.\n"
)
def test_module_is_uninspectable(runner: CliRunner):
result = runner.invoke(cli, ["-m", "broken.submodule"])
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
ERROR: Failed to import 'broken.submodule'.
Oh no, found some problems!
Scanned 0 module(s), 0 class(es).
"""
)
def test_module_is_uninspectable_no_strict_imports(runner: CliRunner):
result = runner.invoke(
cli, ["-m", "broken.submodule", "--no-strict-imports"]
)
assert result.exit_code == 0
assert (
result.output
== """\
NOTE: Failed to import 'broken.submodule'.
All OK!
Scanned 0 module(s), 0 class(es).
"""
)
def test_path_doesnt_exist(runner: CliRunner):
result = runner.invoke(cli, ["doesnt_exist"])
assert result.exit_code == 2
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
Usage: slotscheck [OPTIONS] [FILES]...
Try 'slotscheck --help' for help.
Error: Invalid value for '[FILES]...': Path 'doesnt_exist' does not exist.
"""
)
def test_everything_ok(runner: CliRunner):
result = runner.invoke(cli, ["-m", "module_ok"])
assert result.exit_code == 0
assert result.output == "All OK!\nScanned 6 module(s), 64 class(es).\n"
def test_single_file_module(runner: CliRunner):
result = runner.invoke(
cli, ["-m", "module_singular"], catch_exceptions=False
)
assert result.exit_code == 0
assert result.output == "All OK!\nScanned 1 module(s), 5 class(es).\n"
def test_builtins(runner: CliRunner):
result = runner.invoke(cli, ["-m", "builtins"])
assert result.exit_code == 0
def test_extension(runner: CliRunner):
result = runner.invoke(cli, ["-m", "_pickle"])
assert result.exit_code == 0
assert result.output == ("All OK!\nScanned 1 module(s), 5 class(es).\n")
def test_success_verbose(runner: CliRunner):
result = runner.invoke(
cli, ["-m", "module_ok", "-v"], catch_exceptions=False
)
assert result.exit_code == 0
assert (
result.output
== """\
All OK!
stats:
modules: 7
checked: 6
excluded: 1
skipped: 0
classes: 64
has slots: 44
no slots: 20
n/a: 0
"""
)
def test_submodule(runner: CliRunner):
result = runner.invoke(
cli, ["-m", "module_ok.a.b"], catch_exceptions=False
)
assert result.exit_code == 0
assert result.output == "All OK!\nScanned 4 module(s), 32 class(es).\n"
def test_namespaced(runner: CliRunner):
result = runner.invoke(
cli, ["-m", "namespaced.module"], catch_exceptions=False
)
assert result.exit_code == 0
assert result.output == "All OK!\nScanned 4 module(s), 1 class(es).\n"
def test_multiple_modules(runner: CliRunner):
result = runner.invoke(
cli,
["-m", "module_singular", "-m", "module_ok", "-m", "namespaced"],
catch_exceptions=False,
)
assert result.exit_code == 0
assert result.output == "All OK!\nScanned 11 module(s), 70 class(es).\n"
def test_multiple_paths(runner: CliRunner):
result = runner.invoke(
cli,
[
str(EXAMPLES_DIR / "module_singular.py"),
str(EXAMPLES_DIR / "module_ok/a/b/../b"),
str(EXAMPLES_DIR / "namespaced/module/foo.py"),
],
catch_exceptions=False,
)
assert result.exit_code == 0
assert result.output == "All OK!\nScanned 8 module(s), 38 class(es).\n"
def test_path_is_module_directory(runner: CliRunner):
# let's define the path indirectly to ensure it works
path = str(EXAMPLES_DIR / "module_ok/a/../")
result = runner.invoke(cli, [path], catch_exceptions=False)
assert result.exit_code == 0
assert result.output == "All OK!\nScanned 6 module(s), 64 class(es).\n"
def test_cannot_pass_both_path_and_module(runner: CliRunner):
result = runner.invoke(cli, ["module_ok", "-m", "click"])
assert result.exit_code == 2
assert (
result.output
== "ERROR: Specify either FILES argument or `-m/--module` "
"option, not both.\n"
)
def test_errors_with_default_settings(runner: CliRunner):
result = runner.invoke(cli, ["-m", "module_not_ok"])
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
ERROR: 'module_not_ok.a.b:U' has slots but superclass does not.
ERROR: 'module_not_ok.foo:S' has slots but superclass does not.
ERROR: 'module_not_ok.foo:T' has slots but superclass does not.
ERROR: 'module_not_ok.foo:U' has slots but superclass does not.
ERROR: 'module_not_ok.foo:U.Ua' defines overlapping slots.
ERROR: 'module_not_ok.foo:U.Ub' defines overlapping slots.
ERROR: 'module_not_ok.foo:W' defines overlapping slots.
ERROR: 'module_not_ok.foo:Z' has duplicate slots.
ERROR: 'module_not_ok.foo:Za' defines overlapping slots.
ERROR: 'module_not_ok.foo:Zb' has slots but superclass does not.
Oh no, found some problems!
Scanned 4 module(s), 30 class(es).
"""
)
def test_errors_require_slots_subclass(runner: CliRunner):
result = runner.invoke(cli, ["-m", "module_not_ok", "--require-subclass"])
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
ERROR: 'module_not_ok.a.b:A' has no slots, but it could have.
ERROR: 'module_not_ok.a.b:U' has slots but superclass does not.
ERROR: 'module_not_ok.foo:A' has no slots, but it could have.
ERROR: 'module_not_ok.foo:C' has no slots, but it could have.
ERROR: 'module_not_ok.foo:R' has no slots, but it could have.
ERROR: 'module_not_ok.foo:S' has slots but superclass does not.
ERROR: 'module_not_ok.foo:T' has slots but superclass does not.
ERROR: 'module_not_ok.foo:U' has slots but superclass does not.
ERROR: 'module_not_ok.foo:U.Ua' defines overlapping slots.
ERROR: 'module_not_ok.foo:U.Ub' defines overlapping slots.
ERROR: 'module_not_ok.foo:W' defines overlapping slots.
ERROR: 'module_not_ok.foo:Z' has duplicate slots.
ERROR: 'module_not_ok.foo:Za' defines overlapping slots.
ERROR: 'module_not_ok.foo:Zb' has slots but superclass does not.
Oh no, found some problems!
Scanned 4 module(s), 30 class(es).
"""
)
def test_errors_disallow_nonslot_inherit(runner: CliRunner):
result = runner.invoke(
cli, ["-m", "module_not_ok", "--require-superclass"]
)
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
ERROR: 'module_not_ok.a.b:U' has slots but superclass does not.
ERROR: 'module_not_ok.foo:S' has slots but superclass does not.
ERROR: 'module_not_ok.foo:T' has slots but superclass does not.
ERROR: 'module_not_ok.foo:U' has slots but superclass does not.
ERROR: 'module_not_ok.foo:U.Ua' defines overlapping slots.
ERROR: 'module_not_ok.foo:U.Ub' defines overlapping slots.
ERROR: 'module_not_ok.foo:W' defines overlapping slots.
ERROR: 'module_not_ok.foo:Z' has duplicate slots.
ERROR: 'module_not_ok.foo:Za' defines overlapping slots.
ERROR: 'module_not_ok.foo:Zb' has slots but superclass does not.
Oh no, found some problems!
Scanned 4 module(s), 30 class(es).
"""
)
def test_errors_no_require_superclass(runner: CliRunner):
result = runner.invoke(
cli, ["-m", "module_not_ok", "--no-require-superclass"]
)
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
ERROR: 'module_not_ok.foo:U.Ua' defines overlapping slots.
ERROR: 'module_not_ok.foo:U.Ub' defines overlapping slots.
ERROR: 'module_not_ok.foo:W' defines overlapping slots.
ERROR: 'module_not_ok.foo:Z' has duplicate slots.
ERROR: 'module_not_ok.foo:Za' defines overlapping slots.
Oh no, found some problems!
Scanned 4 module(s), 30 class(es).
"""
)
def test_errors_with_exclude_classes(runner: CliRunner):
result = runner.invoke(
cli,
["-m", "module_not_ok", "--exclude-classes", "(foo:U$|:(W|S))"],
)
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
ERROR: 'module_not_ok.a.b:U' has slots but superclass does not.
ERROR: 'module_not_ok.foo:T' has slots but superclass does not.
ERROR: 'module_not_ok.foo:U.Ua' defines overlapping slots.
ERROR: 'module_not_ok.foo:U.Ub' defines overlapping slots.
ERROR: 'module_not_ok.foo:Z' has duplicate slots.
ERROR: 'module_not_ok.foo:Za' defines overlapping slots.
ERROR: 'module_not_ok.foo:Zb' has slots but superclass does not.
Oh no, found some problems!
Scanned 4 module(s), 30 class(es).
"""
)
def test_errors_with_include_classes(runner: CliRunner):
result = runner.invoke(
cli,
["-m", "module_not_ok", "--include-classes", "(foo:.*a|:(W|S))"],
)
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
ERROR: 'module_not_ok.foo:S' has slots but superclass does not.
ERROR: 'module_not_ok.foo:U.Ua' defines overlapping slots.
ERROR: 'module_not_ok.foo:W' defines overlapping slots.
ERROR: 'module_not_ok.foo:Za' defines overlapping slots.
Oh no, found some problems!
Scanned 4 module(s), 30 class(es).
"""
)
def test_errors_with_include_modules(runner: CliRunner):
result = runner.invoke(
cli,
[
"-m",
"module_not_ok",
"--include-modules",
"(module_not_ok$ | a)",
],
)
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
ERROR: 'module_not_ok.a.b:U' has slots but superclass does not.
Oh no, found some problems!
Scanned 3 module(s), 2 class(es).
"""
)
def test_ignores_given_module_completely(runner: CliRunner):
result = runner.invoke(
cli,
[
"-m",
"module_not_ok",
"--include-modules",
"nomatch",
],
)
assert result.exit_code == 0
assert (
result.output
== "Files or modules given, but filtered out by exclude/include. "
"Nothing to do!\n"
)
def test_module_not_ok_verbose(runner: CliRunner):
result = runner.invoke(cli, ["-m", "module_not_ok", "-v"])
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
ERROR: 'module_not_ok.a.b:U' has slots but superclass does not.
Superclasses without slots:
- 'module_not_ok.a.b:A'
ERROR: 'module_not_ok.foo:S' has slots but superclass does not.
Superclasses without slots:
- 'module_not_ok.foo:R'
ERROR: 'module_not_ok.foo:T' has slots but superclass does not.
Superclasses without slots:
- 'module_not_ok.foo:A'
ERROR: 'module_not_ok.foo:U' has slots but superclass does not.
Superclasses without slots:
- 'module_not_ok.foo:L'
- 'module_not_ok.foo:D'
- 'module_not_ok.foo:C'
ERROR: 'module_not_ok.foo:U.Ua' defines overlapping slots.
Slots already defined in superclass:
- 'w' (module_not_ok.foo:Q)
ERROR: 'module_not_ok.foo:U.Ub' defines overlapping slots.
Slots already defined in superclass:
- 'w' (module_not_ok.foo:U.Ua)
- 'w' (module_not_ok.foo:Q)
ERROR: 'module_not_ok.foo:W' defines overlapping slots.
Slots already defined in superclass:
- 'p' (module_not_ok.foo:U)
- 'v' (module_not_ok.foo:V)
ERROR: 'module_not_ok.foo:Z' has duplicate slots.
Duplicate slot names:
- 'b'
- 'c'
ERROR: 'module_not_ok.foo:Za' defines overlapping slots.
Slots already defined in superclass:
- 'b' (module_not_ok.foo:Z)
- 'c' (module_not_ok.foo:Z)
ERROR: 'module_not_ok.foo:Zb' has slots but superclass does not.
Superclasses without slots:
- 'module_not_ok.foo:MyProto'
Oh no, found some problems!
stats:
modules: 4
checked: 4
excluded: 0
skipped: 0
classes: 30
has slots: 22
no slots: 8
n/a: 0
"""
)
def test_module_misc(runner: CliRunner):
result = runner.invoke(
cli,
["-m", "module_misc", "--no-strict-imports"],
catch_exceptions=False,
)
assert result.exit_code == 0
assert (
result.output
== """\
NOTE: Failed to import 'module_misc.a.evil'.
All OK!
Scanned 18 module(s), 8 class(es).
"""
)
def test_module_exclude(runner: CliRunner):
result = runner.invoke(
cli,
[
"-m",
"module_misc",
"--exclude-modules",
"evil",
"--no-strict-imports",
],
catch_exceptions=False,
)
assert result.exit_code == 0
assert (
result.output
== """\
NOTE: Failed to import 'module_misc.a.b.__main__'.
All OK!
Scanned 16 module(s), 9 class(es).
"""
)
from module_misc import a # type: ignore
assert not a.evil_was_imported
def test_module_disallow_import_failures(runner: CliRunner):
result = runner.invoke(cli, ["-m", "module_misc", "--strict-imports"])
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
ERROR: Failed to import 'module_misc.a.evil'.
Oh no, found some problems!
Scanned 18 module(s), 8 class(es).
"""
)
def test_module_allow_import_failures(runner: CliRunner):
result = runner.invoke(cli, ["-m", "module_misc", "--no-strict-imports"])
assert result.exit_code == 0
assert (
result.output
== """\
NOTE: Failed to import 'module_misc.a.evil'.
All OK!
Scanned 18 module(s), 8 class(es).
"""
)
def test_finds_config(runner: CliRunner, mocker, tmpdir):
(tmpdir / "myconf.toml").write_binary(
b"""
[tool.slotscheck]
require-superclass = false
"""
)
mocker.patch(
"slotscheck.config.find_config_file",
return_value=Path(tmpdir / "myconf.toml"),
)
result = runner.invoke(cli, ["-m", "module_not_ok"])
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
ERROR: 'module_not_ok.foo:U.Ua' defines overlapping slots.
ERROR: 'module_not_ok.foo:U.Ub' defines overlapping slots.
ERROR: 'module_not_ok.foo:W' defines overlapping slots.
ERROR: 'module_not_ok.foo:Z' has duplicate slots.
ERROR: 'module_not_ok.foo:Za' defines overlapping slots.
Oh no, found some problems!
Scanned 4 module(s), 30 class(es).
"""
)
def test_given_config(runner: CliRunner, tmpdir):
my_config = tmpdir / "myconf.toml"
my_config.write_binary(
b"""
[tool.slotscheck]
require-superclass = false
"""
)
result = runner.invoke(
cli,
["-m", "module_not_ok", "--settings", str(my_config)],
catch_exceptions=False,
)
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
ERROR: 'module_not_ok.foo:U.Ua' defines overlapping slots.
ERROR: 'module_not_ok.foo:U.Ub' defines overlapping slots.
ERROR: 'module_not_ok.foo:W' defines overlapping slots.
ERROR: 'module_not_ok.foo:Z' has duplicate slots.
ERROR: 'module_not_ok.foo:Za' defines overlapping slots.
Oh no, found some problems!
Scanned 4 module(s), 30 class(es).
"""
)
def test_ambiguous_import(runner: CliRunner):
result = runner.invoke(
cli,
[str(EXAMPLES_DIR / "other/module_misc/a/b/c.py")],
catch_exceptions=False,
)
assert result.exit_code == 1
assert isinstance(result.exception, SystemExit)
assert (
result.output
== """\
Cannot check due to import ambiguity.
The given files do not correspond with what would be imported:
'import module_misc.a.b.c' would load from:
{}
instead of:
{}
You may need to define $PYTHONPATH or run as 'python -m slotscheck'
to ensure the correct files can be imported.
See slotscheck.rtfd.io/en/latest/discovery.html
for more information on why this happens and how to resolve it.
""".format(
pkgutil.get_loader(
"module_misc.a.b.c"
).path, # type: ignore[union-attr]
EXAMPLES_DIR / "other/module_misc/a/b/c.py",
)
)
def test_ambiguous_import_excluded(runner: CliRunner):
result = runner.invoke(
cli,
["other/module_misc/a/b/c.py", "--exclude-modules", "module_misc"],
catch_exceptions=False,
)
assert result.exit_code == 0
assert (
result.output
== """\
Files or modules given, but filtered out by exclude/include. Nothing to do!
"""
)