rope_py3k / ropetest / refactor / importutilstest.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
import unittest

from rope.refactor.importutils import ImportTools, importinfo, add_import
from ropetest import testutils


class ImportUtilsTest(unittest.TestCase):

    def setUp(self):
        super(ImportUtilsTest, self).setUp()
        self.project = testutils.sample_project()
        self.pycore = self.project.pycore
        self.import_tools = ImportTools(self.pycore)

        self.mod = testutils.create_module(self.project, 'mod')
        self.pkg1 = testutils.create_package(self.project, 'pkg1')
        self.mod1 = testutils.create_module(self.project, 'mod1', self.pkg1)
        self.pkg2 = testutils.create_package(self.project, 'pkg2')
        self.mod2 = testutils.create_module(self.project, 'mod2', self.pkg2)
        self.mod3 = testutils.create_module(self.project, 'mod3', self.pkg2)
        p1 = testutils.create_package(self.project, 'p1')
        p2 = testutils.create_package(self.project, 'p2', p1)
        p3 = testutils.create_package(self.project, 'p3', p2)
        m1 = testutils.create_module(self.project, 'm1', p3)
        l = testutils.create_module(self.project, 'l', p3)

    def tearDown(self):
        testutils.remove_project(self.project)
        super(ImportUtilsTest, self).tearDown()

    def test_get_import_for_module(self):
        mod = self.pycore.find_module('mod')
        import_statement = self.import_tools.get_import(mod)
        self.assertEqual('import mod', import_statement.get_import_statement())

    def test_get_import_for_module_in_nested_modules(self):
        mod = self.pycore.find_module('pkg1.mod1')
        import_statement = self.import_tools.get_import(mod)
        self.assertEqual('import pkg1.mod1', import_statement.get_import_statement())

    def test_get_import_for_module_in_init_dot_py(self):
        init_dot_py = self.pkg1.get_child('__init__.py')
        import_statement = self.import_tools.get_import(init_dot_py)
        self.assertEqual('import pkg1', import_statement.get_import_statement())


    def test_get_from_import_for_module(self):
        mod = self.pycore.find_module('mod')
        import_statement = self.import_tools.get_from_import(mod, 'a_func')
        self.assertEqual('from mod import a_func',
                          import_statement.get_import_statement())

    def test_get_from_import_for_module_in_nested_modules(self):
        mod = self.pycore.find_module('pkg1.mod1')
        import_statement = self.import_tools.get_from_import(mod, 'a_func')
        self.assertEqual('from pkg1.mod1 import a_func',
                          import_statement.get_import_statement())

    def test_get_from_import_for_module_in_init_dot_py(self):
        init_dot_py = self.pkg1.get_child('__init__.py')
        import_statement = self.import_tools.\
                           get_from_import(init_dot_py, 'a_func')
        self.assertEqual('from pkg1 import a_func',
                          import_statement.get_import_statement())


    def test_get_import_statements(self):
        self.mod.write('import pkg1\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.imports
        self.assertEqual('import pkg1',
                          imports[0].import_info.get_import_statement())

    def test_get_import_statements_with_alias(self):
        self.mod.write('import pkg1.mod1 as mod1\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.imports
        self.assertEqual('import pkg1.mod1 as mod1',
                          imports[0].import_info.get_import_statement())

    def test_get_import_statements_for_froms(self):
        self.mod.write('from pkg1 import mod1\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.imports
        self.assertEqual('from pkg1 import mod1',
                          imports[0].import_info.get_import_statement())

    def test_get_multi_line_import_statements_for_froms(self):
        self.mod.write('from pkg1 \\\n    import mod1\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.imports
        self.assertEqual('from pkg1 import mod1',
                          imports[0].import_info.get_import_statement())

    def test_get_import_statements_for_from_star(self):
        self.mod.write('from pkg1 import *\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.imports
        self.assertEqual('from pkg1 import *',
                          imports[0].import_info.get_import_statement())

    @testutils.run_only_for_25
    def test_get_import_statements_for_new_relatives(self):
        self.mod2.write('from .mod3 import x\n')
        pymod = self.pycore.get_module('pkg2.mod2')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.imports
        self.assertEqual('from .mod3 import x',
                          imports[0].import_info.get_import_statement())

    def test_ignoring_indented_imports(self):
        self.mod.write('if True:\n    import pkg1\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.imports
        self.assertEqual(0, len(imports))

    def test_import_get_names(self):
        self.mod.write('import pkg1 as pkg\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.imports
        context = importinfo.ImportContext(self.pycore, self.project.root)
        self.assertEqual(['pkg'],
                          imports[0].import_info.get_imported_names(context))

    def test_import_get_names_with_alias(self):
        self.mod.write('import pkg1.mod1\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.imports
        context = importinfo.ImportContext(self.pycore, self.project.root)
        self.assertEqual(['pkg1'],
                          imports[0].import_info.get_imported_names(context))

    def test_import_get_names_with_alias2(self):
        self.mod1.write('def a_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import *\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.imports
        context = importinfo.ImportContext(self.pycore, self.project.root)
        self.assertEqual(['a_func'],
                          imports[0].import_info.get_imported_names(context))

    def test_empty_getting_used_imports(self):
        self.mod.write('')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.get_used_imports(pymod)
        self.assertEqual(0, len(imports))

    def test_empty_getting_used_imports2(self):
        self.mod.write('import pkg\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.get_used_imports(pymod)
        self.assertEqual(0, len(imports))

    def test_simple_getting_used_imports(self):
        self.mod.write('import pkg\nprint(pkg)\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.get_used_imports(pymod)
        self.assertEqual(1, len(imports))
        self.assertEqual('import pkg', imports[0].get_import_statement())

    def test_simple_getting_used_imports2(self):
        self.mod.write('import pkg\ndef a_func():\n    print(pkg)\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.get_used_imports(pymod)
        self.assertEqual(1, len(imports))
        self.assertEqual('import pkg', imports[0].get_import_statement())

    def test_getting_used_imports_for_nested_scopes(self):
        self.mod.write('import pkg1\nprint(pkg1)\ndef a_func():\n    pass\nprint(pkg1)\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.get_used_imports(
            pymod['a_func'].get_object())
        self.assertEqual(0, len(imports))

    def test_getting_used_imports_for_nested_scopes2(self):
        self.mod.write('from pkg1 import mod1\ndef a_func():\n    print(mod1)\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.get_used_imports(
            pymod['a_func'].get_object())
        self.assertEqual(1, len(imports))
        self.assertEqual('from pkg1 import mod1', imports[0].get_import_statement())

    def test_empty_removing_unused_imports(self):
        self.mod.write('import pkg1\nprint(pkg1)\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('import pkg1\nprint(pkg1)\n',
                          module_with_imports.get_changed_source())

    def test_simple_removing_unused_imports(self):
        self.mod.write('import pkg1\n\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('', module_with_imports.get_changed_source())

    def test_simple_removing_unused_imports_for_froms(self):
        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import a_func, another_func\n\na_func()\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('from pkg1.mod1 import a_func\n\na_func()\n',
                          module_with_imports.get_changed_source())

    def test_simple_removing_unused_imports_for_from_stars(self):
        self.mod.write('from pkg1.mod1 import *\n\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('', module_with_imports.get_changed_source())

    def test_simple_removing_unused_imports_for_nested_modules(self):
        self.mod1.write('def a_func():\n    pass\n')
        self.mod.write('import pkg1.mod1\npkg1.mod1.a_func()')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('import pkg1.mod1\npkg1.mod1.a_func()',
                          module_with_imports.get_changed_source())

    def test_removing_unused_imports_and_functions_of_the_same_name(self):
        self.mod.write('def a_func():\n    pass\ndef a_func():\n    pass\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('def a_func():\n    pass\ndef a_func():\n    pass\n',
                          module_with_imports.get_changed_source())

    def test_removing_unused_imports_for_from_import_with_as(self):
        self.mod.write('a_var = 1\n')
        self.mod1.write('from mod import a_var as myvar\na_var = myvar\n')
        pymod = self.pycore.resource_to_pyobject(self.mod1)
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('from mod import a_var as myvar\na_var = myvar\n',
                          module_with_imports.get_changed_source())

    def test_not_removing_imports_that_conflict_with_class_names(self):
        code = 'import pkg1\nclass A(object):\n    pkg1 = 0\n' \
               '    def f(self):\n        a_var = pkg1\n'
        self.mod.write(code)
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual(code, module_with_imports.get_changed_source())

    def test_adding_imports(self):
        self.mod.write('\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        new_import = self.import_tools.get_import(self.mod1)
        module_with_imports.add_import(new_import)
        self.assertEqual('import pkg1.mod1\n',
                          module_with_imports.get_changed_source())

    def test_adding_from_imports(self):
        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import a_func\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        new_import = self.import_tools.get_from_import(
            self.mod1, 'another_func')
        module_with_imports.add_import(new_import)
        self.assertEqual('from pkg1.mod1 import a_func, another_func\n',
                          module_with_imports.get_changed_source())

    def test_adding_to_star_imports(self):
        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import *\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        new_import = self.import_tools.get_from_import(
            self.mod1, 'another_func')
        module_with_imports.add_import(new_import)
        self.assertEqual('from pkg1.mod1 import *\n',
                          module_with_imports.get_changed_source())

    def test_adding_star_imports(self):
        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import a_func\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        new_import = self.import_tools.get_from_import(self.mod1,
                                                                  '*')
        module_with_imports.add_import(new_import)
        self.assertEqual('from pkg1.mod1 import *\n',
                          module_with_imports.get_changed_source())

    def test_adding_imports_and_preserving_spaces_after_imports(self):
        self.mod.write('import pkg1\n\n\nprint(pkg1)\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        new_import = self.import_tools.get_import(self.pkg2)
        module_with_imports.add_import(new_import)
        self.assertEqual('import pkg1\nimport pkg2\n\n\nprint(pkg1)\n',
                          module_with_imports.get_changed_source())

    def test_not_changing_the_format_of_unchanged_imports(self):
        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import (a_func,\n    another_func)\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        self.assertEqual(
            'from pkg1.mod1 import (a_func,\n    another_func)\n',
            module_with_imports.get_changed_source())

    def test_not_changing_the_format_of_unchanged_imports2(self):
        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import (a_func)\na_func()\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('from pkg1.mod1 import (a_func)\na_func()\n',
                          module_with_imports.get_changed_source())

    def test_removing_unused_imports_and_reoccuring_names(self):
        self.mod1.write('def a_func():\n    pass\n'
                        'def another_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import *\n'
                       'from pkg1.mod1 import a_func\na_func()\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('from pkg1.mod1 import *\na_func()\n',
                          module_with_imports.get_changed_source())

    def test_removing_unused_imports_and_reoccuring_names2(self):
        self.mod.write('import pkg2.mod2\nimport pkg2.mod3\n'
                       'print(pkg2.mod2, pkg2.mod3)')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual(
            'import pkg2.mod2\nimport pkg2.mod3\nprint(pkg2.mod2, pkg2.mod3)',
            module_with_imports.get_changed_source())

    def test_removing_unused_imports_and_common_packages(self):
        self.mod.write('import pkg1.mod1\nimport pkg1\nprint(pkg1, pkg1.mod1)\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('import pkg1.mod1\nprint(pkg1, pkg1.mod1)\n',
                          module_with_imports.get_changed_source())

    def test_removing_unused_imports_and_common_packages_reversed(self):
        self.mod.write('import pkg1\nimport pkg1.mod1\nprint(pkg1, pkg1.mod1)\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_duplicates()
        self.assertEqual('import pkg1.mod1\nprint(pkg1, pkg1.mod1)\n',
                          module_with_imports.get_changed_source())

    def test_removing_unused_imports_and_common_packages2(self):
        self.mod.write('import pkg1.mod1\nimport pkg1.mod2\nprint(pkg1)\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('import pkg1.mod1\nprint(pkg1)\n',
                          module_with_imports.get_changed_source())

    def test_removing_unused_imports_and_froms(self):
        self.mod1.write('def func1():\n    pass\n')
        self.mod.write('from pkg1.mod1 import func1\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('', module_with_imports.get_changed_source())

    def test_removing_unused_imports_and_froms2(self):
        self.mod1.write('def func1():\n    pass\n')
        self.mod.write('from pkg1.mod1 import func1\nfunc1()')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('from pkg1.mod1 import func1\nfunc1()',
                          module_with_imports.get_changed_source())

    def test_removing_unused_imports_and_froms3(self):
        self.mod1.write('def func1():\n    pass\n')
        self.mod.write('from pkg1.mod1 import func1\n'
                       'def a_func():\n    func1()\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual(
            'from pkg1.mod1 import func1\ndef a_func():\n    func1()\n',
            module_with_imports.get_changed_source())

    def test_removing_unused_imports_and_froms4(self):
        self.mod1.write('def func1():\n    pass\n')
        self.mod.write('from pkg1.mod1 import func1\nclass A(object):\n'
                       '    def a_func(self):\n        func1()\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('from pkg1.mod1 import func1\nclass A(object):\n'
                          '    def a_func(self):\n        func1()\n',
                          module_with_imports.get_changed_source())

    def test_removing_unused_imports_and_getting_attributes(self):
        self.mod1.write('class A(object):\n    def f(self):\n        pass\n')
        self.mod.write('from pkg1.mod1 import A\nvar = A().f()')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('from pkg1.mod1 import A\nvar = A().f()',
                          module_with_imports.get_changed_source())

    def test_removing_unused_imports_function_parameters(self):
        self.mod1.write('def func1():\n    pass\n')
        self.mod.write('import pkg1\ndef a_func(pkg1):\n    my_var = pkg1\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('def a_func(pkg1):\n    my_var = pkg1\n',
                          module_with_imports.get_changed_source())

    def test_trivial_expanding_star_imports(self):
        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import *\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.expand_stars()
        self.assertEqual('', module_with_imports.get_changed_source())

    def test_expanding_star_imports(self):
        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import *\na_func()\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.expand_stars()
        self.assertEqual('from pkg1.mod1 import a_func\na_func()\n',
                          module_with_imports.get_changed_source())

    def test_removing_duplicate_imports(self):
        self.mod.write('import pkg1\nimport pkg1\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_duplicates()
        self.assertEqual('import pkg1\n',
                          module_with_imports.get_changed_source())

    def test_removing_duplicates_and_reoccuring_names(self):
        self.mod.write('import pkg2.mod2\nimport pkg2.mod3\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_duplicates()
        self.assertEqual('import pkg2.mod2\nimport pkg2.mod3\n',
                          module_with_imports.get_changed_source())

    def test_removing_duplicate_imports_for_froms(self):
        self.mod1.write(
            'def a_func():\n    pass\ndef another_func():\n    pass\n')
        self.mod.write('from pkg1 import a_func\n'
                       'from pkg1 import a_func, another_func\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_duplicates()
        self.assertEqual('from pkg1 import a_func, another_func\n',
                          module_with_imports.get_changed_source())

    def test_transforming_froms_to_normal_changing_imports(self):
        self.mod1.write('def a_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import a_func\nprint(a_func)\n')
        pymod = self.pycore.get_module('mod')
        changed_module = self.import_tools.froms_to_imports(pymod)
        self.assertEqual('import pkg1.mod1\nprint(pkg1.mod1.a_func)\n',
                          changed_module)

    def test_transforming_froms_to_normal_changing_occurances(self):
        self.mod1.write('def a_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import a_func\na_func()')
        pymod = self.pycore.get_module('mod')
        changed_module = self.import_tools.froms_to_imports(pymod)
        self.assertEqual('import pkg1.mod1\npkg1.mod1.a_func()',
                          changed_module)

    def test_transforming_froms_to_normal_for_multi_imports(self):
        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import *\na_func()\nanother_func()\n')
        pymod = self.pycore.get_module('mod')
        changed_module = self.import_tools.froms_to_imports(pymod)
        self.assertEqual(
            'import pkg1.mod1\npkg1.mod1.a_func()\npkg1.mod1.another_func()\n',
            changed_module)

    def test_transforming_froms_to_normal_for_multi_imports_inside_parens(self):
        self.mod1.write('def a_func():\n    pass\ndef another_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import (a_func, \n    another_func)' \
                       '\na_func()\nanother_func()\n')
        pymod = self.pycore.get_module('mod')
        changed_module = self.import_tools.froms_to_imports(pymod)
        self.assertEqual(
            'import pkg1.mod1\npkg1.mod1.a_func()\npkg1.mod1.another_func()\n',
            changed_module)

    def test_transforming_froms_to_normal_from_stars(self):
        self.mod1.write('def a_func():\n    pass\n')
        self.mod.write('from pkg1.mod1 import *\na_func()\n')
        pymod = self.pycore.get_module('mod')
        changed_module = self.import_tools.froms_to_imports(pymod)
        self.assertEqual('import pkg1.mod1\npkg1.mod1.a_func()\n', changed_module)

    def test_transforming_froms_to_normal_from_stars2(self):
        self.mod1.write('a_var = 10')
        self.mod.write('import pkg1.mod1\nfrom pkg1.mod1 import a_var\n' \
                       'def a_func():\n    print(pkg1.mod1, a_var)\n')
        pymod = self.pycore.get_module('mod')
        changed_module = self.import_tools.froms_to_imports(pymod)
        self.assertEqual('import pkg1.mod1\n' \
                          'def a_func():\n    print(pkg1.mod1, pkg1.mod1.a_var)\n',
                          changed_module)

    def test_transforming_froms_to_normal_from_with_alias(self):
        self.mod1.write('def a_func():\n    pass\n')
        self.mod.write(
            'from pkg1.mod1 import a_func as another_func\nanother_func()\n')
        pymod = self.pycore.get_module('mod')
        changed_module = self.import_tools.froms_to_imports(pymod)
        self.assertEqual('import pkg1.mod1\npkg1.mod1.a_func()\n',
                          changed_module)

    def test_transforming_froms_to_normal_for_relatives(self):
        self.mod2.write('def a_func():\n    pass\n')
        self.mod3.write('from mod2 import *\na_func()\n')
        pymod = self.pycore.resource_to_pyobject(self.mod3)
        changed_module = self.import_tools.froms_to_imports(pymod)
        self.assertEqual('import pkg2.mod2\npkg2.mod2.a_func()\n',
                          changed_module)

    def test_transforming_froms_to_normal_for_os_path(self):
        self.mod.write('from os import path\npath.exists(\'.\')\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        changed_module = self.import_tools.froms_to_imports(pymod)
        self.assertEqual('import os\nos.path.exists(\'.\')\n', changed_module)

    def test_transform_relatives_imports_to_absolute_imports_doing_nothing(self):
        self.mod2.write('from pkg1 import mod1\nimport mod1\n')
        pymod = self.pycore.resource_to_pyobject(self.mod2)
        self.assertEqual('from pkg1 import mod1\nimport mod1\n',
                          self.import_tools.relatives_to_absolutes(pymod))

    def test_transform_relatives_to_absolute_imports_for_normal_imports(self):
        self.mod2.write('import mod3\n')
        pymod = self.pycore.resource_to_pyobject(self.mod2)
        self.assertEqual('import pkg2.mod3\n',
                          self.import_tools.relatives_to_absolutes(pymod))

    def test_transform_relatives_imports_to_absolute_imports_for_froms(self):
        self.mod3.write('def a_func():\n    pass\n')
        self.mod2.write('from mod3 import a_func\n')
        pymod = self.pycore.resource_to_pyobject(self.mod2)
        self.assertEqual('from pkg2.mod3 import a_func\n',
                          self.import_tools.relatives_to_absolutes(pymod))

    @testutils.run_only_for_25
    def test_transform_relatives_imports_to_absolute_imports_for_new_relatives(self):
        self.mod3.write('def a_func():\n    pass\n')
        self.mod2.write('from .mod3 import a_func\n')
        pymod = self.pycore.resource_to_pyobject(self.mod2)
        self.assertEqual('from pkg2.mod3 import a_func\n',
                          self.import_tools.relatives_to_absolutes(pymod))

    def test_transform_relatives_to_absolute_imports_for_normal_imports2(self):
        self.mod2.write('import mod3\nprint(mod3)')
        pymod = self.pycore.resource_to_pyobject(self.mod2)
        self.assertEqual('import pkg2.mod3\nprint(pkg2.mod3)',
                          self.import_tools.relatives_to_absolutes(pymod))

    def test_transform_relatives_to_absolute_imports_for_aliases(self):
        self.mod2.write('import mod3 as mod3\nprint(mod3)')
        pymod = self.pycore.resource_to_pyobject(self.mod2)
        self.assertEqual('import pkg2.mod3 as mod3\nprint(mod3)',
                          self.import_tools.relatives_to_absolutes(pymod))

    def test_organizing_imports(self):
        self.mod1.write('import mod1\n')
        pymod = self.pycore.resource_to_pyobject(self.mod1)
        self.assertEqual('', self.import_tools.organize_imports(pymod))

    def test_removing_self_imports(self):
        self.mod.write('import mod\nmod.a_var = 1\nprint(mod.a_var)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('a_var = 1\nprint(a_var)\n',
                          self.import_tools.organize_imports(pymod))

    def test_removing_self_imports2(self):
        self.mod1.write('import pkg1.mod1\npkg1.mod1.a_var = 1\n'
                        'print(pkg1.mod1.a_var)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod1)
        self.assertEqual('a_var = 1\nprint(a_var)\n',
                          self.import_tools.organize_imports(pymod))

    def test_removing_self_imports_with_as(self):
        self.mod.write('import mod as mymod\n'
                       'mymod.a_var = 1\nprint(mymod.a_var)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('a_var = 1\nprint(a_var)\n',
                          self.import_tools.organize_imports(pymod))

    def test_removing_self_imports_for_froms(self):
        self.mod1.write('from pkg1 import mod1\n'
                        'mod1.a_var = 1\nprint(mod1.a_var)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod1)
        self.assertEqual('a_var = 1\nprint(a_var)\n',
                          self.import_tools.organize_imports(pymod))

    def test_removing_self_imports_for_froms_with_as(self):
        self.mod1.write('from pkg1 import mod1 as mymod\n'
                        'mymod.a_var = 1\nprint(mymod.a_var)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod1)
        self.assertEqual('a_var = 1\nprint(a_var)\n',
                          self.import_tools.organize_imports(pymod))

    def test_removing_self_imports_for_froms2(self):
        self.mod.write('from mod import a_var\na_var = 1\nprint(a_var)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('a_var = 1\nprint(a_var)\n',
                          self.import_tools.organize_imports(pymod))

    def test_removing_self_imports_for_froms3(self):
        self.mod.write('from mod import a_var\na_var = 1\nprint(a_var)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('a_var = 1\nprint(a_var)\n',
                          self.import_tools.organize_imports(pymod))

    def test_removing_self_imports_for_froms4(self):
        self.mod.write('from mod import a_var as myvar\n'
                       'a_var = 1\nprint(myvar)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('a_var = 1\nprint(a_var)\n',
                          self.import_tools.organize_imports(pymod))

    def test_removing_self_imports_with_no_dot_after_mod(self):
        self.mod.write('import mod\nprint(mod)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('import mod\n\n\nprint(mod)\n',
                          self.import_tools.organize_imports(pymod))

    def test_removing_self_imports_with_no_dot_after_mod2(self):
        self.mod.write('import mod\na_var = 1\n'
                       'print(mod\\\n     \\\n     .var)\n\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('a_var = 1\nprint(var)\n\n',
                          self.import_tools.organize_imports(pymod))

    def test_removing_self_imports_for_from_import_star(self):
        self.mod.write('from mod import *\na_var = 1\nprint(myvar)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('a_var = 1\nprint(myvar)\n',
                          self.import_tools.organize_imports(pymod))

    def test_not_removing_future_imports(self):
        self.mod.write('from __future__ import division\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('from __future__ import division\n',
                          self.import_tools.organize_imports(pymod))

    def test_sorting_empty_imports(self):
        self.mod.write('')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('', self.import_tools.sort_imports(pymod))

    def test_sorting_one_import(self):
        self.mod.write('import pkg1.mod1\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('import pkg1.mod1\n',
                          self.import_tools.sort_imports(pymod))

    def test_sorting_imports_alphabetically(self):
        self.mod.write('import pkg2.mod2\nimport pkg1.mod1\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('import pkg1.mod1\nimport pkg2.mod2\n',
                          self.import_tools.sort_imports(pymod))

    def test_sorting_imports_and_froms(self):
        self.mod.write('import pkg2.mod2\nfrom pkg1 import mod1\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('import pkg2.mod2\nfrom pkg1 import mod1\n',
                          self.import_tools.sort_imports(pymod))

    def test_sorting_imports_and_standard_modules(self):
        self.mod.write('import pkg1\nimport sys\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('import sys\n\nimport pkg1\n',
                          self.import_tools.sort_imports(pymod))

    def test_sorting_imports_and_standard_modules2(self):
        self.mod.write('import sys\n\nimport time\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('import sys\nimport time\n',
                          self.import_tools.sort_imports(pymod))

    def test_sorting_only_standard_modules(self):
        self.mod.write('import sys\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('import sys\n',
                          self.import_tools.sort_imports(pymod))

    def test_sorting_third_party(self):
        self.mod.write('import pkg1\nimport a_third_party\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('import a_third_party\n\nimport pkg1\n',
                          self.import_tools.sort_imports(pymod))

    def test_sorting_only_third_parties(self):
        self.mod.write('import a_third_party\na_var = 1\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual('import a_third_party\n\n\na_var = 1\n',
                          self.import_tools.sort_imports(pymod))

    def test_simple_handling_long_imports(self):
        self.mod.write('import pkg1.mod1\n\n\nm = pkg1.mod1\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'import pkg1.mod1\n\n\nm = pkg1.mod1\n',
            self.import_tools.handle_long_imports(pymod, maxdots=2))

    def test_handling_long_imports_for_many_dots(self):
        self.mod.write('import p1.p2.p3.m1\n\n\nm = p1.p2.p3.m1\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'from p1.p2.p3 import m1\n\n\nm = m1\n',
            self.import_tools.handle_long_imports(pymod, maxdots=2))

    def test_handling_long_imports_for_their_length(self):
        self.mod.write('import p1.p2.p3.m1\n\n\nm = p1.p2.p3.m1\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'import p1.p2.p3.m1\n\n\nm = p1.p2.p3.m1\n',
            self.import_tools.handle_long_imports(pymod, maxdots=3,
                                                  maxlength=20))

    def test_handling_long_imports_for_many_dots2(self):
        self.mod.write('import p1.p2.p3.m1\n\n\nm = p1.p2.p3.m1\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'from p1.p2.p3 import m1\n\n\nm = m1\n',
            self.import_tools.handle_long_imports(pymod, maxdots=3,
                                                  maxlength=10))

    def test_handling_long_imports_with_one_letter_last(self):
        self.mod.write('import p1.p2.p3.l\n\n\nm = p1.p2.p3.l\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'from p1.p2.p3 import l\n\n\nm = l\n',
            self.import_tools.handle_long_imports(pymod, maxdots=2))

    def test_empty_removing_unused_imports_and_eating_blank_lines(self):
        self.mod.write('import pkg1\nimport pkg2\n\n\nprint(pkg1)\n')
        pymod = self.pycore.get_module('mod')
        module_with_imports = self.import_tools.module_imports(pymod)
        module_with_imports.remove_unused_imports()
        self.assertEqual('import pkg1\n\n\nprint(pkg1)\n',
                          module_with_imports.get_changed_source())

    def test_sorting_imports_moving_to_top(self):
        self.mod.write('import mod\ndef f():\n    print(mod, pkg1, pkg2)\n'
                       'import pkg1\nimport pkg2\n')
        pymod = self.pycore.get_module('mod')
        self.assertEqual('import mod\nimport pkg1\nimport pkg2\n\n\n'
                          'def f():\n    print(mod, pkg1, pkg2)\n',
                          self.import_tools.sort_imports(pymod))

    def test_sorting_imports_moving_to_top2(self):
        self.mod.write('def f():\n    print(mod)\nimport mod\n')
        pymod = self.pycore.get_module('mod')
        self.assertEqual('import mod\n\n\ndef f():\n    print(mod)\n',
                          self.import_tools.sort_imports(pymod))

    def test_sorting_imports_moving_to_top_and_module_docs(self):
        self.mod.write('"""\ndocs\n"""\ndef f():\n    print(mod)\nimport mod\n')
        pymod = self.pycore.get_module('mod')
        self.assertEqual(
            '"""\ndocs\n"""\nimport mod\n\n\ndef f():\n    print(mod)\n',
            self.import_tools.sort_imports(pymod))

    def test_sorting_future_imports(self):
        self.mod.write('import os\nfrom __future__ import devision\n')
        pymod = self.pycore.get_module('mod')
        self.assertEqual(
            'from __future__ import devision\n\nimport os\n',
            self.import_tools.sort_imports(pymod))

    def test_customized_import_organization(self):
        self.mod.write('import sys\nimport sys\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'import sys\n',
            self.import_tools.organize_imports(pymod, unused=False))

    def test_customized_import_organization2(self):
        self.mod.write('import sys\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'import sys\n',
            self.import_tools.organize_imports(pymod, unused=False))

    def test_customized_import_organization3(self):
        self.mod.write('import sys\nimport mod\n\n\nvar = 1\nprint(mod.var)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'import sys\n\n\nvar = 1\nprint(var)\n',
            self.import_tools.organize_imports(pymod, unused=False))

    def test_trivial_filtered_expand_stars(self):
        self.pkg1.get_child('__init__.py').write('var1 = 1\n')
        self.pkg2.get_child('__init__.py').write('var2 = 1\n')
        self.mod.write('from pkg1 import *\nfrom pkg2 import *\n\n'
                       'print(var1, var2)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'from pkg1 import *\nfrom pkg2 import *\n\nprint(var1, var2)\n',
             self.import_tools.expand_stars(pymod, lambda stmt: False))

    def _line_filter(self, lineno):
        def import_filter(import_stmt):
            return import_stmt.start_line <= lineno < import_stmt.end_line
        return import_filter

    def test_filtered_expand_stars(self):
        self.pkg1.get_child('__init__.py').write('var1 = 1\n')
        self.pkg2.get_child('__init__.py').write('var2 = 1\n')
        self.mod.write('from pkg1 import *\nfrom pkg2 import *\n\n'
                       'print(var1, var2)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'from pkg1 import *\nfrom pkg2 import var2\n\nprint(var1, var2)\n',
             self.import_tools.expand_stars(pymod, self._line_filter(2)))

    def test_filtered_relative_to_absolute(self):
        self.mod3.write('var = 1')
        self.mod2.write('import mod3\n\nprint(mod3.var)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod2)
        self.assertEqual(
            'import mod3\n\nprint(mod3.var)\n',
             self.import_tools.relatives_to_absolutes(
                          pymod, lambda stmt: False))
        self.assertEqual(
            'import pkg2.mod3\n\nprint(pkg2.mod3.var)\n',
             self.import_tools.relatives_to_absolutes(
                          pymod, self._line_filter(1)))

    def test_filtered_froms_to_normals(self):
        self.pkg1.get_child('__init__.py').write('var1 = 1\n')
        self.pkg2.get_child('__init__.py').write('var2 = 1\n')
        self.mod.write('from pkg1 import var1\nfrom pkg2 import var2\n\n'
                       'print(var1, var2)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'from pkg1 import var1\nfrom pkg2 import var2\n\nprint(var1, var2)\n',
             self.import_tools.expand_stars(pymod, lambda stmt: False))
        self.assertEqual(
            'from pkg1 import var1\nimport pkg2\n\nprint(var1, pkg2.var2)\n',
             self.import_tools.froms_to_imports(pymod, self._line_filter(2)))

    def test_filtered_froms_to_normals2(self):
        self.pkg1.get_child('__init__.py').write('var1 = 1\n')
        self.pkg2.get_child('__init__.py').write('var2 = 1\n')
        self.mod.write('from pkg1 import *\nfrom pkg2 import *\n\n'
                       'print(var1, var2)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'from pkg1 import *\nimport pkg2\n\nprint(var1, pkg2.var2)\n',
             self.import_tools.froms_to_imports(pymod, self._line_filter(2)))

    def test_filtered_handle_long_imports(self):
        self.mod.write('import p1.p2.p3.m1\nimport pkg1.mod1\n\n\n'
                       'm = p1.p2.p3.m1, pkg1.mod1\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'import p1.p2.p3.m1\nfrom pkg1 import mod1\n\n\n'
            'm = p1.p2.p3.m1, mod1\n',
            self.import_tools.handle_long_imports(
                          pymod, maxlength=5,
                          import_filter=self._line_filter(2)))

    def test_filtering_and_import_actions_with_more_than_one_phase(self):
        self.pkg1.get_child('__init__.py').write('var1 = 1\n')
        self.pkg2.get_child('__init__.py').write('var2 = 1\n')
        self.mod.write('from pkg1 import *\nfrom pkg2 import *\n\n'
                       'print(var2)\n')
        pymod = self.pycore.resource_to_pyobject(self.mod)
        self.assertEqual(
            'from pkg2 import *\n\nprint(var2)\n',
             self.import_tools.expand_stars(pymod, self._line_filter(1)))

    def test_non_existent_module_and_used_imports(self):
        self.mod.write(
            'from does_not_exist import func\n\nfunc()\n')
        pymod = self.pycore.get_module('mod')

        module_with_imports = self.import_tools.module_imports(pymod)
        imports = module_with_imports.get_used_imports(pymod)
        self.assertEqual(1, len(imports))


class AddImportTest(unittest.TestCase):

    def setUp(self):
        super(AddImportTest, self).setUp()
        self.project = testutils.sample_project()
        self.pycore = self.project.pycore

        self.mod1 = testutils.create_module(self.project, 'mod1')
        self.mod2 = testutils.create_module(self.project, 'mod2')
        self.pkg = testutils.create_package(self.project, 'pkg')
        self.mod3 = testutils.create_module(self.project, 'mod3', self.pkg)

    def tearDown(self):
        testutils.remove_project(self.project)
        super(AddImportTest, self).tearDown()

    def test_normal_imports(self):
        self.mod2.write('myvar = None\n')
        self.mod1.write('\n')
        pymod = self.pycore.get_module('mod1')
        result, name = add_import(self.pycore, pymod, 'mod2', 'myvar')
        self.assertEqual('import mod2\n', result)
        self.assertEqual('mod2.myvar', name)

    def test_not_reimporting_a_name(self):
        self.mod2.write('myvar = None\n')
        self.mod1.write('from mod2 import myvar\n')
        pymod = self.pycore.get_module('mod1')
        result, name = add_import(self.pycore, pymod, 'mod2', 'myvar')
        self.assertEqual('from mod2 import myvar\n', result)
        self.assertEqual('myvar', name)

    def test_adding_import_when_siblings_are_imported(self):
        self.mod2.write('var1 = None\nvar2 = None\n')
        self.mod1.write('from mod2 import var1\n')
        pymod = self.pycore.get_module('mod1')
        result, name = add_import(self.pycore, pymod, 'mod2', 'var2')
        self.assertEqual('from mod2 import var1, var2\n', result)
        self.assertEqual('var2', name)

    def test_adding_import_when_the_package_is_imported(self):
        self.pkg.get_child('__init__.py').write('var1 = None\n')
        self.mod3.write('var2 = None\n')
        self.mod1.write('from pkg import var1\n')
        pymod = self.pycore.get_module('mod1')
        result, name = add_import(self.pycore, pymod, 'pkg.mod3', 'var2')
        self.assertEqual('from pkg import var1, mod3\n', result)
        self.assertEqual('mod3.var2', name)

    def test_adding_import_for_modules_instead_of_names(self):
        self.pkg.get_child('__init__.py').write('var1 = None\n')
        self.mod3.write('\n')
        self.mod1.write('from pkg import var1\n')
        pymod = self.pycore.get_module('mod1')
        result, name = add_import(self.pycore, pymod, 'pkg.mod3', None)
        self.assertEqual('from pkg import var1, mod3\n', result)
        self.assertEqual('mod3', name)

    def test_adding_import_for_modules_with_normal_duplicate_imports(self):
        self.pkg.get_child('__init__.py').write('var1 = None\n')
        self.mod3.write('\n')
        self.mod1.write('import pkg.mod3\n')
        pymod = self.pycore.get_module('mod1')
        result, name = add_import(self.pycore, pymod, 'pkg.mod3', None)
        self.assertEqual('import pkg.mod3\n', result)
        self.assertEqual('pkg.mod3', name)


def suite():
    result = unittest.TestSuite()
    result.addTests(unittest.makeSuite(ImportUtilsTest))
    result.addTests(unittest.makeSuite(AddImportTest))
    return result

if __name__ == '__main__':
    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.