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