Show More
@@ -1,148 +1,145 b'' | |||||
1 | """Base test class for nbconvert""" |
|
1 | """Base test class for nbconvert""" | |
2 |
|
2 | |||
3 | # Copyright (c) IPython Development Team. |
|
3 | # Copyright (c) IPython Development Team. | |
4 | # Distributed under the terms of the Modified BSD License. |
|
4 | # Distributed under the terms of the Modified BSD License. | |
5 |
|
5 | |||
6 | import io |
|
6 | import io | |
7 | import os |
|
7 | import os | |
8 | import glob |
|
8 | import glob | |
9 | import shutil |
|
9 | import shutil | |
10 | import unittest |
|
10 | import unittest | |
11 |
|
11 | |||
12 | import IPython |
|
|||
13 | from IPython.nbformat import v4, write |
|
12 | from IPython.nbformat import v4, write | |
14 | from IPython.utils.tempdir import TemporaryWorkingDirectory |
|
13 | from IPython.utils.tempdir import TemporaryWorkingDirectory | |
15 | from IPython.utils.path import get_ipython_package_dir |
|
|||
16 | from IPython.utils.process import get_output_error_code |
|
14 | from IPython.utils.process import get_output_error_code | |
17 | from IPython.testing.tools import get_ipython_cmd |
|
15 | from IPython.testing.tools import get_ipython_cmd | |
18 |
|
16 | |||
19 | # a trailing space allows for simpler concatenation with the other arguments |
|
17 | # a trailing space allows for simpler concatenation with the other arguments | |
20 | ipy_cmd = get_ipython_cmd(as_string=True) + " " |
|
18 | ipy_cmd = get_ipython_cmd(as_string=True) + " " | |
21 |
|
19 | |||
22 |
|
20 | |||
23 | class TestsBase(unittest.TestCase): |
|
21 | class TestsBase(unittest.TestCase): | |
24 | """Base tests class. Contains useful fuzzy comparison and nbconvert |
|
22 | """Base tests class. Contains useful fuzzy comparison and nbconvert | |
25 | functions.""" |
|
23 | functions.""" | |
26 |
|
24 | |||
27 |
|
25 | |||
28 | def fuzzy_compare(self, a, b, newlines_are_spaces=True, tabs_are_spaces=True, |
|
26 | def fuzzy_compare(self, a, b, newlines_are_spaces=True, tabs_are_spaces=True, | |
29 | fuzzy_spacing=True, ignore_spaces=False, |
|
27 | fuzzy_spacing=True, ignore_spaces=False, | |
30 | ignore_newlines=False, case_sensitive=False, leave_padding=False): |
|
28 | ignore_newlines=False, case_sensitive=False, leave_padding=False): | |
31 | """ |
|
29 | """ | |
32 | Performs a fuzzy comparison of two strings. A fuzzy comparison is a |
|
30 | Performs a fuzzy comparison of two strings. A fuzzy comparison is a | |
33 | comparison that ignores insignificant differences in the two comparands. |
|
31 | comparison that ignores insignificant differences in the two comparands. | |
34 | The significance of certain differences can be specified via the keyword |
|
32 | The significance of certain differences can be specified via the keyword | |
35 | parameters of this method. |
|
33 | parameters of this method. | |
36 | """ |
|
34 | """ | |
37 |
|
35 | |||
38 | if not leave_padding: |
|
36 | if not leave_padding: | |
39 | a = a.strip() |
|
37 | a = a.strip() | |
40 | b = b.strip() |
|
38 | b = b.strip() | |
41 |
|
39 | |||
42 | if ignore_newlines: |
|
40 | if ignore_newlines: | |
43 | a = a.replace('\n', '') |
|
41 | a = a.replace('\n', '') | |
44 | b = b.replace('\n', '') |
|
42 | b = b.replace('\n', '') | |
45 |
|
43 | |||
46 | if newlines_are_spaces: |
|
44 | if newlines_are_spaces: | |
47 | a = a.replace('\n', ' ') |
|
45 | a = a.replace('\n', ' ') | |
48 | b = b.replace('\n', ' ') |
|
46 | b = b.replace('\n', ' ') | |
49 |
|
47 | |||
50 | if tabs_are_spaces: |
|
48 | if tabs_are_spaces: | |
51 | a = a.replace('\t', ' ') |
|
49 | a = a.replace('\t', ' ') | |
52 | b = b.replace('\t', ' ') |
|
50 | b = b.replace('\t', ' ') | |
53 |
|
51 | |||
54 | if ignore_spaces: |
|
52 | if ignore_spaces: | |
55 | a = a.replace(' ', '') |
|
53 | a = a.replace(' ', '') | |
56 | b = b.replace(' ', '') |
|
54 | b = b.replace(' ', '') | |
57 |
|
55 | |||
58 | if fuzzy_spacing: |
|
56 | if fuzzy_spacing: | |
59 | a = self.recursive_replace(a, ' ', ' ') |
|
57 | a = self.recursive_replace(a, ' ', ' ') | |
60 | b = self.recursive_replace(b, ' ', ' ') |
|
58 | b = self.recursive_replace(b, ' ', ' ') | |
61 |
|
59 | |||
62 | if not case_sensitive: |
|
60 | if not case_sensitive: | |
63 | a = a.lower() |
|
61 | a = a.lower() | |
64 | b = b.lower() |
|
62 | b = b.lower() | |
65 |
|
63 | |||
66 | self.assertEqual(a, b) |
|
64 | self.assertEqual(a, b) | |
67 |
|
65 | |||
68 |
|
66 | |||
69 | def recursive_replace(self, text, search, replacement): |
|
67 | def recursive_replace(self, text, search, replacement): | |
70 | """ |
|
68 | """ | |
71 | Performs a recursive replacement operation. Replaces all instances |
|
69 | Performs a recursive replacement operation. Replaces all instances | |
72 | of a search string in a text string with a replacement string until |
|
70 | of a search string in a text string with a replacement string until | |
73 | the search string no longer exists. Recursion is needed because the |
|
71 | the search string no longer exists. Recursion is needed because the | |
74 | replacement string may generate additional search strings. |
|
72 | replacement string may generate additional search strings. | |
75 |
|
73 | |||
76 | For example: |
|
74 | For example: | |
77 | Replace "ii" with "i" in the string "Hiiii" yields "Hii" |
|
75 | Replace "ii" with "i" in the string "Hiiii" yields "Hii" | |
78 | Another replacement cds "Hi" (the desired output) |
|
76 | Another replacement cds "Hi" (the desired output) | |
79 |
|
77 | |||
80 | Parameters |
|
78 | Parameters | |
81 | ---------- |
|
79 | ---------- | |
82 | text : string |
|
80 | text : string | |
83 | Text to replace in. |
|
81 | Text to replace in. | |
84 | search : string |
|
82 | search : string | |
85 | String to search for within "text" |
|
83 | String to search for within "text" | |
86 | replacement : string |
|
84 | replacement : string | |
87 | String to replace "search" with |
|
85 | String to replace "search" with | |
88 | """ |
|
86 | """ | |
89 | while search in text: |
|
87 | while search in text: | |
90 | text = text.replace(search, replacement) |
|
88 | text = text.replace(search, replacement) | |
91 | return text |
|
89 | return text | |
92 |
|
90 | |||
93 | def create_temp_cwd(self, copy_filenames=None): |
|
91 | def create_temp_cwd(self, copy_filenames=None): | |
94 | temp_dir = TemporaryWorkingDirectory() |
|
92 | temp_dir = TemporaryWorkingDirectory() | |
95 |
|
93 | |||
96 | #Copy the files if requested. |
|
94 | #Copy the files if requested. | |
97 | if copy_filenames is not None: |
|
95 | if copy_filenames is not None: | |
98 | self.copy_files_to(copy_filenames, dest=temp_dir.name) |
|
96 | self.copy_files_to(copy_filenames, dest=temp_dir.name) | |
99 |
|
97 | |||
100 | #Return directory handler |
|
98 | #Return directory handler | |
101 | return temp_dir |
|
99 | return temp_dir | |
102 |
|
100 | |||
103 | def create_empty_notebook(self, path): |
|
101 | def create_empty_notebook(self, path): | |
104 | nb = v4.new_notebook() |
|
102 | nb = v4.new_notebook() | |
105 | with io.open(path, 'w', encoding='utf-8') as f: |
|
103 | with io.open(path, 'w', encoding='utf-8') as f: | |
106 | write(nb, f, 4) |
|
104 | write(nb, f, 4) | |
107 |
|
105 | |||
108 | def copy_files_to(self, copy_filenames, dest='.'): |
|
106 | def copy_files_to(self, copy_filenames, dest='.'): | |
109 | "Copy test files into the destination directory" |
|
107 | "Copy test files into the destination directory" | |
110 | if not os.path.isdir(dest): |
|
108 | if not os.path.isdir(dest): | |
111 | os.makedirs(dest) |
|
109 | os.makedirs(dest) | |
112 | files_path = self._get_files_path() |
|
110 | files_path = self._get_files_path() | |
113 | for pattern in copy_filenames: |
|
111 | for pattern in copy_filenames: | |
114 | for match in glob.glob(os.path.join(files_path, pattern)): |
|
112 | for match in glob.glob(os.path.join(files_path, pattern)): | |
115 | shutil.copyfile(match, os.path.join(dest, os.path.basename(match))) |
|
113 | shutil.copyfile(match, os.path.join(dest, os.path.basename(match))) | |
116 |
|
114 | |||
117 |
|
115 | |||
118 | def _get_files_path(self): |
|
116 | def _get_files_path(self): | |
119 |
|
117 | |||
120 | #Get the relative path to this module in the IPython directory. |
|
118 | #Get the relative path to this module in the IPython directory. | |
121 | names = self.__module__.split('.')[1:-1] |
|
119 | names = self.__module__.split('.')[1:-1] | |
122 | names.append('files') |
|
120 | names.append('files') | |
123 |
|
121 | |||
124 |
#Build a path using the |
|
122 | #Build a path using the nbconvert directory and the relative path we just | |
125 | #found. |
|
123 | #found. | |
126 | path = get_ipython_package_dir() |
|
124 | import jupyter_nbconvert | |
127 | for name in names: |
|
125 | path = os.path.dirname(jupyter_nbconvert.__file__) | |
128 |
|
|
126 | return os.path.join(path, *names) | |
129 | return path |
|
|||
130 |
|
127 | |||
131 |
|
128 | |||
132 | def call(self, parameters, ignore_return_code=False): |
|
129 | def call(self, parameters, ignore_return_code=False): | |
133 | """ |
|
130 | """ | |
134 | Execute a, IPython shell command, listening for both Errors and non-zero |
|
131 | Execute a, IPython shell command, listening for both Errors and non-zero | |
135 | return codes. |
|
132 | return codes. | |
136 |
|
133 | |||
137 | Parameters |
|
134 | Parameters | |
138 | ---------- |
|
135 | ---------- | |
139 | parameters : str |
|
136 | parameters : str | |
140 | List of parameters to pass to IPython. |
|
137 | List of parameters to pass to IPython. | |
141 | ignore_return_code : optional bool (default False) |
|
138 | ignore_return_code : optional bool (default False) | |
142 | Throw an OSError if the return code |
|
139 | Throw an OSError if the return code | |
143 | """ |
|
140 | """ | |
144 |
|
141 | |||
145 | stdout, stderr, retcode = get_output_error_code(ipy_cmd + parameters) |
|
142 | stdout, stderr, retcode = get_output_error_code(ipy_cmd + parameters) | |
146 | if not (retcode == 0 or ignore_return_code): |
|
143 | if not (retcode == 0 or ignore_return_code): | |
147 | raise OSError(stderr) |
|
144 | raise OSError(stderr) | |
148 | return stdout, stderr |
|
145 | return stdout, stderr |
General Comments 0
You need to be logged in to leave comments.
Login now