Changeset 77431 in webkit


Ignore:
Timestamp:
Feb 2, 2011 4:33:29 PM (13 years ago)
Author:
dpranke@chromium.org
Message:

2011-02-02 Dirk Pranke <dpranke@chromium.org>

Reviewed by Tony Chang.

new-run-webkit-tests: support chromium code paths in mock_drt

https://bugs.webkit.org/show_bug.cgi?id=53471

  • Scripts/webkitpy/layout_tests/port/mock_drt.py:
  • Scripts/webkitpy/layout_tests/port/mock_drt_unittest.py:
Location:
trunk/Tools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Tools/ChangeLog

    r77415 r77431  
     12011-02-02  Dirk Pranke  <dpranke@chromium.org>
     2
     3        Reviewed by Tony Chang.
     4
     5        new-run-webkit-tests: support chromium code paths in mock_drt
     6
     7        https://bugs.webkit.org/show_bug.cgi?id=53471
     8
     9        * Scripts/webkitpy/layout_tests/port/mock_drt.py:
     10        * Scripts/webkitpy/layout_tests/port/mock_drt_unittest.py:
     11
    1122011-02-02  Adam Klein  <adamk@chromium.org>
    213
  • trunk/Tools/Scripts/webkitpy/layout_tests/port/mock_drt.py

    r76982 r77431  
    3838import sys
    3939
     40from webkitpy.common.system import filesystem
     41
    4042from webkitpy.layout_tests.port import base
    4143from webkitpy.layout_tests.port import factory
     
    8284        # in place of the actual path to the driver binary.
    8385
    84         # FIXME: This doesn't yet work for Chromium test_shell ports.
    8586        def overriding_cmd_line():
    8687            cmd = self.__original_driver_cmd_line()
     
    114115
    115116
    116 def main(argv, stdin, stdout, stderr):
     117def main(argv, fs, stdin, stdout, stderr):
    117118    """Run the tests."""
    118119
    119120    options, args = parse_options(argv)
    120     drt = MockDRT(options, args, stdin, stdout, stderr)
     121    if options.chromium:
     122        drt = MockChromiumDRT(options, args, fs, stdin, stdout, stderr)
     123    else:
     124        drt = MockDRT(options, args, fs, stdin, stdout, stderr)
    121125    return drt.run()
    122126
    123127
    124128def parse_options(argv):
    125     # FIXME: We need to figure out how to handle variants that have
    126     # different command-line conventions.
    127     option_list = [
    128         optparse.make_option('--platform', action='store',
    129                              help='platform to emulate'),
    130         optparse.make_option('--layout-tests', action='store_true',
    131                              default=True, help='run layout tests'),
    132         optparse.make_option('--pixel-tests', action='store_true',
    133                              default=False,
    134                              help='output image for pixel tests'),
    135     ]
    136     option_parser = optparse.OptionParser(option_list=option_list)
    137     return option_parser.parse_args(argv)
     129    # FIXME: We have to do custom arg parsing instead of using the optparse
     130    # module.  First, Chromium and non-Chromium DRTs have a different argument
     131    # syntax.  Chromium uses --pixel-tests=<path>, and non-Chromium uses
     132    # --pixel-tests as a boolean flag. Second, we don't want to have to list
     133    # every command line flag DRT accepts, but optparse complains about
     134    # unrecognized flags. At some point it might be good to share a common
     135    # DRT options class between this file and webkit.py and chromium.py
     136    # just to get better type checking.
     137    platform_index = argv.index('--platform')
     138    platform = argv[platform_index + 1]
     139
     140    pixel_tests = False
     141    pixel_path = None
     142    chromium = False
     143    if platform.startswith('chromium'):
     144        chromium = True
     145        for arg in argv:
     146            if arg.startswith('--pixel-tests'):
     147                pixel_tests = True
     148                pixel_path = arg[len('--pixel-tests='):]
     149    else:
     150        pixel_tests = '--pixel-tests' in argv
     151    options = base.DummyOptions(chromium=chromium,
     152                                platform=platform,
     153                                pixel_tests=pixel_tests,
     154                                pixel_path=pixel_path)
     155    return (options, [])
     156
     157
     158# FIXME: Should probably change this to use DriverInput after
     159# https://bugs.webkit.org/show_bug.cgi?id=53004 lands.
     160class _DRTInput(object):
     161    def __init__(self, line):
     162        vals = line.strip().split("'")
     163        if len(vals) == 1:
     164            self.uri = vals[0]
     165            self.checksum = None
     166        else:
     167            self.uri = vals[0]
     168            self.checksum = vals[1]
    138169
    139170
    140171class MockDRT(object):
    141     def __init__(self, options, args, stdin, stdout, stderr):
     172    def __init__(self, options, args, filesystem, stdin, stdout, stderr):
    142173        self._options = options
    143174        self._args = args
     175        self._filesystem = filesystem
    144176        self._stdout = stdout
    145177        self._stdin = stdin
     
    149181        if options.platform:
    150182            port_name = options.platform
    151         self._port = factory.get(port_name, options=options)
     183        self._port = factory.get(port_name, options=options, filesystem=filesystem)
    152184
    153185    def run(self):
     
    156188            if not line:
    157189                break
    158 
    159             url, expected_checksum = self.parse_input(line)
    160             self.run_one_test(url, expected_checksum)
     190            self.run_one_test(self.parse_input(line))
    161191        return 0
    162192
    163193    def parse_input(self, line):
    164         line = line.strip()
    165         if "'" in line:
    166             return line.split("'", 1)
    167         return (line, None)
    168 
    169     def raw_bytes(self, unicode_str):
    170         return unicode_str.encode('utf-8')
    171 
    172     def run_one_test(self, url, expected_checksum):
     194        return _DRTInput(line)
     195
     196    def run_one_test(self, test_input):
    173197        port = self._port
    174         if url.startswith('http'):
    175             test_name = port.uri_to_test_name(url)
    176             test_path = port._filesystem.join(port.layout_tests_dir(), test_name)
     198        if test_input.uri.startswith('http'):
     199            test_name = port.uri_to_test_name(test_input.uri)
     200            test_path = self._filesystem.join(port.layout_tests_dir(), test_name)
    177201        else:
    178             test_path = url
    179 
    180         actual_text_bytes = self.raw_bytes(port.expected_text(test_path))
    181         if self._options.pixel_tests and expected_checksum:
    182             actual_checksum_bytes = self.raw_bytes(port.expected_checksum(test_path))
    183             actual_image_bytes = port.expected_image(test_path)
     202            test_path = test_input.uri
     203
     204        actual_text = port.expected_text(test_path)
     205        if self._options.pixel_tests and test_input.checksum:
     206            actual_checksum = port.expected_checksum(test_path)
     207            actual_image = port.expected_image(test_path)
    184208
    185209        self._stdout.write('Content-Type: text/plain\n')
    186         self._stdout.write(actual_text_bytes)
     210
     211        # FIXME: Note that we don't ensure there is a trailing newline!
     212        # This mirrors actual (Mac) DRT behavior but is a bug.
     213        self._stdout.write(actual_text)
    187214        self._stdout.write('#EOF\n')
    188215
    189         if self._options.pixel_tests and expected_checksum:
    190             expected_checksum_bytes = self.raw_bytes(expected_checksum)
     216        if self._options.pixel_tests and test_input.checksum:
    191217            self._stdout.write('\n')
    192             self._stdout.write('ActualHash: %s\n' % actual_checksum_bytes)
    193             self._stdout.write('ExpectedHash: %s\n' % expected_checksum_bytes)
    194             if actual_checksum_bytes != expected_checksum_bytes:
     218            self._stdout.write('ActualHash: %s\n' % actual_checksum)
     219            self._stdout.write('ExpectedHash: %s\n' % test_input.checksum)
     220            if actual_checksum != test_input.checksum:
    195221                self._stdout.write('Content-Type: image/png\n')
    196                 self._stdout.write('Content-Length: %s\n\n' % len(actual_image_bytes))
    197                 self._stdout.write(actual_image_bytes)
     222                self._stdout.write('Content-Length: %s\n\n' % len(actual_image))
     223                self._stdout.write(actual_image)
    198224        self._stdout.write('#EOF\n')
    199225        self._stdout.flush()
     
    201227
    202228
     229# FIXME: Should probably change this to use DriverInput after
     230# https://bugs.webkit.org/show_bug.cgi?id=53004 lands.
     231class _ChromiumDRTInput(_DRTInput):
     232    def __init__(self, line):
     233        vals = line.strip().split()
     234        if len(vals) == 3:
     235            self.uri, self.timeout, self.checksum = vals
     236        else:
     237            self.uri = vals[0]
     238            self.timeout = vals[1]
     239            self.checksum = None
     240
     241
     242class MockChromiumDRT(MockDRT):
     243    def parse_input(self, line):
     244        return _ChromiumDRTInput(line)
     245
     246    def run_one_test(self, test_input):
     247        port = self._port
     248        test_name = self._port.uri_to_test_name(test_input.uri)
     249        test_path = self._filesystem.join(port.layout_tests_dir(), test_name)
     250
     251        actual_text = port.expected_text(test_path)
     252        actual_image = ''
     253        actual_checksum = ''
     254        if self._options.pixel_tests and test_input.checksum:
     255            actual_checksum = port.expected_checksum(test_path)
     256            if actual_checksum != test_input.checksum:
     257                actual_image = port.expected_image(test_path)
     258
     259        self._stdout.write("#URL:%s\n" % test_input.uri)
     260        if self._options.pixel_tests and test_input.checksum:
     261            self._stdout.write("#MD5:%s\n" % actual_checksum)
     262            self._filesystem.write_binary_file(self._options.pixel_path,
     263                                               actual_image)
     264        self._stdout.write(actual_text)
     265
     266        # FIXME: (See above FIXME as well). Chromium DRT appears to always
     267        # ensure the text output has a trailing newline. Mac DRT does not.
     268        if not actual_text.endswith('\n'):
     269            self._stdout.write('\n')
     270        self._stdout.write('#EOF\n')
     271        self._stdout.flush()
     272
     273
     274
    203275if __name__ == '__main__':
    204     sys.exit(main(sys.argv[1:], sys.stdin, sys.stdout, sys.stderr))
     276    fs = filesystem.FileSystem()
     277    sys.exit(main(sys.argv[1:], fs, sys.stdin, sys.stdout, sys.stderr))
  • trunk/Tools/Scripts/webkitpy/layout_tests/port/mock_drt_unittest.py

    r77023 r77431  
    3737from webkitpy.layout_tests.port import factory
    3838from webkitpy.layout_tests.port import port_testcase
     39from webkitpy.layout_tests.port import test
    3940
    4041
     
    8889
    8990class MockDRTTest(unittest.TestCase):
    90     def setUp(self):
    91         self._port = factory.get('test')
    92         self._layout_tests_dir = self._port.layout_tests_dir()
    93 
    94     def to_path(self, test_name):
    95         return self._port._filesystem.join(self._layout_tests_dir, test_name)
    96 
    97     def input_line(self, test_name, checksum=None):
    98         url = self._port.filename_to_uri(self.to_path(test_name))
     91    def to_path(self, port, test_name):
     92        return port._filesystem.join(port.layout_tests_dir(), test_name)
     93
     94    def input_line(self, port, test_name, checksum=None):
     95        url = port.filename_to_uri(self.to_path(port, test_name))
    9996        # FIXME: we shouldn't have to work around platform-specific issues
    10097        # here.
     
    108105        return url + '\n'
    109106
    110     def make_drt(self, input_string, extra_args=None):
    111         args = ['--platform', 'test', '-']
    112         extra_args = extra_args or []
    113         args += extra_args
    114         stdin = newstringio.StringIO(input_string)
     107    def extra_args(self, pixel_tests):
     108        if pixel_tests:
     109            return ['--pixel-tests', '-']
     110        return ['-']
     111
     112    def make_drt(self, options, args, filesystem, stdin, stdout, stderr):
     113        return mock_drt.MockDRT(options, args, filesystem, stdin, stdout, stderr)
     114
     115    def make_input_output(self, port, test_name, pixel_tests,
     116                          expected_checksum, drt_output, drt_input=None):
     117        path = self.to_path(port, test_name)
     118        if pixel_tests:
     119            if not expected_checksum:
     120                expected_checksum = port.expected_checksum(path)
     121        if not drt_input:
     122            drt_input = self.input_line(port, test_name, expected_checksum)
     123        text_output = port.expected_text(path)
     124
     125        if not drt_output:
     126            drt_output = self.expected_output(port, test_name, pixel_tests,
     127                                              text_output, expected_checksum)
     128        return (drt_input, drt_output)
     129
     130    def expected_output(self, port, test_name, pixel_tests, text_output, expected_checksum):
     131        if pixel_tests and expected_checksum:
     132            return ['Content-Type: text/plain\n',
     133                    text_output,
     134                    '#EOF\n',
     135                    '\n',
     136                    'ActualHash: %s\n' % expected_checksum,
     137                    'ExpectedHash: %s\n' % expected_checksum,
     138                    '#EOF\n']
     139        else:
     140            return ['Content-Type: text/plain\n',
     141                    text_output,
     142                    '#EOF\n',
     143                    '#EOF\n']
     144
     145    def assertTest(self, test_name, pixel_tests, expected_checksum=None,
     146                   drt_output=None, filesystem=None):
     147        platform = 'test'
     148        filesystem = filesystem or test.unit_test_filesystem()
     149        port = factory.get(platform, filesystem=filesystem)
     150        drt_input, drt_output = self.make_input_output(port, test_name,
     151            pixel_tests, expected_checksum, drt_output)
     152
     153        args = ['--platform', 'test'] + self.extra_args(pixel_tests)
     154        stdin = newstringio.StringIO(drt_input)
    115155        stdout = newstringio.StringIO()
    116156        stderr = newstringio.StringIO()
    117157        options, args = mock_drt.parse_options(args)
    118         drt = mock_drt.MockDRT(options, args, stdin, stdout, stderr)
    119         return (drt, stdout, stderr)
    120 
    121     def make_input_output(self, test_name, pixel_tests, expected_checksum,
    122                           drt_output, drt_input=None):
    123         path = self.to_path(test_name)
    124         if pixel_tests:
    125             if not expected_checksum:
    126                 expected_checksum = self._port.expected_checksum(path)
    127         if not drt_input:
    128             drt_input = self.input_line(test_name, expected_checksum)
    129         text_output = self._port.expected_text(path)
    130 
    131         if not drt_output:
    132             if pixel_tests:
    133                 drt_output = [
    134                     'Content-Type: text/plain\n',
    135                     text_output.encode('utf-8'),
    136                     '#EOF\n',
    137                     '\n',
    138                     'ActualHash: %s\n' % expected_checksum.encode('utf-8'),
    139                     'ExpectedHash: %s\n' % expected_checksum.encode('utf-8'),
    140                     '#EOF\n']
    141             else:
    142                 drt_output = [
    143                     'Content-Type: text/plain\n',
    144                     text_output.encode('utf-8'),
    145                     '#EOF\n',
    146                     '#EOF\n']
    147 
    148         return (drt_input, drt_output)
    149 
    150     def assertTest(self, test_name, pixel_tests, expected_checksum=None, drt_output=None):
    151         drt_input, drt_output = self.make_input_output(test_name, pixel_tests,
    152             expected_checksum, drt_output)
    153         extra_args = []
    154         if pixel_tests:
    155             extra_args = ['--pixel-tests']
    156         drt, stdout, stderr = self.make_drt(drt_input, extra_args)
     158
     159        drt = self.make_drt(options, args, filesystem, stdin, stdout, stderr)
    157160        res = drt.run()
     161
    158162        self.assertEqual(res, 0)
    159163
     
    164168
    165169    def test_main(self):
     170        filesystem = test.unit_test_filesystem()
    166171        stdin = newstringio.StringIO()
    167172        stdout = newstringio.StringIO()
    168173        stderr = newstringio.StringIO()
    169         res = mock_drt.main(['--platform', 'test', '-'], stdin, stdout, stderr)
     174        res = mock_drt.main(['--platform', 'test'] + self.extra_args(False),
     175                            filesystem, stdin, stdout, stderr)
    170176        self.assertEqual(res, 0)
    171177        self.assertEqual(stdout.getvalue(), '')
    172178        self.assertEqual(stderr.getvalue(), '')
     179        self.assertEqual(filesystem.written_files, {})
    173180
    174181    def test_pixeltest_passes(self):
     
    194201
    195202
     203class MockChromiumDRTTest(MockDRTTest):
     204    def extra_args(self, pixel_tests):
     205        if pixel_tests:
     206            return ['--pixel-tests=/tmp/png_result0.png']
     207        return []
     208
     209    def make_drt(self, options, args, filesystem, stdin, stdout, stderr):
     210        options.chromium = True
     211
     212        # We have to set these by hand because --platform test won't trigger
     213        # the Chromium code paths.
     214        options.pixel_path = '/tmp/png_result0.png'
     215        options.pixel_tests = True
     216
     217        return mock_drt.MockChromiumDRT(options, args, filesystem, stdin, stdout, stderr)
     218
     219    def input_line(self, port, test_name, checksum=None):
     220        url = port.filename_to_uri(self.to_path(port, test_name))
     221        if checksum:
     222            return url + ' 6000 ' + checksum + '\n'
     223        return url + ' 6000\n'
     224
     225    def expected_output(self, port, test_name, pixel_tests, text_output, expected_checksum):
     226        url = port.filename_to_uri(self.to_path(port, test_name))
     227        if pixel_tests and expected_checksum:
     228            return ['#URL:%s\n' % url,
     229                    '#MD5:%s\n' % expected_checksum,
     230                    text_output,
     231                    '\n',
     232                    '#EOF\n']
     233        else:
     234            return ['#URL:%s\n' % url,
     235                    text_output,
     236                    '\n',
     237                    '#EOF\n']
     238
     239    def test_pixeltest__fails(self):
     240        filesystem = test.unit_test_filesystem()
     241        self.assertTest('failures/expected/checksum.html', pixel_tests=True,
     242            expected_checksum='wrong-checksum',
     243            drt_output=['#URL:file:///test.checkout/LayoutTests/failures/expected/checksum.html\n',
     244                        '#MD5:checksum-checksum\n',
     245                        'checksum-txt',
     246                        '\n',
     247                        '#EOF\n'],
     248            filesystem=filesystem)
     249        self.assertEquals(filesystem.written_files,
     250            {'/tmp/png_result0.png': 'checksum\x8a-png'})
     251
     252    def test_chromium_parse_options(self):
     253        options, args = mock_drt.parse_options(['--platform', 'chromium-mac',
     254            '--pixel-tests=/tmp/png_result0.png'])
     255        self.assertTrue(options.chromium)
     256        self.assertTrue(options.pixel_tests)
     257        self.assertEquals(options.pixel_path, '/tmp/png_result0.png')
     258
     259
    196260if __name__ == '__main__':
    197261    unittest.main()
Note: See TracChangeset for help on using the changeset viewer.