test_rmd_reader.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. logging.debug('CLEAN')
  51. if os.path.isdir(self.outputdir):
  52. shutil.rmtree(self.outputdir)
  53. if os.path.isdir(self.contentdir):
  54. shutil.rmtree(self.contentdir)
  55. def testKnitrSettings(self):
  56. settings = read_settings(path=None, override={
  57. 'LOAD_CONTENT_CACHE': False,
  58. 'PATH': self.contentdir,
  59. 'OUTPUT_PATH': self.outputdir,
  60. 'RMD_READER_KNITR_OPTS_CHUNK': {'fig.path' : '%s/' % self.figpath},
  61. 'RMD_READER_KNITR_OPTS_KNIT': {'progress' : True, 'verbose': True},
  62. 'RMD_READER_RENAME_PLOT': 'disable',
  63. 'PLUGIN_PATHS': ['../'],
  64. 'PLUGINS': ['rmd_reader'],
  65. })
  66. pelican = Pelican(settings=settings)
  67. pelican.run()
  68. outputfilename = os.path.join(self.outputdir,'%s.html' % self.testtitle)
  69. self.assertTrue(os.path.exists(outputfilename),'File %s was not created.' % outputfilename)
  70. imagesdir = os.path.join(self.outputdir, self.figpath)
  71. self.assertTrue(os.path.exists(imagesdir), 'figpath not created.')
  72. imagefile = os.path.join(imagesdir, 'unnamed-chunk') + '-1-1.png'
  73. logging.debug(imagefile)
  74. images = glob.glob('%s/*' % imagesdir)
  75. logging.debug(images)
  76. self.assertTrue(os.path.exists(imagefile), 'image correctly named.')
  77. self.assertTrue(len(images) == 1,'Contents of images dir is not correct: %s' % ','.join(images))
  78. def testKnitrSettings2(self):
  79. settings = read_settings(path=None, override={
  80. 'LOAD_CONTENT_CACHE': False,
  81. 'PATH': self.contentdir,
  82. 'OUTPUT_PATH': self.outputdir,
  83. 'RMD_READER_KNITR_OPTS_CHUNK': {'fig.path' : '%s/' % self.figpath},
  84. 'RMD_READER_KNITR_OPTS_KNIT': {'progress' : True, 'verbose': True},
  85. 'RMD_READER_RENAME_PLOT': 'chunklabel',
  86. 'PLUGIN_PATHS': ['../'],
  87. 'PLUGINS': ['rmd_reader'],
  88. })
  89. pelican = Pelican(settings=settings)
  90. pelican.run()
  91. outputfilename = os.path.join(self.outputdir,'%s.html' % self.testtitle)
  92. self.assertTrue(os.path.exists(outputfilename),'File %s was not created.' % outputfilename)
  93. imagesdir = os.path.join(self.outputdir, self.figpath)
  94. self.assertTrue(os.path.exists(imagesdir), 'figpath not created.')
  95. imagefile = os.path.join(imagesdir, os.path.splitext(os.path.split(self.contentfile)[1])[0]) + '-1-1.png'
  96. logging.debug(imagefile)
  97. self.assertTrue(os.path.exists(imagefile), 'image correctly named.')
  98. images = glob.glob('%s/*' % imagesdir)
  99. logging.debug(images)
  100. self.assertTrue(len(images) == 1,'Contents of images dir is not correct: %s' % ','.join(images))
  101. def testKnitrSettings3(self):
  102. settings = read_settings(path=None, override={
  103. 'LOAD_CONTENT_CACHE': False,
  104. 'PATH': self.contentdir,
  105. 'OUTPUT_PATH': self.outputdir,
  106. 'RMD_READER_KNITR_OPTS_CHUNK': {'fig.path' : '%s/' % self.figpath},
  107. 'RMD_READER_KNITR_OPTS_KNIT': {'progress' : True, 'verbose': True},
  108. 'RMD_READER_RENAME_PLOT': 'directory',
  109. 'PLUGIN_PATHS': ['../'],
  110. 'PLUGINS': ['rmd_reader'],
  111. })
  112. pelican = Pelican(settings=settings)
  113. pelican.run()
  114. outputfilename = os.path.join(self.outputdir,'%s.html' % self.testtitle)
  115. self.assertTrue(os.path.exists(outputfilename),'File %s was not created.' % outputfilename)
  116. imagesdir = os.path.join(self.outputdir, self.figpath)
  117. self.assertTrue(os.path.exists(imagesdir), 'figpath not created.')
  118. imagefile = os.path.join(imagesdir, os.path.splitext(os.path.split(self.contentfile)[1])[0]) + '-unnamed-chunk-1-1.png'
  119. logging.debug(imagefile)
  120. self.assertTrue(os.path.exists(imagefile), 'image correctly named.')
  121. images = glob.glob('%s/*' % imagesdir)
  122. logging.debug(images)
  123. self.assertTrue(len(images) == 1,'Contents of images dir is not correct: %s' % ','.join(images))
  124. if __name__ == "__main__":
  125. #import sys;sys.argv = ['', 'Test.testName']
  126. unittest.main()