##// END OF EJS Templates
use new get_ipython_cmd functionality
Paul Ivanov -
Show More
@@ -1,178 +1,175 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 TemporaryDirectory
21 from IPython.utils.tempdir import TemporaryDirectory
22 from IPython.utils.process import get_output_error_code
22 from IPython.utils.process import get_output_error_code
23 from IPython.utils import py3compat
23 from IPython.testing.tools import get_ipython_cmd
24
24
25 # Define ipython command line name
25 # a trailing space allows for simpler concatenation with the other arguments
26 if py3compat.PY3:
26 ipy_cmd = get_ipython_cmd(as_string=True) + " "
27 ipy_cmd = 'ipython3 '
28 else:
29 ipy_cmd = 'ipython '
30
27
31 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
32 # Classes and functions
29 # Classes and functions
33 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
34
31
35 class TemporaryWorkingDirectory(TemporaryDirectory):
32 class TemporaryWorkingDirectory(TemporaryDirectory):
36 """
33 """
37 Creates a temporary directory and sets the cwd to that directory.
34 Creates a temporary directory and sets the cwd to that directory.
38 Automatically reverts to previous cwd upon cleanup.
35 Automatically reverts to previous cwd upon cleanup.
39 Usage example:
36 Usage example:
40
37
41 with TemporaryWorakingDirectory() as tmpdir:
38 with TemporaryWorakingDirectory() as tmpdir:
42 ...
39 ...
43 """
40 """
44
41
45 def __init__(self, **kw):
42 def __init__(self, **kw):
46 """
43 """
47 Constructor
44 Constructor
48 """
45 """
49 super(TemporaryWorkingDirectory, self).__init__(**kw)
46 super(TemporaryWorkingDirectory, self).__init__(**kw)
50
47
51 #Change cwd to new temp dir. Remember old cwd.
48 #Change cwd to new temp dir. Remember old cwd.
52 self.old_wd = os.getcwd()
49 self.old_wd = os.getcwd()
53 os.chdir(self.name)
50 os.chdir(self.name)
54
51
55
52
56 def cleanup(self):
53 def cleanup(self):
57 """
54 """
58 Destructor
55 Destructor
59 """
56 """
60
57
61 #Revert to old cwd.
58 #Revert to old cwd.
62 os.chdir(self.old_wd)
59 os.chdir(self.old_wd)
63
60
64 #Cleanup
61 #Cleanup
65 super(TemporaryWorkingDirectory, self).cleanup()
62 super(TemporaryWorkingDirectory, self).cleanup()
66
63
67
64
68 class TestsBase(object):
65 class TestsBase(object):
69 """Base tests class. Contains usefull fuzzy comparison and nbconvert
66 """Base tests class. Contains usefull fuzzy comparison and nbconvert
70 functions."""
67 functions."""
71
68
72
69
73 def fuzzy_compare(self, a, b, newlines_are_spaces=True, tabs_are_spaces=True,
70 def fuzzy_compare(self, a, b, newlines_are_spaces=True, tabs_are_spaces=True,
74 fuzzy_spacing=True, ignore_spaces=False,
71 fuzzy_spacing=True, ignore_spaces=False,
75 ignore_newlines=False, case_sensitive=False):
72 ignore_newlines=False, case_sensitive=False):
76 """
73 """
77 Performs a fuzzy comparison of two strings. A fuzzy comparison is a
74 Performs a fuzzy comparison of two strings. A fuzzy comparison is a
78 comparison that ignores insignificant differences in the two comparands.
75 comparison that ignores insignificant differences in the two comparands.
79 The significance of certain differences can be specified via the keyword
76 The significance of certain differences can be specified via the keyword
80 parameters of this method.
77 parameters of this method.
81 """
78 """
82
79
83 if newlines_are_spaces:
80 if newlines_are_spaces:
84 a = a.replace('\n', ' ')
81 a = a.replace('\n', ' ')
85 b = b.replace('\n', ' ')
82 b = b.replace('\n', ' ')
86
83
87 if tabs_are_spaces:
84 if tabs_are_spaces:
88 a = a.replace('\t', ' ')
85 a = a.replace('\t', ' ')
89 b = b.replace('\t', ' ')
86 b = b.replace('\t', ' ')
90
87
91 if ignore_spaces:
88 if ignore_spaces:
92 a = a.replace(' ', '')
89 a = a.replace(' ', '')
93 b = b.replace(' ', '')
90 b = b.replace(' ', '')
94
91
95 if fuzzy_spacing:
92 if fuzzy_spacing:
96 a = self.recursive_replace(a, ' ', ' ')
93 a = self.recursive_replace(a, ' ', ' ')
97 b = self.recursive_replace(b, ' ', ' ')
94 b = self.recursive_replace(b, ' ', ' ')
98
95
99 if ignore_newlines:
96 if ignore_newlines:
100 a = a.replace('\n', '')
97 a = a.replace('\n', '')
101 b = b.replace('\n', '')
98 b = b.replace('\n', '')
102
99
103 if not case_sensitive:
100 if not case_sensitive:
104 a = a.lower()
101 a = a.lower()
105 b = b.lower()
102 b = b.lower()
106
103
107 return a == b
104 return a == b
108
105
109
106
110 def recursive_replace(self, text, search, replacement):
107 def recursive_replace(self, text, search, replacement):
111 """
108 """
112 Performs a recursive replacement operation. Replaces all instances
109 Performs a recursive replacement operation. Replaces all instances
113 of a search string in a text string with a replacement string until
110 of a search string in a text string with a replacement string until
114 the search string no longer exists. Recursion is needed because the
111 the search string no longer exists. Recursion is needed because the
115 replacement string may generate additional search strings.
112 replacement string may generate additional search strings.
116
113
117 For example:
114 For example:
118 Replace "ii" with "i" in the string "Hiiii" yields "Hii"
115 Replace "ii" with "i" in the string "Hiiii" yields "Hii"
119 Another replacement yields "Hi" (the desired output)
116 Another replacement yields "Hi" (the desired output)
120
117
121 Parameters:
118 Parameters:
122 -----------
119 -----------
123 text : string
120 text : string
124 Text to replace in.
121 Text to replace in.
125 search : string
122 search : string
126 String to search for within "text"
123 String to search for within "text"
127 replacement : string
124 replacement : string
128 String to replace "search" with
125 String to replace "search" with
129 """
126 """
130 while search in text:
127 while search in text:
131 text = text.replace(search, replacement)
128 text = text.replace(search, replacement)
132 return text
129 return text
133
130
134
131
135 def create_temp_cwd(self, copy_filenames=None):
132 def create_temp_cwd(self, copy_filenames=None):
136 temp_dir = TemporaryWorkingDirectory()
133 temp_dir = TemporaryWorkingDirectory()
137
134
138 #Copy the files if requested.
135 #Copy the files if requested.
139 if not copy_filenames is None:
136 if not copy_filenames is None:
140 self.copy_files_to(copy_filenames)
137 self.copy_files_to(copy_filenames)
141
138
142 #Return directory handler
139 #Return directory handler
143 return temp_dir
140 return temp_dir
144
141
145
142
146 def copy_files_to(self, copy_filenames=None, destination=None):
143 def copy_files_to(self, copy_filenames=None, destination=None):
147
144
148 #Copy test files into the destination directory.
145 #Copy test files into the destination directory.
149 if copy_filenames:
146 if copy_filenames:
150 for pattern in copy_filenames:
147 for pattern in copy_filenames:
151 for match in glob.glob(os.path.join(self._get_files_path(), pattern)):
148 for match in glob.glob(os.path.join(self._get_files_path(), pattern)):
152 if destination is None:
149 if destination is None:
153 shutil.copyfile(match, os.path.basename(match))
150 shutil.copyfile(match, os.path.basename(match))
154 else:
151 else:
155 if not os.path.isdir(destination):
152 if not os.path.isdir(destination):
156 os.makedirs(destination)
153 os.makedirs(destination)
157 shutil.copyfile(match, os.path.join(destination, os.path.basename(match)))
154 shutil.copyfile(match, os.path.join(destination, os.path.basename(match)))
158
155
159
156
160 def _get_files_path(self):
157 def _get_files_path(self):
161
158
162 #Get the relative path to this module in the IPython directory.
159 #Get the relative path to this module in the IPython directory.
163 names = self.__module__.split('.')[1:-1]
160 names = self.__module__.split('.')[1:-1]
164 names.append('files')
161 names.append('files')
165
162
166 #Build a path using the IPython directory and the relative path we just
163 #Build a path using the IPython directory and the relative path we just
167 #found.
164 #found.
168 path = IPython.__path__[0]
165 path = IPython.__path__[0]
169 for name in names:
166 for name in names:
170 path = os.path.join(path, name)
167 path = os.path.join(path, name)
171 return path
168 return path
172
169
173
170
174 def call(self, parameters, raise_on_error=True):
171 def call(self, parameters, raise_on_error=True):
175 stdout, stderr, retcode = get_output_error_code(ipy_cmd + parameters)
172 stdout, stderr, retcode = get_output_error_code(ipy_cmd + parameters)
176 if retcode != 0 and raise_on_error:
173 if retcode != 0 and raise_on_error:
177 raise OSError(stderr)
174 raise OSError(stderr)
178 return stdout, stderr
175 return stdout, stderr
General Comments 0
You need to be logged in to leave comments. Login now