A fork of hyde, the static site generation. Some patches will be pushed upstream.
 
 
 

100 lines
3.6 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Use nose
  4. `$ pip install nose`
  5. `$ nosetests`
  6. """
  7. from hyde.fs import File, Folder
  8. from hyde.model import Expando
  9. from hyde.generator import Generator
  10. from hyde.site import Site
  11. UGLIFY_SOURCE = File(__file__).parent.child_folder('uglify')
  12. TEST_SITE = File(__file__).parent.parent.child_folder('_test')
  13. class TestLess(object):
  14. def setUp(self):
  15. TEST_SITE.make()
  16. TEST_SITE.parent.child_folder(
  17. 'sites/test_jinja').copy_contents_to(TEST_SITE)
  18. JS = TEST_SITE.child_folder('content/media/js')
  19. JS.make()
  20. UGLIFY_SOURCE.copy_contents_to(JS)
  21. def tearDown(self):
  22. TEST_SITE.delete()
  23. def test_can_uglify(self):
  24. s = Site(TEST_SITE)
  25. s.config.plugins = ['hyde.ext.plugins.uglify.UglifyPlugin']
  26. s.config.mode = "production"
  27. paths = ['/usr/local/share/npm/bin/uglifyjs', '~/local/bin/uglifyjs',
  28. '/usr/bin/uglifyjs', '~/bin/uglifyjs']
  29. uglify = [path for path in paths if File(path).exists]
  30. if not uglify:
  31. assert False, "Cannot find the uglify executable"
  32. uglify = uglify[0]
  33. s.config.uglify = Expando(dict(app=uglify))
  34. source = TEST_SITE.child('content/media/js/jquery.js')
  35. target = File(Folder(s.config.deploy_root_path).child('media/js/jquery.js'))
  36. gen = Generator(s)
  37. gen.generate_resource_at_path(source)
  38. assert target.exists
  39. expected = File(UGLIFY_SOURCE.child('expected-jquery.js'))
  40. # TODO: Very fragile. Better comparison needed.
  41. assert target.read_all() == expected.read_all()
  42. def test_uglify_with_extra_options(self):
  43. s = Site(TEST_SITE)
  44. s.config.plugins = ['hyde.ext.plugins.uglify.UglifyPlugin']
  45. s.config.mode = "production"
  46. paths = ['/usr/local/share/npm/bin/uglifyjs', '~/local/bin/uglifyjs',
  47. '/usr/bin/uglifyjs', '~/bin/uglifyjs']
  48. uglify = [path for path in paths if File(path).exists]
  49. if not uglify:
  50. assert False, "Cannot find the uglify executable"
  51. uglify = uglify[0]
  52. s.config.uglify = Expando(dict(app=uglify, args={"nc":""}))
  53. source = TEST_SITE.child('content/media/js/jquery.js')
  54. target = File(Folder(s.config.deploy_root_path).child('media/js/jquery.js'))
  55. gen = Generator(s)
  56. gen.generate_resource_at_path(source)
  57. assert target.exists
  58. expected = File(UGLIFY_SOURCE.child('expected-jquery-nc.js'))
  59. # TODO: Very fragile. Better comparison needed.
  60. text = target.read_all()
  61. assert text.startswith("(function(")
  62. def test_no_uglify_in_dev_mode(self):
  63. s = Site(TEST_SITE)
  64. s.config.plugins = ['hyde.ext.plugins.uglify.UglifyPlugin']
  65. s.config.mode = "dev"
  66. paths = ['/usr/local/share/npm/bin/uglifyjs', '~/local/bin/uglifyjs',
  67. '/usr/bin/uglifyjs', '~/bin/uglifyjs']
  68. uglify = [path for path in paths if File(path).exists]
  69. if not uglify:
  70. assert False, "Cannot find the uglify executable"
  71. uglify = uglify[0]
  72. s.config.uglify = Expando(dict(app=path))
  73. source = TEST_SITE.child('content/media/js/jquery.js')
  74. target = File(Folder(s.config.deploy_root_path).child('media/js/jquery.js'))
  75. gen = Generator(s)
  76. gen.generate_resource_at_path(source)
  77. assert target.exists
  78. expected = File(UGLIFY_SOURCE.child('jquery.js'))
  79. # TODO: Very fragile. Better comparison needed.
  80. text = target.read_all()
  81. expected = expected.read_all()
  82. assert text == expected