File tree 2 files changed +28
-1
lines changed
2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change 5
5
6
6
import pytest
7
7
8
- from isort import api
8
+ from isort import ImportKey , api
9
9
from isort .settings import Config
10
10
11
11
imperfect_content = "import b\n import a\n "
@@ -86,3 +86,18 @@ def test_sort_code_string_mixed_newlines():
86
86
def test_find_imports_in_file (imperfect ):
87
87
found_imports = list (api .find_imports_in_file (imperfect ))
88
88
assert "b" in [found_import .module for found_import in found_imports ]
89
+
90
+
91
+ def test_find_imports_in_code ():
92
+ code = """
93
+ from x.y import z as a
94
+ from x.y import z as a
95
+ from x.y import z
96
+ import x.y
97
+ import x
98
+ """
99
+ assert len (list (api .find_imports_in_code (code ))) == 5
100
+ assert len (list (api .find_imports_in_code (code , unique = True ))) == 4
101
+ assert len (list (api .find_imports_in_code (code , unique = ImportKey .ATTRIBUTE ))) == 3
102
+ assert len (list (api .find_imports_in_code (code , unique = ImportKey .MODULE ))) == 2
103
+ assert len (list (api .find_imports_in_code (code , unique = ImportKey .PACKAGE ))) == 1
Original file line number Diff line number Diff line change @@ -1022,3 +1022,15 @@ def test_identify_imports_main(tmpdir, capsys):
1022
1022
assert out .replace ("\r \n " , "\n " ) == file_imports_with_dupes .replace (str (some_file ), "" )
1023
1023
1024
1024
main .identify_imports_main ([str (tmpdir )])
1025
+
1026
+ main .identify_imports_main (["-" , "--packages" ], stdin = as_stream (file_content ))
1027
+ out , error = capsys .readouterr ()
1028
+ len (out .split ("\n " )) == 2
1029
+
1030
+ main .identify_imports_main (["-" , "--modules" ], stdin = as_stream (file_content ))
1031
+ out , error = capsys .readouterr ()
1032
+ len (out .split ("\n " )) == 2
1033
+
1034
+ main .identify_imports_main (["-" , "--attributes" ], stdin = as_stream (file_content ))
1035
+ out , error = capsys .readouterr ()
1036
+ len (out .split ("\n " )) == 2
You can’t perform that action at this time.
0 commit comments