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