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