test_rmd_reader.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. '''
  2. Created on Jan 25, 2016
  3. @author: Aaron Kitzmiller <aaron_kitzmiller@harvard.edu?
  4. '''
  5. import unittest, os, sys
  6. import shutil
  7. import logging
  8. import glob
  9. from pelican import Pelican
  10. from pelican.settings import read_settings
  11. logging.basicConfig(stream=sys.stderr, level=logging.CRITICAL)
  12. class Test(unittest.TestCase):
  13. def setUp(self):
  14. try:
  15. import rpy2
  16. import rmd_reader
  17. except Exception, e:
  18. raise unittest.SkipTest("rpy not installed. Will not test rmd_reader.")
  19. self.testtitle = 'rtest'
  20. self.cwd = os.path.dirname(os.path.abspath(__file__))
  21. # Setup content dir and test rmd file
  22. self.contentdir = os.path.join(self.cwd,'test-content')
  23. try:
  24. os.mkdir(self.contentdir)
  25. except Exception:
  26. pass
  27. self.contentfile = os.path.join(self.contentdir,'test.rmd')
  28. self.testrmd = '''Title: %s
  29. Date: 2014-06-23
  30. Let's make a simple plot about cars.
  31. ```{r}
  32. cars <- c(1, 3, 6, 4, 9)
  33. plot(cars)
  34. ```
  35. ''' % self.testtitle
  36. with open(self.contentfile,'w') as f:
  37. f.write(self.testrmd)
  38. # Setup output dir
  39. self.outputdir = os.path.join(self.cwd,'test-output')
  40. try:
  41. os.mkdir(self.outputdir)
  42. except Exception:
  43. pass
  44. self.figpath = 'images'
  45. def tearDown(self):
  46. if os.path.isdir(self.outputdir):
  47. shutil.rmtree(self.outputdir)
  48. if os.path.isdir(self.contentdir):
  49. shutil.rmtree(self.contentdir)
  50. def testKnitrSettings(self):
  51. settings = read_settings(path=None, override={
  52. 'PATH': self.contentdir,
  53. 'OUTPUT_PATH': self.outputdir,
  54. 'KNITR_OPTS_CHUNK': {'fig.path' : '%s/' % self.figpath},
  55. 'PLUGIN_PATHS': ['../'],
  56. 'PLUGINS': ['rmd_reader'],
  57. })
  58. pelican = Pelican(settings=settings)
  59. pelican.run()
  60. outputfilename = os.path.join(self.outputdir,'%s.html' % self.testtitle)
  61. self.assertTrue(os.path.exists(outputfilename),'File %s was not created.' % outputfilename)
  62. imagesdir = os.path.join(self.outputdir,self.figpath)
  63. self.assertTrue(os.path.exists(imagesdir), 'figpath not created.')
  64. images = glob.glob('%s/*' % imagesdir)
  65. self.assertTrue(len(images) == 1,'Contents of images dir is not correct: %s' % ','.join(images))
  66. if __name__ == "__main__":
  67. #import sys;sys.argv = ['', 'Test.testName']
  68. unittest.main()