prettypatch_unittest.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (C) 2010 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. import os.path
  29. import sys
  30. import unittest2 as unittest
  31. from webkitpy.common.system.executive import Executive
  32. from webkitpy.common.prettypatch import PrettyPatch
  33. class PrettyPatchTest(unittest.TestCase):
  34. def check_ruby(self):
  35. executive = Executive()
  36. try:
  37. result = executive.run_command(['ruby', '--version'])
  38. except OSError, e:
  39. return False
  40. return True
  41. _diff_with_multiple_encodings = """
  42. Index: utf8_test
  43. ===================================================================
  44. --- utf8_test\t(revision 0)
  45. +++ utf8_test\t(revision 0)
  46. @@ -0,0 +1 @@
  47. +utf-8 test: \xc2\xa0
  48. Index: latin1_test
  49. ===================================================================
  50. --- latin1_test\t(revision 0)
  51. +++ latin1_test\t(revision 0)
  52. @@ -0,0 +1 @@
  53. +latin1 test: \xa0
  54. """
  55. def _webkit_root(self):
  56. webkitpy_common = os.path.dirname(__file__)
  57. webkitpy = os.path.dirname(webkitpy_common)
  58. scripts = os.path.dirname(webkitpy)
  59. webkit_tools = os.path.dirname(scripts)
  60. webkit_root = os.path.dirname(webkit_tools)
  61. return webkit_root
  62. def test_pretty_diff_encodings(self):
  63. if not self.check_ruby():
  64. return
  65. if sys.platform == 'win32':
  66. # FIXME: disabled due to https://bugs.webkit.org/show_bug.cgi?id=93192
  67. return
  68. pretty_patch = PrettyPatch(Executive(), self._webkit_root())
  69. pretty = pretty_patch.pretty_diff(self._diff_with_multiple_encodings)
  70. self.assertTrue(pretty) # We got some output
  71. self.assertIsInstance(pretty, str) # It's a byte array, not unicode
  72. def test_pretty_print_empty_string(self):
  73. if not self.check_ruby():
  74. return
  75. # Make sure that an empty diff does not hang the process.
  76. pretty_patch = PrettyPatch(Executive(), self._webkit_root())
  77. self.assertEqual(pretty_patch.pretty_diff(""), "")