patchreader_unittest.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2010 Google Inc. All rights reserved.
  2. # Copyright (C) 2009 Torch Mobile Inc.
  3. # Copyright (C) 2009 Apple Inc. All rights reserved.
  4. # Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@gmail.com)
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. #
  10. # * Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # * Redistributions in binary form must reproduce the above
  13. # copyright notice, this list of conditions and the following disclaimer
  14. # in the documentation and/or other materials provided with the
  15. # distribution.
  16. # * Neither the name of Google Inc. nor the names of its
  17. # contributors may be used to endorse or promote products derived from
  18. # this software without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. import unittest2 as unittest
  32. from webkitpy.common.system.filesystem_mock import MockFileSystem
  33. from webkitpy.style.patchreader import PatchReader
  34. class PatchReaderTest(unittest.TestCase):
  35. """Test the PatchReader class."""
  36. class MockTextFileReader(object):
  37. def __init__(self):
  38. self.passed_to_process_file = []
  39. """A list of (file_path, line_numbers) pairs."""
  40. self.delete_only_file_count = 0
  41. """A number of times count_delete_only_file() called"""
  42. def process_file(self, file_path, line_numbers):
  43. self.passed_to_process_file.append((file_path, line_numbers))
  44. def count_delete_only_file(self):
  45. self.delete_only_file_count += 1
  46. def setUp(self):
  47. file_reader = self.MockTextFileReader()
  48. self._file_reader = file_reader
  49. self._patch_checker = PatchReader(file_reader)
  50. def _call_check_patch(self, patch_string):
  51. self._patch_checker.check(patch_string)
  52. def _assert_checked(self, passed_to_process_file, delete_only_file_count):
  53. self.assertEqual(self._file_reader.passed_to_process_file,
  54. passed_to_process_file)
  55. self.assertEqual(self._file_reader.delete_only_file_count,
  56. delete_only_file_count)
  57. def test_check_patch(self):
  58. # The modified line_numbers array for this patch is: [2].
  59. self._call_check_patch("""diff --git a/__init__.py b/__init__.py
  60. index ef65bee..e3db70e 100644
  61. --- a/__init__.py
  62. +++ b/__init__.py
  63. @@ -1,1 +1,2 @@
  64. # Required for Python to search this directory for module files
  65. +# New line
  66. """)
  67. self._assert_checked([("__init__.py", [2])], 0)
  68. def test_check_patch_with_deletion(self):
  69. self._call_check_patch("""Index: __init__.py
  70. ===================================================================
  71. --- __init__.py (revision 3593)
  72. +++ __init__.py (working copy)
  73. @@ -1 +0,0 @@
  74. -foobar
  75. """)
  76. # _mock_check_file should not be called for the deletion patch.
  77. self._assert_checked([], 1)
  78. def test_check_patch_with_png_deletion(self):
  79. fs = MockFileSystem()
  80. diff_text = """Index: LayoutTests/platform/mac/foo-expected.png
  81. ===================================================================
  82. Cannot display: file marked as a binary type.
  83. svn:mime-type = image/png
  84. """
  85. self._patch_checker.check(diff_text, fs)
  86. self._assert_checked([], 1)