Changeset 112150 in webkit


Ignore:
Timestamp:
Mar 26, 2012 2:58:15 PM (12 years ago)
Author:
dpranke@chromium.org
Message:

add a Tree abstraction to test-webkitpy to better encapsulate things
https://bugs.webkit.org/show_bug.cgi?id=82158

Reviewed by Adam Barth.

There are no functional changes in this patch, but adding a
basic abstraction for the trees we are looking in for python
tests will allow me to add some features later on more easily
(like ignoring certain directories like webkitpy/thirdparty),
and it makes things slightly less hard-coded.

  • Scripts/test-webkitpy:
  • Scripts/webkitpy/test/main.py:

(Tester.init):
(Tester):
(Tester.add_tree):
(Tester._parse_args):
(Tester._configure):
(Tester.run):
(Tester._find_modules):
(Tester._run_tests):
(Tester._is_module):
(Tester._log_exception):
(TestDirectoryTree):
(TestDirectoryTree.init):
(TestDirectoryTree.find_modules):
(TestDirectoryTree.clean):

Location:
trunk/Tools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Tools/ChangeLog

    r112149 r112150  
     12012-03-26  Dirk Pranke  <dpranke@chromium.org>
     2
     3        add a Tree abstraction to test-webkitpy to better encapsulate things
     4        https://bugs.webkit.org/show_bug.cgi?id=82158
     5
     6        Reviewed by Adam Barth.
     7
     8        There are no functional changes in this patch, but adding a
     9        basic abstraction for the trees we are looking in for python
     10        tests will allow me to add some features later on more easily
     11        (like ignoring certain directories like webkitpy/thirdparty),
     12        and it makes things slightly less hard-coded.
     13
     14        * Scripts/test-webkitpy:
     15        * Scripts/webkitpy/test/main.py:
     16        (Tester.__init__):
     17        (Tester):
     18        (Tester.add_tree):
     19        (Tester._parse_args):
     20        (Tester._configure):
     21        (Tester.run):
     22        (Tester._find_modules):
     23        (Tester._run_tests):
     24        (Tester._is_module):
     25        (Tester._log_exception):
     26        (TestDirectoryTree):
     27        (TestDirectoryTree.__init__):
     28        (TestDirectoryTree.find_modules):
     29        (TestDirectoryTree.clean):
     30
    1312012-03-26  Ojan Vafai  <ojan@chromium.org>
    232
  • trunk/Tools/Scripts/test-webkitpy

    r106060 r112150  
    4545    webkit_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    4646
    47     # FIXME: We should probably test each package separately to avoid naming conflicts.
    48     dirs = [
    49         os.path.join(webkit_root, 'Tools', 'Scripts'),
    50         os.path.join(webkit_root, 'Source', 'WebKit2', 'Scripts'),
    51     ]
     47    tester = main.Tester()
     48    tester.add_tree(os.path.join(webkit_root, 'Tools', 'Scripts'), 'webkitpy')
     49    tester.add_tree(os.path.join(webkit_root, 'Source', 'WebKit2', 'Scripts'), 'webkit2')
    5250
    5351    # FIXME: Do we need to be able to test QueueStatusServer on Windows as well?
     
    6058        use_library('django', '1.2')
    6159        dev_appserver.fix_sys_path()
    62         dirs.append(os.path.join(webkit_root, 'Tools', 'QueueStatusServer'))
     60        tester.add_tree(os.path.join(webkit_root, 'Tools', 'QueueStatusServer'))
    6361    else:
    6462        _log.info('Skipping QueueStatusServer tests; the Google AppEngine Python SDK is not installed.')
    6563
    66     main.Tester.clean_packages(dirs)
    67 
    68     tester = main.Tester()
    69     options, args = tester.parse_args(sys.argv)
    70     tester.configure(options)
    71 
    72     # Make sure PYTHONPATH is set up correctly so that all of the imports will work.
    73     sys.path = [d for d in dirs if d not in sys.path] + sys.path
    74 
    75     sys.exit(not tester.run(dirs, args))
     64    sys.exit(not tester.run())
  • trunk/Tools/Scripts/webkitpy/test/main.py

    r108862 r112150  
    3737
    3838class Tester(object):
    39     @staticmethod
    40     def clean_packages(dirs):
    41         """Delete all .pyc files under dirs that have no .py file."""
    42         for dir_to_clean in dirs:
    43             _log.debug("Cleaning orphaned *.pyc files from: %s" % dir_to_clean)
    44             for dir_path, dir_names, file_names in os.walk(dir_to_clean):
    45                 for file_name in file_names:
    46                     if file_name.endswith(".pyc") and file_name[:-1] not in file_names:
    47                         file_path = os.path.join(dir_path, file_name)
    48                         _log.info("Deleting orphan *.pyc file: %s" % file_path)
    49                         os.remove(file_path)
    50 
    5139    def __init__(self):
    5240        self._verbosity = 1
    53 
    54     def parse_args(self, argv):
     41        self._trees = []
     42
     43    def add_tree(self, top_directory, starting_subdirectory=None):
     44        self._trees.append(TestDirectoryTree(top_directory, starting_subdirectory))
     45
     46    def _parse_args(self):
    5547        parser = optparse.OptionParser(usage='usage: %prog [options] [args...]')
    5648        parser.add_option('-a', '--all', action='store_true', default=False,
     
    7264                         'If no args are given, all the tests will be run.')
    7365
    74         self.progName = os.path.basename(argv[0])
    75         return parser.parse_args(argv[1:])
    76 
    77     def configure(self, options):
     66        return parser.parse_args()
     67
     68    def _configure(self, options):
    7869        self._options = options
    7970
     
    142133        handler.addFilter(testing_filter)
    143134
    144     def run(self, dirs, args):
    145         args = args or self._find_modules(dirs)
    146         return self._run_tests(dirs, args)
    147 
    148     def _find_modules(self, dirs):
     135    def run(self):
     136        options, args = self._parse_args()
     137        self._configure(options)
     138
     139        for tree in self._trees:
     140            tree.clean()
     141
     142        args = args or self._find_modules()
     143        return self._run_tests(args)
     144
     145    def _find_modules(self):
     146        suffixes = ['_unittest.py']
     147        if not self._options.skip_integrationtests:
     148            suffixes.append('_integrationtest.py')
     149
    149150        modules = []
    150         for dir_to_search in dirs:
    151             modules.extend(self._find_modules_under(dir_to_search, '_unittest.py'))
    152             if not self._options.skip_integrationtests:
    153                 modules.extend(self._find_modules_under(dir_to_search, '_integrationtest.py'))
     151        for tree in self._trees:
     152            modules.extend(tree.find_modules(suffixes))
    154153        modules.sort()
    155154
     
    182181        _log.info('')
    183182
    184     def _find_modules_under(self, dir_to_search, suffix):
    185 
    186         def to_package(dir_path):
    187             return dir_path.replace(dir_to_search + os.sep, '').replace(os.sep, '.')
    188 
    189         def to_module(filename, package):
    190             return package + '.' + filename.replace('.py', '')
    191 
    192         modules = []
    193         for dir_path, _, filenames in os.walk(dir_to_search):
    194             package = to_package(dir_path)
    195             modules.extend(to_module(f, package) for f in filenames if f.endswith(suffix))
    196         return modules
    197 
    198     def _run_tests(self, dirs, args):
     183    def _run_tests(self, args):
    199184        if self._options.coverage:
    200185            try:
     
    206191            cov.start()
    207192
     193        # Make sure PYTHONPATH is set up properly.
     194        sys.path = [tree.top_directory for tree in self._trees if tree.top_directory not in sys.path] + sys.path
     195
    208196        _log.debug("Loading the tests...")
    209197
     
    211199        suites = []
    212200        for name in args:
    213             if self._is_module(dirs, name):
     201            if self._is_module(name):
    214202                # import modules explicitly before loading their tests because
    215203                # loadTestsFromName() produces lousy error messages for bad modules.
     
    237225        return result.wasSuccessful()
    238226
    239     def _is_module(self, dirs, name):
     227    def _is_module(self, name):
    240228        relpath = name.replace('.', os.sep) + '.py'
    241         return any(os.path.exists(os.path.join(d, relpath)) for d in dirs)
     229        return any(os.path.exists(os.path.join(tree.top_directory, relpath)) for tree in self._trees)
    242230
    243231    def _log_exception(self):
     
    246234        for l in s.buflist:
    247235            _log.error('  ' + l.rstrip())
     236
     237
     238class TestDirectoryTree(object):
     239    def __init__(self, top_directory, starting_subdirectory):
     240        self.top_directory = os.path.realpath(top_directory)
     241        self.search_directory = self.top_directory
     242        self.top_package = ''
     243        if starting_subdirectory:
     244            self.top_package = starting_subdirectory.replace(os.sep, '.') + '.'
     245            self.search_directory = os.path.join(self.top_directory, starting_subdirectory)
     246
     247    def find_modules(self, suffixes):
     248        modules = []
     249        for dir_path, _, filenames in os.walk(self.search_directory):
     250            dir_path = os.path.join(dir_path, '')
     251            package = dir_path.replace(self.top_directory + os.sep, '').replace(os.sep, '.')
     252            for f in filenames:
     253                if any(f.endswith(suffix) for suffix in suffixes):
     254                    modules.append(package + f.replace('.py', ''))
     255        return modules
     256
     257    def clean(self):
     258        """Delete all .pyc files in the tree that have no matching .py file."""
     259        _log.debug("Cleaning orphaned *.pyc files from: %s" % self.search_directory)
     260        for dir_path, dir_names, file_names in os.walk(self.search_directory):
     261            for file_name in file_names:
     262                if file_name.endswith(".pyc") and file_name[:-1] not in file_names:
     263                    file_path = os.path.join(dir_path, file_name)
     264                    _log.info("Deleting orphan *.pyc file: %s" % file_path)
     265                    os.remove(file_path)
Note: See TracChangeset for help on using the changeset viewer.