Commit f5c00723 authored by shadi@chromium.org's avatar shadi@chromium.org

[Telemetry] discover.DiscoverClasses() does not add inheritted classes.

If a PageAction inherits another PageAction class, then DiscoverClasses() 
might mess up the map from simple module name to class object name. See 
bug for more details.

BUG=281568

Review URL: https://chromiumcodereview.appspot.com/23602015

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@220649 0039d316-1c4b-4281-b951-d872f2087c98
parent 357b18ca
......@@ -50,6 +50,8 @@ def DiscoverClasses(start_dir, top_level_dir, base_class, pattern='*',
index_by_class_name=False):
"""Discover all classes in |start_dir| which subclass |base_class|.
Base classes that contain subclasses are ignored by default.
Args:
start_dir: The directory to recursively search.
top_level_dir: The top level of the package, for importing.
......@@ -66,7 +68,8 @@ def DiscoverClasses(start_dir, top_level_dir, base_class, pattern='*',
for module in modules:
for _, obj in inspect.getmembers(module):
if (inspect.isclass(obj) and obj is not base_class and
issubclass(obj, base_class)):
issubclass(obj, base_class) and obj.__module__ == module.__name__
and len(obj.__subclasses__()) == 0):
if index_by_class_name:
key_name = camel_case.ToUnderscore(obj.__name__)
else:
......
......@@ -14,11 +14,10 @@ class DiscoverTest(unittest.TestCase):
base_class = Exception
classes = discover.DiscoverClasses(start_dir, base_dir, base_class)
self.assertTrue(len(classes) > 0)
found_dummy_exception = False
for c in classes.values():
if c.__name__ == 'DummyException':
found_dummy_exception = True
self.assertTrue(issubclass(c, Exception))
self.assertTrue(found_dummy_exception)
# discover_dummyclass is a base class and should not show up.
self.assertFalse('discover_dummyclass' in classes)
self.assertEqual(classes['another_discover_dummyclass'].__name__,
'AnotherDummyException')
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Another dummy exception subclasses used by core/discover.py's unit tests."""
# Import class instead of module explicitly so that inspect.getmembers() returns
# two Exception subclasses in this current file.
# Suppress complaints about unable to import class. The directory path is
# added at runtime by telemetry test runner.
#pylint: disable=F0401
from discoverable_classes.discover_dummyclass import DummyException
class AnotherDummyException(DummyException):
pass
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment