test_rmd_reader.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.DEBUG)
  12. class Test(unittest.TestCase):
  13. def setUp(self):
  14. try:
  15. import rpy2
  16. import rmd_reader
  17. except Exception:
  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. logging.debug(self.cwd)
  22. # Setup content dir and test rmd file
  23. self.contentdir = os.path.join(self.cwd,'test-content')
  24. logging.debug(self.contentdir)
  25. try:
  26. os.mkdir(self.contentdir)
  27. except Exception:
  28. pass
  29. self.contentfile = os.path.join(self.contentdir,'test.rmd')
  30. logging.debug(self.contentfile)
  31. self.testrmd = '''Title: %s
  32. Date: 2014-06-23
  33. Let's make a simple plot about cars.
  34. ```{r}
  35. cars <- c(1, 3, 6, 4, 9)
  36. plot(cars)
  37. ```
  38. ''' % self.testtitle
  39. with open(self.contentfile,'w') as f:
  40. f.write(self.testrmd)
  41. # Setup output dir
  42. self.outputdir = os.path.join(self.cwd,'test-output')
  43. logging.debug(self.outputdir)
  44. try:
  45. os.mkdir(self.outputdir)
  46. except Exception:
  47. pass
  48. self.figpath = 'images'
  49. def tearDown(self):
  50. if os.path.isdir(self.outputdir):
  51. shutil.rmtree(self.outputdir)
  52. if os.path.isdir(self.contentdir):
  53. shutil.rmtree(self.contentdir)
  54. def testKnitrSettings(self):
  55. settings = read_settings(path=None, override={
  56. 'PATH': self.contentdir,
  57. 'OUTPUT_PATH': self.outputdir,
  58. 'RMD_READER_KNITR_OPTS_CHUNK': {'fig.path' : '%s/' % self.figpath},
  59. 'PLUGIN_PATHS': ['../'],
  60. 'PLUGINS': ['rmd_reader'],
  61. })
  62. pelican = Pelican(settings=settings)
  63. pelican.run()
  64. outputfilename = os.path.join(self.outputdir,'%s.html' % self.testtitle)
  65. self.assertTrue(os.path.exists(outputfilename),'File %s was not created.' % outputfilename)
  66. imagesdir = os.path.join(self.outputdir,self.figpath)
  67. self.assertTrue(os.path.exists(imagesdir), 'figpath not created.')
  68. images = glob.glob('%s/*' % imagesdir)
  69. self.assertTrue(len(images) == 1,'Contents of images dir is not correct: %s' % ','.join(images))
  70. if __name__ == "__main__":
  71. #import sys;sys.argv = ['', 'Test.testName']
  72. unittest.main()