forked from asottile/reorder-python-imports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reorder_python_imports_test.py
1312 lines (1099 loc) · 35.1 KB
/
reorder_python_imports_test.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
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import io
import os
import sys
from unittest import mock
import pytest
from classify_imports import Settings
from reorder_python_imports import apply_import_sorting
from reorder_python_imports import fix_file_contents
from reorder_python_imports import main
from reorder_python_imports import parse_imports
from reorder_python_imports import partition_source
from reorder_python_imports import remove_duplicated_imports
from reorder_python_imports import Replacements
from reorder_python_imports import Tok
from reorder_python_imports import TOKENIZE
@pytest.fixture
def in_tmpdir(tmpdir):
with tmpdir.as_cwd():
yield tmpdir
@pytest.mark.parametrize(
's',
(
"''",
'""',
'r"hi"',
'u"hello"',
'"""hello\nworld"""',
"'''\n'''",
"'''\\\n'''",
"'''\\\r\n'''",
),
)
def test_tokenize_can_match_strings(s):
tp, pat = TOKENIZE[-1]
assert tp == Tok.STRING
assert pat.fullmatch(s)
@pytest.mark.parametrize(
's',
(
pytest.param('\n', id='trivial'),
pytest.param('#!/usr/bin/env python\n', id='shebang'),
pytest.param('# -*- coding: UTF-8 -*-\n', id='source encoding'),
pytest.param(' # coding: UTF-8\n', id='source encoding indented'),
pytest.param(
'#!/usr/bin/env python\n'
'# -*- coding: UTF-8 -*-\n',
id='shebang and source encoding',
),
pytest.param('"""foo"""\n', id='docstring'),
pytest.param(
'"""foo"""\n'
'"""bar"""\n',
id='multiple docstrings',
),
pytest.param(
'# -*- coding UTF-8 -*-\n'
'u"""☃☃☃"""\n',
id='unicode docstring',
),
),
)
def test_partition_source_before_code_only(s):
before, imports, after, nl = partition_source(s)
assert before == s
assert imports == []
assert after == ''
assert nl == '\n'
@pytest.mark.parametrize(
's',
(
pytest.param('#!/usr/bin/env python', id='shebang no nl'),
pytest.param('# -*- coding: UTF-8 -*-', id='source encoding no nl'),
pytest.param('"""foo"""', id='docstring no nl'),
),
)
def test_adds_newline_no_nl(s):
before, imports, after, nl = partition_source(s)
assert before == f'{s}\n'
assert imports == []
assert after == ''
assert nl == '\n'
@pytest.mark.parametrize(
's',
(
pytest.param('x = 1\n', id='code only'),
pytest.param(
'x = 1\n'
'import os\n',
id='found code before imports',
),
pytest.param(
'# noreorder\n'
'import os\n',
id='noreorder before imports',
),
pytest.param(
'"""docstring""" # noreorder\n'
'import os\n',
id='noreorder on docstring\n',
),
pytest.param(
'"""docstring""" + "nope"\n',
id='docstring with expression',
),
),
)
def test_partition_source_code_only(s):
before, imports, after, nl = partition_source(s)
assert before == ''
assert imports == []
assert after == s
assert nl == '\n'
def test_partition_source_code_only_adds_newline():
before, imports, after, nl = partition_source('x = 1')
assert before == ''
assert imports == []
assert after == 'x = 1\n'
assert nl == '\n'
@pytest.mark.parametrize(
('s', 'expected'),
(
pytest.param('import os\n', ['import os\n'], id='simple import'),
pytest.param('import os', ['import os\n'], id='simple import no nl'),
pytest.param(
'from foo import * # noqa\n',
['from foo import * # noqa\n'],
id='preserves comments on imports',
),
pytest.param(
'import sys\nimport os\n',
['import sys\n', 'import os\n'],
id='multiple imports',
),
pytest.param(
'import sys\n\n\nimport os\n',
['import sys\n', 'import os\n'],
id='discards whitespace within imports',
),
pytest.param(
'import os\n'
'\n'
' \n'
'import sys\n',
['import os\n', 'import sys\n'],
id='discards trailing whitespace within imports',
),
pytest.param(
'from foo import (\n'
' bar,\n'
')\n',
['from foo import (\n bar,\n)\n'],
id='multiline imports',
),
pytest.param(
'from foo import ( # c1 )\n'
' bar, # c2 )\n'
') # c3\n',
['from foo import ( # c1 )\n bar, # c2 )\n) # c3\n'],
id='multiline imports with comments',
),
pytest.param(
'from foo import *\n',
['from foo import *\n'],
id='star imports',
),
),
)
def test_partition_source_imports_only(s, expected):
before, imports, after, nl = partition_source(s)
assert imports == expected
assert before == after == ''
assert nl == '\n'
def test_partition_source_before_leaves_newlines():
before, imports, after, nl = partition_source(
'# comment here\n'
'\n'
'# another comment here\n'
'\n'
'import os\n'
)
assert before == (
'# comment here\n'
'\n'
'# another comment here\n'
'\n'
)
assert imports == ['import os\n']
assert after == ''
assert nl == '\n'
def test_partition_source_before_and_code_only():
before, imports, after, nl = partition_source(
'# before\n'
'x = 1\n',
)
assert before == '# before\n'
assert imports == []
assert after == 'x = 1\n'
assert nl == '\n'
def test_partition_source_all_three_types():
before, imports, after, nl = partition_source(
'# comment before\n'
'import os\n'
'import sys\n'
'print("code after")\n',
)
assert before == '# comment before\n'
assert imports == ['import os\n', 'import sys\n']
assert after == 'print("code after")\n'
assert nl == '\n'
def test_partition_source_preserves_whitespace_after():
before, imports, after, nl = partition_source(
'import os\n'
'\n'
'print(1)\n',
)
assert before == ''
assert imports == ['import os\n']
assert after == '\nprint(1)\n'
assert nl == '\n'
def test_partition_source_preserves_comments_after():
before, imports, after, nl = partition_source(
'import os\n'
'\n'
'# comment here!\n'
'\n'
'print(1)\n',
)
assert before == ''
assert imports == ['import os\n']
assert after == (
'\n'
'# comment here!\n'
'\n'
'print(1)\n'
)
assert nl == '\n'
def test_partition_source_interspersed_comments_become_code():
before, imports, after, nl = partition_source(
'import os\n'
'# hello world\n'
'import sys\n'
'\n'
'print(1)\n',
)
assert before == ''
assert imports == ['import os\n', 'import sys\n']
assert after == (
'# hello world\n'
'\n'
'print(1)\n'
)
assert nl == '\n'
def test_partition_source_noreorder_becomes_code():
before, imports, after, nl = partition_source(
'import os\n'
'import sys # noreorder\n'
'import re\n',
)
assert before == ''
assert imports == ['import os\n']
assert after == (
'import sys # noreorder\n'
'import re\n'
)
assert nl == '\n'
def test_partition_source_noreorder_becomes_code_on_first_import():
before, imports, after, nl = partition_source(
'# before\n'
'import os # noreorder\n'
'import sys\n',
)
assert before == '# before\n'
assert imports == []
assert after == (
'import os # noreorder\n'
'import sys\n'
)
assert nl == '\n'
def test_partition_source_noreorder_import_whitespace_above_preserved():
before, imports, after, nl = partition_source(
'import os\n'
'\n'
'import sys # noreorder\n'
'import re\n',
)
assert before == ''
assert imports == ['import os\n']
assert after == (
'\n'
'import sys # noreorder\n'
'import re\n'
)
assert nl == '\n'
def test_partition_source_noreorder_starts_code():
before, imports, after, nl = partition_source(
'import os\n'
'# noreorder\n'
'import sys\n',
)
assert before == ''
assert imports == ['import os\n']
assert after == (
'# noreorder\n'
'import sys\n'
)
assert nl == '\n'
def test_partition_source_noreorder_keeps_whitespace_above():
before, imports, after, nl = partition_source(
'import os\n'
'\n'
'# noreorder\n'
'import sys\n',
)
assert before == ''
assert imports == ['import os\n']
assert after == (
'\n'
'# noreorder\n'
'import sys\n'
)
assert nl == '\n'
@pytest.mark.parametrize(
('imports', 'expected'),
(
pytest.param([], [], id='trivial'),
pytest.param(
['import os\n', 'import sys\n'],
['import os\n', 'import sys\n'],
id='nothing to separate',
),
pytest.param(
['import os, sys\n'],
['import os\n', 'import sys\n'],
id='separates imports',
),
pytest.param(
# can't know what the comment points to, so we just remove it
['import os, sys # derp\n'],
['import os\n', 'import sys\n'],
id='separating removes comments',
),
pytest.param(
['import sys # noqa\n'],
['import sys # noqa\n'],
id='comments preserved when not separating',
),
),
)
def test_parse_imports(imports, expected):
parsed = parse_imports(imports, to_add=())
unparsed = [s for s, _ in parsed]
assert unparsed == expected
@pytest.mark.parametrize(
('imports', 'expected'),
(
pytest.param([], [], id='trivial'),
pytest.param(
['import sys\n', 'from six import text_type\n'],
['import sys\n', 'from six import text_type\n'],
id='no duplicates removed',
),
pytest.param(
['import sys\n', 'import sys\n'],
['import sys\n'],
id='removes exact duplicate',
),
pytest.param(
['import os\n', 'import os.path\n'],
['import os.path\n'],
id='removes redundant import, keeps last',
),
pytest.param(
['import os.path\n', 'import os\n'],
['import os.path\n'],
id='removes redundant import, keeps first',
),
pytest.param(
['import os\n', 'import os.path as os_path\n'],
['import os\n', 'import os.path as os_path\n'],
id='aliased is not redundant, last aliased',
),
pytest.param(
['import os as osmod\n', 'import os.path\n'],
['import os as osmod\n', 'import os.path\n'],
id='aliased is not redundant, first aliased',
),
),
)
def test_remove_duplicated_imports(imports, expected):
parsed = parse_imports(imports, to_add=())
ret = remove_duplicated_imports(parsed, to_remove=set())
unparsed = [s for s, _ in ret]
assert unparsed == expected
def test_apply_import_sorting_trivial():
assert apply_import_sorting(parse_imports([], to_add=())) == []
def test_apply_import_sorting_all_types():
imports = ['import os\n']
assert apply_import_sorting(parse_imports(imports, to_add=())) == imports
def test_apply_import_sorting_sorts_imports():
imports = [
# local imports
'from reorder_python_imports import main\n',
'import reorder_python_imports\n',
# site-package imports
'from six import text_type\n',
'import aspy\n',
# System imports (out of order)
'from os import path\n',
'import os\n',
]
expected = [
'import os\n',
'from os import path\n',
'\n',
'import aspy\n',
'from six import text_type\n',
'\n',
'import reorder_python_imports\n',
'from reorder_python_imports import main\n',
]
assert apply_import_sorting(parse_imports(imports, to_add=())) == expected
def test_apply_import_sorting_sorts_imports_with_application_module():
imports = [
'import _c_module\n',
'import reorder_python_imports\n',
'import third_party\n',
]
expected = [
'import third_party\n',
'\n',
'import _c_module\n',
'import reorder_python_imports\n',
]
ret = apply_import_sorting(
parse_imports(imports, to_add=()),
settings=Settings(unclassifiable_application_modules=('_c_module',)),
)
assert ret == expected
def test_apply_import_sorting_maintains_comments():
imports = ['import foo # noqa\n']
assert apply_import_sorting(parse_imports(imports)) == imports
@pytest.mark.parametrize('s', ('', '\n', '\n\n\n'))
def test_add_imports_empty_file(s):
assert fix_file_contents(
s,
to_add=('from __future__ import absolute_import\n',),
to_remove=set(),
to_replace=Replacements.make([]),
) == ''
def test_add_import_import_already_there():
assert fix_file_contents(
'from __future__ import absolute_import\n',
to_add=('from __future__ import absolute_import\n',),
to_remove=set(),
to_replace=Replacements.make([]),
) == 'from __future__ import absolute_import\n'
def test_add_import_not_there():
assert fix_file_contents(
'import os',
to_add=('from __future__ import absolute_import\n',),
to_remove=set(),
to_replace=Replacements.make([]),
) == (
'from __future__ import absolute_import\n'
'\n'
'import os\n'
)
def test_does_not_put_before_leading_comment():
assert fix_file_contents(
'# -*- coding: UTF-8 -*-',
to_add=('from __future__ import absolute_import\n',),
to_remove=set(),
to_replace=Replacements.make([]),
) == (
'# -*- coding: UTF-8 -*-\n'
'from __future__ import absolute_import\n'
)
def test_remove_import_trivial():
assert fix_file_contents(
'',
to_add=(),
to_remove={('__future__', 'with_statement', '')},
to_replace=Replacements.make([]),
) == ''
def test_remove_import_import_not_there():
assert fix_file_contents(
'import os\n',
to_add=(),
to_remove={('__future__', 'with_statement', '')},
to_replace=Replacements.make([]),
) == 'import os\n'
def test_remove_imports_actually_removes():
assert fix_file_contents(
'from __future__ import with_statement\n\n'
'import os\n',
to_add=(),
to_remove={('__future__', 'with_statement', '')},
to_replace=Replacements.make([]),
) == 'import os\n'
def test_replace_imports_noop():
ret = fix_file_contents(
'import os\n'
'import sys\n',
to_add=(),
to_remove=set(),
# import imports are not rewritten
to_replace=Replacements.make([('os', 'fail', '')]),
)
assert ret == 'import os\nimport sys\n'
def test_replace_imports_basic_from():
ret = fix_file_contents(
'from foo import bar\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('foo', 'baz', '')]),
)
assert ret == 'from baz import bar\n'
def test_replace_imports_relative_module():
ret = fix_file_contents(
'from .foo import bar\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('.foo', 'baz', '')]),
)
assert ret == 'from baz import bar\n'
def test_replace_imports_from_does_not_replace_name():
ret = fix_file_contents(
'from foo import bar\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('foo.bar', 'baz.hi', '')]),
)
assert ret == 'from foo import bar\n'
def test_replace_imports_from_asname():
ret = fix_file_contents(
'from foo import bar as baz\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('foo', 'baz', '')]),
)
assert ret == 'from baz import bar as baz\n'
def test_replace_imports_specific_attribute_name():
ret = fix_file_contents(
'from foo import bar\n'
'from foo import baz\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('foo', 'aaa', 'bar')]),
)
assert ret == (
'from aaa import bar\n'
'from foo import baz\n'
)
def test_replace_imports_specific_attribute_name_relative():
ret = fix_file_contents(
'from .foo import bar\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('.foo', 'aaa', 'bar')]),
)
assert ret == 'from aaa import bar\n'
def test_replace_module_imported():
ret = fix_file_contents(
'from six.moves import queue\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('six.moves.queue', 'queue', '')]),
)
assert ret == 'import queue\n'
def test_replace_module_imported_asname():
ret = fix_file_contents(
'from six.moves import queue as Queue\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('six.moves.queue', 'queue', '')]),
)
assert ret == 'import queue as Queue\n'
def test_replace_module_imported_relative():
ret = fix_file_contents(
'from .foo import bar as thing\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('.foo.bar', 'womp.baz', '')]),
)
assert ret == 'from womp import baz as thing\n'
def test_replace_module_imported_becomes_relative():
ret = fix_file_contents(
'from a.b import c as d\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('a.b.c', '.e', '')]),
)
assert ret == 'from a.b import c as d\n'
def test_replace_module_imported_with_nested_replacement():
ret = fix_file_contents(
'from six.moves.urllib import parse\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([
('six.moves.urllib.parse', 'urllib.parse', ''),
]),
)
assert ret == 'from urllib import parse\n'
def test_replace_module_imported_with_nested_replacement_asname():
ret = fix_file_contents(
'from six.moves.urllib import parse as urllib_parse\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([
('six.moves.urllib.parse', 'urllib.parse', ''),
]),
)
assert ret == 'from urllib import parse as urllib_parse\n'
def test_replace_module_imported_with_nested_renamed_replacement_asname():
ret = fix_file_contents(
'from six.moves.urllib import parse as urllib_parse\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([
('six.moves.urllib.parse', 'urllib.parse2', ''),
]),
)
assert ret == 'from urllib import parse2 as urllib_parse\n'
def test_replace_module_prefix():
ret = fix_file_contents(
'from a.b.c import d\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('a.b', 'e.f', '')]),
)
assert ret == 'from e.f.c import d\n'
def test_replace_module_prefix_relative():
ret = fix_file_contents(
'from .a.b.c import d\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('.a.b', 'e.f', '')]),
)
assert ret == 'from e.f.c import d\n'
def test_replace_module_skips_attr_specific_rules():
ret = fix_file_contents(
'from libone import util\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([
('libone.util', 'libtwo.util', 'is_valid'),
]),
)
assert ret == 'from libone import util\n'
def test_replace_module_would_make_incorrect_new_import():
ret = fix_file_contents(
'from foo import bar\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('foo.bar', 'baz', '')]),
)
assert ret == 'from foo import bar\n'
def test_replace_module_skips_nonmatching_rules():
ret = fix_file_contents(
'from libthree import util\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('libone.util', 'libtwo.util', '')]),
)
assert ret == 'from libthree import util\n'
def test_replace_import_import_with_asname():
ret = fix_file_contents(
'import a.b as c\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('a.b', 'd.e', '')]),
)
assert ret == 'import d.e as c\n'
def test_replace_import_import_prefix_with_asname():
ret = fix_file_contents(
'import a.b as c\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('a', 'd', '')]),
)
assert ret == 'import d.b as c\n'
def test_replace_import_unrelated_import_with_asname():
ret = fix_file_contents(
'import something.unrelated as c\n',
to_add=(),
to_remove=set(),
to_replace=Replacements.make([('a', 'd', '')]),
)
assert ret == 'import something.unrelated as c\n'
cases = pytest.mark.parametrize(
('s', 'expected'),
(
pytest.param('', '', id='trivial'),
pytest.param(
'import os\n'
"# I'm right after imports\n"
'x = os.path\n',
'import os\n'
"# I'm right after imports\n"
'x = os.path\n',
id='code right after imports',
),
pytest.param(
'# Mostly to demonstrate the (potentially non-ideal) behaviour\n'
'import os\n\n'
'# Hello from nomansland\n'
'import six\n',
'# Mostly to demonstrate the (potentially non-ideal) behaviour\n'
'import os\n\n'
'import six\n'
'# Hello from nomansland\n',
id='comment in imports',
),
pytest.param(
'import os\n'
'"""misplaced docstring"""\n'
'import sys\n',
'import os\n'
'import sys\n'
'"""misplaced docstring"""\n',
id='docstring in imports',
),
pytest.param(
"# I'm a license comment\n"
'import os\n',
"# I'm a license comment\n"
'import os\n',
id='license comment',
),
pytest.param(
'import reorder_python_imports\n'
'import os\n'
'import six\n',
'import os\n\n'
'import six\n\n'
'import reorder_python_imports\n',
id='needs reordering',
),
pytest.param(
'import sys\n'
'import os',
'import os\n'
'import sys\n',
id='no eol',
),
pytest.param(
'# noreorder\n'
'import reorder_python_imports\n'
'import os\n'
'import six\n',
'# noreorder\n'
'import reorder_python_imports\n'
'import os\n'
'import six\n',
id='noreorder all',
),
pytest.param(
'import sys\n'
'import reorder_python_imports\n\n'
'import matplotlib # noreorder\n'
"matplotlib.use('Agg')\n",
'import sys\n\n'
'import reorder_python_imports\n\n'
'import matplotlib # noreorder\n'
"matplotlib.use('Agg')\n",
id='noreorder inline',
),
pytest.param(
'import sys\n'
'import reorder_python_imports\n\n'
'# noreorder\n'
'import matplotlib\n'
"matplotlib.use('Agg')\n",
'import sys\n\n'
'import reorder_python_imports\n\n'
'# noreorder\n'
'import matplotlib\n'
"matplotlib.use('Agg')\n",
id='noreorder not at beginning',
),
pytest.param(
'from __future__ import annotations\n'
'\n'
'import __future__\n',
'from __future__ import annotations\n'
'\n'
'import __future__\n',
id='__future__ from and import',
),
),
)
@cases
def test_fix_file_contents(s, expected):
ret = fix_file_contents(
s,
to_add=(),
to_remove=set(),
to_replace=Replacements.make([]),
)
assert ret == expected
@cases
def test_integration_main(s, expected, tmpdir):
test_file = tmpdir.join('test.py')
test_file.write(s)
retv = main((str(test_file),))
# Check return value
assert retv == int(s != expected)
# Check the contents rewritten
assert test_file.read() == expected
@pytest.fixture
def restore_sys_path():
before = sys.path[:]
yield
sys.path[:] = before
@pytest.mark.usefixtures('restore_sys_path')
def test_additional_directories_integration(in_tmpdir):
if '' in sys.path: # pragma: no cover (depends on run environment)
sys.path.remove('')
# Intentionally avoiding 'tests' and 'testing' because those would clash
# with the names of this project
in_tmpdir.join('nottests/nottesting/__init__.py').ensure()
in_tmpdir.join('foo.py').write(
'import thirdparty\n'
'import nottests\n'
'import nottesting\n',
)
# Without the new option
main(('foo.py',))
assert in_tmpdir.join('foo.py').read() == (
'import nottesting\n'
'import thirdparty\n'
'\n'
'import nottests\n'
)
# With the new option
main(('foo.py', '--application-directories', '.:nottests'))
assert in_tmpdir.join('foo.py').read() == (
'import thirdparty\n'
'\n'
'import nottesting\n'
'import nottests\n'
)
def test_fix_crlf():
s = (
'"""foo"""\r\n'
'import os\r\n'
'import sys\r\n'
'x = 1\r\n'
)
ret = fix_file_contents(
s,
to_add=(),