PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/test_pipreqs.py

https://gitlab.com/Kravcenko/pipreqs
Python | 188 lines | 158 code | 11 blank | 19 comment | 2 complexity | 0197fb29b0d0fb530bfa6c4feaa239b5 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. test_pipreqs
  5. ----------------------------------
  6. Tests for `pipreqs` module.
  7. """
  8. import unittest
  9. import os
  10. import requests
  11. from pipreqs import pipreqs
  12. class TestPipreqs(unittest.TestCase):
  13. def setUp(self):
  14. self.modules = ['flask', 'requests', 'sqlalchemy',
  15. 'docopt', 'boto', 'ipython', 'pyflakes', 'nose',
  16. 'peewee', 'ujson', 'nonexistendmodule', 'bs4', 'after_method_is_valid_even_if_not_pep8' ]
  17. self.modules2 = ['beautifulsoup4']
  18. self.local = ["docopt", "requests", "nose", 'pyflakes']
  19. self.project = os.path.join(os.path.dirname(__file__), "_data")
  20. self.project_invalid = os.path.join(os.path.dirname(__file__), "_invalid_data")
  21. self.project_with_ignore_directory = os.path.join(os.path.dirname(__file__), "_data_ignore")
  22. self.project_with_duplicated_deps = os.path.join(os.path.dirname(__file__), "_data_duplicated_deps")
  23. self.requirements_path = os.path.join(self.project, "requirements.txt")
  24. self.alt_requirement_path = os.path.join(
  25. self.project, "requirements2.txt")
  26. def test_get_all_imports(self):
  27. imports = pipreqs.get_all_imports(self.project)
  28. self.assertEqual(len(imports), 13)
  29. for item in imports:
  30. self.assertTrue(
  31. item.lower() in self.modules, "Import is missing: " + item)
  32. self.assertFalse("time" in imports)
  33. self.assertFalse("logging" in imports)
  34. self.assertFalse("curses" in imports)
  35. self.assertFalse("__future__" in imports)
  36. self.assertFalse("django" in imports)
  37. self.assertFalse("models" in imports)
  38. def test_deduplicate_dependencies(self):
  39. imports = pipreqs.get_all_imports(self.project_with_duplicated_deps)
  40. pkgs = pipreqs.get_pkg_names(imports)
  41. self.assertEqual(len(pkgs), 1)
  42. self.assertTrue("pymongo" in pkgs)
  43. def test_invalid_python(self):
  44. """
  45. Test that invalid python files cannot be imported.
  46. """
  47. self.assertRaises(SyntaxError, pipreqs.get_all_imports, self.project_invalid)
  48. def test_get_imports_info(self):
  49. """
  50. Test to see that the right number of packages were found on PyPI
  51. """
  52. imports = pipreqs.get_all_imports(self.project)
  53. with_info = pipreqs.get_imports_info(imports)
  54. # Should contain 10 items without the "nonexistendmodule" and "after_method_is_valid_even_if_not_pep8"
  55. self.assertEqual(len(with_info), 10)
  56. for item in with_info:
  57. self.assertTrue(
  58. item['name'].lower() in self.modules,
  59. "Import item appears to be missing " + item['name'])
  60. def test_get_use_local_only(self):
  61. """
  62. Test without checking PyPI, check to see if names of local imports matches what we expect
  63. - Note even though pyflakes isn't in requirements.txt,
  64. It's added to locals since it is a development dependency for testing
  65. """
  66. # should find only docopt and requests
  67. imports_with_info = pipreqs.get_import_local(self.modules)
  68. for item in imports_with_info:
  69. self.assertTrue(item['name'].lower() in self.local)
  70. def test_init(self):
  71. """
  72. Test that all modules we will test upon, are in requirements file
  73. """
  74. pipreqs.init({'<path>': self.project, '--savepath': None,
  75. '--use-local': None, '--force': True, '--proxy':None, '--pypi-server':None})
  76. assert os.path.exists(self.requirements_path) == 1
  77. with open(self.requirements_path, "r") as f:
  78. data = f.read().lower()
  79. for item in self.modules[:-3]:
  80. self.assertTrue(item.lower() in data)
  81. def test_init_local_only(self):
  82. """
  83. Test that items listed in requirements.text are the same as locals expected
  84. """
  85. pipreqs.init({'<path>': self.project, '--savepath': None,
  86. '--use-local': True, '--force': True, '--proxy':None, '--pypi-server':None})
  87. assert os.path.exists(self.requirements_path) == 1
  88. with open(self.requirements_path, "r") as f:
  89. data = f.readlines()
  90. for item in data:
  91. item = item.strip().split(" == ")
  92. self.assertTrue(item[0].lower() in self.local)
  93. def test_init_savepath(self):
  94. """
  95. Test that we can save requiremnts.tt correctly to a different path
  96. """
  97. pipreqs.init({'<path>': self.project, '--savepath':
  98. self.alt_requirement_path, '--use-local': None, '--proxy':None, '--pypi-server':None})
  99. assert os.path.exists(self.alt_requirement_path) == 1
  100. with open(self.alt_requirement_path, "r") as f:
  101. data = f.read().lower()
  102. for item in self.modules[:-3]:
  103. self.assertTrue(item.lower() in data)
  104. for item in self.modules2:
  105. self.assertTrue(item.lower() in data)
  106. def test_init_overwrite(self):
  107. """
  108. Test that if requiremnts.txt exists, it will not automatically be overwritten
  109. """
  110. with open(self.requirements_path, "w") as f:
  111. f.write("should_not_be_overwritten")
  112. pipreqs.init({'<path>': self.project, '--savepath': None,
  113. '--use-local': None, '--force': None, '--proxy':None, '--pypi-server':None})
  114. assert os.path.exists(self.requirements_path) == 1
  115. with open(self.requirements_path, "r") as f:
  116. data = f.read().lower()
  117. self.assertEqual(data, "should_not_be_overwritten")
  118. def test_get_import_name_without_alias(self):
  119. """
  120. Test that function get_name_without_alias() will work on a string.
  121. - Note: This isn't truly needed when pipreqs is walking the AST to find imports
  122. """
  123. import_name_with_alias = "requests as R"
  124. expected_import_name_without_alias = "requests"
  125. import_name_without_aliases = pipreqs.get_name_without_alias(
  126. import_name_with_alias)
  127. self.assertEqual(
  128. import_name_without_aliases, expected_import_name_without_alias)
  129. def test_custom_pypi_server(self):
  130. """
  131. Test that trying to get a custom pypi sever fails correctly
  132. """
  133. self.assertRaises(requests.exceptions.MissingSchema, pipreqs.init, {'<path>': self.project, '--savepath': None,
  134. '--use-local': None, '--force': True, '--proxy': None, '--pypi-server': 'nonexistent'})
  135. def test_ignored_directory(self):
  136. """
  137. Test --ignore parameter
  138. """
  139. pipreqs.init(
  140. {'<path>': self.project_with_ignore_directory, '--savepath': None,
  141. '--use-local': None, '--force': True,
  142. '--proxy':None,
  143. '--pypi-server':None,
  144. '--ignore':'.ignored_dir,.ignore_second'
  145. }
  146. )
  147. with open(os.path.join(self.project_with_ignore_directory, "requirements.txt"), "r") as f:
  148. data = f.read().lower()
  149. for item in ['click', 'getpass']:
  150. self.assertFalse(item.lower() in data)
  151. def tearDown(self):
  152. """
  153. Remove requiremnts.txt files that were written
  154. """
  155. try:
  156. os.remove(self.requirements_path)
  157. except OSError:
  158. pass
  159. try:
  160. os.remove(self.alt_requirement_path)
  161. except OSError:
  162. pass
  163. if __name__ == '__main__':
  164. unittest.main()