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