Home | History | Annotate | Download | only in common
      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 
     29 import os.path
     30 import unittest
     31 
     32 from webkitpy.common.system.executive import Executive
     33 from webkitpy.common.prettypatch import PrettyPatch
     34 
     35 
     36 class PrettyPatchTest(unittest.TestCase):
     37     def check_ruby(self):
     38         executive = Executive()
     39         try:
     40             result = executive.run_command(['ruby', '--version'])
     41         except OSError, e:
     42             return False
     43         return True
     44 
     45     _diff_with_multiple_encodings = """
     46 Index: utf8_test
     47 ===================================================================
     48 --- utf8_test\t(revision 0)
     49 +++ utf8_test\t(revision 0)
     50 @@ -0,0 +1 @@
     51 +utf-8 test: \xc2\xa0
     52 Index: latin1_test
     53 ===================================================================
     54 --- latin1_test\t(revision 0)
     55 +++ latin1_test\t(revision 0)
     56 @@ -0,0 +1 @@
     57 +latin1 test: \xa0
     58 """
     59 
     60     def _webkit_root(self):
     61         webkitpy_common = os.path.dirname(__file__)
     62         webkitpy = os.path.dirname(webkitpy_common)
     63         scripts = os.path.dirname(webkitpy)
     64         webkit_tools = os.path.dirname(scripts)
     65         webkit_root = os.path.dirname(webkit_tools)
     66         return webkit_root
     67 
     68     def test_pretty_diff_encodings(self):
     69         if not self.check_ruby():
     70             return
     71 
     72         pretty_patch = PrettyPatch(Executive(), self._webkit_root())
     73         pretty = pretty_patch.pretty_diff(self._diff_with_multiple_encodings)
     74         self.assertTrue(pretty)  # We got some output
     75         self.assertTrue(isinstance(pretty, str))  # It's a byte array, not unicode
     76 
     77     def test_pretty_print_empty_string(self):
     78         if not self.check_ruby():
     79             return
     80 
     81         # Make sure that an empty diff does not hang the process.
     82         pretty_patch = PrettyPatch(Executive(), self._webkit_root())
     83         self.assertEqual(pretty_patch.pretty_diff(""), "")
     84