- Revision
- 100694
- Author
- e...@webkit.org
- Date
- 2011-11-17 15:55:10 -0800 (Thu, 17 Nov 2011)
Log Message
Teach TextFileReader about FileSystem
https://bugs.webkit.org/show_bug.cgi?id=72673
Reviewed by Adam Barth.
Unfortunately TextFileReader doesn't use FileSystem
everywhere yet, so we can't move the unittests to
using MockFileSystem, but we're close.
* Scripts/webkitpy/style/filereader.py:
* Scripts/webkitpy/style/filereader_unittest.py:
* Scripts/webkitpy/style/main.py:
Modified Paths
Diff
Modified: trunk/Tools/ChangeLog (100693 => 100694)
--- trunk/Tools/ChangeLog 2011-11-17 23:53:20 UTC (rev 100693)
+++ trunk/Tools/ChangeLog 2011-11-17 23:55:10 UTC (rev 100694)
@@ -1,5 +1,20 @@
2011-11-17 Eric Seidel <e...@webkit.org>
+ Teach TextFileReader about FileSystem
+ https://bugs.webkit.org/show_bug.cgi?id=72673
+
+ Reviewed by Adam Barth.
+
+ Unfortunately TextFileReader doesn't use FileSystem
+ everywhere yet, so we can't move the unittests to
+ using MockFileSystem, but we're close.
+
+ * Scripts/webkitpy/style/filereader.py:
+ * Scripts/webkitpy/style/filereader_unittest.py:
+ * Scripts/webkitpy/style/main.py:
+
+2011-11-17 Eric Seidel <e...@webkit.org>
+
Give check-webkit-style a Host
https://bugs.webkit.org/show_bug.cgi?id=72670
Modified: trunk/Tools/Scripts/webkitpy/style/filereader.py (100693 => 100694)
--- trunk/Tools/Scripts/webkitpy/style/filereader.py 2011-11-17 23:53:20 UTC (rev 100693)
+++ trunk/Tools/Scripts/webkitpy/style/filereader.py 2011-11-17 23:55:10 UTC (rev 100694)
@@ -54,13 +54,15 @@
"""
- def __init__(self, processor):
+ def __init__(self, filesystem, processor):
"""Create an instance.
Arguments:
processor: A ProcessorBase instance.
"""
+ # FIXME: Although TextFileReader requires a FileSystem it circumvents it in two places!
+ self.filesystem = filesystem
self._processor = processor
self.file_count = 0
self.delete_only_file_count = 0
@@ -83,6 +85,7 @@
# (codecs does not support it anyway), so the resulting
# lines contain trailing "\r" characters if we are reading
# a file with CRLF endings.
+ # FIXME: This should use self.filesystem
file = codecs.open(file_path, 'r', 'utf8', 'replace')
try:
@@ -108,9 +111,9 @@
"""
self.file_count += 1
- if not os.path.exists(file_path) and file_path != "-":
+ if not self.filesystem.exists(file_path) and file_path != "-":
_log.error("File does not exist: '%s'" % file_path)
- sys.exit(1)
+ sys.exit(1) # FIXME: This should throw or return instead of exiting directly.
if not self._processor.should_process(file_path):
_log.debug("Skipping file: '%s'" % file_path)
@@ -120,34 +123,23 @@
try:
lines = self._read_lines(file_path)
except IOError, err:
- message = ("Could not read file. Skipping: '%s'\n %s"
- % (file_path, err))
+ message = ("Could not read file. Skipping: '%s'\n %s" % (file_path, err))
_log.warn(message)
return
self._processor.process(lines, file_path, **kwargs)
def _process_directory(self, directory):
- """Process all files in the given directory, recursively.
-
- Args:
- directory: A directory path.
-
- """
+ """Process all files in the given directory, recursively."""
+ # FIXME: We should consider moving to self.filesystem.files_under() (or adding walk() to FileSystem)
for dir_path, dir_names, file_names in os.walk(directory):
for file_name in file_names:
- file_path = os.path.join(dir_path, file_name)
+ file_path = self.filesystem.join(dir_path, file_name)
self.process_file(file_path)
def process_paths(self, paths):
- """Process the given file and directory paths.
-
- Args:
- paths: A list of file and directory paths.
-
- """
for path in paths:
- if os.path.isdir(path):
+ if self.filesystem.isdir(path):
self._process_directory(directory=path)
else:
self.process_file(path)
Modified: trunk/Tools/Scripts/webkitpy/style/filereader_unittest.py (100693 => 100694)
--- trunk/Tools/Scripts/webkitpy/style/filereader_unittest.py 2011-11-17 23:53:20 UTC (rev 100693)
+++ trunk/Tools/Scripts/webkitpy/style/filereader_unittest.py 2011-11-17 23:55:10 UTC (rev 100694)
@@ -24,12 +24,9 @@
from __future__ import with_statement
-import codecs
-import os
-import shutil
-import tempfile
import unittest
+from webkitpy.common.system.filesystem import FileSystem
from webkitpy.common.system.logtesting import LoggingTestCase
from webkitpy.style.checker import ProcessorBase
from webkitpy.style.filereader import TextFileReader
@@ -58,24 +55,21 @@
def setUp(self):
LoggingTestCase.setUp(self)
- processor = TextFileReaderTest.MockProcessor()
+ # 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)
- 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)
+ self.filesystem.rmtree(self._temp_dir)
- def _create_file(self, rel_path, text, encoding="utf-8"):
+ 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 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)
+ # 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):
@@ -98,8 +92,8 @@
self.assertLog(["ERROR: File does not exist: 'does_not_exist.txt'\n"])
def test_process_file__is_dir(self):
- temp_dir = os.path.join(self._temp_dir, 'test_dir')
- os.mkdir(temp_dir)
+ temp_dir = self.filesystem.join(self._temp_dir, 'test_dir')
+ self.filesystem.maybe_make_directory(temp_dir)
self._file_reader.process_file(temp_dir)
@@ -113,8 +107,7 @@
# remain.
message = log_messages.pop()
- self.assertTrue(message.startswith('WARNING: Could not read file. '
- "Skipping: '%s'\n " % temp_dir))
+ self.assertTrue(message.startswith("WARNING: Could not read file. Skipping: '%s'\n " % temp_dir))
self._assert_file_reader([], 1)
@@ -147,12 +140,12 @@
def test_process_paths(self):
# We test a list of paths that contains both a file and a directory.
- dir = os.path.join(self._temp_dir, 'foo_dir')
- os.mkdir(dir)
+ 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 = os.path.join('foo_dir', 'file2.txt')
+ 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])
Modified: trunk/Tools/Scripts/webkitpy/style/main.py (100693 => 100694)
--- trunk/Tools/Scripts/webkitpy/style/main.py 2011-11-17 23:53:20 UTC (rev 100693)
+++ trunk/Tools/Scripts/webkitpy/style/main.py 2011-11-17 23:55:10 UTC (rev 100694)
@@ -192,7 +192,7 @@
paths = change_directory(host.filesystem, checkout_root=checkout_root, paths=paths)
style_processor = StyleProcessor(configuration)
- file_reader = TextFileReader(style_processor)
+ file_reader = TextFileReader(host.filesystem, style_processor)
if paths and not options.diff_files:
file_reader.process_paths(paths)