test_main.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import unittest
  2. import makesite
  3. import os
  4. import shutil
  5. import json
  6. from test import path
  7. class MainTest(unittest.TestCase):
  8. def setUp(self):
  9. path.move('_site', '_site.backup')
  10. path.move('params.json', 'params.json.backup')
  11. def tearDown(self):
  12. path.move('_site.backup', '_site')
  13. path.move('params.json.backup', 'params')
  14. def test_site_missing(self):
  15. makesite.main()
  16. def test_site_exists(self):
  17. os.mkdir('_site')
  18. with open('_site/foo.txt', 'w') as f:
  19. f.write('foo')
  20. self.assertTrue(os.path.isfile('_site/foo.txt'))
  21. makesite.main()
  22. self.assertFalse(os.path.isfile('_site/foo.txt'))
  23. def test_default_params(self):
  24. makesite.main()
  25. with open('_site/blog/proin-quam/index.html') as f:
  26. s1 = f.read()
  27. with open('_site/blog/rss.xml') as f:
  28. s2 = f.read()
  29. shutil.rmtree('_site')
  30. self.assertIn('<a href="/">Home</a>', s1)
  31. self.assertIn('<title>Proin Quam - Lorem Ipsum</title>', s1)
  32. self.assertIn('Published on 2018-01-01 by <b>Admin</b>', s1)
  33. self.assertIn('<link>http://localhost:8000/</link>', s2)
  34. self.assertIn('<link>http://localhost:8000/blog/proin-quam/</link>', s2)
  35. def test_json_params(self):
  36. params = {
  37. 'base_path': '/base',
  38. 'subtitle': 'Foo',
  39. 'author': 'Bar',
  40. 'site_url': 'http://localhost/base'
  41. }
  42. with open('params.json', 'w') as f:
  43. json.dump(params, f)
  44. makesite.main()
  45. with open('_site/blog/proin-quam/index.html') as f:
  46. s1 = f.read()
  47. with open('_site/blog/rss.xml') as f:
  48. s2 = f.read()
  49. shutil.rmtree('_site')
  50. self.assertIn('<a href="/base/">Home</a>', s1)
  51. self.assertIn('<title>Proin Quam - Foo</title>', s1)
  52. self.assertIn('Published on 2018-01-01 by <b>Bar</b>', s1)
  53. self.assertIn('<link>http://localhost/base/</link>', s2)
  54. self.assertIn('<link>http://localhost/base/blog/proin-quam/</link>', s2)