class MockDRT(object): def __init__(self, options, args, host, stdin, stdout, stderr): self._options = options self._args = args self._host = host self._stdout = stdout self._stdin = stdin self._stderr = stderr port_name = None if options.platform: port_name = options.platform self._port = PortFactory(host).get(port_name=port_name, options=options) self._driver = self._port.create_driver(0) def run(self): while True: line = self._stdin.readline() if not line: return 0 driver_input = self.input_from_line(line) dirname, basename = self._port.split_test(driver_input.test_name) is_reftest = (self._port.reference_files(driver_input.test_name) or self._port.is_reference_html_file(self._port._filesystem, dirname, basename)) output = self.output_for_test(driver_input, is_reftest) self.write_test_output(driver_input, output, is_reftest) def input_from_line(self, line): vals = line.strip().split("'") if len(vals) == 1: uri = vals[0] checksum = None else: uri = vals[0] checksum = vals[1] if uri.startswith('http://') or uri.startswith('https://'): test_name = self._driver.uri_to_test(uri) else: test_name = self._port.relative_test_filename(uri) return DriverInput(test_name, 0, checksum, self._options.pixel_tests) def output_for_test(self, test_input, is_reftest): port = self._port actual_text = port.expected_text(test_input.test_name) actual_audio = port.expected_audio(test_input.test_name) actual_image = None actual_checksum = None if is_reftest: # Make up some output for reftests. actual_text = 'reference text\n' actual_checksum = 'mock-checksum' actual_image = 'blank' if test_input.test_name.endswith('-mismatch.html'): actual_text = 'not reference text\n' actual_checksum = 'not-mock-checksum' actual_image = 'not blank' elif self._options.pixel_tests and test_input.image_hash: actual_checksum = port.expected_checksum(test_input.test_name) actual_image = port.expected_image(test_input.test_name) return DriverOutput(actual_text, actual_image, actual_checksum, actual_audio) def write_test_output(self, test_input, output, is_reftest): if output.audio: self._stdout.write('Content-Type: audio/wav\n') self._stdout.write('Content-Transfer-Encoding: base64\n') self._stdout.write(base64.b64encode(output.audio)) else: self._stdout.write('Content-Type: text/plain\n') # FIXME: Note that we don't ensure there is a trailing newline! # This mirrors actual (Mac) DRT behavior but is a bug. if output.text: self._stdout.write(output.text) self._stdout.write('#EOF\n') if self._options.pixel_tests and output.image_hash: self._stdout.write('\n') self._stdout.write('ActualHash: %s\n' % output.image_hash) self._stdout.write('ExpectedHash: %s\n' % test_input.image_hash) if output.image_hash != test_input.image_hash: self._stdout.write('Content-Type: image/png\n') self._stdout.write('Content-Length: %s\n' % len(output.image)) self._stdout.write(output.image) self._stdout.write('#EOF\n') self._stdout.flush() self._stderr.write('#EOF\n') self._stderr.flush()
class MockDRT(object): def __init__(self, options, args, host, stdin, stdout, stderr): self._options = options self._args = args self._host = host self._stdout = stdout self._stdin = stdin self._stderr = stderr port_name = None if options.platform: port_name = options.platform self._port = PortFactory(host).get(port_name=port_name, options=options) self._driver = self._port.create_driver(0) def run(self): while True: line = self._stdin.readline() if not line: return 0 driver_input = self.input_from_line(line) dirname, basename = self._port.split_test(driver_input.test_name) is_reftest = (self._port.reference_files(driver_input.test_name) or self._port.is_reference_html_file(self._port._filesystem, dirname, basename)) output = self.output_for_test(driver_input, is_reftest) self.write_test_output(driver_input, output, is_reftest) def input_from_line(self, line): vals = line.strip().split("'") uri = vals[0] checksum = None should_run_pixel_tests = False if len(vals) == 2 and vals[1] == '--pixel-test': should_run_pixel_tests = True elif len(vals) == 3 and vals[1] == '--pixel-test': should_run_pixel_tests = True checksum = vals[2] elif len(vals) != 1: raise NotImplementedError if uri.startswith('http://') or uri.startswith('https://'): test_name = self._driver.uri_to_test(uri) else: test_name = self._port.relative_test_filename(uri) return DriverInput(test_name, 0, checksum, should_run_pixel_tests) def output_for_test(self, test_input, is_reftest): port = self._port if self._options.virtual_test_suite_name: test_input.test_name = test_input.test_name.replace(self._options.virtual_test_suite_base, self._options.virtual_test_suite_name) actual_text = port.expected_text(test_input.test_name) actual_audio = port.expected_audio(test_input.test_name) actual_image = None actual_checksum = None if is_reftest: # Make up some output for reftests. actual_text = 'reference text\n' actual_checksum = 'mock-checksum' actual_image = 'blank' if test_input.test_name.endswith('-mismatch.html'): actual_text = 'not reference text\n' actual_checksum = 'not-mock-checksum' actual_image = 'not blank' elif test_input.should_run_pixel_test and test_input.image_hash: actual_checksum = port.expected_checksum(test_input.test_name) actual_image = port.expected_image(test_input.test_name) if self._options.actual_directory: actual_path = port._filesystem.join(self._options.actual_directory, test_input.test_name) root, _ = port._filesystem.splitext(actual_path) text_path = root + '-actual.txt' if port._filesystem.exists(text_path): actual_text = port._filesystem.read_binary_file(text_path) audio_path = root + '-actual.wav' if port._filesystem.exists(audio_path): actual_audio = port._filesystem.read_binary_file(audio_path) image_path = root + '-actual.png' if port._filesystem.exists(image_path): actual_image = port._filesystem.read_binary_file(image_path) with port._filesystem.open_binary_file_for_reading(image_path) as filehandle: actual_checksum = read_checksum_from_png.read_checksum(filehandle) return DriverOutput(actual_text, actual_image, actual_checksum, actual_audio) def write_test_output(self, test_input, output, is_reftest): if output.audio: self._stdout.write('Content-Type: audio/wav\n') self._stdout.write('Content-Transfer-Encoding: base64\n') self._stdout.write(base64.b64encode(output.audio)) self._stdout.write('\n') else: self._stdout.write('Content-Type: text/plain\n') # FIXME: Note that we don't ensure there is a trailing newline! # This mirrors actual (Mac) DRT behavior but is a bug. if output.text: self._stdout.write(output.text) self._stdout.write('#EOF\n') if test_input.should_run_pixel_test and output.image_hash: self._stdout.write('\n') self._stdout.write('ActualHash: %s\n' % output.image_hash) self._stdout.write('ExpectedHash: %s\n' % test_input.image_hash) if output.image_hash != test_input.image_hash: self._stdout.write('Content-Type: image/png\n') self._stdout.write('Content-Length: %s\n' % len(output.image)) self._stdout.write(output.image) self._stdout.write('#EOF\n') self._stdout.flush() self._stderr.write('#EOF\n') self._stderr.flush()
class MockDRTPort(object): """MockPort implementation of the Port interface.""" def __init__(self, host, **kwargs): prefix = 'mock-' if 'port_name' in kwargs: kwargs['port_name'] = kwargs['port_name'][len(prefix):] self._host = host self.__delegate = PortFactory(host).get(**kwargs) self.__real_name = prefix + self.__delegate.name() def real_name(self): return self.__real_name def __getattr__(self, name): return getattr(self.__delegate, name) def check_build(self, needs_http): return True def check_sys_deps(self, needs_http): return True def driver_cmd_line(self): driver = self.create_driver(0) return driver.cmd_line() def _path_to_driver(self): return self._host.filesystem.abspath(__file__) def create_driver(self, worker_number): # We need to create a driver object as the delegate would, but # overwrite the path to the driver binary in its command line. We do # this by actually overwriting its cmd_line() method with a proxy # method that splices in the mock_drt path and command line arguments # in place of the actual path to the driver binary. def overriding_cmd_line(): cmd = self.__original_driver_cmd_line() index = cmd.index(self.__delegate._path_to_driver()) # FIXME: Why does this need to use sys.executable (instead of something mockable)? cmd[index:index + 1] = [ sys.executable, self._path_to_driver(), '--platform', self.name() ] return cmd delegated_driver = self.__delegate.create_driver(worker_number) self.__original_driver_cmd_line = delegated_driver.cmd_line delegated_driver.cmd_line = overriding_cmd_line return delegated_driver def start_helper(self): pass def start_http_server(self): pass def start_websocket_server(self): pass def acquire_http_lock(self): pass def stop_helper(self): pass def stop_http_server(self): pass def stop_websocket_server(self): pass def release_http_lock(self): pass
class MockDRTPort(object): """MockPort implementation of the Port interface.""" def __init__(self, host, **kwargs): prefix = "mock-" if "port_name" in kwargs: kwargs["port_name"] = kwargs["port_name"][len(prefix) :] self._host = host self.__delegate = PortFactory(host).get(**kwargs) self.__real_name = prefix + self.__delegate.name() def real_name(self): return self.__real_name def __getattr__(self, name): return getattr(self.__delegate, name) def check_build(self, needs_http): return True def check_sys_deps(self, needs_http): return True def driver_cmd_line(self): driver = self.create_driver(0) return driver.cmd_line() def _path_to_driver(self): return self._host.filesystem.abspath(__file__) def create_driver(self, worker_number): # We need to create a driver object as the delegate would, but # overwrite the path to the driver binary in its command line. We do # this by actually overwriting its cmd_line() method with a proxy # method that splices in the mock_drt path and command line arguments # in place of the actual path to the driver binary. def overriding_cmd_line(): cmd = self.__original_driver_cmd_line() index = cmd.index(self.__delegate._path_to_driver()) # FIXME: Why does this need to use sys.executable (instead of something mockable)? cmd[index : index + 1] = [sys.executable, self._path_to_driver(), "--platform", self.name()] return cmd delegated_driver = self.__delegate.create_driver(worker_number) self.__original_driver_cmd_line = delegated_driver.cmd_line delegated_driver.cmd_line = overriding_cmd_line return delegated_driver def start_helper(self): pass def start_http_server(self): pass def start_websocket_server(self): pass def acquire_http_lock(self): pass def stop_helper(self): pass def stop_http_server(self): pass def stop_websocket_server(self): pass def release_http_lock(self): pass
class MockDRT(object): def __init__(self, options, args, host, stdin, stdout, stderr): self._options = options self._args = args self._host = host self._stdout = stdout self._stdin = stdin self._stderr = stderr port_name = None if options.platform: port_name = options.platform self._port = PortFactory(host).get(port_name=port_name, options=options) self._driver = self._port.create_driver(0) def run(self): self._stdout.write("#READY\n") self._stdout.flush() while True: line = self._stdin.readline() if not line: return 0 driver_input = self.input_from_line(line) dirname, basename = self._port.split_test(driver_input.test_name) is_reftest = (self._port.reference_files(driver_input.test_name) or self._port.is_reference_html_file( self._port._filesystem, dirname, basename)) output = self.output_for_test(driver_input, is_reftest) self.write_test_output(driver_input, output, is_reftest) def input_from_line(self, line): vals = line.strip().split("'") uri = vals[0] checksum = None should_run_pixel_tests = False if len(vals) == 2 and vals[1] == '--pixel-test': should_run_pixel_tests = True elif len(vals) == 3 and vals[1] == '--pixel-test': should_run_pixel_tests = True checksum = vals[2] elif len(vals) != 1: raise NotImplementedError if uri.startswith('http://') or uri.startswith('https://'): test_name = self._driver.uri_to_test(uri) else: test_name = self._port.relative_test_filename(uri) return DriverInput(test_name, 0, checksum, should_run_pixel_tests, args=[]) def output_for_test(self, test_input, is_reftest): port = self._port if self._options.virtual_test_suite_name: test_input.test_name = test_input.test_name.replace( self._options.virtual_test_suite_base, self._options.virtual_test_suite_name) actual_text = port.expected_text(test_input.test_name) actual_audio = port.expected_audio(test_input.test_name) actual_image = None actual_checksum = None if is_reftest: # Make up some output for reftests. actual_text = 'reference text\n' actual_checksum = 'mock-checksum' actual_image = 'blank' if test_input.test_name.endswith('-mismatch.html'): actual_text = 'not reference text\n' actual_checksum = 'not-mock-checksum' actual_image = 'not blank' elif test_input.should_run_pixel_test and test_input.image_hash: actual_checksum = port.expected_checksum(test_input.test_name) actual_image = port.expected_image(test_input.test_name) if self._options.actual_directory: actual_path = port._filesystem.join(self._options.actual_directory, test_input.test_name) root, _ = port._filesystem.splitext(actual_path) text_path = root + '-actual.txt' if port._filesystem.exists(text_path): actual_text = port._filesystem.read_binary_file(text_path) audio_path = root + '-actual.wav' if port._filesystem.exists(audio_path): actual_audio = port._filesystem.read_binary_file(audio_path) image_path = root + '-actual.png' if port._filesystem.exists(image_path): actual_image = port._filesystem.read_binary_file(image_path) with port._filesystem.open_binary_file_for_reading( image_path) as filehandle: actual_checksum = read_checksum_from_png.read_checksum( filehandle) return DriverOutput(actual_text, actual_image, actual_checksum, actual_audio) def write_test_output(self, test_input, output, is_reftest): if output.audio: self._stdout.write('Content-Type: audio/wav\n') self._stdout.write('Content-Transfer-Encoding: base64\n') self._stdout.write(base64.b64encode(output.audio)) self._stdout.write('\n') else: self._stdout.write('Content-Type: text/plain\n') # FIXME: Note that we don't ensure there is a trailing newline! # This mirrors actual (Mac) DRT behavior but is a bug. if output.text: self._stdout.write(output.text) self._stdout.write('#EOF\n') if test_input.should_run_pixel_test and output.image_hash: self._stdout.write('\n') self._stdout.write('ActualHash: %s\n' % output.image_hash) self._stdout.write('ExpectedHash: %s\n' % test_input.image_hash) if output.image_hash != test_input.image_hash: self._stdout.write('Content-Type: image/png\n') self._stdout.write('Content-Length: %s\n' % len(output.image)) self._stdout.write(output.image) self._stdout.write('#EOF\n') self._stdout.flush() self._stderr.write('#EOF\n') self._stderr.flush()
class MockDRT(object): def __init__(self, options, args, host, stdin, stdout, stderr): self._options = options self._args = args self._host = host self._stdout = stdout self._stdin = stdin self._stderr = stderr port_name = None if options.platform: port_name = options.platform self._port = PortFactory(host).get(port_name=port_name, options=options) self._driver = self._port.create_driver(0) def run(self): while True: line = self._stdin.readline() if not line: break self.run_one_test(self.parse_input(line)) return 0 def parse_input(self, line): return _DRTInput(line) def run_one_test(self, test_input): port = self._port if test_input.uri.startswith("http://") or test_input.uri.startswith("https://"): test_name = self._driver.uri_to_test(test_input.uri) else: test_name = port.relative_test_filename(test_input.uri) actual_text = port.expected_text(test_name) actual_audio = port.expected_audio(test_name) if self._options.pixel_tests and test_input.checksum: actual_checksum = port.expected_checksum(test_name) actual_image = port.expected_image(test_name) if actual_audio: self._stdout.write("Content-Type: audio/wav\n") self._stdout.write("Content-Transfer-Encoding: base64\n") output = base64.b64encode(actual_audio) self._stdout.write(output) else: self._stdout.write("Content-Type: text/plain\n") # FIXME: Note that we don't ensure there is a trailing newline! # This mirrors actual (Mac) DRT behavior but is a bug. self._stdout.write(actual_text) self._stdout.write("#EOF\n") if self._options.pixel_tests and test_input.checksum: self._stdout.write("\n") self._stdout.write("ActualHash: %s\n" % actual_checksum) self._stdout.write("ExpectedHash: %s\n" % test_input.checksum) if actual_checksum != test_input.checksum: self._stdout.write("Content-Type: image/png\n") self._stdout.write("Content-Length: %s\n" % len(actual_image)) self._stdout.write(actual_image) self._stdout.write("#EOF\n") self._stdout.flush() self._stderr.flush()
class MockDRTPort(object): """MockPort implementation of the Port interface.""" port_name = 'mock' @classmethod def determine_full_port_name(cls, host, options, port_name): """Return a fully-specified port name that can be used to construct objects.""" # Subclasses will usually override this. return port_name def __init__(self, host, port_name, **kwargs): self.__delegate = PortFactory(host).get(port_name.replace('mock-', ''), **kwargs) self.__real_name = port_name self._host = host def real_name(self): return self.__real_name def __getattr__(self, name): return getattr(self.__delegate, name) def check_build(self, needs_http): return True def check_sys_deps(self, needs_http): return True def driver_cmd_line(self): driver = self.create_driver(0) return driver.cmd_line() def _path_to_driver(self): return self._host.filesystem.abspath(__file__) def create_driver(self, worker_number): # We need to create a driver object as the delegate would, but # overwrite the path to the driver binary in its command line. We do # this by actually overwriting its cmd_line() method with a proxy # method that splices in the mock_drt path and command line arguments # in place of the actual path to the driver binary. def overriding_cmd_line(): cmd = self.__original_driver_cmd_line() index = cmd.index(self.__delegate._path_to_driver()) # FIXME: Why does this need to use sys.executable (instead of something mockable)? cmd[index:index + 1] = [sys.executable, self._path_to_driver(), '--platform', self.name()] return cmd delegated_driver = self.__delegate.create_driver(worker_number) self.__original_driver_cmd_line = delegated_driver.cmd_line delegated_driver.cmd_line = overriding_cmd_line return delegated_driver def start_helper(self): pass def start_http_server(self): pass def start_websocket_server(self): pass def acquire_http_lock(self): pass def stop_helper(self): pass def stop_http_server(self): pass def stop_websocket_server(self): pass def release_http_lock(self): pass
class MockDRT(object): @classmethod def determine_full_port_name(cls, host, options, port_name): """Return a fully-specified port name that can be used to construct objects.""" # Subclasses will usually override this. return cls.port_name def __init__(self, options, args, host, stdin, stdout, stderr): self._options = options self._args = args self._host = host self._stdout = stdout self._stdin = stdin self._stderr = stderr port_name = None if options.platform: port_name = options.platform self._port = PortFactory(host).get(port_name=port_name, options=options) self._driver = self._port.create_driver(0) def run(self): while True: line = self._stdin.readline() if not line: break self.run_one_test(self.parse_input(line)) return 0 def parse_input(self, line): return _DRTInput(line) def run_one_test(self, test_input): port = self._port if test_input.uri.startswith('http://') or test_input.uri.startswith('https://'): test_name = self._driver.uri_to_test(test_input.uri) else: test_name = port.relative_test_filename(test_input.uri) actual_text = port.expected_text(test_name) actual_audio = port.expected_audio(test_name) if self._options.pixel_tests and test_input.checksum: actual_checksum = port.expected_checksum(test_name) actual_image = port.expected_image(test_name) if actual_audio: self._stdout.write('Content-Type: audio/wav\n') self._stdout.write('Content-Transfer-Encoding: base64\n') output = base64.b64encode(actual_audio) self._stdout.write(output) else: self._stdout.write('Content-Type: text/plain\n') # FIXME: Note that we don't ensure there is a trailing newline! # This mirrors actual (Mac) DRT behavior but is a bug. self._stdout.write(actual_text) self._stdout.write('#EOF\n') if self._options.pixel_tests and test_input.checksum: self._stdout.write('\n') self._stdout.write('ActualHash: %s\n' % actual_checksum) self._stdout.write('ExpectedHash: %s\n' % test_input.checksum) if actual_checksum != test_input.checksum: self._stdout.write('Content-Type: image/png\n') self._stdout.write('Content-Length: %s\n' % len(actual_image)) self._stdout.write(actual_image) self._stdout.write('#EOF\n') self._stdout.flush() self._stderr.flush()
class MockDRTPort(object): """MockPort implementation of the Port interface.""" port_name = 'mock' @classmethod def determine_full_port_name(cls, host, options, port_name): """Return a fully-specified port name that can be used to construct objects.""" # Subclasses will usually override this. return port_name def __init__(self, host, port_name, **kwargs): self.__delegate = PortFactory(host).get(port_name.replace('mock-', ''), **kwargs) self.__real_name = port_name self._host = host def real_name(self): return self.__real_name def __getattr__(self, name): return getattr(self.__delegate, name) def check_build(self, needs_http): return True def check_sys_deps(self, needs_http): return True def driver_cmd_line(self): driver = self.create_driver(0) return driver.cmd_line() def _path_to_driver(self): return self._host.filesystem.abspath(__file__) def create_driver(self, worker_number): # We need to create a driver object as the delegate would, but # overwrite the path to the driver binary in its command line. We do # this by actually overwriting its cmd_line() method with a proxy # method that splices in the mock_drt path and command line arguments # in place of the actual path to the driver binary. def overriding_cmd_line(): cmd = self.__original_driver_cmd_line() index = cmd.index(self.__delegate._path_to_driver()) # FIXME: Why does this need to use sys.executable (instead of something mockable)? cmd[index:index + 1] = [ sys.executable, self._path_to_driver(), '--platform', self.name() ] return cmd delegated_driver = self.__delegate.create_driver(worker_number) self.__original_driver_cmd_line = delegated_driver.cmd_line delegated_driver.cmd_line = overriding_cmd_line return delegated_driver def start_helper(self): pass def start_http_server(self): pass def start_websocket_server(self): pass def acquire_http_lock(self): pass def stop_helper(self): pass def stop_http_server(self): pass def stop_websocket_server(self): pass def release_http_lock(self): pass
class MockDRT(object): @classmethod def determine_full_port_name(cls, host, options, port_name): """Return a fully-specified port name that can be used to construct objects.""" # Subclasses will usually override this. return cls.port_name def __init__(self, options, args, host, stdin, stdout, stderr): self._options = options self._args = args self._host = host self._stdout = stdout self._stdin = stdin self._stderr = stderr port_name = None if options.platform: port_name = options.platform self._port = PortFactory(host).get(port_name=port_name, options=options) self._driver = self._port.create_driver(0) def run(self): while True: line = self._stdin.readline() if not line: break self.run_one_test(self.parse_input(line)) return 0 def parse_input(self, line): return _DRTInput(line) def run_one_test(self, test_input): port = self._port if test_input.uri.startswith('http://') or test_input.uri.startswith( 'https://'): test_name = self._driver.uri_to_test(test_input.uri) else: test_name = port.relative_test_filename(test_input.uri) actual_text = port.expected_text(test_name) actual_audio = port.expected_audio(test_name) if self._options.pixel_tests and test_input.checksum: actual_checksum = port.expected_checksum(test_name) actual_image = port.expected_image(test_name) if actual_audio: self._stdout.write('Content-Type: audio/wav\n') self._stdout.write('Content-Transfer-Encoding: base64\n') output = base64.b64encode(actual_audio) self._stdout.write(output) else: self._stdout.write('Content-Type: text/plain\n') # FIXME: Note that we don't ensure there is a trailing newline! # This mirrors actual (Mac) DRT behavior but is a bug. self._stdout.write(actual_text) self._stdout.write('#EOF\n') if self._options.pixel_tests and test_input.checksum: self._stdout.write('\n') self._stdout.write('ActualHash: %s\n' % actual_checksum) self._stdout.write('ExpectedHash: %s\n' % test_input.checksum) if actual_checksum != test_input.checksum: self._stdout.write('Content-Type: image/png\n') self._stdout.write('Content-Length: %s\n' % len(actual_image)) self._stdout.write(actual_image) self._stdout.write('#EOF\n') self._stdout.flush() self._stderr.flush()