From 2022f25ae96edc7e6a8cd71aaef2c6702fbaedf3 Mon Sep 17 00:00:00 2001 From: AlexPHorta Date: Thu, 27 Feb 2020 00:59:02 -0300 Subject: [PATCH] Working on issue #88 --- pipreqs/pipreqs.py | 5 +++++ tests/_data/test.py | 4 ++++ tests/test_pipreqs.py | 30 +++++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/pipreqs/pipreqs.py b/pipreqs/pipreqs.py index 4b817c3..e280841 100755 --- a/pipreqs/pipreqs.py +++ b/pipreqs/pipreqs.py @@ -368,6 +368,11 @@ def diff(file_, imports): def clean(file_, imports): """Remove modules that aren't imported in project from file.""" modules_not_imported = compare_modules(file_, imports) + + if len(modules_not_imported) == 0: + logging.info("Nothing to clean in " + file_) + return + re_remove = re.compile("|".join(modules_not_imported)) to_write = [] diff --git a/tests/_data/test.py b/tests/_data/test.py index cfd039c..fdb6ec3 100644 --- a/tests/_data/test.py +++ b/tests/_data/test.py @@ -31,6 +31,10 @@ from pyflakes.test.test_imports import Test as TestImports # Nose from nose.importer import Importer, add_path, remove_path # loader.py +# see issue #88 +import analytics +import flask_seasurf + import atexit from __future__ import print_function from docopt import docopt diff --git a/tests/test_pipreqs.py b/tests/test_pipreqs.py index dcd75c5..acdac0b 100755 --- a/tests/test_pipreqs.py +++ b/tests/test_pipreqs.py @@ -19,7 +19,7 @@ class TestPipreqs(unittest.TestCase): def setUp(self): self.modules = ['flask', 'requests', 'sqlalchemy', - 'docopt', 'boto', 'ipython', 'pyflakes', 'nose', + 'docopt', 'boto', 'ipython', 'pyflakes', 'nose', 'analytics_python', 'flask_seasurf', 'peewee', 'ujson', 'nonexistendmodule', 'bs4', 'after_method_is_valid_even_if_not_pep8' ] self.modules2 = ['beautifulsoup4'] self.local = ["docopt", "requests", "nose", 'pyflakes'] @@ -33,7 +33,7 @@ class TestPipreqs(unittest.TestCase): def test_get_all_imports(self): imports = pipreqs.get_all_imports(self.project) - self.assertEqual(len(imports), 13) + self.assertEqual(len(imports), 15) # old value, 13 for item in imports: self.assertTrue( item.lower() in self.modules, "Import is missing: " + item) @@ -63,7 +63,7 @@ class TestPipreqs(unittest.TestCase): imports = pipreqs.get_all_imports(self.project) with_info = pipreqs.get_imports_info(imports) # Should contain 10 items without the "nonexistendmodule" and "after_method_is_valid_even_if_not_pep8" - self.assertEqual(len(with_info), 11) + self.assertEqual(len(with_info), 13) # old value, 11 for item in with_info: self.assertTrue( item['name'].lower() in self.modules, @@ -200,6 +200,30 @@ class TestPipreqs(unittest.TestCase): for item in ['beautifulsoup4==4.8.1', 'boto==2.49.0']: self.assertFalse(item.lower() in data) + def test_clean(self): + """ + Test --clean parameter + """ + pipreqs.init({'': self.project, '--savepath': None, '--print': False, + '--use-local': None, '--force': True, '--proxy': None, + '--pypi-server': None, '--diff': None, '--clean': None}) + assert os.path.exists(self.requirements_path) == 1 + # with open(self.requirements_path, "r") as f: + # print('A') + # for l in f: print(l) + pipreqs.init({'': self.project, '--savepath': None, '--print': False, + '--use-local': None, '--force': None, '--proxy': None, + '--pypi-server': None, '--diff': None, + '--clean': self.requirements_path, '--no-pin': True}) + # with open(self.requirements_path, "r") as f: + # print('B') + # for l in f: print(l) + with open(self.requirements_path, "r") as f: + data = f.read().lower() + print(data) + for item in self.modules[:-3]: # ['analytics_python', 'flask_seasurf']: + self.assertTrue(item.lower() in data) + def tearDown(self): """ Remove requiremnts.txt files that were written