Home | History | Annotate | Download | only in style
      1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek (at] webkit.org)
      2 #
      3 # Redistribution and use in source and binary forms, with or without
      4 # modification, are permitted provided that the following conditions
      5 # are met:
      6 # 1.  Redistributions of source code must retain the above copyright
      7 #     notice, this list of conditions and the following disclaimer.
      8 # 2.  Redistributions in binary form must reproduce the above copyright
      9 #     notice, this list of conditions and the following disclaimer in the
     10 #     documentation and/or other materials provided with the distribution.
     11 #
     12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
     13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
     16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     22 
     23 import webkitpy.thirdparty.unittest2 as unittest
     24 
     25 from webkitpy.common.system.filesystem import FileSystem
     26 from webkitpy.common.system.logtesting import LoggingTestCase
     27 from webkitpy.style.checker import ProcessorBase
     28 from webkitpy.style.filereader import TextFileReader
     29 
     30 
     31 class TextFileReaderTest(LoggingTestCase):
     32 
     33     class MockProcessor(ProcessorBase):
     34 
     35         """A processor for test purposes.
     36 
     37         This processor simply records the parameters passed to its process()
     38         method for later checking by the unittest test methods.
     39 
     40         """
     41 
     42         def __init__(self):
     43             self.processed = []
     44             """The parameters passed for all calls to the process() method."""
     45 
     46         def should_process(self, file_path):
     47             return not file_path.endswith('should_not_process.txt')
     48 
     49         def process(self, lines, file_path, test_kwarg=None):
     50             self.processed.append((lines, file_path, test_kwarg))
     51 
     52     def setUp(self):
     53         LoggingTestCase.setUp(self)
     54         # FIXME: This should be a MockFileSystem once TextFileReader is moved entirely on top of FileSystem.
     55         self.filesystem = FileSystem()
     56         self._temp_dir = str(self.filesystem.mkdtemp())
     57         self._processor = TextFileReaderTest.MockProcessor()
     58         self._file_reader = TextFileReader(self.filesystem, self._processor)
     59 
     60     def tearDown(self):
     61         LoggingTestCase.tearDown(self)
     62         self.filesystem.rmtree(self._temp_dir)
     63 
     64     def _create_file(self, rel_path, text):
     65         """Create a file with given text and return the path to the file."""
     66         # FIXME: There are better/more secure APIs for creating tmp file paths.
     67         file_path = self.filesystem.join(self._temp_dir, rel_path)
     68         self.filesystem.write_text_file(file_path, text)
     69         return file_path
     70 
     71     def _passed_to_processor(self):
     72         """Return the parameters passed to MockProcessor.process()."""
     73         return self._processor.processed
     74 
     75     def _assert_file_reader(self, passed_to_processor, file_count):
     76         """Assert the state of the file reader."""
     77         self.assertEqual(passed_to_processor, self._passed_to_processor())
     78         self.assertEqual(file_count, self._file_reader.file_count)
     79 
     80     def test_process_file__does_not_exist(self):
     81         try:
     82             self._file_reader.process_file('does_not_exist.txt')
     83         except SystemExit, err:
     84             self.assertEqual(str(err), '1')
     85         else:
     86             self.fail('No Exception raised.')
     87         self._assert_file_reader([], 1)
     88         self.assertLog(["ERROR: File does not exist: 'does_not_exist.txt'\n"])
     89 
     90     def test_process_file__is_dir(self):
     91         temp_dir = self.filesystem.join(self._temp_dir, 'test_dir')
     92         self.filesystem.maybe_make_directory(temp_dir)
     93 
     94         self._file_reader.process_file(temp_dir)
     95 
     96         # Because the log message below contains exception text, it is
     97         # possible that the text varies across platforms.  For this reason,
     98         # we check only the portion of the log message that we control,
     99         # namely the text at the beginning.
    100         log_messages = self.logMessages()
    101         # We remove the message we are looking at to prevent the tearDown()
    102         # from raising an exception when it asserts that no log messages
    103         # remain.
    104         message = log_messages.pop()
    105 
    106         self.assertTrue(message.startswith("WARNING: Could not read file. Skipping: '%s'\n  " % temp_dir))
    107 
    108         self._assert_file_reader([], 1)
    109 
    110     def test_process_file__should_not_process(self):
    111         file_path = self._create_file('should_not_process.txt', 'contents')
    112 
    113         self._file_reader.process_file(file_path)
    114         self._assert_file_reader([], 1)
    115 
    116     def test_process_file__multiple_lines(self):
    117         file_path = self._create_file('foo.txt', 'line one\r\nline two\n')
    118 
    119         self._file_reader.process_file(file_path)
    120         processed = [(['line one\r', 'line two', ''], file_path, None)]
    121         self._assert_file_reader(processed, 1)
    122 
    123     def test_process_file__file_stdin(self):
    124         file_path = self._create_file('-', 'file contents')
    125 
    126         self._file_reader.process_file(file_path=file_path, test_kwarg='foo')
    127         processed = [(['file contents'], file_path, 'foo')]
    128         self._assert_file_reader(processed, 1)
    129 
    130     def test_process_file__with_kwarg(self):
    131         file_path = self._create_file('foo.txt', 'file contents')
    132 
    133         self._file_reader.process_file(file_path=file_path, test_kwarg='foo')
    134         processed = [(['file contents'], file_path, 'foo')]
    135         self._assert_file_reader(processed, 1)
    136 
    137     def test_process_paths(self):
    138         # We test a list of paths that contains both a file and a directory.
    139         dir = self.filesystem.join(self._temp_dir, 'foo_dir')
    140         self.filesystem.maybe_make_directory(dir)
    141 
    142         file_path1 = self._create_file('file1.txt', 'foo')
    143 
    144         rel_path = self.filesystem.join('foo_dir', 'file2.txt')
    145         file_path2 = self._create_file(rel_path, 'bar')
    146 
    147         self._file_reader.process_paths([dir, file_path1])
    148         processed = [(['bar'], file_path2, None),
    149                      (['foo'], file_path1, None)]
    150         self._assert_file_reader(processed, 2)
    151 
    152     def test_count_delete_only_file(self):
    153         self._file_reader.count_delete_only_file()
    154         delete_only_file_count = self._file_reader.delete_only_file_count
    155         self.assertEqual(delete_only_file_count, 1)
    156