test_content.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import unittest
  2. import shutil
  3. import os
  4. import makesite
  5. from test import path
  6. class ContentTest(unittest.TestCase):
  7. def setUp(self):
  8. self.blog_path = path.temppath('blog')
  9. self.undated_path = os.path.join(self.blog_path, 'foo.txt')
  10. self.dated_path = os.path.join(self.blog_path, '2018-01-01-foo.txt')
  11. self.normal_post_path = os.path.join(self.blog_path, 'baz.txt')
  12. self.md_post_path = os.path.join(self.blog_path, 'qux.md')
  13. self.no_md_post_path = os.path.join(self.blog_path, 'qux.txt')
  14. os.makedirs(self.blog_path)
  15. with open(self.undated_path, 'w') as f:
  16. f.write('hello world')
  17. with open(self.dated_path, 'w') as f:
  18. f.write('hello world')
  19. with open(self.normal_post_path, 'w') as f:
  20. f.write('<!-- a: 1 -->\n<!-- b: 2 -->\nFoo')
  21. with open(self.md_post_path, 'w') as f:
  22. f.write('*Foo*')
  23. with open(self.no_md_post_path, 'w') as f:
  24. f.write('*Foo*')
  25. def tearDown(self):
  26. shutil.rmtree(self.blog_path)
  27. # Rudimentary mock because unittest.mock is unavailable in Python 2.7.
  28. def mock(self, *args):
  29. self.mock_args = args
  30. def test_content_content(self):
  31. content = makesite.read_content(self.undated_path)
  32. self.assertEqual(content['content'], 'hello world')
  33. def test_content_date(self):
  34. content = makesite.read_content(self.dated_path)
  35. self.assertEqual(content['date'], '2018-01-01')
  36. def test_content_date_missing(self):
  37. content = makesite.read_content(self.undated_path)
  38. self.assertEqual(content['date'], '1970-01-01')
  39. def test_content_slug_dated(self):
  40. content = makesite.read_content(self.dated_path)
  41. self.assertEqual(content['slug'], 'foo')
  42. def test_content_slug_undated(self):
  43. content = makesite.read_content(self.undated_path)
  44. self.assertEqual(content['slug'], 'foo')
  45. def test_content_headers(self):
  46. content = makesite.read_content(self.normal_post_path)
  47. self.assertEqual(content['a'], '1')
  48. self.assertEqual(content['b'], '2')
  49. self.assertEqual(content['content'], 'Foo')
  50. def test_markdown_rendering(self):
  51. content = makesite.read_content(self.md_post_path)
  52. self.assertEqual(content['content'], '<p><em>Foo</em></p>\n')
  53. def test_markdown_import_error(self):
  54. makesite._test = 'ImportError'
  55. original_log = makesite.log
  56. makesite.log = self.mock
  57. self.mock_args = None
  58. content = makesite.read_content(self.md_post_path)
  59. makesite._test = None
  60. makesite.log = original_log
  61. self.assertEqual(content['content'], '*Foo*')
  62. self.assertEqual(self.mock_args,
  63. ('WARNING: Cannot render Markdown in {}: {}',
  64. self.md_post_path, 'Error forced by test'))
  65. def test_no_markdown_rendering(self):
  66. content = makesite.read_content(self.no_md_post_path)
  67. self.assertEqual(content['content'], '*Foo*')
  68. def test_no_markdown_import_error(self):
  69. makesite._test = 'ImportError'
  70. original_log = makesite.log
  71. makesite.log = self.mock
  72. self.mock_args = None
  73. content = makesite.read_content(self.no_md_post_path)
  74. makesite._test = None
  75. makesite.log = original_log
  76. self.assertEqual(content['content'], '*Foo*')
  77. self.assertIsNone(self.mock_args)