test_pandoc.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """Test Pandoc module"""
  2. #-----------------------------------------------------------------------------
  3. # Copyright (C) 2014 The IPython Development Team
  4. #
  5. # Distributed under the terms of the BSD License. The full license is in
  6. # the file COPYING, distributed as part of this software.
  7. #-----------------------------------------------------------------------------
  8. #-----------------------------------------------------------------------------
  9. # Imports
  10. #-----------------------------------------------------------------------------
  11. import os
  12. import warnings
  13. from ...tests.utils import onlyif_cmds_exist
  14. from nbconvert.tests.base import TestsBase
  15. from .. import pandoc
  16. #-----------------------------------------------------------------------------
  17. # Classes and functions
  18. #-----------------------------------------------------------------------------
  19. class TestPandoc(TestsBase):
  20. """Collection of Pandoc tests"""
  21. def __init__(self, *args, **kwargs):
  22. super(TestPandoc, self).__init__(*args, **kwargs)
  23. self.original_env = os.environ.copy()
  24. def setUp(self):
  25. super(TestPandoc, self).setUp()
  26. pandoc.check_pandoc_version._cached = None
  27. @onlyif_cmds_exist('pandoc')
  28. def test_pandoc_available(self):
  29. """ Test behaviour that pandoc functions raise PandocMissing as documented """
  30. pandoc.clean_cache()
  31. os.environ["PATH"] = ""
  32. with self.assertRaises(pandoc.PandocMissing):
  33. pandoc.get_pandoc_version()
  34. with self.assertRaises(pandoc.PandocMissing):
  35. pandoc.check_pandoc_version()
  36. with self.assertRaises(pandoc.PandocMissing):
  37. pandoc.pandoc("", "markdown", "html")
  38. # original_env["PATH"] should contain pandoc
  39. os.environ["PATH"] = self.original_env["PATH"]
  40. with warnings.catch_warnings(record=True) as w:
  41. pandoc.get_pandoc_version()
  42. pandoc.check_pandoc_version()
  43. pandoc.pandoc("", "markdown", "html")
  44. self.assertEqual(w, [])
  45. @onlyif_cmds_exist('pandoc')
  46. def test_minimal_version(self):
  47. original_minversion = pandoc._minimal_version
  48. pandoc._minimal_version = "120.0"
  49. with warnings.catch_warnings(record=True) as w:
  50. # call it twice to verify the cached value is used
  51. assert not pandoc.check_pandoc_version()
  52. assert not pandoc.check_pandoc_version()
  53. # only one warning after two calls, due to cache
  54. self.assertEqual(len(w), 1)
  55. # clear cache
  56. pandoc.check_pandoc_version._cached = None
  57. pandoc._minimal_version = pandoc.get_pandoc_version()
  58. assert pandoc.check_pandoc_version()
  59. def pandoc_function_raised_missing(f, *args, **kwargs):
  60. try:
  61. f(*args, **kwargs)
  62. except pandoc.PandocMissing:
  63. return True
  64. else:
  65. return False