rietveld_patcher_test.py revision f2477e01787aa58f445919b809d89e252beef54f
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 posixpath
8import sys
9import unittest
10from appengine_url_fetcher import AppEngineUrlFetcher
11from extensions_paths import (
12    ARTICLES_TEMPLATES, EXTENSIONS, DOCS, JSON_TEMPLATES, PUBLIC_TEMPLATES)
13from fake_fetchers import ConfigureFakeFetchers
14from file_system import FileNotFoundError
15from rietveld_patcher import RietveldPatcher
16import url_constants
17
18
19def _PrefixWith(prefix, lst):
20  return [posixpath.join(prefix, item) for item in lst]
21
22
23class RietveldPatcherTest(unittest.TestCase):
24  def setUp(self):
25    ConfigureFakeFetchers()
26    self._patcher = RietveldPatcher(
27        '14096030',
28        AppEngineUrlFetcher(url_constants.CODEREVIEW_SERVER))
29
30  def _ReadLocalFile(self, filename):
31    with open(os.path.join(sys.path[0],
32                           'test_data',
33                           'rietveld_patcher',
34                           'expected',
35                           filename), 'r') as f:
36      return f.read()
37
38  def _ApplySingle(self, path, binary=False):
39    return self._patcher.Apply([path], None, binary).Get()[path]
40
41  def testGetVersion(self):
42    self.assertEqual(self._patcher.GetVersion(), '22002')
43
44  def testGetPatchedFiles(self):
45    added, deleted, modified = self._patcher.GetPatchedFiles()
46    self.assertEqual(
47        sorted(added),
48        _PrefixWith(DOCS, ['examples/test',
49                           'templates/articles/test_foo.html',
50                           'templates/public/extensions/test_foo.html']))
51    self.assertEqual(deleted,
52                     ['%s/extensions/runtime.html' % PUBLIC_TEMPLATES])
53    self.assertEqual(
54        sorted(modified),
55        _PrefixWith(EXTENSIONS, ['api/test.json',
56                                 'docs/templates/json/extensions_sidenav.json',
57                                 'manifest.h']))
58
59  def testApply(self):
60    article_path = '%s/test_foo.html' % ARTICLES_TEMPLATES
61
62    # Make sure RietveldPatcher handles |binary| correctly.
63    self.assertTrue(isinstance(self._ApplySingle(article_path, True), str),
64        'Expected result is binary. It was text.')
65    self.assertTrue(isinstance(self._ApplySingle(article_path), unicode),
66        'Expected result is text. It was binary.')
67
68    # Apply to an added file.
69    self.assertEqual(
70        self._ReadLocalFile('test_foo.html'),
71        self._ApplySingle('%s/extensions/test_foo.html' % PUBLIC_TEMPLATES))
72
73    # Apply to a modified file.
74    self.assertEqual(
75        self._ReadLocalFile('extensions_sidenav.json'),
76        self._ApplySingle('%s/extensions_sidenav.json' % JSON_TEMPLATES))
77
78    # Applying to a deleted file doesn't throw exceptions. It just returns
79    # empty content.
80    # self.assertRaises(FileNotFoundError, self._ApplySingle,
81    #     'docs/templates/public/extensions/runtime.html')
82
83    # Apply to an unknown file.
84    self.assertRaises(FileNotFoundError, self._ApplySingle, 'not_existing')
85
86if __name__ == '__main__':
87  unittest.main()
88