##// END OF EJS Templates
Added files writer tests
Jonathan Frederic -
Show More
@@ -0,0 +1,159 b''
1 """
2 Module with tests for files
3 """
4
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
7 #
8 # Distributed under the terms of the Modified BSD License.
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 import sys
18 import os
19 from StringIO import StringIO
20
21 from ...tests.base import TestsBase
22 from ..files import FilesWriter
23
24
25 #-----------------------------------------------------------------------------
26 # Class
27 #-----------------------------------------------------------------------------
28
29 class Testfiles(TestsBase):
30 """Contains test functions for files.py"""
31
32 def test_basic_output(self):
33 """Is FilesWriter basic output correct?"""
34
35 # Work in a temporary directory.
36 with self.create_temp_cwd():
37
38 # Create the resoruces dictionary
39 res = {}
40
41 # Create files writer, test output
42 writer = FilesWriter()
43 writer.write(u'y', res, notebook_name="z")
44
45 # Check the output of the file
46 with open('z', 'r') as f:
47 output = f.read()
48 self.assertEqual(output, u'y')
49
50 def test_ext(self):
51 """Does the FilesWriter add the correct extension to the output?"""
52
53 # Work in a temporary directory.
54 with self.create_temp_cwd():
55
56 # Create the resoruces dictionary
57 res = {'output_extension': 'txt'}
58
59 # Create files writer, test output
60 writer = FilesWriter()
61 writer.write(u'y', res, notebook_name="z")
62
63 # Check the output of the file
64 assert os.path.isfile('z.txt')
65 with open('z.txt', 'r') as f:
66 output = f.read()
67 self.assertEqual(output, u'y')
68
69
70 def test_extract(self):
71 """Can FilesWriter write extracted figures correctly?"""
72
73 # Work in a temporary directory.
74 with self.create_temp_cwd():
75
76 # Create the resoruces dictionary
77 res = {'outputs': {os.path.join('z_files', 'a'): b'b'}}
78
79 # Create files writer, test output
80 writer = FilesWriter()
81 writer.write(u'y', res, notebook_name="z")
82
83 # Check the output of the file
84 with open('z', 'r') as f:
85 output = f.read()
86 self.assertEqual(output, u'y')
87
88 # Check the output of the extracted file
89 extracted_file_dest = os.path.join('z_files', 'a')
90 assert os.path.isfile(extracted_file_dest)
91 with open(extracted_file_dest, 'r') as f:
92 output = f.read()
93 self.assertEqual(output, 'b')
94
95
96 def test_builddir(self):
97 """Can FilesWriter write to a build dir correctly?"""
98
99 # Work in a temporary directory.
100 with self.create_temp_cwd():
101
102 # Create the resoruces dictionary
103 res = {'outputs': {os.path.join('z_files', 'a'): b'b'}}
104
105 # Create files writer, test output
106 writer = FilesWriter()
107 writer.build_directory = u'build'
108 writer.write(u'y', res, notebook_name="z")
109
110 # Check the output of the file
111 assert os.path.isdir(writer.build_directory)
112 dest = os.path.join(writer.build_directory, 'z')
113 with open(dest, 'r') as f:
114 output = f.read()
115 self.assertEqual(output, u'y')
116
117 # Check the output of the extracted file
118 extracted_file_dest = os.path.join(writer.build_directory, 'z_files', 'a')
119 assert os.path.isfile(extracted_file_dest)
120 with open(extracted_file_dest, 'r') as f:
121 output = f.read()
122 self.assertEqual(output, 'b')
123
124
125 def test_links(self):
126 """Can the FilesWriter handle linked files correctly?"""
127
128 # Work in a temporary directory.
129 with self.create_temp_cwd():
130
131 # Create test file
132 os.mkdir('sub')
133 with open(os.path.join('sub', 'c'), 'w') as f:
134 f.write('d')
135
136 # Create the resoruces dictionary
137 res = {}
138
139 # Create files writer, test output
140 writer = FilesWriter()
141 writer.files = [os.path.join('sub', 'c')]
142 writer.build_directory = u'build'
143 writer.write(u'y', res, notebook_name="z")
144
145 # Check the output of the file
146 assert os.path.isdir(writer.build_directory)
147 dest = os.path.join(writer.build_directory, 'z')
148 with open(dest, 'r') as f:
149 output = f.read()
150 self.assertEqual(output, u'y')
151
152 # Check to make sure the linked file was copied
153 path = os.path.join(writer.build_directory, 'sub')
154 assert os.path.isdir(path)
155 dest = os.path.join(path, 'c')
156 assert os.path.isfile(dest)
157 with open(dest, 'r') as f:
158 output = f.read()
159 self.assertEqual(output, 'd')
General Comments 0
You need to be logged in to leave comments. Login now