OLD | NEW |
---|---|
(Empty) | |
1 import os | |
2 import sys | |
3 import unittest | |
4 | |
5 from expect_tests import pipeline | |
6 from expect_tests.type_definitions import Test, MultiTest | |
7 | |
8 | |
9 SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) | |
10 DATA_DIR = os.path.join(SCRIPT_DIR, 'data') | |
11 | |
12 class ParsingAndConfigTest(unittest.TestCase): | |
13 def test_get_config(self): | |
14 """Testing that reading the config file works. Tests requires a specific | |
15 content for data/.expect_tests.cfg""" | |
agable
2014/11/12 03:15:10
nit: First line of docstring should be a single li
pgervais
2014/11/13 00:28:27
Done.
| |
16 path = os.path.join(SCRIPT_DIR, 'data') | |
agable
2014/11/12 03:15:10
You already have DATA_DIR defined.
pgervais
2014/11/13 00:28:27
Done.
| |
17 black_list = pipeline.get_config(path) | |
18 self.assertEqual(black_list, | |
19 set(['directory1', 'directory2', 'ignored'])) | |
20 | |
21 def test_parse_test_glob(self): | |
22 self.assertEqual(pipeline.parse_test_glob('a/b/c'), | |
23 (os.path.abspath('a/b/c'), '*')) | |
24 self.assertEqual(pipeline.parse_test_glob('a/b/c:'), | |
25 (os.path.abspath('a/b/c'), '*')) | |
26 self.assertEqual(pipeline.parse_test_glob('a/b/c:Test'), | |
27 (os.path.abspath('a/b/c'), 'Test')) | |
28 self.assertEqual(pipeline.parse_test_glob('a/b/c:Test.Name'), | |
29 (os.path.abspath('a/b/c'), 'Test.Name')) | |
30 self.assertRaises(ValueError, pipeline.parse_test_glob, 'a:b:c',) | |
31 self.assertRaises(ValueError, pipeline.parse_test_glob, 'a:b/c',) | |
32 | |
33 | |
34 class PathManipulationTest(unittest.TestCase): | |
35 """Tests for all path-manipulating functions. | |
36 | |
37 This set uses checked-out files in the present repository to avoid mocking | |
38 the I/O functions. | |
39 """ | |
40 | |
41 def test_get_python_root(self): | |
agable
2014/11/12 03:15:10
I know this looks nice and compact, but this test
iannucci
2014/11/12 20:26:25
I'm not sure I entirely agree with that.. 1 assert
agable
2014/11/12 23:45:11
Please refer to go/bhnol (internal link, sorry). T
pgervais
2014/11/13 00:28:27
While I agree with what is stated in this document
| |
42 """This function uses the directory structure under data/""" | |
43 self.assertEqual(pipeline.get_python_root(DATA_DIR), DATA_DIR) | |
44 self.assertEqual(pipeline.get_python_root( | |
45 os.path.join(DATA_DIR, 'package1')), DATA_DIR) | |
46 self.assertEqual(pipeline.get_python_root( | |
47 os.path.join(DATA_DIR, 'package1', 'subpackage1_1')), DATA_DIR) | |
48 self.assertRaises(ValueError, pipeline.get_python_root, | |
49 '____non-existing-path____') | |
50 | |
51 def test_get_runtime_context(self): | |
52 """Test the get_runtime_context function""" | |
53 datadir = os.path.join(SCRIPT_DIR, 'data') | |
agable
2014/11/12 03:15:10
DATA_DIR
pgervais
2014/11/13 00:28:27
Done.
| |
54 | |
55 # Single directory case | |
56 test_globs = [datadir] | |
57 contexts = pipeline.get_runtime_contexts(test_globs) | |
58 self.assertEqual(len(contexts), 1) | |
59 self.assertEqual(contexts[0].cwd, datadir) | |
60 for testing_c in contexts[0].testing_contexts: | |
61 self.assertNotEqual(testing_c.package_name, 'ignored') | |
62 | |
63 # Single package | |
64 test_globs = [os.path.join(datadir, 'package1')] | |
65 contexts = pipeline.get_runtime_contexts(test_globs) | |
66 self.assertEqual(len(contexts), 1) | |
67 self.assertEqual(contexts[0].cwd, datadir) | |
68 testing_c = contexts[0].testing_contexts | |
69 self.assertEqual(len(testing_c), 1) | |
70 self.assertEqual(testing_c[0].package_name, 'package1') | |
71 | |
72 # Two packages in the same directory | |
73 test_globs = [os.path.join(datadir, 'package1'), | |
74 os.path.join(datadir, 'package2')] | |
75 contexts = pipeline.get_runtime_contexts(test_globs) | |
76 self.assertEqual(len(contexts), 1) | |
77 self.assertEqual(contexts[0].cwd, datadir) | |
78 testing_c = contexts[0].testing_contexts | |
79 self.assertEqual(len(testing_c), 2) | |
80 package_names = set() | |
81 for testing_c in contexts[0].testing_contexts: | |
82 package_names.add(testing_c.package_name) | |
83 | |
84 self.assertEqual(package_names, set(('package1', 'package2'))) | |
85 | |
86 # Packages inside directory | |
87 test_globs = [datadir, os.path.join(datadir, 'package1')] | |
88 contexts = pipeline.get_runtime_contexts(test_globs) | |
89 self.assertEqual(len(contexts), 1) | |
90 # 2 is the number of packages under datadir, not counting | |
91 # the ignored ones. | |
92 self.assertEqual(len(contexts[0].testing_contexts), 2) | |
93 | |
94 test_globs = [datadir, os.path.join(datadir, 'package1'), | |
95 os.path.join(datadir, 'package1', 'subpackage1_1')] | |
96 contexts = pipeline.get_runtime_contexts(test_globs) | |
97 self.assertEqual(len(contexts), 1) | |
98 self.assertEqual(len(contexts[0].testing_contexts), 2) | |
99 | |
agable
2014/11/12 03:15:10
Please have a test for what happens if you explici
pgervais
2014/11/13 00:28:27
I did not write such a test because the behavior i
agable
2014/11/14 20:00:08
Yeah, I asked for the test because the behavior is
| |
100 | |
101 def test_package_testing_context(self): | |
102 """Test the PackageTestingContext class""" | |
103 package_name = 'package1' | |
104 package1 = os.path.join(SCRIPT_DIR, 'data', package_name) | |
105 package2 = os.path.join(SCRIPT_DIR, 'data', 'package2') | |
106 other_package1 = os.path.join(SCRIPT_DIR, 'data', 'other', package_name) | |
107 | |
108 # from_path | |
109 context = pipeline.PackageTestingContext.from_path(package1) | |
110 self.assertTrue(os.path.isabs(context.cwd)) | |
111 self.assertTrue(len(context.package_name) > 0) | |
112 self.assertEqual(len(context.filters), 1) | |
113 self.assertEqual(context.filters[0][1], '*') | |
114 | |
115 context = pipeline.PackageTestingContext.from_path(package1, | |
116 filters='*') | |
117 self.assertTrue(os.path.isabs(context.cwd)) | |
118 self.assertTrue(len(context.package_name) > 0) | |
119 self.assertEqual(len(context.filters), 1) | |
120 self.assertEqual(context.filters[0][1], '*') | |
121 | |
122 context = pipeline.PackageTestingContext.from_path(package1, | |
123 filters=('a*', 'b*')) | |
124 self.assertTrue(os.path.isabs(context.cwd)) | |
125 self.assertTrue(len(context.package_name) > 0) | |
126 self.assertEqual(len(context.filters), 2) | |
127 self.assertEqual(context.filters[0][1], 'a*') | |
128 self.assertEqual(context.filters[1][1], 'b*') | |
129 | |
130 # from_context_list | |
131 c1 = pipeline.PackageTestingContext.from_path(package1, filters='a') | |
132 c2 = pipeline.PackageTestingContext.from_path(package1, filters=('b','c')) | |
133 context = pipeline.PackageTestingContext.from_context_list((c1, c2)) | |
134 self.assertEqual(len(context.filters), 3) | |
135 self.assertEqual(set(filt[1] for filt in context.filters), | |
136 set(('a', 'b', 'c'))) | |
137 | |
138 c1 = pipeline.PackageTestingContext.from_path(package1, filters='a') | |
139 c2 = pipeline.PackageTestingContext.from_path(package2, filters=('b','c')) | |
140 self.assertRaises(AssertionError, | |
141 pipeline.PackageTestingContext.from_context_list, | |
142 (c1, c2)) | |
143 | |
144 # Same package name, different paths. | |
145 c1 = pipeline.PackageTestingContext.from_path(package1, filters='a') | |
146 c2 = pipeline.PackageTestingContext.from_path(other_package1, | |
147 filters=('b','c')) | |
148 self.assertRaises(AssertionError, | |
149 pipeline.PackageTestingContext.from_context_list, | |
150 (c1, c2)) | |
151 | |
152 # Subpackage | |
153 subpackage_path = 'subpackage1_1' | |
154 subpackage1 = os.path.join(package1, subpackage_path) | |
155 c1 = pipeline.PackageTestingContext.from_path(subpackage1) | |
156 self.assertEqual(c1.package_name, 'package1') | |
157 self.assertEqual(c1.filters, [(subpackage_path, '*')]) | |
158 | |
159 c1 = pipeline.PackageTestingContext.from_path(subpackage1, filters='a*') | |
160 self.assertEqual(c1.package_name, 'package1') | |
161 self.assertEqual(c1.filters, [(subpackage_path, 'a*')]) | |
162 | |
163 for subpath, matcher in c1.itermatchers(): | |
164 print matcher.pattern | |
165 self.assertIsNotNone(matcher.match('a')) | |
166 self.assertIsNotNone(matcher.match('ab')) | |
167 self.assertIsNone(matcher.match('ba')) | |
168 self.assertIsNone(matcher.match('b')) | |
169 self.assertEqual(subpath, subpackage_path) | |
170 | |
171 # Test that a star is added to the filter. | |
172 c1 = pipeline.PackageTestingContext.from_path(subpackage1, filters='a') | |
173 self.assertEqual(c1.package_name, 'package1') | |
174 self.assertEqual(c1.filters, [(subpackage_path, 'a')]) | |
175 | |
176 for subpath, matcher in c1.itermatchers(): | |
177 print matcher.pattern | |
178 self.assertIsNotNone(matcher.match('a')) | |
179 self.assertIsNotNone(matcher.match('ab')) | |
180 self.assertIsNone(matcher.match('ba')) | |
181 self.assertIsNone(matcher.match('b')) | |
182 self.assertEqual(subpath, subpackage_path) | |
183 | |
184 | |
185 def test_processing_context(self): | |
186 """Test the ProcessingContext class""" | |
187 package_name = 'package1' | |
188 package1 = os.path.join(SCRIPT_DIR, 'data', package_name) | |
189 subpackage1 = os.path.join(SCRIPT_DIR, 'data', | |
190 package_name, 'subpackage1_1') | |
191 package2 = os.path.join(SCRIPT_DIR, 'data', 'package2') | |
192 other_package1 = os.path.join(SCRIPT_DIR, 'data', 'other', package_name) | |
193 | |
194 c0 = pipeline.PackageTestingContext.from_path(package1) | |
195 c1 = pipeline.PackageTestingContext.from_path(package1, filters='a') | |
196 c2 = pipeline.PackageTestingContext.from_path(subpackage1, filters='d') | |
197 c3 = pipeline.PackageTestingContext.from_path(package2, filters=('b','c')) | |
198 c4 = pipeline.PackageTestingContext.from_path(other_package1) | |
199 | |
200 # A processing context is a cwd + testing contexts. | |
201 # A testing context is cwd + one package name. | |
202 context = pipeline.ProcessingContext((c1, c2)) | |
203 self.assertEqual(len(context.testing_contexts), 1) | |
204 self.assertEqual(set(filt[1] | |
205 for filt in context.testing_contexts[0].filters), | |
206 set(('a', 'd'))) | |
207 | |
208 context = pipeline.ProcessingContext((c0, c1, c2)) | |
209 self.assertEqual(len(context.testing_contexts), 1) | |
210 self.assertEqual(set(filt[1] | |
211 for filt in context.testing_contexts[0].filters), | |
212 set(('*', 'a', 'd'))) | |
213 | |
214 | |
215 context = pipeline.ProcessingContext((c1, c2, c3)) | |
216 self.assertEqual(len(context.testing_contexts), 2) | |
217 | |
218 # Fails because there are two different cwd. | |
219 self.assertRaises(ValueError, pipeline.ProcessingContext, (c1, c4)) | |
220 | |
221 | |
222 class TestListingTest(unittest.TestCase): | |
223 """Test functions related to listing tests.""" | |
224 def test_walk_package(self): | |
225 """This function uses the directory structure under data/""" | |
226 modules = pipeline.walk_package('package1', DATA_DIR) | |
227 self.assertEqual(modules, | |
228 ['package1.file1_test', 'package1.file2_test', | |
229 'package1.subpackage1_1.file3_test', | |
230 'package1.subpackage1_1.subpackage1_1_1.file4_test']) | |
231 | |
232 modules = pipeline.walk_package('package1', DATA_DIR, | |
233 subpath='subpackage1_1') | |
234 self.assertEqual(modules, | |
235 ['package1.subpackage1_1.file3_test', | |
236 'package1.subpackage1_1.subpackage1_1_1.file4_test']) | |
237 | |
238 self.assertRaises(ValueError, pipeline.walk_package, | |
239 'package1', DATA_DIR, 'non-existing') | |
240 | |
241 def test_get_test_gens_package(self): | |
242 def get_test_names(tests): | |
243 test_names = [] | |
244 | |
245 for gen in tests: | |
246 for test in gen(): | |
247 if isinstance(test, MultiTest): | |
248 subtests = test.tests | |
249 else: | |
250 subtests = [test] | |
251 | |
252 for subtest in subtests: | |
253 self.assertIsInstance(subtest, Test) | |
254 test_names.append(subtest.name) | |
255 return test_names | |
256 | |
257 sys.path.insert(0, DATA_DIR) # Ugh. But won't work otherwise. | |
258 | |
259 | |
260 # TODO(pgervais): add a MultiTest in a package under data/ | |
261 package1 = os.path.join(DATA_DIR, 'package1') | |
262 testing_context = pipeline.PackageTestingContext.from_path(package1) | |
263 tests = pipeline.get_test_gens_package(testing_context) | |
264 self.assertEqual( | |
265 get_test_names(tests), | |
266 ['package1.file1_test.File1Test.test_trivial_1', | |
267 'package1.file2_test.File2Test.test_trivial_2', | |
268 'package1.subpackage1_1.file3_test.File3Test.test_trivial_3', | |
269 'package1.subpackage1_1.subpackage1_1_1.file4_test.' + | |
270 'File4Test.test_trivial_4']) | |
271 | |
272 tests = pipeline.get_test_gens_package( | |
273 testing_context, subpath='subpackage1_1') | |
274 self.assertEqual( | |
275 get_test_names(tests), | |
276 ['package1.subpackage1_1.file3_test.File3Test.test_trivial_3', | |
277 'package1.subpackage1_1.subpackage1_1_1.file4_test.' + | |
278 'File4Test.test_trivial_4']) | |
279 | |
280 tests = pipeline.get_test_gens_package( | |
281 testing_context, subpath='subpackage1_1/subpackage1_1_1') | |
282 | |
283 self.assertEqual( | |
284 get_test_names(tests), | |
285 ['package1.subpackage1_1.subpackage1_1_1.file4_test.' + | |
286 'File4Test.test_trivial_4']) | |
OLD | NEW |