test_files.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. """
  2. Module with tests for files
  3. """
  4. # Copyright (c) Jupyter Development Team.
  5. # Distributed under the terms of the Modified BSD License.
  6. import os
  7. from ...tests.base import TestsBase
  8. from ..files import FilesWriter
  9. class Testfiles(TestsBase):
  10. """Contains test functions for files.py"""
  11. def test_basic_output(self):
  12. """Is FilesWriter basic output correct?"""
  13. # Work in a temporary directory.
  14. with self.create_temp_cwd():
  15. # Create the resoruces dictionary
  16. res = {}
  17. # Create files writer, test output
  18. writer = FilesWriter()
  19. writer.write(u'y', res, notebook_name="z")
  20. # Check the output of the file
  21. with open('z', 'r') as f:
  22. output = f.read()
  23. self.assertEqual(output, u'y')
  24. def test_ext(self):
  25. """Does the FilesWriter add the correct extension to the output?"""
  26. # Work in a temporary directory.
  27. with self.create_temp_cwd():
  28. # Create the resoruces dictionary
  29. res = {'output_extension': '.txt'}
  30. # Create files writer, test output
  31. writer = FilesWriter()
  32. writer.write(u'y', res, notebook_name="z")
  33. # Check the output of the file
  34. assert os.path.isfile('z.txt')
  35. with open('z.txt', 'r') as f:
  36. output = f.read()
  37. self.assertEqual(output, u'y')
  38. def test_extract(self):
  39. """Can FilesWriter write extracted figures correctly?"""
  40. # Work in a temporary directory.
  41. with self.create_temp_cwd():
  42. # Create the resoruces dictionary
  43. res = {'outputs': {os.path.join('z_files', 'a'): b'b'}}
  44. # Create files writer, test output
  45. writer = FilesWriter()
  46. writer.write(u'y', res, notebook_name="z")
  47. # Check the output of the file
  48. with open('z', 'r') as f:
  49. output = f.read()
  50. self.assertEqual(output, u'y')
  51. # Check the output of the extracted file
  52. extracted_file_dest = os.path.join('z_files', 'a')
  53. assert os.path.isfile(extracted_file_dest)
  54. with open(extracted_file_dest, 'r') as f:
  55. output = f.read()
  56. self.assertEqual(output, 'b')
  57. def test_build_dir(self):
  58. """Can FilesWriter write to a build dir correctly?"""
  59. # Work in a temporary directory.
  60. with self.create_temp_cwd():
  61. # Create the resoruces dictionary
  62. res = {'outputs': {os.path.join('z_files', 'a'): b'b'}}
  63. # Create files writer, test output
  64. writer = FilesWriter()
  65. writer.build_directory = u'build'
  66. writer.write(u'y', res, notebook_name="z")
  67. # Check the output of the file
  68. assert os.path.isdir(writer.build_directory)
  69. dest = os.path.join(writer.build_directory, 'z')
  70. with open(dest, 'r') as f:
  71. output = f.read()
  72. self.assertEqual(output, u'y')
  73. # Check the output of the extracted file
  74. extracted_file_dest = os.path.join(writer.build_directory, 'z_files', 'a')
  75. assert os.path.isfile(extracted_file_dest)
  76. with open(extracted_file_dest, 'r') as f:
  77. output = f.read()
  78. self.assertEqual(output, 'b')
  79. def test_build_dir_default(self):
  80. """FilesWriter defaults to input path"""
  81. with self.create_temp_cwd():
  82. os.mkdir('sub')
  83. resources = {
  84. 'metadata': {'path': 'sub'}
  85. }
  86. writer = FilesWriter()
  87. writer.write(u'content', resources, notebook_name="out")
  88. dest = os.path.join('sub', 'out')
  89. assert os.path.isfile(dest)
  90. with open(dest) as f:
  91. self.assertEqual(f.read().strip(), 'content')
  92. def test_links(self):
  93. """Can the FilesWriter handle linked files correctly?"""
  94. # Work in a temporary directory.
  95. with self.create_temp_cwd():
  96. # Create test file
  97. os.mkdir('sub')
  98. with open(os.path.join('sub', 'c'), 'w') as f:
  99. f.write('d')
  100. # Create the resoruces dictionary
  101. res = {}
  102. # Create files writer, test output
  103. writer = FilesWriter()
  104. writer.files = [os.path.join('sub', 'c')]
  105. writer.build_directory = u'build'
  106. writer.write(u'y', res, notebook_name="z")
  107. # Check the output of the file
  108. assert os.path.isdir(writer.build_directory)
  109. dest = os.path.join(writer.build_directory, 'z')
  110. with open(dest, 'r') as f:
  111. output = f.read()
  112. self.assertEqual(output, u'y')
  113. # Check to make sure the linked file was copied
  114. path = os.path.join(writer.build_directory, 'sub')
  115. assert os.path.isdir(path)
  116. dest = os.path.join(path, 'c')
  117. assert os.path.isfile(dest)
  118. with open(dest, 'r') as f:
  119. output = f.read()
  120. self.assertEqual(output, 'd')
  121. def test_glob(self):
  122. """Can the FilesWriter handle globbed files correctly?"""
  123. # Work in a temporary directory.
  124. with self.create_temp_cwd():
  125. # Create test files
  126. os.mkdir('sub')
  127. with open(os.path.join('sub', 'c'), 'w') as f:
  128. f.write('e')
  129. with open(os.path.join('sub', 'd'), 'w') as f:
  130. f.write('e')
  131. # Create the resoruces dictionary
  132. res = {}
  133. # Create files writer, test output
  134. writer = FilesWriter()
  135. writer.files = ['sub/*']
  136. writer.build_directory = u'build'
  137. writer.write(u'y', res, notebook_name="z")
  138. # Check the output of the file
  139. assert os.path.isdir(writer.build_directory)
  140. dest = os.path.join(writer.build_directory, 'z')
  141. with open(dest, 'r') as f:
  142. output = f.read()
  143. self.assertEqual(output, u'y')
  144. # Check to make sure the globbed files were copied
  145. path = os.path.join(writer.build_directory, 'sub')
  146. assert os.path.isdir(path)
  147. for filename in ['c', 'd']:
  148. dest = os.path.join(path, filename)
  149. assert os.path.isfile(dest)
  150. with open(dest, 'r') as f:
  151. output = f.read()
  152. self.assertEqual(output, 'e')
  153. def test_relpath(self):
  154. """Can the FilesWriter handle relative paths for linked files correctly?"""
  155. # Work in a temporary directory.
  156. with self.create_temp_cwd():
  157. # Create test file
  158. os.mkdir('sub')
  159. with open(os.path.join('sub', 'c'), 'w') as f:
  160. f.write('d')
  161. # Create the resoruces dictionary
  162. res = {}
  163. # Create files writer, test output
  164. writer = FilesWriter()
  165. writer.files = [os.path.join('sub', 'c')]
  166. writer.build_directory = u'build'
  167. writer.relpath = 'sub'
  168. writer.write(u'y', res, notebook_name="z")
  169. # Check the output of the file
  170. assert os.path.isdir(writer.build_directory)
  171. dest = os.path.join(writer.build_directory, 'z')
  172. with open(dest, 'r') as f:
  173. output = f.read()
  174. self.assertEqual(output, u'y')
  175. # Check to make sure the linked file was copied
  176. dest = os.path.join(writer.build_directory, 'c')
  177. assert os.path.isfile(dest)
  178. with open(dest, 'r') as f:
  179. output = f.read()
  180. self.assertEqual(output, 'd')
  181. def test_relpath_default(self):
  182. """Is the FilesWriter default relative path correct?"""
  183. # Work in a temporary directory.
  184. with self.create_temp_cwd():
  185. # Create test file
  186. os.mkdir('sub')
  187. with open(os.path.join('sub', 'c'), 'w') as f:
  188. f.write('d')
  189. # Create the resoruces dictionary
  190. res = dict(metadata=dict(path="sub"))
  191. # Create files writer, test output
  192. writer = FilesWriter()
  193. writer.files = [os.path.join('sub', 'c')]
  194. writer.build_directory = u'build'
  195. writer.write(u'y', res, notebook_name="z")
  196. # Check the output of the file
  197. assert os.path.isdir(writer.build_directory)
  198. dest = os.path.join(writer.build_directory, 'z')
  199. with open(dest, 'r') as f:
  200. output = f.read()
  201. self.assertEqual(output, u'y')
  202. # Check to make sure the linked file was copied
  203. dest = os.path.join(writer.build_directory, 'c')
  204. assert os.path.isfile(dest)
  205. with open(dest, 'r') as f:
  206. output = f.read()
  207. self.assertEqual(output, 'd')
  208. def test_relpath_precedence(self):
  209. """Does the FilesWriter relpath option take precedence over the path?"""
  210. # Work in a temporary directory.
  211. with self.create_temp_cwd():
  212. # Create test file
  213. os.mkdir('sub')
  214. with open(os.path.join('sub', 'c'), 'w') as f:
  215. f.write('d')
  216. # Create the resoruces dictionary
  217. res = dict(metadata=dict(path="other_sub"))
  218. # Create files writer, test output
  219. writer = FilesWriter()
  220. writer.files = [os.path.join('sub', 'c')]
  221. writer.build_directory = u'build'
  222. writer.relpath = 'sub'
  223. writer.write(u'y', res, notebook_name="z")
  224. # Check the output of the file
  225. assert os.path.isdir(writer.build_directory)
  226. dest = os.path.join(writer.build_directory, 'z')
  227. with open(dest, 'r') as f:
  228. output = f.read()
  229. self.assertEqual(output, u'y')
  230. # Check to make sure the linked file was copied
  231. dest = os.path.join(writer.build_directory, 'c')
  232. assert os.path.isfile(dest)
  233. with open(dest, 'r') as f:
  234. output = f.read()
  235. self.assertEqual(output, 'd')