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