Exemplo n.º 1
0
class PatchReaderTest(unittest.TestCase):

    """Test the PatchReader class."""

    class MockTextFileReader(object):

        def __init__(self):
            self.passed_to_process_file = []
            """A list of (file_path, line_numbers) pairs."""
            self.delete_only_file_count = 0
            """A number of times count_delete_only_file() called"""

        def process_file(self, file_path, line_numbers):
            self.passed_to_process_file.append((file_path, line_numbers))

        def count_delete_only_file(self):
            self.delete_only_file_count += 1

    def setUp(self):
        file_reader = self.MockTextFileReader()
        self._file_reader = file_reader
        self._patch_checker = PatchReader(file_reader)

    def _call_check_patch(self, patch_string):
        self._patch_checker.check(patch_string)

    def _assert_checked(self, passed_to_process_file, delete_only_file_count):
        self.assertEquals(self._file_reader.passed_to_process_file,
                          passed_to_process_file)
        self.assertEquals(self._file_reader.delete_only_file_count,
                          delete_only_file_count)

    def test_check_patch(self):
        # The modified line_numbers array for this patch is: [2].
        self._call_check_patch("""diff --git a/__init__.py b/__init__.py
index ef65bee..e3db70e 100644
--- a/__init__.py
+++ b/__init__.py
@@ -1,1 +1,2 @@
 # Required for Python to search this directory for module files
+# New line
""")
        self._assert_checked([("__init__.py", set([2]))], 0)

    def test_check_patch_with_deletion(self):
        self._call_check_patch("""Index: __init__.py
===================================================================
--- __init__.py  (revision 3593)
+++ __init__.py  (working copy)
@@ -1 +0,0 @@
-foobar
""")
        # _mock_check_file should not be called for the deletion patch.
        self._assert_checked([], 1)
Exemplo n.º 2
0
class PatchReaderTest(unittest.TestCase):
    """Test the PatchReader class."""
    class MockTextFileReader(object):
        def __init__(self):
            self.passed_to_process_file = []
            """A list of (file_path, line_numbers) pairs."""
            self.delete_only_file_count = 0
            """A number of times count_delete_only_file() called"""

        def process_file(self, file_path, line_numbers):
            self.passed_to_process_file.append((file_path, line_numbers))

        def count_delete_only_file(self):
            self.delete_only_file_count += 1

    def setUp(self):
        file_reader = self.MockTextFileReader()
        self._file_reader = file_reader
        self._patch_checker = PatchReader(file_reader)

    def _call_check_patch(self, patch_string):
        self._patch_checker.check(patch_string)

    def _assert_checked(self, passed_to_process_file, delete_only_file_count):
        self.assertEquals(self._file_reader.passed_to_process_file,
                          passed_to_process_file)
        self.assertEquals(self._file_reader.delete_only_file_count,
                          delete_only_file_count)

    def test_check_patch(self):
        # The modified line_numbers array for this patch is: [2].
        self._call_check_patch("""diff --git a/__init__.py b/__init__.py
index ef65bee..e3db70e 100644
--- a/__init__.py
+++ b/__init__.py
@@ -1,1 +1,2 @@
 # Required for Python to search this directory for module files
+# New line
""")
        self._assert_checked([("__init__.py", set([2]))], 0)

    def test_check_patch_with_deletion(self):
        self._call_check_patch("""Index: __init__.py
===================================================================
--- __init__.py  (revision 3593)
+++ __init__.py  (working copy)
@@ -1 +0,0 @@
-foobar
""")
        # _mock_check_file should not be called for the deletion patch.
        self._assert_checked([], 1)
Exemplo n.º 3
0
    def main(self):
        args = sys.argv[1:]

        host = Host()
        host.initialize_scm()

        stderr = self._engage_awesome_stderr_hacks()

        # Checking for the verbose flag before calling check_webkit_style_parser()
        # lets us enable verbose logging earlier.
        is_verbose = "-v" in args or "--verbose" in args

        checker.configure_logging(stream=stderr, is_verbose=is_verbose)
        _log.debug("Verbose logging enabled.")

        parser = checker.check_webkit_style_parser()
        (paths, options) = parser.parse(args)

        configuration = checker.check_webkit_style_configuration(options)

        paths = change_directory(host.filesystem,
                                 checkout_root=host.scm().checkout_root,
                                 paths=paths)

        style_processor = StyleProcessor(configuration)
        file_reader = TextFileReader(host.filesystem, style_processor)

        if paths and not options.diff_files:
            file_reader.process_paths(paths)
            file_reader.do_association_check(host.scm().checkout_root)
        else:
            changed_files = paths if options.diff_files else None
            patch = host.scm().create_patch(options.git_commit,
                                            changed_files=changed_files,
                                            git_index=options.git_index)
            patch_checker = PatchReader(file_reader)
            patch_checker.check(patch)

        error_count = style_processor.error_count
        file_count = file_reader.file_count
        delete_only_file_count = file_reader.delete_only_file_count

        _log.info("Total errors found: %d in %d files" %
                  (error_count, file_count))
        # We fail when style errors are found or there are no checked files.
        return error_count > 0 or (file_count == 0
                                   and delete_only_file_count == 0)
Exemplo n.º 4
0
 def test_check_patch_with_png_deletion(self):
     PatchReader(self._file_reader).check(
         'diff --git a/foo-expected.png b/foo-expected.png\n'
         'deleted file mode 100644\n'
         'index ef65bee..0000000\n'
         'Binary files a/foo-expected.png and /dev/null differ\n')
     self._assert_checked(passed_to_process_file=[],
                          delete_only_file_count=1)
Exemplo n.º 5
0
 def test_check_patch(self):
     PatchReader(self._file_reader).check(
         'diff --git a/__init__.py b/__init__.py\n'
         'index ef65bee..e3db70e 100644\n'
         '--- a/__init__.py\n'
         '+++ b/__init__.py\n'
         '@@ -1,1 +1,2 @@\n'
         ' # Required for Python to search this directory for module files\n'
         '+# New line\n')
     self._assert_checked(passed_to_process_file=[('__init__.py', [2])],
                          delete_only_file_count=0)
Exemplo n.º 6
0
 def test_check_patch_with_deletion(self):
     PatchReader(self._file_reader).check(
         'diff --git a/__init__.py b/__init.py\n'
         'deleted file mode 100644\n'
         'index ef65bee..0000000\n'
         '--- a/__init__.py\n'
         '+++ /dev/null\n'
         '@@ -1 +0,0 @@\n'
         '-foobar\n')
     # The deleted file isn't be processed.
     self._assert_checked(passed_to_process_file=[],
                          delete_only_file_count=1)
Exemplo n.º 7
0
    def main(self):
        args = sys.argv[1:]

        host = Host()
        host.initialize_scm()

        stderr = self._engage_awesome_stderr_hacks()

        # Checking for the verbose flag before calling check_webkit_style_parser()
        # lets us enable verbose logging earlier.
        is_verbose = "-v" in args or "--verbose" in args

        checker.configure_logging(stream=stderr, is_verbose=is_verbose)
        _log.debug("Verbose logging enabled.")

        parser = checker.check_webkit_style_parser()
        (paths, options) = parser.parse(args)

        configuration = checker.check_webkit_style_configuration(options)

        paths = change_directory(host.filesystem, checkout_root=host.scm().checkout_root, paths=paths)

        style_processor = StyleProcessor(configuration)
        file_reader = TextFileReader(host.filesystem, style_processor)

        if paths and not options.diff_files:
            file_reader.process_paths(paths)
        else:
            changed_files = paths if options.diff_files else None
            patch = host.scm().create_patch(options.git_commit, changed_files=changed_files)
            patch_checker = PatchReader(file_reader)
            patch_checker.check(patch)

        error_count = style_processor.error_count
        file_count = file_reader.file_count
        delete_only_file_count = file_reader.delete_only_file_count

        _log.info("Total errors found: %d in %d files" % (error_count, file_count))
        # We fail when style errors are found or there are no checked files.
        return error_count > 0
Exemplo n.º 8
0
 def setUp(self):
     file_reader = self.MockTextFileReader()
     self._file_reader = file_reader
     self._patch_checker = PatchReader(file_reader)
 def setUp(self):
     file_reader = self.MockTextFileReader()
     self._file_reader = file_reader
     self._patch_checker = PatchReader(file_reader)
Exemplo n.º 10
0
class PatchReaderTest(unittest.TestCase):
    """Test the PatchReader class."""
    class MockTextFileReader(object):
        def __init__(self):
            self.passed_to_process_file = []
            """A list of (file_path, line_numbers) pairs."""
            self.delete_only_file_count = 0
            """A number of times delete_file() called"""

        def process_file(self, file_path, line_numbers):
            self.passed_to_process_file.append((file_path, line_numbers))

        def delete_file(self, file_path=None):
            self.delete_only_file_count += 1

        def do_association_check(self, cwd):
            pass

    def setUp(self):
        file_reader = self.MockTextFileReader()
        self._file_reader = file_reader
        self._patch_checker = PatchReader(file_reader)

    def _call_check_patch(self, patch_string):
        self._patch_checker.check(patch_string)

    def _assert_checked(self, passed_to_process_file, delete_only_file_count):
        self.assertEqual(self._file_reader.passed_to_process_file,
                         passed_to_process_file)
        self.assertEqual(self._file_reader.delete_only_file_count,
                         delete_only_file_count)

    def test_check_patch(self):
        # The modified line_numbers array for this patch is: [2].
        self._call_check_patch("""diff --git a/__init__.py b/__init__.py
index ef65bee..e3db70e 100644
--- a/__init__.py
+++ b/__init__.py
@@ -1,1 +1,2 @@
 # Required for Python to search this directory for module files
+# New line
""")
        self._assert_checked([("__init__.py", [2])], 0)

    def test_check_patch_with_deletion(self):
        self._call_check_patch("""Index: __init__.py
===================================================================
--- __init__.py  (revision 3593)
+++ __init__.py  (working copy)
@@ -1 +0,0 @@
-foobar
""")
        # _mock_check_file should not be called for the deletion patch.
        self._assert_checked([], 1)

    def test_check_patch_with_png_deletion(self):
        fs = MockFileSystem()
        diff_text = """Index: LayoutTests/platform/mac/foo-expected.png
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = image/png
"""
        self._patch_checker.check(diff_text, fs)
        self._assert_checked([], 1)
Exemplo n.º 11
0
    def run_style_tests(self, suite_args):

        # TODO: not yet ready
        return True

        # Code based on http://svn.webkit.org/repository/webkit/trunk/Tools/Scripts/check-webkit-style

        thisdir = os.path.dirname(os.path.abspath(__file__))

        args = suite_args
        if not args:
            args = [os.path.abspath(
                os.path.join(thisdir, os.pardir, os.pardir, os.pardir)
            )]
        _log = log

        sys.path.append(os.path.join(thisdir, os.pardir, 'third_party'))

        from webkitpy.style_references import detect_checkout
        import webkitpy.style.checker as checker
        from webkitpy.style.patchreader import PatchReader
        from webkitpy.style.checker import StyleProcessor
        from webkitpy.style.filereader import TextFileReader
        from webkitpy.style.main import change_directory

        # Change stderr to write with replacement characters so we don't die
        # if we try to print something containing non-ASCII characters.
        stderr = codecs.StreamReaderWriter(sys.stderr,
                                           codecs.getreader('utf8'),
                                           codecs.getwriter('utf8'),
                                           'replace')
        # Setting an "encoding" attribute on the stream is necessary to
        # prevent the logging module from raising an error.  See
        # the checker.configure_logging() function for more information.
        stderr.encoding = "UTF-8"

        # FIXME: Change webkitpy.style so that we do not need to overwrite
        #        the global sys.stderr.  This involves updating the code to
        #        accept a stream parameter where necessary, and not calling
        #        sys.stderr explicitly anywhere.
        sys.stderr = stderr

        #SYNTH args = sys.argv[1:]

        # Checking for the verbose flag before calling check_webkit_style_parser()
        # lets us enable verbose logging earlier.
        is_verbose = "-v" in args or "--verbose" in args

        checker.configure_logging(stream=stderr, is_verbose=is_verbose)
        _log.debug("Verbose logging enabled.")

        parser = checker.check_webkit_style_parser()
        (paths, options) = parser.parse(args)

        checkout = detect_checkout()

        if checkout is None:
            if not paths:
                _log.error("WebKit checkout not found: You must run this script "
                           "from within a WebKit checkout if you are not passing "
                           "specific paths to check.")
                sys.exit(1)

            checkout_root = None
            _log.debug("WebKit checkout not found for current directory.")
        else:
            checkout_root = checkout.root_path()
            _log.debug("WebKit checkout found with root: %s" % checkout_root)

        configuration = checker.check_webkit_style_configuration(options)

        paths = change_directory(checkout_root=checkout_root, paths=paths)

        style_processor = StyleProcessor(configuration)

        file_reader = TextFileReader(style_processor)

        if paths and not options.diff_files:
            file_reader.process_paths(paths)
        else:
            changed_files = paths if options.diff_files else None
            patch = checkout.create_patch(options.git_commit, changed_files=changed_files)
            patch_checker = PatchReader(file_reader)
            patch_checker.check(patch)

        error_count = style_processor.error_count
        file_count = file_reader.file_count
        delete_only_file_count = file_reader.delete_only_file_count

        _log.info("Total errors found: %d in %d files"
                  % (error_count, file_count))
        # We fail when style errors are found or there are no checked files.
        #sys.exit(error_count > 0 or (file_count == 0 and delete_only_file_count == 0))
        return not (error_count > 0 or (file_count == 0 and delete_only_file_count == 0))