/tests/test_index.py

http://github.com/SurveyMonkey/CheesePrism · Python · 226 lines · 161 code · 37 blank · 28 comment · 6 complexity · 93c82889347c11f59f4510049d268767 MD5 · raw file

  1. from cheeseprism.utils import resource_spec
  2. from itertools import count
  3. from mock import Mock
  4. from mock import patch
  5. from nose.tools import raises
  6. from path import path
  7. from pprint import pformat as pprint
  8. import logging
  9. import textwrap
  10. import unittest
  11. logger = logging.getLogger(__name__)
  12. here = path(__file__).parent
  13. def test_data_from_path():
  14. from cheeseprism import index
  15. datafile = here / 'index.json'
  16. assert index.IndexManager.data_from_path(datafile) == {}
  17. datafile.write_text("{}")
  18. assert index.IndexManager.data_from_path(datafile) == {}
  19. class IndexTestCase(unittest.TestCase):
  20. counter = count()
  21. index_parent = "egg:CheesePrism#tests/test-indexes"
  22. tdir = path(resource_spec('egg:CheesePrism#tests'))
  23. dummy = here / "dummypackage/dist/dummypackage-0.0dev.tar.gz"
  24. @classmethod
  25. def get_base(cls):
  26. return path(resource_spec(cls.index_parent))
  27. @property
  28. def base(self): return self.get_base()
  29. def make_one(self, index_name='test-index'):
  30. from cheeseprism import index
  31. self.count = next(self.counter)
  32. index_path = self.base / ("%s-%s" %(self.count, index_name))
  33. return index.IndexManager(index_path)
  34. def setUp(self):
  35. #@@ factor out im creation
  36. self.im = self.make_one()
  37. self.dummy.copy(self.im.path)
  38. self.dummypath = self.im.path / self.dummy.name
  39. def test_register_archive(self):
  40. pkgdata, md5 = self.im.register_archive(self.dummypath)
  41. assert md5 == '3ac58d89cb7f7b718bc6d0beae85c282'
  42. assert pkgdata
  43. idxjson = self.im.data_from_path(self.im.datafile_path)
  44. assert md5 in idxjson
  45. assert idxjson[md5] == pkgdata
  46. def test_write_datafile(self):
  47. """
  48. create and write archive data to index.json
  49. """
  50. data = self.im.write_datafile(hello='computer')
  51. assert 'hello' in data
  52. assert self.im.datafile_path.exists()
  53. assert 'hello' in self.im.data_from_path(self.im.datafile_path)
  54. def test_write_datafile_w_existing_datafile(self):
  55. """
  56. write data to an existing datafile
  57. """
  58. data = self.im.write_datafile(hello='computer')
  59. assert self.im.datafile_path.exists()
  60. data = self.im.write_datafile(hello='operator')
  61. assert data['hello'] == 'operator'
  62. assert self.im.data_from_path(self.im.datafile_path)['hello'] == 'operator'
  63. def test_regenerate_index(self):
  64. home, leaves = self.im.regenerate_all()
  65. pth = self.im.path
  66. file_structure = [(x.parent.name, x.name) for x in pth.walk()]
  67. index_name = u'%s-test-index' %self.count
  68. expected = [(index_name, u'dummypackage'),
  69. (u'dummypackage', u'index.html'),
  70. (path(u'dummypackage'), path(u'index.json')),
  71. (index_name, u'dummypackage-0.0dev.tar.gz'),
  72. (index_name, u'index.html')]
  73. assert len(leaves) == 1
  74. assert leaves[0].exists()
  75. assert leaves[0].name == 'index.html'
  76. assert leaves[0].parent.name == 'dummypackage'
  77. assert file_structure == expected, \
  78. textwrap.dedent("""
  79. File structure does not match::
  80. expected:
  81. %s
  82. actual:
  83. %s""") %(pprint(expected), pprint(file_structure))
  84. @patch('cheeseprism.index.IndexManager.regenerate_leaf')
  85. def test_regenerate_leaf_event(self, rl):
  86. """
  87. Cover event subscriber
  88. """
  89. from cheeseprism.event import PackageAdded
  90. from cheeseprism.index import rebuild_leaf
  91. event = PackageAdded(self.im, self.tdir / path('dummypackage2/dist/dummypackage-0.1.tar.gz'))
  92. out = rebuild_leaf(event)
  93. assert out is not None
  94. assert rl.call_args == (('dummypackage',), {})
  95. def test_regenerate_leaf(self):
  96. [x for x in self.im.regenerate_all()]
  97. leafindex = self.im.path / 'dummypackage/index.html'
  98. new_arch = self.tdir / path('dummypackage2/dist/dummypackage-0.1.tar.gz')
  99. new_arch.copy(self.im.path)
  100. added = self.im.path / new_arch.name
  101. before_txt = leafindex.text()
  102. info = self.im.pkginfo_from_file(added)
  103. out = self.im.regenerate_leaf(info.name)
  104. assert before_txt != out.text()
  105. @patch('pyramid.threadlocal.get_current_registry')
  106. def test_notify_packages_added(self, getreg):
  107. from cheeseprism.index import notify_packages_added
  108. pkg = dict(name='pkg', version='0.1'); pkgs = pkg,
  109. index = Mock(name='index')
  110. reg = getreg.return_value = Mock(name='registry')
  111. out = list(notify_packages_added(index, pkgs))
  112. assert len(out) == 1
  113. assert getreg.called
  114. assert reg.notify.called
  115. (event,), _ = reg.notify.call_args
  116. assert event.im is index
  117. assert event.version == '0.1'
  118. assert event.name == 'pkg'
  119. @raises(StopIteration)
  120. def test_notify_packages_added_raises(self):
  121. from cheeseprism.index import notify_packages_added
  122. next(notify_packages_added(Mock(name='index'), []))
  123. def tearDown(self):
  124. logger.debug("teardown: %s", self.count)
  125. dirs = self.base.dirs()
  126. logger.info(pprint(dirs))
  127. logger.info(pprint([x.rmtree() for x in dirs]))
  128. class ClassOrStaticMethods(unittest.TestCase):
  129. def test_move_on_error(self):
  130. from cheeseprism.index import IndexManager
  131. exc, path = Mock(), Mock()
  132. IndexManager.move_on_error('errors', exc, path)
  133. assert path.rename.called
  134. assert path.rename.call_args[0][0] == 'errors'
  135. @patch('pkginfo.bdist.BDist', new=Mock(return_value=True))
  136. def test_pkginfo_from_file_egg(self):
  137. """
  138. .pkginfo_from_file: bdist
  139. """
  140. from cheeseprism.index import IndexManager
  141. assert IndexManager.pkginfo_from_file('blah.egg') is True
  142. @patch('pkginfo.sdist.SDist', new=Mock(return_value=True))
  143. def test_pkginfo_from_file_sdist(self):
  144. """
  145. .pkginfo_from_file: sdist
  146. """
  147. from cheeseprism.index import IndexManager
  148. for ext in ('.gz','.tgz', '.bz2', '.zip'):
  149. assert IndexManager.pkginfo_from_file('blah.%s' %ext) is True
  150. @raises(RuntimeError)
  151. def test_pkginfo_from_bad_ext(self):
  152. """
  153. .pkginfo_from_file with unrecognized extension
  154. """
  155. from cheeseprism.index import IndexManager
  156. IndexManager.pkginfo_from_file('adfasdkfha.adkfhalsdk')
  157. @raises(RuntimeError)
  158. def test_pkginfo_from_no_ext(self):
  159. """
  160. .pkginfo_from_file with no extension
  161. """
  162. from cheeseprism.index import IndexManager
  163. IndexManager.pkginfo_from_file('adfasdkfha')
  164. def test_pkginfo_from_file_exc_and_handler(self):
  165. """
  166. .pkginfo_from_file with exception and handler
  167. """
  168. from cheeseprism.index import IndexManager
  169. exc = Exception("BOOM")
  170. with patch('pkginfo.bdist.BDist', side_effect=exc):
  171. eh = Mock(name='error_handler')
  172. IndexManager.pkginfo_from_file('bad.egg', handle_error=eh)
  173. assert eh.called
  174. assert eh.call_args[0] == (exc, 'bad.egg'), eh.call_args[0]
  175. @raises(ValueError)
  176. def test_pkginfo_from_file_exc(self):
  177. """
  178. .pkginfo_from_file with exception and no handler
  179. """
  180. from cheeseprism.index import IndexManager
  181. exc = ValueError("BOOM")
  182. with patch('pkginfo.bdist.BDist', side_effect=exc):
  183. IndexManager.pkginfo_from_file('bad.egg')
  184. def test_cleanup():
  185. assert not IndexTestCase.get_base().dirs()