mirror of
https://github.com/bndr/pipreqs.git
synced 2025-06-06 03:25:21 +00:00
Include packages imported with "as" and add tests.
This commit is contained in:
parent
f219e3b105
commit
1dee19e3f5
@ -33,10 +33,8 @@ def get_all_imports(start_path):
|
|||||||
logging.debug('Traversing tree, start: %s', start_path)
|
logging.debug('Traversing tree, start: %s', start_path)
|
||||||
for root, dirs, files in os.walk(start_path):
|
for root, dirs, files in os.walk(start_path):
|
||||||
packages.append(os.path.basename(root))
|
packages.append(os.path.basename(root))
|
||||||
|
files = filter(lambda fn:os.path.splitext(fn)[1] == ".py", files)
|
||||||
for file_name in files:
|
for file_name in files:
|
||||||
if file_name[-3:] != ".py":
|
|
||||||
continue
|
|
||||||
|
|
||||||
with open(os.path.join(root, file_name), "r") as file_object:
|
with open(os.path.join(root, file_name), "r") as file_object:
|
||||||
for line in file_object:
|
for line in file_object:
|
||||||
if line[0] == "#":
|
if line[0] == "#":
|
||||||
@ -51,6 +49,9 @@ def get_all_imports(start_path):
|
|||||||
if "," in item:
|
if "," in item:
|
||||||
for match in item.split(","):
|
for match in item.split(","):
|
||||||
imports.append(match.strip())
|
imports.append(match.strip())
|
||||||
|
elif " as " in item:
|
||||||
|
to_append = item.split(" as ")[0]
|
||||||
|
imports.append(to_append.strip())
|
||||||
else:
|
else:
|
||||||
to_append = item if "." not in item else item.split(".")[0]
|
to_append = item if "." not in item else item.split(".")[0]
|
||||||
imports.append(to_append.strip())
|
imports.append(to_append.strip())
|
||||||
@ -109,4 +110,4 @@ def main(): # pragma: no cover
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main() # pragma: no cover
|
main() # pragma: no cover
|
||||||
|
0
tests/_data/models.py
Normal file
0
tests/_data/models.py
Normal file
@ -10,11 +10,10 @@ import sys
|
|||||||
import signal
|
import signal
|
||||||
import requests
|
import requests
|
||||||
import nonexistendmodule
|
import nonexistendmodule
|
||||||
# Ignore this Line
|
# import django
|
||||||
import flask.ext.somext
|
import flask.ext.somext
|
||||||
from sqlalchemy import model
|
from sqlalchemy import model
|
||||||
|
import ujson as json
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
pass
|
pass
|
||||||
|
@ -15,25 +15,26 @@ from pipreqs import pipreqs
|
|||||||
|
|
||||||
class TestPipreqs(unittest.TestCase):
|
class TestPipreqs(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.modules = ['flask', 'requests', 'sqlalchemy', 'docopt', 'nonexistendmodule']
|
self.modules = ['flask', 'requests', 'sqlalchemy', 'docopt', 'ujson', 'nonexistendmodule']
|
||||||
self.project = os.path.join(os.path.dirname(__file__), "_data")
|
self.project = os.path.join(os.path.dirname(__file__), "_data")
|
||||||
self.requirements_path = os.path.join(self.project, "requirements.txt")
|
self.requirements_path = os.path.join(self.project, "requirements.txt")
|
||||||
|
|
||||||
def test_get_all_imports(self):
|
def test_get_all_imports(self):
|
||||||
imports = pipreqs.get_all_imports(self.project)
|
imports = pipreqs.get_all_imports(self.project)
|
||||||
self.assertEqual(len(imports), 5, "Incorrect Imports array length")
|
self.assertEqual(len(imports), 6, "Incorrect Imports array length")
|
||||||
for item in imports:
|
for item in imports:
|
||||||
self.assertTrue(item in self.modules, "Import is missing")
|
self.assertTrue(item in self.modules, "Import is missing")
|
||||||
self.assertFalse("time" in imports)
|
self.assertFalse("time" in imports)
|
||||||
self.assertFalse("logging" in imports)
|
self.assertFalse("logging" in imports)
|
||||||
self.assertFalse("curses" in imports)
|
self.assertFalse("curses" in imports)
|
||||||
self.assertFalse("__future__" in imports)
|
self.assertFalse("__future__" in imports)
|
||||||
|
self.assertFalse("django" in imports)
|
||||||
|
|
||||||
def test_get_imports_info(self):
|
def test_get_imports_info(self):
|
||||||
imports = pipreqs.get_all_imports(self.project)
|
imports = pipreqs.get_all_imports(self.project)
|
||||||
with_info = pipreqs.get_imports_info(imports)
|
with_info = pipreqs.get_imports_info(imports)
|
||||||
# Should contain only 4 Elements without the "nonexistendmodule"
|
# Should contain only 5 Elements without the "nonexistendmodule"
|
||||||
self.assertEqual(len(with_info), 4, "Length of imports array with info is wrong")
|
self.assertEqual(len(with_info), 5, "Length of imports array with info is wrong")
|
||||||
for item in with_info:
|
for item in with_info:
|
||||||
self.assertTrue(item['name'] in self.modules, "Import item appears to be missing")
|
self.assertTrue(item['name'] in self.modules, "Import item appears to be missing")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user