This commit is contained in:
AlexPHorta 2020-02-27 21:24:31 -03:00
parent 386e677d80
commit 21d3907e96

View File

@ -18,24 +18,40 @@ from pipreqs import pipreqs
class TestPipreqs(unittest.TestCase): class TestPipreqs(unittest.TestCase):
def setUp(self): def setUp(self):
self.modules = ['flask', 'requests', 'sqlalchemy', self.modules = [
'docopt', 'boto', 'ipython', 'pyflakes', 'nose', 'analytics', 'flask_seasurf', 'flask', 'requests', 'sqlalchemy', 'docopt', 'boto', 'ipython',
'peewee', 'ujson', 'nonexistendmodule', 'bs4', 'after_method_is_valid_even_if_not_pep8' ] 'pyflakes', 'nose', 'analytics', 'flask_seasurf', 'peewee',
'ujson', 'nonexistendmodule', 'bs4',
'after_method_is_valid_even_if_not_pep8'
]
self.modules2 = ['beautifulsoup4'] self.modules2 = ['beautifulsoup4']
self.local = ["docopt", "requests", "nose", 'pyflakes'] self.local = ["docopt", "requests", "nose", 'pyflakes']
self.project = os.path.join(os.path.dirname(__file__), "_data") self.project = os.path.join(os.path.dirname(__file__), "_data")
self.project_clean = os.path.join(os.path.dirname(__file__), "_data_clean") self.project_clean = os.path.join(
self.project_invalid = os.path.join(os.path.dirname(__file__), "_invalid_data") os.path.dirname(__file__),
self.project_with_ignore_directory = os.path.join(os.path.dirname(__file__), "_data_ignore") "_data_clean"
self.project_with_duplicated_deps = os.path.join(os.path.dirname(__file__), "_data_duplicated_deps") )
self.project_invalid = os.path.join(
os.path.dirname(__file__),
"_invalid_data"
)
self.project_with_ignore_directory = os.path.join(
os.path.dirname(__file__),
"_data_ignore"
)
self.project_with_duplicated_deps = os.path.join(
os.path.dirname(__file__),
"_data_duplicated_deps"
)
self.requirements_path = os.path.join(self.project, "requirements.txt") self.requirements_path = os.path.join(self.project, "requirements.txt")
self.alt_requirement_path = os.path.join( self.alt_requirement_path = os.path.join(
self.project, "requirements2.txt") self.project,
"requirements2.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)
print(imports) self.assertEqual(len(imports), 15)
self.assertEqual(len(imports), 15) # old value, 13
for item in imports: for item in imports:
self.assertTrue( self.assertTrue(
item.lower() in self.modules, "Import is missing: " + item) item.lower() in self.modules, "Import is missing: " + item)
@ -56,7 +72,8 @@ class TestPipreqs(unittest.TestCase):
""" """
Test that invalid python files cannot be imported. Test that invalid python files cannot be imported.
""" """
self.assertRaises(SyntaxError, pipreqs.get_all_imports, self.project_invalid) self.assertRaises(
SyntaxError, pipreqs.get_all_imports, self.project_invalid)
def test_get_imports_info(self): def test_get_imports_info(self):
""" """
@ -64,8 +81,9 @@ class TestPipreqs(unittest.TestCase):
""" """
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 10 items without the "nonexistendmodule" and "after_method_is_valid_even_if_not_pep8" # Should contain 10 items without the "nonexistendmodule" and
self.assertEqual(len(with_info), 13) # old value, 11 # "after_method_is_valid_even_if_not_pep8"
self.assertEqual(len(with_info), 13)
for item in with_info: for item in with_info:
self.assertTrue( self.assertTrue(
item['name'].lower() in self.modules, item['name'].lower() in self.modules,
@ -79,10 +97,12 @@ class TestPipreqs(unittest.TestCase):
def test_get_use_local_only(self): def test_get_use_local_only(self):
""" """
Test without checking PyPI, check to see if names of local imports matches what we expect Test without checking PyPI, check to see if names of local
imports matches what we expect
- Note even though pyflakes isn't in requirements.txt, - Note even though pyflakes isn't in requirements.txt,
It's added to locals since it is a development dependency for testing It's added to locals since it is a development dependency
for testing
""" """
# should find only docopt and requests # should find only docopt and requests
imports_with_info = pipreqs.get_import_local(self.modules) imports_with_info = pipreqs.get_import_local(self.modules)
@ -91,11 +111,13 @@ class TestPipreqs(unittest.TestCase):
def test_init(self): def test_init(self):
""" """
Test that all modules we will test upon, are in requirements file Test that all modules we will test upon are in requirements file
""" """
pipreqs.init({'<path>': self.project, '--savepath': None, '--print': False, pipreqs.init(
'--use-local': None, '--force': True, '--proxy':None, '--pypi-server':None, {'<path>': self.project, '--savepath': None, '--print': False,
'--diff': None, '--clean': None}) '--use-local': None, '--force': True, '--proxy': None,
'--pypi-server': None, '--diff': None, '--clean': None}
)
assert os.path.exists(self.requirements_path) == 1 assert os.path.exists(self.requirements_path) == 1
with open(self.requirements_path, "r") as f: with open(self.requirements_path, "r") as f:
data = f.read().lower() data = f.read().lower()
@ -104,11 +126,14 @@ class TestPipreqs(unittest.TestCase):
def test_init_local_only(self): def test_init_local_only(self):
""" """
Test that items listed in requirements.text are the same as locals expected Test that items listed in requirements.text are the same
as locals expected
""" """
pipreqs.init({'<path>': self.project, '--savepath': None, '--print': False, pipreqs.init(
'--use-local': True, '--force': True, '--proxy':None, '--pypi-server':None, {'<path>': self.project, '--savepath': None, '--print': False,
'--diff': None, '--clean': None}) '--use-local': True, '--force': True, '--proxy': None,
'--pypi-server': None, '--diff': None, '--clean': None}
)
assert os.path.exists(self.requirements_path) == 1 assert os.path.exists(self.requirements_path) == 1
with open(self.requirements_path, "r") as f: with open(self.requirements_path, "r") as f:
data = f.readlines() data = f.readlines()
@ -118,11 +143,14 @@ class TestPipreqs(unittest.TestCase):
def test_init_savepath(self): def test_init_savepath(self):
""" """
Test that we can save requiremnts.tt correctly to a different path Test that we can save requirements.txt correctly
to a different path
""" """
pipreqs.init({'<path>': self.project, '--savepath': pipreqs.init(
self.alt_requirement_path, '--use-local': None, '--proxy':None, '--pypi-server':None, '--print': False, {'<path>': self.project, '--savepath': self.alt_requirement_path,
"--diff": None, "--clean": None}) '--use-local': None, '--proxy': None, '--pypi-server': None,
'--print': False, "--diff": None, "--clean": None}
)
assert os.path.exists(self.alt_requirement_path) == 1 assert os.path.exists(self.alt_requirement_path) == 1
with open(self.alt_requirement_path, "r") as f: with open(self.alt_requirement_path, "r") as f:
data = f.read().lower() data = f.read().lower()
@ -133,13 +161,16 @@ class TestPipreqs(unittest.TestCase):
def test_init_overwrite(self): def test_init_overwrite(self):
""" """
Test that if requiremnts.txt exists, it will not automatically be overwritten Test that if requiremnts.txt exists, it will not be
automatically overwritten
""" """
with open(self.requirements_path, "w") as f: with open(self.requirements_path, "w") as f:
f.write("should_not_be_overwritten") f.write("should_not_be_overwritten")
pipreqs.init({'<path>': self.project, '--savepath': None, pipreqs.init(
'--use-local': None, '--force': None, '--proxy':None, '--pypi-server':None, '--print': False, {'<path>': self.project, '--savepath': None, '--use-local': None,
"--diff": None, "--clean": None}) '--force': None, '--proxy': None, '--pypi-server': None,
'--print': False, '--diff': None, '--clean': None}
)
assert os.path.exists(self.requirements_path) == 1 assert os.path.exists(self.requirements_path) == 1
with open(self.requirements_path, "r") as f: with open(self.requirements_path, "r") as f:
data = f.read().lower() data = f.read().lower()
@ -147,38 +178,48 @@ class TestPipreqs(unittest.TestCase):
def test_get_import_name_without_alias(self): def test_get_import_name_without_alias(self):
""" """
Test that function get_name_without_alias() will work on a string. Test that function get_name_without_alias()
- Note: This isn't truly needed when pipreqs is walking the AST to find imports will work on a string.
- Note: This isn't truly needed when pipreqs is walking
the AST to find imports
""" """
import_name_with_alias = "requests as R" import_name_with_alias = "requests as R"
expected_import_name_without_alias = "requests" expected_import_name_without_alias = "requests"
import_name_without_aliases = pipreqs.get_name_without_alias( import_name_without_aliases = pipreqs.get_name_without_alias(
import_name_with_alias) import_name_with_alias)
self.assertEqual( self.assertEqual(
import_name_without_aliases, expected_import_name_without_alias) import_name_without_aliases,
expected_import_name_without_alias
)
def test_custom_pypi_server(self): def test_custom_pypi_server(self):
""" """
Test that trying to get a custom pypi sever fails correctly Test that trying to get a custom pypi sever fails correctly
""" """
self.assertRaises(requests.exceptions.MissingSchema, pipreqs.init, {'<path>': self.project, '--savepath': None, '--print': False, self.assertRaises(
'--use-local': None, '--force': True, '--proxy': None, '--pypi-server': 'nonexistent'}) requests.exceptions.MissingSchema, pipreqs.init,
{'<path>': self.project, '--savepath': None, '--print': False,
'--use-local': None, '--force': True, '--proxy': None,
'--pypi-server': 'nonexistent'}
)
def test_ignored_directory(self): def test_ignored_directory(self):
""" """
Test --ignore parameter Test --ignore parameter
""" """
pipreqs.init( pipreqs.init(
{'<path>': self.project_with_ignore_directory, '--savepath': None, '--print': False, {'<path>': self.project_with_ignore_directory, '--savepath': None,
'--use-local': None, '--force': True, '--print': False, '--use-local': None, '--force': True,
'--proxy':None, '--proxy': None, '--pypi-server': None,
'--pypi-server':None, '--ignore': '.ignored_dir,.ignore_second', '--diff': None,
'--ignore':'.ignored_dir,.ignore_second', '--clean': None}
'--diff': None, )
'--clean': None with open(
} os.path.join(
) self.project_with_ignore_directory,
with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f: "requirements.txt"
), "r"
) as f:
data = f.read().lower() data = f.read().lower()
for item in ['click', 'getpass']: for item in ['click', 'getpass']:
self.assertFalse(item.lower() in data) self.assertFalse(item.lower() in data)
@ -188,16 +229,15 @@ class TestPipreqs(unittest.TestCase):
Test --no-pin parameter Test --no-pin parameter
""" """
pipreqs.init( pipreqs.init(
{'<path>': self.project_with_ignore_directory, '--savepath': None, '--print': False, {'<path>': self.project_with_ignore_directory, '--savepath': None,
'--use-local': None, '--force': True, '--print': False, '--use-local': None, '--force': True,
'--proxy': None, '--proxy': None, '--pypi-server': None, '--diff': None,
'--pypi-server': None, '--clean': None, '--no-pin': True}
'--diff': None, )
'--clean': None, with open(os.path.join(
'--no-pin': True self.project_with_ignore_directory,
} "requirements.txt"), "r"
) ) as f:
with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f:
data = f.read().lower() data = f.read().lower()
for item in ['beautifulsoup4==4.8.1', 'boto==2.49.0']: for item in ['beautifulsoup4==4.8.1', 'boto==2.49.0']:
self.assertFalse(item.lower() in data) self.assertFalse(item.lower() in data)
@ -206,38 +246,44 @@ class TestPipreqs(unittest.TestCase):
""" """
Test --clean parameter Test --clean parameter
""" """
pipreqs.init({'<path>': self.project, '--savepath': None, '--print': False, pipreqs.init(
'--use-local': None, '--force': True, '--proxy': None, {'<path>': self.project, '--savepath': None, '--print': False,
'--pypi-server': None, '--diff': None, '--clean': None}) '--use-local': None, '--force': True, '--proxy': None,
'--pypi-server': None, '--diff': None, '--clean': None}
)
assert os.path.exists(self.requirements_path) == 1 assert os.path.exists(self.requirements_path) == 1
pipreqs.init({'<path>': self.project, '--savepath': None, '--print': False, pipreqs.init(
'--use-local': None, '--force': None, '--proxy': None, {'<path>': self.project, '--savepath': None, '--print': False,
'--pypi-server': None, '--diff': None, '--use-local': None, '--force': None, '--proxy': None,
'--clean': self.requirements_path, '--no-pin': True}) '--pypi-server': None, '--diff': None,
'--clean': self.requirements_path, '--no-pin': True}
)
with open(self.requirements_path, "r") as f: with open(self.requirements_path, "r") as f:
data = f.read().lower() data = f.read().lower()
print(data) for item in self.modules[:-3]:
for item in self.modules[:-3]: # ['analytics_python', 'flask_seasurf']:
self.assertTrue(item.lower() in data) self.assertTrue(item.lower() in data)
def test_clean_with_extras(self): def test_clean_with_imports_to_clean(self):
""" """
Test --clean parameter when there are imports to clean Test --clean parameter when there are imports to clean
""" """
pipreqs.init({'<path>': self.project, '--savepath': None, '--print': False, cleaned_module = 'sqlalchemy'
'--use-local': None, '--force': True, '--proxy': None, pipreqs.init(
'--pypi-server': None, '--diff': None, '--clean': None}) {'<path>': 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 assert os.path.exists(self.requirements_path) == 1
# Continua... modules_clean = [m for m in self.modules if m != cleaned_module]
pipreqs.init({'<path>': self.project, '--savepath': None, '--print': False, pipreqs.init(
'--use-local': None, '--force': None, '--proxy': None, {'<path>': self.project_clean, '--savepath': None,
'--pypi-server': None, '--diff': None, '--print': False, '--use-local': None, '--force': None,
'--clean': self.requirements_path, '--no-pin': True}) '--proxy': None, '--pypi-server': None, '--diff': None,
'--clean': self.requirements_path, '--no-pin': True}
)
with open(self.requirements_path, "r") as f: with open(self.requirements_path, "r") as f:
data = f.read().lower() data = f.read().lower()
print(data) self.assertTrue(cleaned_module not in data)
for item in self.modules[:-3]: # ['analytics_python', 'flask_seasurf']:
self.assertTrue(item.lower() in data)
def tearDown(self): def tearDown(self):
""" """