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)
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) processor = TextFileReaderTest.MockProcessor() temp_dir = tempfile.mkdtemp() self._file_reader = TextFileReader(processor) self._processor = processor self._temp_dir = temp_dir def tearDown(self): LoggingTestCase.tearDown(self) shutil.rmtree(self._temp_dir) def _create_file(self, rel_path, text, encoding="utf-8"): """Create a file with given text and return the path to the file.""" # FIXME: There are better/more secure APIs for creatin tmp file paths. file_path = os.path.join(self._temp_dir, rel_path) with codecs.open(file_path, "w", encoding) as file: file.write(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.assertEquals(passed_to_processor, self._passed_to_processor()) self.assertEquals(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.assertEquals(str(err), '1') else:
def setUp(self): LoggingTestCase.setUp(self) processor = TextFileReaderTest.MockProcessor() temp_dir = tempfile.mkdtemp() self._file_reader = TextFileReader(processor) self._processor = processor self._temp_dir = temp_dir
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 main(): # Change stderr to write with replacement characters so we don't die # if we try to print something containing non-ASCII characters. stderr_stream = codecs.StreamReaderWriter(sys.stderr, codecs.getreader("utf8"), codecs.getwriter("utf8"), "replace") # Setting an "encoding" attribute on the stream is necessary to # prevent the logging module from raising an error. See # the checker.configure_logging() function for more information. stderr_stream.encoding = "UTF-8" args = sys.argv[1:] # Checking for the verbose flag before calling check_webkit_style_parser() # lets us enable verbose logging earlier. is_verbose = "-v" in args or "--verbose" in args checker.configure_logging(stream=stderr_stream, is_verbose=is_verbose) _log.debug("Verbose logging enabled.") parser = OptionParser() parser.add_option( "-v", "--verbose", action="store_true", dest="verbose", default=False, help="print status messages to stdout" ) parser.add_option( "-d", "--define", action="store", type="string", dest="define", default="", help="specify define to eliminate" ) (options, paths) = parser.parse_args() _log.debug("paths: %s, options: %s" % (paths, options)) cwd = os.path.abspath(os.curdir) if not paths: _log.error("Give at least one specific path to eliminate define.") sys.exit(1) checkout_root = None paths = change_directory(checkout_root=checkout_root, paths=paths) code_eliminator = CodeEliminator(options.define) file_reader = TextFileReader(code_eliminator) file_reader.process_paths(paths) file_count = file_reader.file_count _log.info("%d files" % (file_count)) # We fail when there are no checked files. sys.exit((file_count == 0))
def main(self): args = sys.argv[1:] host = Host() host.initialize_scm() stderr = self._engage_awesome_stderr_hacks() # Checking for the verbose flag before calling check_webkit_style_parser() # lets us enable verbose logging earlier. is_verbose = "-v" in args or "--verbose" in args checker.configure_logging(stream=stderr, is_verbose=is_verbose) _log.debug("Verbose logging enabled.") parser = checker.check_webkit_style_parser() (paths, options) = parser.parse(args) configuration = checker.check_webkit_style_configuration(options) paths = change_directory(host.filesystem, checkout_root=host.scm().checkout_root, paths=paths) style_processor = StyleProcessor(configuration) file_reader = TextFileReader(host.filesystem, style_processor) if paths and not options.diff_files: file_reader.process_paths(paths) else: changed_files = paths if options.diff_files else None patch = host.scm().create_patch(options.git_commit, changed_files=changed_files, git_index=options.git_index) patch_checker = PatchReader(file_reader) patch_checker.check(patch) error_count = style_processor.error_count file_count = file_reader.file_count delete_only_file_count = file_reader.delete_only_file_count _log.info("Total errors found: %d in %d files" % (error_count, file_count)) # We fail when style errors are found or there are no checked files. return error_count > 0 or (file_count == 0 and delete_only_file_count == 0)
def main(self): args = sys.argv[1:] host = Host() host.initialize_scm() stderr = self._engage_awesome_stderr_hacks() # Checking for the verbose flag before calling check_webkit_style_parser() # lets us enable verbose logging earlier. is_verbose = "-v" in args or "--verbose" in args checker.configure_logging(stream=stderr, is_verbose=is_verbose) _log.debug("Verbose logging enabled.") parser = checker.check_webkit_style_parser() (paths, options) = parser.parse(args) configuration = checker.check_webkit_style_configuration(options) paths = change_directory(host.filesystem, checkout_root=host.scm().checkout_root, paths=paths) style_processor = StyleProcessor(configuration) file_reader = TextFileReader(host.filesystem, style_processor) if paths and not options.diff_files: file_reader.process_paths(paths) else: changed_files = paths if options.diff_files else None patch = host.scm().create_patch(options.git_commit, changed_files=changed_files) patch_checker = PatchReader(file_reader) patch_checker.check(patch) error_count = style_processor.error_count file_count = file_reader.file_count delete_only_file_count = file_reader.delete_only_file_count _log.info("Total errors found: %d in %d files" % (error_count, file_count)) # We fail when style errors are found or there are no checked files. return error_count > 0
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 as err: self.assertEqual(str(err), '1') else: self.fail('No Exception raised.') self._assert_file_reader([], 1) self.assertLog(["ERROR: File does not exist: 'does_not_exist.txt'\n"]) def test_process_file__is_dir(self): temp_dir = self.filesystem.join(self._temp_dir, 'test_dir') self.filesystem.maybe_make_directory(temp_dir) self._file_reader.process_file(temp_dir) # Because the log message below contains exception text, it is # possible that the text varies across platforms. For this reason, # we check only the portion of the log message that we control, # namely the text at the beginning. log_messages = self.logMessages() # We remove the message we are looking at to prevent the tearDown() # from raising an exception when it asserts that no log messages # remain. message = log_messages.pop() self.assertTrue( message.startswith( "WARNING: Could not read file. Skipping: '%s'\n " % temp_dir)) self._assert_file_reader([], 1) def test_process_file__should_not_process(self): file_path = self._create_file('should_not_process.txt', 'contents') self._file_reader.process_file(file_path) self._assert_file_reader([], 1) def test_process_file__multiple_lines(self): file_path = self._create_file('foo.txt', 'line one\r\nline two\n') self._file_reader.process_file(file_path) processed = [(['line one\r', 'line two', ''], file_path, None)] self._assert_file_reader(processed, 1) def test_process_file__file_stdin(self): file_path = self._create_file('-', 'file contents') self._file_reader.process_file(file_path=file_path, test_kwarg='foo') processed = [(['file contents'], file_path, 'foo')] self._assert_file_reader(processed, 1) def test_process_file__with_kwarg(self): file_path = self._create_file('foo.txt', 'file contents') self._file_reader.process_file(file_path=file_path, test_kwarg='foo') processed = [(['file contents'], file_path, 'foo')] self._assert_file_reader(processed, 1) def test_process_paths(self): # We test a list of paths that contains both a file and a directory. dir = self.filesystem.join(self._temp_dir, 'foo_dir') self.filesystem.maybe_make_directory(dir) file_path1 = self._create_file('file1.txt', 'foo') rel_path = self.filesystem.join('foo_dir', 'file2.txt') file_path2 = self._create_file(rel_path, 'bar') self._file_reader.process_paths([dir, file_path1]) processed = [(['bar'], file_path2, None), (['foo'], file_path1, None)] self._assert_file_reader(processed, 2) def test_count_delete_only_file(self): self._file_reader.count_delete_only_file() delete_only_file_count = self._file_reader.delete_only_file_count self.assertEqual(delete_only_file_count, 1)
def _generate_file_reader(self, file_system): style_processor = StyleProcessor(self._style_checker_configuration) return TextFileReader(file_system, style_processor)
def run_style_tests(self, suite_args): # TODO: not yet ready return True # Code based on http://svn.webkit.org/repository/webkit/trunk/Tools/Scripts/check-webkit-style thisdir = os.path.dirname(os.path.abspath(__file__)) args = suite_args if not args: args = [os.path.abspath( os.path.join(thisdir, os.pardir, os.pardir, os.pardir) )] _log = log sys.path.append(os.path.join(thisdir, os.pardir, 'third_party')) from webkitpy.style_references import detect_checkout import webkitpy.style.checker as checker from webkitpy.style.patchreader import PatchReader from webkitpy.style.checker import StyleProcessor from webkitpy.style.filereader import TextFileReader from webkitpy.style.main import change_directory # Change stderr to write with replacement characters so we don't die # if we try to print something containing non-ASCII characters. stderr = codecs.StreamReaderWriter(sys.stderr, codecs.getreader('utf8'), codecs.getwriter('utf8'), 'replace') # Setting an "encoding" attribute on the stream is necessary to # prevent the logging module from raising an error. See # the checker.configure_logging() function for more information. stderr.encoding = "UTF-8" # FIXME: Change webkitpy.style so that we do not need to overwrite # the global sys.stderr. This involves updating the code to # accept a stream parameter where necessary, and not calling # sys.stderr explicitly anywhere. sys.stderr = stderr #SYNTH args = sys.argv[1:] # Checking for the verbose flag before calling check_webkit_style_parser() # lets us enable verbose logging earlier. is_verbose = "-v" in args or "--verbose" in args checker.configure_logging(stream=stderr, is_verbose=is_verbose) _log.debug("Verbose logging enabled.") parser = checker.check_webkit_style_parser() (paths, options) = parser.parse(args) checkout = detect_checkout() if checkout is None: if not paths: _log.error("WebKit checkout not found: You must run this script " "from within a WebKit checkout if you are not passing " "specific paths to check.") sys.exit(1) checkout_root = None _log.debug("WebKit checkout not found for current directory.") else: checkout_root = checkout.root_path() _log.debug("WebKit checkout found with root: %s" % checkout_root) configuration = checker.check_webkit_style_configuration(options) paths = change_directory(checkout_root=checkout_root, paths=paths) style_processor = StyleProcessor(configuration) file_reader = TextFileReader(style_processor) if paths and not options.diff_files: file_reader.process_paths(paths) else: changed_files = paths if options.diff_files else None patch = checkout.create_patch(options.git_commit, changed_files=changed_files) patch_checker = PatchReader(file_reader) patch_checker.check(patch) error_count = style_processor.error_count file_count = file_reader.file_count delete_only_file_count = file_reader.delete_only_file_count _log.info("Total errors found: %d in %d files" % (error_count, file_count)) # We fail when style errors are found or there are no checked files. #sys.exit(error_count > 0 or (file_count == 0 and delete_only_file_count == 0)) return not (error_count > 0 or (file_count == 0 and delete_only_file_count == 0))
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 as err: self.assertEqual(str(err), '1') else: self.fail('No Exception raised.') self._assert_file_reader([], 1) self.assertLog(["ERROR: File does not exist: 'does_not_exist.txt'\n"]) def test_process_file__is_dir(self): temp_dir = self.filesystem.join(self._temp_dir, 'test_dir') self.filesystem.maybe_make_directory(temp_dir) self._file_reader.process_file(temp_dir) # Because the log message below contains exception text, it is # possible that the text varies across platforms. For this reason, # we check only the portion of the log message that we control, # namely the text at the beginning. log_messages = self.logMessages() # We remove the message we are looking at to prevent the tearDown() # from raising an exception when it asserts that no log messages # remain. message = log_messages.pop() self.assertTrue(message.startswith("WARNING: Could not read file. Skipping: '%s'\n " % temp_dir)) self._assert_file_reader([], 1) def test_process_file__should_not_process(self): file_path = self._create_file('should_not_process.txt', 'contents') self._file_reader.process_file(file_path) self._assert_file_reader([], 1) def test_process_file__multiple_lines(self): file_path = self._create_file('foo.txt', 'line one\r\nline two\n') self._file_reader.process_file(file_path) processed = [(['line one\r', 'line two', ''], file_path, None)] self._assert_file_reader(processed, 1) def test_process_file__file_stdin(self): file_path = self._create_file('-', 'file contents') self._file_reader.process_file(file_path=file_path, test_kwarg='foo') processed = [(['file contents'], file_path, 'foo')] self._assert_file_reader(processed, 1) def test_process_file__with_kwarg(self): file_path = self._create_file('foo.txt', 'file contents') self._file_reader.process_file(file_path=file_path, test_kwarg='foo') processed = [(['file contents'], file_path, 'foo')] self._assert_file_reader(processed, 1) def test_process_paths(self): # We test a list of paths that contains both a file and a directory. dir = self.filesystem.join(self._temp_dir, 'foo_dir') self.filesystem.maybe_make_directory(dir) file_path1 = self._create_file('file1.txt', 'foo') rel_path = self.filesystem.join('foo_dir', 'file2.txt') file_path2 = self._create_file(rel_path, 'bar') self._file_reader.process_paths([dir, file_path1]) processed = [(['bar'], file_path2, None), (['foo'], file_path1, None)] self._assert_file_reader(processed, 2) def test_count_delete_only_file(self): self._file_reader.count_delete_only_file() delete_only_file_count = self._file_reader.delete_only_file_count self.assertEqual(delete_only_file_count, 1)