test_i18n_subsites.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. '''Unit tests for the i18n_subsites plugin'''
  2. import os
  3. import locale
  4. import unittest
  5. import subprocess
  6. from tempfile import mkdtemp
  7. from shutil import rmtree
  8. from . import i18n_subsites as i18ns
  9. from pelican import Pelican
  10. from pelican.tests.support import get_settings
  11. from pelican.settings import read_settings
  12. class TestTemporaryLocale(unittest.TestCase):
  13. '''Test the temporary locale context manager'''
  14. def test_locale_restored(self):
  15. '''Test that the locale is restored after exiting context'''
  16. orig_locale = locale.setlocale(locale.LC_ALL)
  17. with i18ns.temporary_locale():
  18. locale.setlocale(locale.LC_ALL, 'C')
  19. self.assertEqual(locale.setlocale(locale.LC_ALL), 'C')
  20. self.assertEqual(locale.setlocale(locale.LC_ALL), orig_locale)
  21. def test_temp_locale_set(self):
  22. '''Test that the temporary locale is set'''
  23. with i18ns.temporary_locale('C'):
  24. self.assertEqual(locale.setlocale(locale.LC_ALL), 'C')
  25. class TestSettingsManipulation(unittest.TestCase):
  26. '''Test operations on settings dict'''
  27. def setUp(self):
  28. '''Prepare default settings'''
  29. self.settings = get_settings()
  30. def test_get_pelican_cls_class(self):
  31. '''Test that we get class given as an object'''
  32. self.settings['PELICAN_CLASS'] = object
  33. cls = i18ns.get_pelican_cls(self.settings)
  34. self.assertIs(cls, object)
  35. def test_get_pelican_cls_str(self):
  36. '''Test that we get correct class given by string'''
  37. cls = i18ns.get_pelican_cls(self.settings)
  38. self.assertIs(cls, Pelican)
  39. class TestSitesRelpath(unittest.TestCase):
  40. '''Test relative path between sites generation'''
  41. def setUp(self):
  42. '''Generate some sample siteurls'''
  43. self.siteurl = 'http://example.com'
  44. i18ns._SITE_DB['en'] = self.siteurl
  45. i18ns._SITE_DB['de'] = self.siteurl + '/de'
  46. def tearDown(self):
  47. '''Remove sites from db'''
  48. i18ns._SITE_DB.clear()
  49. def test_get_site_path(self):
  50. '''Test getting the path within a site'''
  51. self.assertEqual(i18ns.get_site_path(self.siteurl), '/')
  52. self.assertEqual(i18ns.get_site_path(self.siteurl + '/de'), '/de')
  53. def test_relpath_to_site(self):
  54. '''Test getting relative paths between sites'''
  55. self.assertEqual(i18ns.relpath_to_site('en', 'de'), 'de')
  56. self.assertEqual(i18ns.relpath_to_site('de', 'en'), '..')
  57. class TestRegistration(unittest.TestCase):
  58. '''Test plugin registration'''
  59. def test_return_on_missing_signal(self):
  60. '''Test return on missing required signal'''
  61. i18ns._SIGNAL_HANDLERS_DB['tmp_sig'] = None
  62. i18ns.register()
  63. self.assertNotIn(id(i18ns.save_generator),
  64. i18ns.signals.generator_init.receivers)
  65. def test_registration(self):
  66. '''Test registration of all signal handlers'''
  67. i18ns.register()
  68. for sig_name, handler in i18ns._SIGNAL_HANDLERS_DB.items():
  69. sig = getattr(i18ns.signals, sig_name)
  70. self.assertIn(id(handler), sig.receivers)
  71. # clean up
  72. sig.disconnect(handler)
  73. class TestFullRun(unittest.TestCase):
  74. '''Test running Pelican with the Plugin'''
  75. def setUp(self):
  76. '''Create temporary output and cache folders'''
  77. self.temp_path = mkdtemp(prefix='pelicantests.')
  78. self.temp_cache = mkdtemp(prefix='pelican_cache.')
  79. def tearDown(self):
  80. '''Remove output and cache folders'''
  81. rmtree(self.temp_path)
  82. rmtree(self.temp_cache)
  83. def test_sites_generation(self):
  84. '''Test generation of sites with the plugin
  85. Compare with recorded output via ``git diff``.
  86. To generate output for comparison run the command
  87. ``pelican -o test_data/output -s test_data/pelicanconf.py \
  88. test_data/content``
  89. Remember to remove the output/ folder before that.
  90. '''
  91. base_path = os.path.dirname(os.path.abspath(__file__))
  92. base_path = os.path.join(base_path, 'test_data')
  93. content_path = os.path.join(base_path, 'content')
  94. output_path = os.path.join(base_path, 'output')
  95. settings_path = os.path.join(base_path, 'pelicanconf.py')
  96. settings = read_settings(path=settings_path, override={
  97. 'PATH': content_path,
  98. 'OUTPUT_PATH': self.temp_path,
  99. 'CACHE_PATH': self.temp_cache,
  100. 'PLUGINS': [i18ns],
  101. }
  102. )
  103. pelican = Pelican(settings)
  104. pelican.run()
  105. # compare output
  106. out, err = subprocess.Popen(
  107. ['git', 'diff', '--no-ext-diff', '--exit-code', '-w', output_path,
  108. self.temp_path], env={'PAGER': ''},
  109. stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
  110. self.assertFalse(out, 'non-empty `diff` stdout:\n{}'.format(out))
  111. self.assertFalse(err, 'non-empty `diff` stderr:\n{}'.format(out))