Changeset 181014 in webkit


Ignore:
Timestamp:
Mar 4, 2015 1:02:33 PM (9 years ago)
Author:
youenn.fablet@crf.canon.fr
Message:

W3C test importer should use filesystem instead of os.walk
https://bugs.webkit.org/show_bug.cgi?id=142085

Reviewed by Bem Jones-Bey.

Added FileSystem.dirs_under to remove the use of os.walk in test importer.
Added MockFileSystem.dirs_under and MockFileSystem.getsize to enable unit testing of test importer.
Added unit test for FileSystem.dirs_under and MockFileSystem.dirs_under.

Made use of FileSystem.dirs_under within TestImporter.find_importable_tests.
Added a unit test to check that test importer is now black-box testable using a Mock system.

  • Scripts/webkitpy/common/system/filesystem.py:

(FileSystem.dirs_under): Returns a list of filtered sub-directories.
(FileSystem.dirs_under.filter_all):

  • Scripts/webkitpy/common/system/filesystem_mock.py:

(MockFileSystem.dirs_under):
(MockFileSystem.dirs_under.filter_all):
(MockFileSystem.getsize):

  • Scripts/webkitpy/common/system/filesystem_mock_unittest.py:

(MockFileSystemTest.test_dirs_under):
(MockFileSystemTest.test_dirs_under.filter_dir):

  • Scripts/webkitpy/common/system/filesystem_unittest.py:

(RealFileSystemTest.test_sep):
(RealFileSystemTest):
(RealFileSystemTest.test_dirs_under):
(RealFileSystemTest.test_dirs_under.filter_this_dir):

  • Scripts/webkitpy/w3c/test_importer.py:

(TestImporter.do_import):
(TestImporter.find_importable_tests.should_keep_subdir):
(TestImporter.find_importable_tests):
(TestImporter.should_keep_subdir): Deleted.
(TestImporter.should_skip_file): Deleted.

  • Scripts/webkitpy/w3c/test_importer_unittest.py:

(TestImporterTest.test_import_dir_with_empty_init_py): Added test to ensure empty init.py are no longer empty once imported.

Location:
trunk/Tools
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Tools/ChangeLog

    r181007 r181014  
     12015-03-04  Youenn Fablet  <youenn.fablet@crf.canon.fr>
     2
     3        W3C test importer should use filesystem instead of os.walk
     4        https://bugs.webkit.org/show_bug.cgi?id=142085
     5
     6        Reviewed by Bem Jones-Bey.
     7
     8        Added FileSystem.dirs_under to remove the use of os.walk in test importer.
     9        Added MockFileSystem.dirs_under and MockFileSystem.getsize to enable unit testing of test importer.
     10        Added unit test for FileSystem.dirs_under and MockFileSystem.dirs_under.
     11
     12        Made use of FileSystem.dirs_under within TestImporter.find_importable_tests.
     13        Added a unit test to check that test importer is now black-box testable using a Mock system.
     14
     15        * Scripts/webkitpy/common/system/filesystem.py:
     16        (FileSystem.dirs_under): Returns a list of filtered sub-directories.
     17        (FileSystem.dirs_under.filter_all):
     18        * Scripts/webkitpy/common/system/filesystem_mock.py:
     19        (MockFileSystem.dirs_under):
     20        (MockFileSystem.dirs_under.filter_all):
     21        (MockFileSystem.getsize):
     22        * Scripts/webkitpy/common/system/filesystem_mock_unittest.py:
     23        (MockFileSystemTest.test_dirs_under):
     24        (MockFileSystemTest.test_dirs_under.filter_dir):
     25        * Scripts/webkitpy/common/system/filesystem_unittest.py:
     26        (RealFileSystemTest.test_sep):
     27        (RealFileSystemTest):
     28        (RealFileSystemTest.test_dirs_under):
     29        (RealFileSystemTest.test_dirs_under.filter_this_dir):
     30        * Scripts/webkitpy/w3c/test_importer.py:
     31        (TestImporter.do_import):
     32        (TestImporter.find_importable_tests.should_keep_subdir):
     33        (TestImporter.find_importable_tests):
     34        (TestImporter.should_keep_subdir): Deleted.
     35        (TestImporter.should_skip_file): Deleted.
     36        * Scripts/webkitpy/w3c/test_importer_unittest.py:
     37        (TestImporterTest.test_import_dir_with_empty_init_py): Added test to ensure empty __init__.py are no longer empty once imported.
     38
    1392015-03-04  Timothy Horton  <timothy_horton@apple.com>
    240
  • trunk/Tools/Scripts/webkitpy/common/system/filesystem.py

    r179545 r181014  
    7878        return os.path.exists(path)
    7979
     80    def dirs_under(self, path, dir_filter=None):
     81        """Return the list of all directories under the given path in topdown order.
     82
     83        Args:
     84            dir_filter: if not None, the filter will be invoked
     85                with the filesystem object and the path of each dirfound.
     86                The dir is included in the result if the callback returns True.
     87        """
     88        def filter_all(fs, dirpath):
     89            return True
     90        dir_filter = dir_filter or filter_all
     91
     92        dirs = []
     93        for (dirpath, dirnames, filenames) in os.walk(path):
     94            if dir_filter(self, dirpath):
     95                dirs.append(dirpath)
     96        return dirs
     97
    8098    def files_under(self, path, dirs_to_skip=[], file_filter=None):
    8199        """Return the list of all files under the given path in topdown order.
  • trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock.py

    r146443 r181014  
    124124        return self.isfile(path) or self.isdir(path)
    125125
     126    def dirs_under(self, path, dirs_filter=None):
     127        def filter_all(fs, dirpath):
     128            return True
     129
     130        dirs_filter = dirs_filter or filter_all
     131
     132        dirs = []
     133        for dirpath in self.dirs:
     134            if not dirpath.startswith(path):
     135                continue
     136            if dirs_filter(self, dirpath):
     137                dirs.append(dirpath)
     138        return sorted(dirs)
     139
     140
    126141    def files_under(self, path, dirs_to_skip=[], file_filter=None):
    127142        def filter_all(fs, dirpath, basename):
     
    158173    def getcwd(self):
    159174        return self.cwd
     175
     176    def getsize(self, path):
     177        if not self.isfile(path):
     178            raise OSError("%s is not a file" % path)
     179        return len(self.files[path])
    160180
    161181    def glob(self, glob_string):
  • trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock_unittest.py

    r174136 r181014  
    8383                         'foo/../bar/baz',
    8484                         '../foo')
     85
     86    def test_dirs_under(self):
     87        FAKE_FILES = {
     88            '/tests/test1.txt': '',
     89            '/tests/test3/test2/test.txt': 'test',
     90            '/tests/test2/test.txt': 'test'}
     91        fs = filesystem_mock.MockFileSystem(files=FAKE_FILES)
     92
     93        self.assertEquals(fs.dirs_under('/tests'), ['/tests', '/tests/test2', '/tests/test3', '/tests/test3/test2'])
     94
     95        def filter_dir(fs, dirpath):
     96            return fs.basename(dirpath) != 'test2'
     97
     98        self.assertEquals(fs.dirs_under('/tests', filter_dir), ['/tests', '/tests/test3'])
  • trunk/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py

    r174136 r181014  
    265265        self.assertEqual(fs.join("foo", "bar"),
    266266                          os.path.join("foo", "bar"))
     267
     268    def test_dirs_under(self):
     269        fs = FileSystem()
     270        parentDir = fs.normpath(fs.join(self._this_dir, ".."))
     271
     272        self.assertTrue(self._this_dir in fs.dirs_under(parentDir))
     273
     274        def filter_no_dir(fs, dirpath):
     275            return False
     276        self.assertEqual(len(fs.dirs_under(parentDir, filter_no_dir)), 0)
     277
     278        def filter_this_dir(fs, dirpath):
     279            return dirpath != self._this_dir
     280        self.assertFalse(self._this_dir in fs.dirs_under(parentDir, filter_this_dir))
  • trunk/Tools/Scripts/webkitpy/w3c/test_importer.py

    r180844 r181014  
    162162        self.import_tests()
    163163
    164     def should_keep_subdir(self, root, subdir):
    165         DIRS_TO_SKIP = ('work-in-progress', 'tools', 'support')
    166         should_skip = subdir.startswith('.') or (root == self.source_directory and subdir in DIRS_TO_SKIP)
    167         return not should_skip
    168 
    169164    def should_skip_file(self, filename):
    170165        # For some reason the w3c repo contains random perl scripts we don't care about.
     
    176171
    177172    def find_importable_tests(self, directory):
    178         # FIXME: use filesystem
    179         for root, dirs, files in os.walk(directory):
     173        def should_keep_subdir(filesystem, path):
     174            subdir = path[len(directory):]
     175            DIRS_TO_SKIP = ('work-in-progress', 'tools', 'support')
     176            should_skip = filesystem.basename(subdir).startswith('.') or (subdir in DIRS_TO_SKIP)
     177            return not should_skip
     178
     179        directories = self.filesystem.dirs_under(directory, should_keep_subdir)
     180        for root in directories:
    180181            _log.info('Scanning ' + root + '...')
    181182            total_tests = 0
     
    183184            jstests = 0
    184185
    185             dirs[:] = [subdir for subdir in dirs if self.should_keep_subdir(root, subdir)]
    186 
    187186            copy_list = []
    188187
    189             for filename in files:
     188            for filename in self.filesystem.listdir(root):
     189                if self.filesystem.isdir(self.filesystem.join(root, filename)):
     190                    continue
    190191                # FIXME: This block should really be a separate function, but the early-continues make that difficult.
    191192
  • trunk/Tools/Scripts/webkitpy/w3c/test_importer_unittest.py

    r174136 r181014  
    7575            oc.restore_output()
    7676
     77    def test_import_dir_with_empty_init_py(self):
     78        FAKE_FILES = {
     79            '/tests/csswg/test1/__init__.py': '',
     80            '/tests/csswg/test2/__init__.py': 'NOTEMPTY',
     81        }
     82
     83        host = MockHost()
     84        host.filesystem = MockFileSystem(files=FAKE_FILES)
     85
     86        importer = TestImporter(host, FAKE_SOURCE_DIR, optparse.Values({"overwrite": False, 'destination': 'w3c', 'test_paths': ['/tests/csswg']}))
     87        importer.do_import()
     88
     89        self.assertTrue(host.filesystem.exists("/mock-checkout/LayoutTests/w3c/test1/__init__.py"))
     90        self.assertTrue(host.filesystem.exists("/mock-checkout/LayoutTests/w3c/test2/__init__.py"))
     91        self.assertTrue(host.filesystem.getsize("/mock-checkout/LayoutTests/w3c/test1/__init__.py") > 0)
     92
    7793    # FIXME: Needs more tests.
Note: See TracChangeset for help on using the changeset viewer.