def load_json(self): filesystem = FileSystem() json_path = filesystem.join(filesystem.dirname(filesystem.path_to_module('webkitpy.common.config')), 'contributors.json') try: contributors = json.loads(filesystem.read_text_file(json_path)) except ValueError, e: sys.exit('contributors.json is malformed: ' + str(e))
def parse_args(args): parser = argparse.ArgumentParser(prog='import-w3c-tests [w3c_test_source_directory]') parser.add_argument('-n', '--no-overwrite', dest='overwrite', action='store_false', default=True, help='Flag to prevent duplicate test files from overwriting existing tests. By default, they will be overwritten') parser.add_argument('-l', '--no-links-conversion', dest='convert_test_harness_links', action='store_false', default=True, help='Do not change links (testharness js or css e.g.). This option only applies when providing a source directory, in which case by default, links are converted to point to WebKit testharness files. When tests are downloaded from W3C repository, links are converted for CSS tests and remain unchanged for WPT tests') parser.add_argument('-a', '--all', action='store_true', default=False, help='Import all tests including reftests, JS tests, and manual/pixel tests. By default, only reftests and JS tests are imported') fs = FileSystem() parser.add_argument('-d', '--dest-dir', dest='destination', default=fs.join('imported', 'w3c'), help='Import into a specified directory relative to the LayoutTests root. By default, imports into imported/w3c') list_of_repositories = ' or '.join([test_repository['name'] for test_repository in TestDownloader.load_test_repositories()]) parser.add_argument('-t', '--test-path', action='append', dest='test_paths', default=[], help='Import only tests in the supplied subdirectory of the source directory. Can be supplied multiple times to give multiple paths. For tests directly cloned from W3C repositories, use ' + list_of_repositories + ' prefixes to filter specific tests') parser.add_argument('-v', '--verbose', action='store_true', default=False, help='Print maximal log') parser.add_argument('--no-fetch', action='store_false', dest='fetch', default=True, help='Do not fetch the repositories. By default, repositories are fetched if a source directory is not provided') parser.add_argument('--import-all', action='store_true', default=False, help='Ignore the ImportExpectations file. All tests will be imported. This option only applies when tests are downloaded from W3C repository') parser.add_argument('--clean-dest-dir', action='store_true', dest='clean_destination_directory', default=False, help='Clean destination directory. All files in the destination directory will be deleted except for WebKit specific files (test expectations, .gitignore...) before new tests import. Dangling test expectations (expectation file that is no longer related to a test) are removed after tests import.') options, args = parser.parse_known_args(args) if len(args) > 1: parser.error('Incorrect number of arguments') return options, args
def load_test_repositories(): filesystem = FileSystem() webkit_finder = WebKitFinder(filesystem) test_repositories_path = webkit_finder.path_from_webkit_base( "LayoutTests", "imported", "w3c", "resources", "TestRepositories" ) return json.loads(filesystem.read_text_file(test_repositories_path))
def load_json(): filesystem = FileSystem() json_path = filesystem.join(filesystem.dirname(filesystem.path_to_module('webkitpy.common.config')), 'contributors.json') contributors = json.loads(filesystem.read_text_file(json_path)) return { 'Contributors': [Contributor(name, data.get('emails'), data.get('nicks')) for name, data in contributors['Contributors'].iteritems()], 'Committers': [Committer(name, data.get('emails'), data.get('nicks')) for name, data in contributors['Committers'].iteritems()], 'Reviewers': [Reviewer(name, data.get('emails'), data.get('nicks')) for name, data in contributors['Reviewers'].iteritems()], }
def main(_argv, _stdout, _stderr): options, args = parse_args(_argv) import_dir = args[0] filesystem = FileSystem() if not filesystem.exists(import_dir): sys.exit('Source directory %s not found!' % import_dir) configure_logging() test_importer = TestImporter(Host(), import_dir, options) test_importer.do_import()
def integration_test_coverage_works(self): filesystem = FileSystem() executive = Executive() module_path = filesystem.path_to_module(self.__module__) script_dir = module_path[0:module_path.find('webkitpy') - 1] proc = executive.popen([sys.executable, filesystem.join(script_dir, 'test-webkitpy'), '-c', STUBS_CLASS + '.test_empty'], stdout=executive.PIPE, stderr=executive.PIPE) out, _ = proc.communicate() retcode = proc.returncode self.assertEqual(retcode, 0) self.assertIn('Cover', out)
def test_import_web_platform_test_modules(self): fs = FileSystem() current_dir, name = fs.split(fs.realpath(__file__)) doc_root_dir = fs.join(current_dir, "..", "..", "..", "..", "..", "LayoutTests", "imported", "w3c", "web-platform-tests") tools_dir = fs.join(doc_root_dir, "tools") sys.path.insert(0, doc_root_dir) try: file, pathname, description = imp.find_module("tools") except ImportError, e: self.fail(e)
def test_remove_file_with_retry(self): RealFileSystemTest._remove_failures = 2 def remove_with_exception(filename): RealFileSystemTest._remove_failures -= 1 if RealFileSystemTest._remove_failures >= 0: try: raise WindowsError except NameError: raise FileSystem._WindowsError fs = FileSystem() self.assertTrue(fs.remove('filename', remove_with_exception)) self.assertEqual(-1, RealFileSystemTest._remove_failures)
def setUp(self): LoggingTestCase.setUp(self) # FIXME: This should be a MockFileSystem once TextFileReader is moved entirely on top of FileSystem. self.filesystem = FileSystem() self._temp_dir = str(self.filesystem.mkdtemp()) self._processor = TextFileReaderTest.MockProcessor() self._file_reader = TextFileReader(self.filesystem, self._processor)
def load_ews_classes(cls): filesystem = FileSystem() json_path = filesystem.join(filesystem.dirname(filesystem.path_to_module('webkitpy.common.config')), 'ews.json') try: ewses = json.loads(filesystem.read_text_file(json_path)) except ValueError: return None classes = [] for name, config in ewses.iteritems(): classes.append(type(str(name.replace(' ', '')), (AbstractEarlyWarningSystem,), { 'name': config['port'] + '-ews', 'port_name': config['port'], 'watchers': config.get('watchers', []), 'run_tests': config.get('runTests', cls.run_tests), })) return classes
def setUp(self): self._filesystem = FileSystem() self._temp_dir = str(self._filesystem.mkdtemp(suffix="exportfiles")) self._old_cwd = self._filesystem.getcwd() self._filesystem.chdir(self._temp_dir) self._filesystem.write_text_file(os.path.join(self._temp_dir, "sorted_file.exp.in"), _sorted_file_contents) self._filesystem.write_text_file(os.path.join(self._temp_dir, "non_sorted_file.exp.in"), _non_sorted_file_contents) self._filesystem.write_text_file(os.path.join(self._temp_dir, "parse_error_file.exp.in"), _parse_error_file_contents)
def test_read_and_write_text_file(self): fs = FileSystem() text_path = None unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D' try: text_path = tempfile.mktemp(prefix='tree_unittest_') file = fs.open_text_file_for_writing(text_path) file.write(unicode_text_string) file.close() file = fs.open_text_file_for_reading(text_path) read_text = file.read() file.close() self.assertEqual(read_text, unicode_text_string) finally: if text_path and fs.isfile(text_path): os.remove(text_path)
def load_ews_classes(cls): filesystem = FileSystem() json_path = filesystem.join(filesystem.dirname(filesystem.path_to_module('webkitpy.common.config')), 'ews.json') try: ewses = json.loads(filesystem.read_text_file(json_path)) except ValueError: return None classes = [] for name, config in ewses.iteritems(): classes.append(type(name.encode('utf-8').translate(None, ' -'), (AbstractEarlyWarningSystem,), { 'name': config.get('name', config['port'] + '-ews'), 'port_name': config['port'], 'architecture': config.get('architecture', None), '_build_style': config.get('style', "release"), 'watchers': config.get('watchers', []), 'run_tests': config.get('runTests', cls.run_tests), })) return classes
def test_maybe_make_directory__failure(self): # FIXME: os.chmod() doesn't work on Windows to set directories # as readonly, so we skip this test for now. if sys.platform in ('win32', 'cygwin'): return fs = FileSystem() with fs.mkdtemp(prefix='filesystem_unittest_') as d: # Remove write permissions on the parent directory. os.chmod(d, stat.S_IRUSR) # Now try to create a sub directory - should fail. sub_dir = fs.join(d, 'subdir') self.assertRaises(OSError, fs.maybe_make_directory, sub_dir) # Clean up in case the test failed and we did create the # directory. if os.path.exists(sub_dir): os.rmdir(sub_dir)
def test_chdir(self): fs = FileSystem() cwd = fs.getcwd() newdir = '/' if sys.platform == 'win32': newdir = 'c:\\' fs.chdir(newdir) self.assertEqual(fs.getcwd(), newdir) fs.chdir(cwd)
def __init__(self, lock_path, lock_file_prefix="WebKitHttpd.lock.", guard_lock="WebKit.lock", filesystem=None, executive=None): self._executive = executive or Executive() self._filesystem = filesystem or FileSystem() self._lock_path = lock_path if not self._lock_path: # FIXME: FileSystem should have an accessor for tempdir() self._lock_path = tempfile.gettempdir() self._lock_file_prefix = lock_file_prefix self._lock_file_path_prefix = self._filesystem.join(self._lock_path, self._lock_file_prefix) self._guard_lock_file = self._filesystem.join(self._lock_path, guard_lock) self._guard_lock = FileLock(self._guard_lock_file) self._process_lock_file_name = ""
def default_out_dir(): detector = SCMDetector(FileSystem(), Executive()) current_scm = detector.detect_scm_system(os.path.dirname(sys.argv[0])) if not current_scm: return os.getcwd() root_dir = current_scm.checkout_root if not root_dir: return os.getcwd() out_dir = os.path.join(root_dir, "LayoutTests/fast/canvas/webgl") if os.path.isdir(out_dir): return out_dir return os.getcwd()
def setUp(self): # FIXME: This should not need to touch the filesystem, however # ChangeLog is difficult to mock at current. self.filesystem = FileSystem() self.temp_dir = str(self.filesystem.mkdtemp(suffix="changelogs")) self.old_cwd = self.filesystem.getcwd() self.filesystem.chdir(self.temp_dir) # Trick commit-log-editor into thinking we're in a Subversion working copy so it won't # complain about not being able to figure out what SCM is in use. # FIXME: VCSTools.pm is no longer so easily fooled. It logs because "svn info" doesn't # treat a bare .svn directory being part of an svn checkout. self.filesystem.maybe_make_directory(".svn") self.changelogs = map( self.filesystem.abspath, (self.filesystem.join("Tools", "ChangeLog"), self.filesystem.join("LayoutTests", "ChangeLog"))) for path, contents in zip(self.changelogs, (_changelog1, _changelog2)): self.filesystem.maybe_make_directory(self.filesystem.dirname(path)) self.filesystem.write_text_file(path, contents)
def get_build_path(fatal=True): global build_dir if build_dir: return build_dir def is_valid_build_directory(path): return os.path.exists(os.path.join(path, 'CMakeCache.txt')) or \ os.path.exists(os.path.join(path, 'bin/WebKitTestRunner')) if len(sys.argv[1:]) > 1 and os.path.exists(sys.argv[-1]) and is_valid_build_directory(sys.argv[-1]): return sys.argv[-1] # Debian and Ubuntu build both flavours of the library (with gtk2 # and with gtk3); they use directories build-2.0 and build-3.0 for # that, which is not handled by the above cases; we check that the # directory where we are called from is a valid build directory, # which should handle pretty much all other non-standard cases. build_dir = os.getcwd() if is_valid_build_directory(build_dir): return build_dir base_build_dir = top_level_path('WebKitBuild') scm = SCMDetector(FileSystem(), Executive()).default_scm() if isinstance(scm, Git): is_branch_build = scm.read_config('core.webKitBranchBuild', bool) if is_branch_build and is_branch_build.lower() == 'true': current_branch = scm._current_branch() if current_branch != 'master': base_build_dir = os.path.join(base_build_dir, scm._current_branch()) global build_types for build_type in build_types: build_dir = os.path.join(base_build_dir, build_type) if is_valid_build_directory(build_dir): return build_dir # distcheck builds in a directory named _build in the top-level path. build_dir = top_level_path("_build") if is_valid_build_directory(build_dir): return build_dir build_dir = top_level_path() if is_valid_build_directory(build_dir): return build_dir build_dir = base_build_dir if is_valid_build_directory(build_dir): return build_dir print('Could not determine build directory.') if fatal: sys.exit(1)
def load_ews_classes(cls): filesystem = FileSystem() json_path = filesystem.join( filesystem.dirname( filesystem.path_to_module('webkitpy.common.config')), 'ews.json') try: ewses = json.loads(filesystem.read_text_file(json_path)) except ValueError: return None classes = [] for name, config in ewses.iteritems(): classes.append( type( name.encode('utf-8').translate(None, ' -'), (cls, ), { 'name': config.get('name', config['port'] + '-ews'), 'port_name': config['port'], 'architecture': config.get('architecture', None), '_build_style': config.get('style', "release"), 'watchers': config.get('watchers', []), 'run_tests': config.get('runTests', cls.run_tests), '_group': config.get('group', None), })) return classes
def test_append_to_text_file(self): fs = FileSystem() text_path = None unicode_text_string1 = u'\u016An\u012Dc\u014Dde\u033D' unicode_text_string2 = 'Hello' try: text_path = tempfile.mktemp(prefix='tree_unittest_') file = fs.open_text_file_for_writing(text_path) file.write(unicode_text_string1) file.close() file = fs.open_text_file_for_writing(text_path, should_append=True) file.write(unicode_text_string2) file.close() file = fs.open_text_file_for_reading(text_path) read_text = file.read() file.close() self.assertEqual(read_text, unicode_text_string1 + unicode_text_string2) finally: if text_path and fs.isfile(text_path): os.remove(text_path)
def load_ews_classes(cls): filesystem = FileSystem() json_path = filesystem.join(filesystem.dirname(filesystem.path_to_module('webkitpy.common.config')), 'ews.json') try: ewses = json.loads(filesystem.read_text_file(json_path)) except ValueError: return None classes = [] for name, config in ewses.items(): if sys.version_info > (3, 0): translated = string_utils.encode(name, target_type=str).translate(' -') else: translated = string_utils.encode(name, target_type=str).translate(None, ' -') classes.append(type(translated, (cls,), { 'name': config.get('name', config['port'] + '-ews'), 'port_name': config['port'], 'architecture': config.get('architecture', None), '_build_style': config.get('style', "release"), 'watchers': config.get('watchers', []), 'run_tests': config.get('runTests', cls.run_tests), '_group': config.get('group', None), 'should_build': config.get('shouldBuild', True), })) return classes
def interpreter_for_script(script_path, fs=None): fs = fs or FileSystem() lines = fs.read_text_file(script_path).splitlines() if not len(lines): return None first_line = lines[0] if not first_line.startswith('#!'): return None if first_line.find('python') > -1: return sys.executable if first_line.find('ruby') > -1: return 'ruby' return None
def test_read_and_write_file(self): fs = FileSystem() text_path = None binary_path = None unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D' hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD' try: text_path = tempfile.mktemp(prefix='tree_unittest_') binary_path = tempfile.mktemp(prefix='tree_unittest_') fs.write_text_file(text_path, unicode_text_string) contents = fs.read_binary_file(text_path) self.assertEqual(contents, hex_equivalent) fs.write_binary_file(binary_path, hex_equivalent) text_contents = fs.read_text_file(binary_path) self.assertEqual(text_contents, unicode_text_string) finally: if text_path and fs.isfile(text_path): os.remove(text_path) if binary_path and fs.isfile(binary_path): os.remove(binary_path)
def test_maybe_make_directory__success(self): fs = FileSystem() with fs.mkdtemp(prefix='filesystem_unittest_') as base_path: sub_path = os.path.join(base_path, "newdir") self.assertFalse(os.path.exists(sub_path)) self.assertFalse(fs.isdir(sub_path)) fs.maybe_make_directory(sub_path) self.assertTrue(os.path.exists(sub_path)) self.assertTrue(fs.isdir(sub_path)) # Make sure we can re-create it. fs.maybe_make_directory(sub_path) self.assertTrue(os.path.exists(sub_path)) self.assertTrue(fs.isdir(sub_path)) # Clean up. os.rmdir(sub_path) self.assertFalse(os.path.exists(base_path)) self.assertFalse(fs.isdir(base_path))
def test_listdir(self): fs = FileSystem() with fs.mkdtemp(prefix='filesystem_unittest_') as d: self.assertEqual(fs.listdir(d), []) new_file = os.path.join(d, 'foo') fs.write_text_file(new_file, u'foo') self.assertEqual(fs.listdir(d), ['foo']) os.remove(new_file)
def setUp(self): # FIXME: This should not need to touch the filesystem, however # ChangeLog is difficult to mock at current. self.filesystem = FileSystem() self.temp_dir = str(self.filesystem.mkdtemp(suffix="changelogs")) self.old_cwd = self.filesystem.getcwd() self.filesystem.chdir(self.temp_dir) self.webkit_base = WebKitFinder(self.filesystem).webkit_base() # Trick commit-log-editor into thinking we're in a Subversion working copy so it won't # complain about not being able to figure out what SCM is in use. # FIXME: VCSTools.pm is no longer so easily fooled. It logs because "svn info" doesn't # treat a bare .svn directory being part of an svn checkout. self.filesystem.maybe_make_directory(".svn")
def setUp(self): self.executive = Executive() self.filesystem = FileSystem() self.original_cwd = self.filesystem.getcwd() # Set up fresh git repository with one commit. self.untracking_checkout_path = self._mkdtemp(suffix='-git_unittest_untracking') self._run(['git', 'init', self.untracking_checkout_path]) self._chdir(self.untracking_checkout_path) self._set_user_config() self._write_text_file('foo_file', 'foo') self._run(['git', 'add', 'foo_file']) self._run(['git', 'commit', '-am', 'dummy commit']) self.untracking_git = Git(cwd=self.untracking_checkout_path, filesystem=self.filesystem, executive=self.executive) # Then set up a second git repo that tracks the first one. self.tracking_git_checkout_path = self._mkdtemp(suffix='-git_unittest_tracking') self._run(['git', 'clone', '--quiet', self.untracking_checkout_path, self.tracking_git_checkout_path]) self._chdir(self.tracking_git_checkout_path) self._set_user_config() self.tracking_git = Git(cwd=self.tracking_git_checkout_path, filesystem=self.filesystem, executive=self.executive)
def load_json(self): filesystem = FileSystem() json_path = filesystem.join( filesystem.dirname( filesystem.path_to_module('webkitpy.common.config')), 'contributors.json') try: contributors = json.loads(filesystem.read_text_file(json_path)) except ValueError, e: sys.exit('contributors.json is malformed: ' + str(e))
def test_webkit_base_dir(self): # FIXME: We use a real filesystem here. Should this move to a mocked one? executive = Executive() filesystem = FileSystem() c = config.Config(executive, filesystem) base_dir = c.webkit_base_dir() self.assertTrue(base_dir) self.assertNotEqual(base_dir[-1], '/') # FIXME: Once we use a MockFileSystem for this test we don't need to save the orig_cwd. orig_cwd = filesystem.getcwd() if sys.platform == 'win32': filesystem.chdir(os.environ['USERPROFILE']) else: filesystem.chdir(os.environ['HOME']) c = config.Config(executive, filesystem) try: base_dir_2 = c.webkit_base_dir() self.assertEqual(base_dir, base_dir_2) finally: filesystem.chdir(orig_cwd)
class TextFileReaderTest(LoggingTestCase): class MockProcessor(ProcessorBase): """A processor for test purposes. This processor simply records the parameters passed to its process() method for later checking by the unittest test methods. """ def __init__(self): self.processed = [] """The parameters passed for all calls to the process() method.""" def should_process(self, file_path): return not file_path.endswith('should_not_process.txt') def process(self, lines, file_path, test_kwarg=None): self.processed.append((lines, file_path, test_kwarg)) def setUp(self): LoggingTestCase.setUp(self) # FIXME: This should be a MockFileSystem once TextFileReader is moved entirely on top of FileSystem. self.filesystem = FileSystem() self._temp_dir = str(self.filesystem.mkdtemp()) self._processor = TextFileReaderTest.MockProcessor() self._file_reader = TextFileReader(self.filesystem, self._processor) def tearDown(self): LoggingTestCase.tearDown(self) self.filesystem.rmtree(self._temp_dir) def _create_file(self, rel_path, text): """Create a file with given text and return the path to the file.""" # FIXME: There are better/more secure APIs for creating tmp file paths. file_path = self.filesystem.join(self._temp_dir, rel_path) self.filesystem.write_text_file(file_path, text) return file_path def _passed_to_processor(self): """Return the parameters passed to MockProcessor.process().""" return self._processor.processed def _assert_file_reader(self, passed_to_processor, file_count): """Assert the state of the file reader.""" self.assertEqual(passed_to_processor, self._passed_to_processor()) self.assertEqual(file_count, self._file_reader.file_count) def test_process_file__does_not_exist(self): try: self._file_reader.process_file('does_not_exist.txt') except SystemExit, err: self.assertEqual(str(err), '1') else:
def _run_pylint(self, path): wkf = WebKitFinder(FileSystem()) executive = Executive() env = os.environ.copy() env['PYTHONPATH'] = ('%s%s%s%s%s' % (wkf.path_from_webkit_base('Tools', 'Scripts'), os.pathsep, wkf.path_from_webkit_base('Source', 'build', 'scripts'), os.pathsep, wkf.path_from_webkit_base('Tools', 'Scripts', 'webkitpy', 'thirdparty'))) return executive.run_command([sys.executable, wkf.path_from_depot_tools_base('pylint.py'), '--output-format=parseable', '--errors-only', '--rcfile=' + wkf.path_from_webkit_base('Tools', 'Scripts', 'webkitpy', 'pylintrc'), path], env=env, error_handler=executive.ignore_error)
def test_real_code(self): # This test makes sure the real (unmocked) code actually works. info = PlatformInfo(sys, platform, FileSystem(), Executive()) self.assertNotEquals(info.os_name, '') self.assertNotEquals(info.os_version, '') self.assertNotEquals(info.display_name(), '') self.assertTrue(info.is_mac() or info.is_win() or info.is_linux() or info.is_freebsd()) self.assertIsNotNone(info.terminal_width()) if info.is_linux(): self.assertIsNotNone(info.linux_distribution()) if info.is_mac(): self.assertTrue(info.total_bytes_memory() > 0) else: self.assertIsNone(info.total_bytes_memory())
def __init__(self, cwd=None, executive=None, filesystem=None, platform=None): self._executive = executive or Executive() self._filesystem = filesystem or FileSystem() self._executable_name = self.find_executable_name(self._executive, platform) self.cwd = cwd or self._filesystem.abspath(self._filesystem.getcwd()) if not self.in_working_directory(self.cwd): module_directory = self._filesystem.abspath( self._filesystem.dirname(self._filesystem.path_to_module(self.__module__))) _log.info('The current directory (%s) is not in a git repo, trying directory %s.', cwd, module_directory) if self.in_working_directory(module_directory): self.cwd = module_directory _log.error('Failed to find Git repo for %s or %s', cwd, module_directory) self.checkout_root = self.find_checkout_root(self.cwd)
def main(): filesystem = FileSystem() wkf = WebKitFinder(filesystem) tester = Tester(filesystem, wkf) tester.add_tree(wkf.path_from_webkit_base('tools'), 'webkitpy') tester.skip(('webkitpy.common.checkout.scm.scm_unittest', ), 'are really, really, slow', 31818) if sys.platform == 'win32': tester.skip( ('webkitpy.common.checkout', 'webkitpy.common.config', 'webkitpy.tool', 'webkitpy.w3c', 'webkitpy.layout_tests.layout_package.bot_test_expectations'), 'fail horribly on win32', 54526) return not tester.run()
def _find_parent_path_matching_callback_condition(cls, path, callback, filesystem=None): if not filesystem: filesystem = FileSystem() previous_path = '' path = filesystem.abspath(path) while path and path != previous_path: if filesystem.exists(filesystem.join(path, cls._stub_repository_json)): return callback(path) previous_path = path path = filesystem.dirname(path) return None
def test_read_text_file_unicode_decode_error(self): fs = FileSystem() text_path = None try: text_path = tempfile.mktemp(prefix='write_text_unittest_') fs.write_binary_file(text_path, bytearray(b'\x73\x74\x72\x8b')) self.assertRaises(UnicodeDecodeError, fs.read_text_file, text_path) self.assertEqual(u'str\ufffd', fs.read_text_file(text_path, errors='replace')) self.assertEqual('str', fs.read_text_file(text_path, errors='ignore')) finally: if text_path and fs.isfile(text_path): os.remove(text_path)
def test_default_configuration__standalone(self): # FIXME: This test runs a standalone python script to test # reading the default configuration to work around any possible # caching / reset bugs. See https://bugs.webkit.org/show_bug.cgi?id=49360 # for the motivation. We can remove this test when we remove the # global configuration cache in config.py. e = Executive() fs = FileSystem() c = config.Config(e, fs) script = WebKitFinder(fs).path_from_webkit_base('Tools', 'Scripts', 'webkitpy', 'layout_tests', 'port', 'config_standalone.py') # Note: don't use 'Release' here, since that's the normal default. expected = 'Debug' args = [sys.executable, script, '--mock', expected] actual = e.run_command(args).rstrip() self.assertEqual(actual, expected)
def test_dirs_under(self): fs = FileSystem() parentDir = fs.normpath(fs.join(self._this_dir, "..")) self.assertTrue(self._this_dir in fs.dirs_under(parentDir)) def filter_no_dir(fs, dirpath): return False self.assertEqual(len(fs.dirs_under(parentDir, filter_no_dir)), 0) def filter_this_dir(fs, dirpath): return dirpath != self._this_dir self.assertFalse( self._this_dir in fs.dirs_under(parentDir, filter_this_dir))
def _curent_lock_pid(self): """Return with the current lock pid. If the lock is not valid it deletes the lock file.""" lock_list = self._lock_file_list() if not lock_list: return try: current_lock_file = open(lock_list[0], 'r') current_pid = current_lock_file.readline() current_lock_file.close() if not (current_pid and self._executive.check_running_pid(int(current_pid))): _log.debug("Removing stuck lock file: %s" % lock_list[0]) FileSystem().remove(lock_list[0]) return except (IOError, OSError): return return int(current_pid)
def setUp(self): # FIXME: This should not need to touch the filesystem, however # ChangeLog is difficult to mock at current. self.filesystem = FileSystem() self.temp_dir = str(self.filesystem.mkdtemp(suffix="changelogs")) self.old_cwd = self.filesystem.getcwd() self.filesystem.chdir(self.temp_dir) # Trick commit-log-editor into thinking we're in a Subversion working copy so it won't # complain about not being able to figure out what SCM is in use. # FIXME: VCSTools.pm is no longer so easily fooled. It logs because "svn info" doesn't # treat a bare .svn directory being part of an svn checkout. self.filesystem.maybe_make_directory(".svn") self.changelogs = map(self.filesystem.abspath, (self.filesystem.join("Tools", "ChangeLog"), self.filesystem.join("LayoutTests", "ChangeLog"))) for path, contents in zip(self.changelogs, (_changelog1, _changelog2)): self.filesystem.maybe_make_directory(self.filesystem.dirname(path)) self.filesystem.write_text_file(path, contents)
def test_run(self, verbose=0, timing=False, child_processes=1, quiet=False): options = MockOptions(verbose=verbose, timing=timing, child_processes=child_processes, quiet=quiet, pass_through=False) stream = StringIO.StringIO() loader = FakeLoader(('test1 (Foo)', '.', ''), ('test2 (Foo)', 'F', 'test2\nfailed'), ('test3 (Foo)', 'E', 'test3\nerred')) runner = Runner(Printer(stream, options), loader, WebKitFinder(FileSystem())) runner.run(['Foo.test1', 'Foo.test2', 'Foo.test3'], 1) self.assertEqual(runner.tests_run, 3) self.assertEqual(len(runner.failures), 1) self.assertEqual(len(runner.errors), 1)
def run_pylint(self, path): finder = PathFinder(FileSystem()) executive = Executive() env = os.environ.copy() env['PYTHONPATH'] = os.pathsep.join([ finder.path_from_tools_scripts(), finder.path_from_blink_source('build', 'scripts'), get_blinkpy_thirdparty_dir(), get_blink_tools_dir(), finder.path_from_blink_source('bindings', 'scripts'), finder.path_from_chromium_base('build', 'android'), finder.path_from_chromium_base('third_party', 'catapult', 'devil'), finder.path_from_chromium_base('third_party', 'pymock'), ]) return executive.run_command([ sys.executable, finder.path_from_depot_tools_base('pylint.py'), '--output-format=parseable', '--rcfile=' + finder.path_from_tools_scripts('webkitpy', 'pylintrc'), path, ], env=env, error_handler=executive.ignore_error)
def main(): logging.basicConfig(level=logging.DEBUG, format='[%(asctime)s %(levelname)s %(name)s] %(message)s', datefmt='%H:%M:%S') parser = argparse.ArgumentParser(description='Blink source mover') sub_parsers = parser.add_subparsers() update_parser = sub_parsers.add_parser('update') update_parser.set_defaults(command='update') update_parser.add_argument('--run', dest='run', action='store_true', help='Update file contents') move_parser = sub_parsers.add_parser('move') move_parser.set_defaults(command='move') move_parser.add_argument('--git', dest='run_git', action='store_true', help='Run |git mv| command instead of |mv|.') options = parser.parse_args() mover = MoveBlinkSource(FileSystem(), options, get_chromium_src_dir()) if options.command == 'update': mover.update() elif options.command == 'move': mover.move()
def do_POST(self): json_raw_data = self.rfile.read(int(self.headers.getheader('content-length'))) json_data = json.loads(json_raw_data) test_list = '' for each in json_data['tests']: test_list += each + ' ' filesystem = FileSystem() webkit_finder = WebKitFinder(filesystem) script_dir = webkit_finder.path_from_webkit_base('Tools', 'Scripts') executable_path = script_dir + '/run-webkit-tests' cmd = 'python ' + executable_path + ' --no-show-results ' cmd += test_list process = subprocess.Popen(cmd, shell=True, cwd=script_dir, env=None, stdout=subprocess.PIPE, stderr=STDOUT) self.send_response(200) self.send_header('Access-Control-Allow-Origin', '*') self.send_header('Content-type', 'text/html') self.end_headers() while process.poll() is None: html_output = '<br>' + str(process.stdout.readline()) self.wfile.write(html_output) self.wfile.flush() time.sleep(0.05) process.wait()
def run_pylint(self, path): wkf = WebKitFinder(FileSystem()) executive = Executive() env = os.environ.copy() env['PYTHONPATH'] = os.pathsep.join([ wkf.path_from_webkit_base('Tools', 'Scripts'), wkf.path_from_webkit_base('Source', 'build', 'scripts'), wkf.path_from_webkit_base('Tools', 'Scripts', 'webkitpy', 'thirdparty'), wkf.path_from_webkit_base('Source', 'bindings', 'scripts'), wkf.path_from_chromium_base('build', 'android'), wkf.path_from_chromium_base('third_party', 'catapult', 'devil'), wkf.path_from_chromium_base('third_party', 'pymock'), ]) return executive.run_command([ sys.executable, wkf.path_from_depot_tools_base('pylint.py'), '--output-format=parseable', '--rcfile=' + wkf.path_from_webkit_base('Tools', 'Scripts', 'webkitpy', 'pylintrc'), path, ], env=env, error_handler=executive.ignore_error)
def test_join(self): fs = FileSystem() self.assertEqual(fs.join('foo', 'bar'), os.path.join('foo', 'bar'))
class CommitMessageForThisCommitTest(unittest.TestCase): expected_commit_message = u"""Unreviewed build fix to un-break webkit-patch land. Tools: Move commit_message_for_this_commit from scm to checkout https://bugs.webkit.org/show_bug.cgi?id=36629 * Scripts/webkitpy/common/checkout/api.py: import scm.CommitMessage LayoutTests: Second part of this complicated change by me, Tor Arne Vestb\u00f8! * Path/To/Complicated/File: Added. """ def setUp(self): # FIXME: This should not need to touch the filesystem, however # ChangeLog is difficult to mock at current. self.filesystem = FileSystem() self.temp_dir = str(self.filesystem.mkdtemp(suffix="changelogs")) self.old_cwd = self.filesystem.getcwd() self.filesystem.chdir(self.temp_dir) # Trick commit-log-editor into thinking we're in a Subversion working copy so it won't # complain about not being able to figure out what SCM is in use. # FIXME: VCSTools.pm is no longer so easily fooled. It logs because "svn info" doesn't # treat a bare .svn directory being part of an svn checkout. self.filesystem.maybe_make_directory(".svn") self.changelogs = map(self.filesystem.abspath, (self.filesystem.join("Tools", "ChangeLog"), self.filesystem.join("LayoutTests", "ChangeLog"))) for path, contents in zip(self.changelogs, (_changelog1, _changelog2)): self.filesystem.maybe_make_directory(self.filesystem.dirname(path)) self.filesystem.write_text_file(path, contents) def tearDown(self): self.filesystem.rmtree(self.temp_dir) self.filesystem.chdir(self.old_cwd) def test_commit_message_for_this_commit(self): executive = Executive() def mock_run(*args, **kwargs): # Note that we use a real Executive here, not a MockExecutive, so we can test that we're # invoking commit-log-editor correctly. env = os.environ.copy() env['CHANGE_LOG_EMAIL_ADDRESS'] = '*****@*****.**' kwargs['env'] = env return executive.run_command(*args, **kwargs) detector = SCMDetector(self.filesystem, executive) real_scm = detector.detect_scm_system(self.old_cwd) mock_scm = MockSCM() mock_scm.run = mock_run mock_scm.script_path = real_scm.script_path checkout = Checkout(mock_scm) checkout.modified_changelogs = lambda git_commit, changed_files=None: self.changelogs commit_message = checkout.commit_message_for_this_commit(git_commit=None, return_stderr=True) # Throw away the first line - a warning about unknown VCS root. commit_message.message_lines = commit_message.message_lines[1:] self.assertEqual(commit_message.message(), self.expected_commit_message)
_THIRDPARTY_DIR = os.path.dirname(__file__) _AUTOINSTALLED_DIR = os.path.join(_THIRDPARTY_DIR, "autoinstalled") # Putting the autoinstall code into webkitpy/thirdparty/__init__.py # ensures that no autoinstalling occurs until a caller imports from # webkitpy.thirdparty. This is useful if the caller wants to configure # logging prior to executing autoinstall code. # FIXME: If any of these servers is offline, webkit-patch breaks (and maybe # other scripts do, too). See <http://webkit.org/b/42080>. # We put auto-installed third-party modules in this directory-- # # webkitpy/thirdparty/autoinstalled fs = FileSystem() fs.maybe_make_directory(_AUTOINSTALLED_DIR) init_path = fs.join(_AUTOINSTALLED_DIR, "__init__.py") if not fs.exists(init_path): fs.write_text_file(init_path, "") readme_path = fs.join(_AUTOINSTALLED_DIR, "README") if not fs.exists(readme_path): fs.write_text_file(readme_path, "This directory is auto-generated by WebKit and is " "safe to delete.\nIt contains needed third-party Python " "packages automatically downloaded from the web.") class AutoinstallImportHook(object):
def test_sep(self): fs = FileSystem() self.assertEqual(fs.sep, os.sep) self.assertEqual(fs.join("foo", "bar"), os.path.join("foo", "bar"))
def setUp(self): self.executive = Executive() self.fs = FileSystem() self.original_cwd = self.fs.getcwd()
class SCMTestBase(unittest.TestCase): def __init__(self, *args, **kwargs): super(SCMTestBase, self).__init__(*args, **kwargs) self.scm = None self.executive = None self.fs = None self.original_cwd = None def setUp(self): self.executive = Executive() self.fs = FileSystem() self.original_cwd = self.fs.getcwd() def tearDown(self): self._chdir(self.original_cwd) def _join(self, *comps): return self.fs.join(*comps) def _chdir(self, path): self.fs.chdir(path) def _mkdir(self, path): assert not self.fs.exists(path) self.fs.maybe_make_directory(path) def _mkdtemp(self, **kwargs): return str(self.fs.mkdtemp(**kwargs)) def _remove(self, path): self.fs.remove(path) def _rmtree(self, path): self.fs.rmtree(path) def _run(self, *args, **kwargs): return self.executive.run_command(*args, **kwargs) def _run_silent(self, args, **kwargs): self.executive.run_and_throw_if_fail(args, quiet=True, **kwargs) def _write_text_file(self, path, contents): self.fs.write_text_file(path, contents) def _write_binary_file(self, path, contents): self.fs.write_binary_file(path, contents) def _make_diff(self, command, *args): # We use this wrapper to disable output decoding. diffs should be treated as # binary files since they may include text files of multiple differnet encodings. return self._run([command, "diff"] + list(args), decode_output=False) def _svn_diff(self, *args): return self._make_diff("svn", *args) def _git_diff(self, *args): return self._make_diff("git", *args) def _svn_add(self, path): self._run(["svn", "add", path]) def _svn_commit(self, message): self._run(["svn", "commit", "--quiet", "--message", message]) # This is a hot function since it's invoked by unittest before calling each test_ method in SVNTest and # GitTest. We create a mock SVN repo once and then perform an SVN checkout from a filesystem copy of # it since it's expensive to create the mock repo. def _set_up_svn_checkout(self): global cached_svn_repo_path global original_cwd if not cached_svn_repo_path: cached_svn_repo_path = self._set_up_svn_repo() original_cwd = self.original_cwd self.temp_directory = self._mkdtemp(suffix="svn_test") self.svn_repo_path = self._join(self.temp_directory, "repo") self.svn_repo_url = "file://%s" % self.svn_repo_path self.svn_checkout_path = self._join(self.temp_directory, "checkout") shutil.copytree(cached_svn_repo_path, self.svn_repo_path) self._run(["svn", "checkout", "--quiet", self.svn_repo_url + "/trunk", self.svn_checkout_path]) def _set_up_svn_repo(self): svn_repo_path = self._mkdtemp(suffix="svn_test_repo") svn_repo_url = "file://%s" % svn_repo_path # Not sure this will work on windows # git svn complains if we don't pass --pre-1.5-compatible, not sure why: # Expected FS format '2'; found format '3' at /usr/local/libexec/git-core//git-svn line 1477 self._run(["svnadmin", "create", "--pre-1.5-compatible", svn_repo_path]) # Create a test svn checkout svn_checkout_path = self._mkdtemp(suffix="svn_test_checkout") self._run(["svn", "checkout", "--quiet", svn_repo_url, svn_checkout_path]) # Create and checkout a trunk dir to match the standard svn configuration to match git-svn's expectations self._chdir(svn_checkout_path) self._mkdir("trunk") self._svn_add("trunk") # We can add tags and branches as well if we ever need to test those. self._svn_commit("add trunk") self._rmtree(svn_checkout_path) self._set_up_svn_test_commits(svn_repo_url + "/trunk") return svn_repo_path def _set_up_svn_test_commits(self, svn_repo_url): svn_checkout_path = self._mkdtemp(suffix="svn_test_checkout") self._run(["svn", "checkout", "--quiet", svn_repo_url, svn_checkout_path]) # Add some test commits self._chdir(svn_checkout_path) self._write_text_file("test_file", "test1") self._svn_add("test_file") self._svn_commit("initial commit") self._write_text_file("test_file", "test1test2") # This used to be the last commit, but doing so broke # GitTest.test_apply_git_patch which use the inverse diff of the last commit. # svn-apply fails to remove directories in Git, see: # https://bugs.webkit.org/show_bug.cgi?id=34871 self._mkdir("test_dir") # Slash should always be the right path separator since we use cygwin on Windows. test_file3_path = "test_dir/test_file3" self._write_text_file(test_file3_path, "third file") self._svn_add("test_dir") self._svn_commit("second commit") self._write_text_file("test_file", "test1test2test3\n") self._write_text_file("test_file2", "second file") self._svn_add("test_file2") self._svn_commit("third commit") # This 4th commit is used to make sure that our patch file handling # code correctly treats patches as binary and does not attempt to # decode them assuming they're utf-8. self._write_binary_file("test_file", u"latin1 test: \u00A0\n".encode("latin-1")) self._write_binary_file("test_file2", u"utf-8 test: \u00A0\n".encode("utf-8")) self._svn_commit("fourth commit") # svn does not seem to update after commit as I would expect. self._run(["svn", "update"]) self._rmtree(svn_checkout_path) def _tear_down_svn_checkout(self): self._rmtree(self.temp_directory) def _shared_test_add_recursively(self): self._mkdir("added_dir") self._write_text_file("added_dir/added_file", "new stuff") self.scm.add("added_dir/added_file") self.assertIn("added_dir/added_file", self.scm._added_files()) def _shared_test_delete_recursively(self): self._mkdir("added_dir") self._write_text_file("added_dir/added_file", "new stuff") self.scm.add("added_dir/added_file") self.assertIn("added_dir/added_file", self.scm._added_files()) self.scm.delete("added_dir/added_file") self.assertNotIn("added_dir", self.scm._added_files()) def _shared_test_delete_recursively_or_not(self): self._mkdir("added_dir") self._write_text_file("added_dir/added_file", "new stuff") self._write_text_file("added_dir/another_added_file", "more new stuff") self.scm.add("added_dir/added_file") self.scm.add("added_dir/another_added_file") self.assertIn("added_dir/added_file", self.scm._added_files()) self.assertIn("added_dir/another_added_file", self.scm._added_files()) self.scm.delete("added_dir/added_file") self.assertIn("added_dir/another_added_file", self.scm._added_files()) def _shared_test_exists(self, scm, commit_function): self._chdir(scm.checkout_root) self.assertFalse(scm.exists("foo.txt")) self._write_text_file("foo.txt", "some stuff") self.assertFalse(scm.exists("foo.txt")) scm.add("foo.txt") commit_function("adding foo") self.assertTrue(scm.exists("foo.txt")) scm.delete("foo.txt") commit_function("deleting foo") self.assertFalse(scm.exists("foo.txt")) def _shared_test_move(self): self._write_text_file("added_file", "new stuff") self.scm.add("added_file") self.scm.move("added_file", "moved_file") self.assertIn("moved_file", self.scm._added_files()) def _shared_test_move_recursive(self): self._mkdir("added_dir") self._write_text_file("added_dir/added_file", "new stuff") self._write_text_file("added_dir/another_added_file", "more new stuff") self.scm.add("added_dir") self.scm.move("added_dir", "moved_dir") self.assertIn("moved_dir/added_file", self.scm._added_files()) self.assertIn("moved_dir/another_added_file", self.scm._added_files())
def test_isdir__false(self): fs = FileSystem() self.assertFalse(fs.isdir(self._this_file))
class ExportFileTest(unittest.TestCase): def setUp(self): self._filesystem = FileSystem() self._temp_dir = str(self._filesystem.mkdtemp(suffix="exportfiles")) self._old_cwd = self._filesystem.getcwd() self._filesystem.chdir(self._temp_dir) self._filesystem.write_text_file(os.path.join(self._temp_dir, "sorted_file.exp.in"), _sorted_file_contents) self._filesystem.write_text_file(os.path.join(self._temp_dir, "non_sorted_file.exp.in"), _non_sorted_file_contents) self._filesystem.write_text_file(os.path.join(self._temp_dir, "parse_error_file.exp.in"), _parse_error_file_contents) def tearDown(self): self._filesystem.rmtree(self._temp_dir) self._filesystem.chdir(self._old_cwd) def test_sorted(self): """ Test sorted file. """ file_path = os.path.join(self._temp_dir, "sorted_file.exp.in") error_handler = MockErrorHandler(handle_style_error) error_handler.errors = [] error_handler.had_error = False checker = ExportFileChecker(file_path, error_handler) checker.check() self.assertFalse(error_handler.had_error) def test_non_sorted(self): """ Test non sorted file. """ file_path = os.path.join(self._temp_dir, "non_sorted_file.exp.in") error_handler = MockErrorHandler(handle_style_error) error_handler.errors = [] error_handler.had_error = False checker = ExportFileChecker(file_path, error_handler) checker.check() self.assertTrue(error_handler.had_error) self.assertEqual(error_handler.errors[0], (0, 'list/order', 5, file_path + " should be sorted, use Tools/Scripts/sort-export-file script")) def test_parse_error(self): """ Test parse error file. """ file_path = os.path.join(self._temp_dir, "parse_error_file.exp.in") error_handler = MockErrorHandler(handle_style_error) error_handler.errors = [] error_handler.had_error = False checker = ExportFileChecker(file_path, error_handler) checker.check() self.assertTrue(error_handler.had_error) self.assertEqual(error_handler.errors[0], (0, 'list/order', 5, "Parse error during processing " + file_path + ", use Tools/Scripts/sort-export-files for details"))