rietveld_patcher_test.py revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
1#!/usr/bin/env python
2# Copyright 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import os
7import sys
8import unittest
9from appengine_url_fetcher import AppEngineUrlFetcher
10from fake_fetchers import ConfigureFakeFetchers
11from file_system import FileNotFoundError
12from rietveld_patcher import RietveldPatcher
13from svn_constants import EXTENSIONS_PATH
14import url_constants
15
16class RietveldPatcherTest(unittest.TestCase):
17  def setUp(self):
18    ConfigureFakeFetchers()
19    self._patcher = RietveldPatcher(
20        EXTENSIONS_PATH,
21        '14096030',
22        AppEngineUrlFetcher(url_constants.CODEREVIEW_SERVER))
23
24  def _ReadLocalFile(self, filename):
25    with open(os.path.join(sys.path[0],
26                           'test_data',
27                           'rietveld_patcher',
28                           'expected',
29                           filename), 'r') as f:
30      return f.read()
31
32  def _ApplySingle(self, path, binary=False):
33    return self._patcher.Apply([path], None, binary).Get()[path]
34
35  def testGetVersion(self):
36    self.assertEqual(self._patcher.GetVersion(), '22002')
37
38  def testGetPatchedFiles(self):
39    added, deleted, modified = self._patcher.GetPatchedFiles()
40    self.assertEqual(sorted(added),
41                     sorted([
42        u'docs/templates/articles/test_foo.html',
43        u'docs/templates/public/extensions/test_foo.html']))
44    self.assertEqual(sorted(deleted),
45                     sorted([
46        u'docs/templates/public/extensions/runtime.html']))
47    self.assertEqual(sorted(modified),
48                     sorted([
49        u'api/test.json',
50        u'docs/templates/json/extensions_sidenav.json']))
51
52  def testApply(self):
53    article_path = 'docs/templates/articles/test_foo.html'
54
55    # Make sure RietveldPatcher handles |binary| correctly.
56    self.assertTrue(isinstance(self._ApplySingle(article_path, True), str),
57        'Expected result is binary. It was text.')
58    self.assertTrue(isinstance(self._ApplySingle(article_path), unicode),
59        'Expected result is text. It was binary.')
60
61    # Apply to an added file.
62    self.assertEqual(
63        self._ReadLocalFile('test_foo.html'),
64        self._ApplySingle(
65            'docs/templates/public/extensions/test_foo.html'))
66
67    # Apply to a modified file.
68    self.assertEqual(
69        self._ReadLocalFile('extensions_sidenav.json'),
70        self._ApplySingle(
71            'docs/templates/json/extensions_sidenav.json'))
72
73    # Applying to a deleted file doesn't throw exceptions. It just returns
74    # empty content.
75    # self.assertRaises(FileNotFoundError, self._ApplySingle,
76    #     'docs/templates/public/extensions/runtime.html')
77
78    # Apply to an unknown file.
79    self.assertRaises(FileNotFoundError, self._ApplySingle, 'not_existing')
80
81if __name__ == '__main__':
82  unittest.main()
83