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