OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import os |
| 6 import sys |
| 7 import unittest |
| 8 |
| 9 from expect_tests import pipeline |
| 10 from expect_tests import listing |
| 11 from expect_tests.type_definitions import Test, MultiTest |
| 12 |
| 13 SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 14 DATA_DIR = os.path.join(SCRIPT_DIR, 'data') |
| 15 |
| 16 class ParsingAndConfigTest(unittest.TestCase): |
| 17 def test_get_config(self): |
| 18 """Testing that reading the config file works. |
| 19 |
| 20 Tests requires a specific content for data/.expect_tests.cfg""" |
| 21 black_list = listing.get_config(DATA_DIR) |
| 22 self.assertEqual(black_list, |
| 23 set(['directory1', 'directory2', 'ignored'])) |
| 24 |
| 25 def test_parse_test_glob(self): |
| 26 self.assertEqual(listing.parse_test_glob('a/b/c'), |
| 27 (os.path.abspath('a/b/c'), ('*',))) |
| 28 self.assertEqual(listing.parse_test_glob('a/b/c:'), |
| 29 (os.path.abspath('a/b/c'), ('*',))) |
| 30 self.assertEqual(listing.parse_test_glob('a/b/c:Test'), |
| 31 (os.path.abspath('a/b/c'), 'Test')) |
| 32 self.assertEqual(listing.parse_test_glob('a/b/c:Test.Name'), |
| 33 (os.path.abspath('a/b/c'), 'Test.Name')) |
| 34 self.assertRaises(ValueError, listing.parse_test_glob, 'a:b:c',) |
| 35 self.assertRaises(ValueError, listing.parse_test_glob, 'a:b/c',) |
| 36 |
| 37 |
| 38 class PathManipulationTest(unittest.TestCase): |
| 39 """Tests for all path-manipulating functions. |
| 40 |
| 41 This set uses checked-out files in the present repository to avoid mocking |
| 42 the I/O functions. |
| 43 """ |
| 44 |
| 45 def test_get_python_root(self): |
| 46 """This function uses the directory structure under data/""" |
| 47 |
| 48 cases = [ |
| 49 # The root of a directory with no __init__.py file is that directory |
| 50 (DATA_DIR, DATA_DIR), |
| 51 # The root of a package is the parent directory |
| 52 (os.path.join(DATA_DIR, 'package1'), DATA_DIR), |
| 53 # The root of a subpackage is the parent directory of the root package. |
| 54 (os.path.join(DATA_DIR, 'package1', 'subpackage1_1'), DATA_DIR) |
| 55 ] |
| 56 for path, result in cases: |
| 57 self.assertEqual(listing.get_python_root(path), result) |
| 58 |
| 59 # When the path does not exist, you get an error. |
| 60 self.assertRaises(ValueError, listing.get_python_root, |
| 61 '____non-existing-path____') |
| 62 |
| 63 def test_single_dir_runtime_context(self): |
| 64 """Computing RuntimeContext from a single directory path.""" |
| 65 test_globs = [DATA_DIR] |
| 66 contexts = listing.get_runtime_contexts(test_globs) |
| 67 self.assertEqual(len(contexts), 1) |
| 68 self.assertEqual(contexts[0].cwd, DATA_DIR) |
| 69 for testing_c in contexts[0].testing_contexts: |
| 70 self.assertNotEqual(testing_c.package_name, 'ignored') |
| 71 |
| 72 def test_single_package_runtime_context(self): |
| 73 """Computing RuntimeContext from a single package path.""" |
| 74 test_globs = [os.path.join(DATA_DIR, 'package1')] |
| 75 contexts = listing.get_runtime_contexts(test_globs) |
| 76 self.assertEqual(len(contexts), 1) |
| 77 self.assertEqual(contexts[0].cwd, DATA_DIR) |
| 78 testing_c = contexts[0].testing_contexts |
| 79 self.assertEqual(len(testing_c), 1) |
| 80 self.assertEqual(testing_c[0].package_name, 'package1') |
| 81 |
| 82 def test_two_packages_runtime_context(self): |
| 83 """Computing RuntimeContext from two package paths.""" |
| 84 test_globs = [os.path.join(DATA_DIR, 'package1'), |
| 85 os.path.join(DATA_DIR, 'package2')] |
| 86 contexts = listing.get_runtime_contexts(test_globs) |
| 87 self.assertEqual(len(contexts), 1) |
| 88 self.assertEqual(contexts[0].cwd, DATA_DIR) |
| 89 testing_c = contexts[0].testing_contexts |
| 90 self.assertEqual(len(testing_c), 2) |
| 91 package_names = set() |
| 92 for testing_c in contexts[0].testing_contexts: |
| 93 package_names.add(testing_c.package_name) |
| 94 |
| 95 self.assertEqual(package_names, set(('package1', 'package2'))) |
| 96 |
| 97 def test_package_and_directory_runtime_context(self): |
| 98 """Computing RuntimeContext from a package and a directory paths. |
| 99 """ |
| 100 # 'package1' is specified both explicit and implicitly through DATA_DIR |
| 101 # We check that it is accounted for only once. |
| 102 test_globs = [DATA_DIR, os.path.join(DATA_DIR, 'package1')] |
| 103 contexts = listing.get_runtime_contexts(test_globs) |
| 104 self.assertEqual(len(contexts), 1) |
| 105 # 2 is the number of packages under DATA_DIR, not counting |
| 106 # the ignored ones. |
| 107 self.assertEqual(len(contexts[0].testing_contexts), 2) |
| 108 |
| 109 test_globs = [DATA_DIR, os.path.join(DATA_DIR, 'package1'), |
| 110 os.path.join(DATA_DIR, 'package1', 'subpackage1_1')] |
| 111 contexts = listing.get_runtime_contexts(test_globs) |
| 112 self.assertEqual(len(contexts), 1) |
| 113 self.assertEqual(len(contexts[0].testing_contexts), 2) |
| 114 |
| 115 def test_package_testing_context_from_path(self): |
| 116 """Test the PackageTestingContext class""" |
| 117 package_name = 'package1' |
| 118 package1 = os.path.join(SCRIPT_DIR, 'data', package_name) |
| 119 |
| 120 context = listing.PackageTestingContext.from_path(package1) |
| 121 self.assertTrue(os.path.isabs(context.cwd)) |
| 122 self.assertTrue(len(context.package_name) > 0) |
| 123 self.assertEqual(len(context.filters), 1) |
| 124 self.assertEqual(context.filters[0][1], '*') |
| 125 |
| 126 # Testing with an empty tuple. |
| 127 context = listing.PackageTestingContext.from_path(package1, |
| 128 filters=()) |
| 129 self.assertTrue(os.path.isabs(context.cwd)) |
| 130 self.assertTrue(len(context.package_name) > 0) |
| 131 self.assertEqual(len(context.filters), 1) |
| 132 self.assertEqual(context.filters[0][1], '*') |
| 133 |
| 134 context = listing.PackageTestingContext.from_path(package1, |
| 135 filters=('a*', 'b*')) |
| 136 self.assertTrue(os.path.isabs(context.cwd)) |
| 137 self.assertTrue(len(context.package_name) > 0) |
| 138 self.assertEqual(len(context.filters), 2) |
| 139 self.assertEqual(context.filters[0][1], 'a*') |
| 140 self.assertEqual(context.filters[1][1], 'b*') |
| 141 |
| 142 self.assertRaises(ValueError, |
| 143 listing.PackageTestingContext.from_path, |
| 144 package1, filters=None) |
| 145 |
| 146 def test_merging_package_testing_context(self): |
| 147 """Merging PackageTestingContexts pointing at the same package. |
| 148 """ |
| 149 package_name = 'package1' |
| 150 package1 = os.path.join(SCRIPT_DIR, 'data', package_name) |
| 151 package2 = os.path.join(SCRIPT_DIR, 'data', 'package2') |
| 152 other_package1 = os.path.join(SCRIPT_DIR, 'data', 'other', package_name) |
| 153 |
| 154 # from_context_list |
| 155 c1 = listing.PackageTestingContext.from_path(package1, filters=('a',)) |
| 156 c2 = listing.PackageTestingContext.from_path(package1, |
| 157 filters=('b','c')) |
| 158 context = listing.PackageTestingContext.from_context_list((c1, c2)) |
| 159 self.assertEqual(len(context.filters), 3) |
| 160 self.assertEqual(set(filt[1] for filt in context.filters), |
| 161 set(('a', 'b', 'c'))) |
| 162 |
| 163 c1 = listing.PackageTestingContext.from_path(package1, filters=('a',)) |
| 164 c2 = listing.PackageTestingContext.from_path(package2, |
| 165 filters=('b','c')) |
| 166 self.assertRaises(ValueError, |
| 167 listing.PackageTestingContext.from_context_list, |
| 168 (c1, c2)) |
| 169 |
| 170 # Same package name, different paths. |
| 171 c1 = listing.PackageTestingContext.from_path(package1, filters=('a',)) |
| 172 c2 = listing.PackageTestingContext.from_path(other_package1, |
| 173 filters=('b','c')) |
| 174 self.assertRaises(ValueError, |
| 175 listing.PackageTestingContext.from_context_list, |
| 176 (c1, c2)) |
| 177 |
| 178 # Subpackage |
| 179 subpackage_path = 'subpackage1_1' |
| 180 subpackage1 = os.path.join(package1, subpackage_path) |
| 181 c1 = listing.PackageTestingContext.from_path(subpackage1) |
| 182 self.assertEqual(c1.package_name, 'package1') |
| 183 self.assertEqual(c1.filters, [(subpackage_path, '*')]) |
| 184 |
| 185 def test_filter_glob_manipulation(self): |
| 186 """globs to filter tests are modified if they don't end with a *.""" |
| 187 package_name = 'package1' |
| 188 package1 = os.path.join(SCRIPT_DIR, 'data', package_name) |
| 189 subpackage_path = 'subpackage1_1' |
| 190 subpackage1 = os.path.join(package1, subpackage_path) |
| 191 c1 = listing.PackageTestingContext.from_path(subpackage1, |
| 192 filters=('a*',)) |
| 193 self.assertEqual(c1.package_name, 'package1') |
| 194 self.assertEqual(c1.filters, [(subpackage_path, 'a*')]) |
| 195 |
| 196 for subpath, matcher in c1.itermatchers(): |
| 197 print matcher.pattern |
| 198 self.assertIsNotNone(matcher.match('a')) |
| 199 self.assertIsNotNone(matcher.match('ab')) |
| 200 self.assertIsNone(matcher.match('ba')) |
| 201 self.assertIsNone(matcher.match('b')) |
| 202 self.assertEqual(subpath, subpackage_path) |
| 203 |
| 204 # Test that a star is added to the filter. |
| 205 c1 = listing.PackageTestingContext.from_path(subpackage1, |
| 206 filters=('a',)) |
| 207 self.assertEqual(c1.package_name, 'package1') |
| 208 self.assertEqual(c1.filters, [(subpackage_path, 'a')]) |
| 209 |
| 210 for subpath, matcher in c1.itermatchers(): |
| 211 print matcher.pattern |
| 212 self.assertIsNotNone(matcher.match('a')) |
| 213 self.assertIsNotNone(matcher.match('ab')) |
| 214 self.assertIsNone(matcher.match('ba')) |
| 215 self.assertIsNone(matcher.match('b')) |
| 216 self.assertEqual(subpath, subpackage_path) |
| 217 |
| 218 |
| 219 def test_processing_context(self): |
| 220 """Test the ProcessingContext class""" |
| 221 package_name = 'package1' |
| 222 package1 = os.path.join(SCRIPT_DIR, 'data', package_name) |
| 223 subpackage1 = os.path.join(SCRIPT_DIR, 'data', |
| 224 package_name, 'subpackage1_1') |
| 225 package2 = os.path.join(SCRIPT_DIR, 'data', 'package2') |
| 226 other_package1 = os.path.join(SCRIPT_DIR, 'data', 'other', package_name) |
| 227 |
| 228 c0 = listing.PackageTestingContext.from_path(package1) |
| 229 c1 = listing.PackageTestingContext.from_path(package1, filters=('a',)) |
| 230 c2 = listing.PackageTestingContext.from_path(subpackage1, |
| 231 filters=('d',)) |
| 232 c3 = listing.PackageTestingContext.from_path(package2, |
| 233 filters=('b','c')) |
| 234 c4 = listing.PackageTestingContext.from_path(other_package1) |
| 235 |
| 236 # A processing context is a cwd + testing contexts. |
| 237 # A testing context is cwd + one package name. |
| 238 context = listing.ProcessingContext((c1, c2)) |
| 239 self.assertEqual(len(context.testing_contexts), 1) |
| 240 self.assertEqual(set(filt[1] |
| 241 for filt in context.testing_contexts[0].filters), |
| 242 set(('a', 'd'))) |
| 243 |
| 244 context = listing.ProcessingContext((c0, c1, c2)) |
| 245 self.assertEqual(len(context.testing_contexts), 1) |
| 246 self.assertEqual(set(filt[1] |
| 247 for filt in context.testing_contexts[0].filters), |
| 248 set(('*', 'a', 'd'))) |
| 249 |
| 250 |
| 251 context = listing.ProcessingContext((c1, c2, c3)) |
| 252 self.assertEqual(len(context.testing_contexts), 2) |
| 253 |
| 254 # Fails because there are two different cwd. |
| 255 self.assertRaises(ValueError, listing.ProcessingContext, (c1, c4)) |
| 256 |
| 257 |
| 258 class TestListingTest(unittest.TestCase): |
| 259 """Test functions related to listing tests.""" |
| 260 def test_walk_package(self): |
| 261 """This function uses the directory structure under data/""" |
| 262 modules = pipeline.walk_package('package1', DATA_DIR) |
| 263 self.assertEqual(modules, |
| 264 ['package1.file1_test', 'package1.file2_test', |
| 265 'package1.subpackage1_1.file3_test', |
| 266 'package1.subpackage1_1.subpackage1_1_1.file4_test']) |
| 267 |
| 268 modules = pipeline.walk_package('package1', DATA_DIR, |
| 269 subpath='subpackage1_1') |
| 270 self.assertEqual(modules, |
| 271 ['package1.subpackage1_1.file3_test', |
| 272 'package1.subpackage1_1.subpackage1_1_1.file4_test']) |
| 273 |
| 274 self.assertRaises(ValueError, pipeline.walk_package, |
| 275 'package1', DATA_DIR, 'non-existing') |
| 276 |
| 277 def test_get_test_gens_package(self): |
| 278 def get_test_names(tests): |
| 279 test_names = [] |
| 280 |
| 281 for gen in tests: |
| 282 for test in gen(): |
| 283 if isinstance(test, MultiTest): |
| 284 subtests = test.tests |
| 285 else: |
| 286 subtests = [test] |
| 287 |
| 288 for subtest in subtests: |
| 289 self.assertIsInstance(subtest, Test) |
| 290 test_names.append(subtest.name) |
| 291 return test_names |
| 292 |
| 293 sys.path.insert(0, DATA_DIR) # Ugh. But won't work otherwise. |
| 294 |
| 295 |
| 296 # TODO(pgervais): add a MultiTest in a package under data/ |
| 297 package1 = os.path.join(DATA_DIR, 'package1') |
| 298 testing_context = listing.PackageTestingContext.from_path(package1) |
| 299 tests = pipeline.get_test_gens_package(testing_context) |
| 300 self.assertEqual( |
| 301 get_test_names(tests), |
| 302 ['package1.file1_test.File1Test.test_trivial_1', |
| 303 'package1.file2_test.File2Test.test_trivial_2', |
| 304 'package1.subpackage1_1.file3_test.File3Test.test_trivial_3', |
| 305 'package1.subpackage1_1.subpackage1_1_1.file4_test.' + |
| 306 'File4Test.test_trivial_4']) |
| 307 |
| 308 tests = pipeline.get_test_gens_package( |
| 309 testing_context, subpath='subpackage1_1') |
| 310 self.assertEqual( |
| 311 get_test_names(tests), |
| 312 ['package1.subpackage1_1.file3_test.File3Test.test_trivial_3', |
| 313 'package1.subpackage1_1.subpackage1_1_1.file4_test.' + |
| 314 'File4Test.test_trivial_4']) |
| 315 |
| 316 tests = pipeline.get_test_gens_package( |
| 317 testing_context, subpath='subpackage1_1/subpackage1_1_1') |
| 318 |
| 319 self.assertEqual( |
| 320 get_test_names(tests), |
| 321 ['package1.subpackage1_1.subpackage1_1_1.file4_test.' + |
| 322 'File4Test.test_trivial_4']) |
OLD | NEW |