rietveld_patcher_test.py revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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
16from test_util import Server2Path
17import url_constants
18
19
20def _PrefixWith(prefix, lst):
21  return [posixpath.join(prefix, item) for item in lst]
22
23
24class RietveldPatcherTest(unittest.TestCase):
25  def setUp(self):
26    ConfigureFakeFetchers()
27    self._patcher = RietveldPatcher(
28        '14096030',
29        AppEngineUrlFetcher(url_constants.CODEREVIEW_SERVER))
30
31  def _ReadLocalFile(self, filename):
32    with open(Server2Path('test_data',
33                          'rietveld_patcher',
34                          'expected',
35                          filename), 'r') as f:
36      return f.read()
37
38  def _ApplySingle(self, path):
39    return self._patcher.Apply([path], None).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                     ['%sextensions/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 = '%stest_foo.html' % ARTICLES_TEMPLATES
61
62    # Apply to an added file.
63    self.assertEqual(
64        self._ReadLocalFile('test_foo.html'),
65        self._ApplySingle('%sextensions/test_foo.html' % PUBLIC_TEMPLATES))
66
67    # Apply to a modified file.
68    self.assertEqual(
69        self._ReadLocalFile('extensions_sidenav.json'),
70        self._ApplySingle('%sextensions_sidenav.json' % JSON_TEMPLATES))
71
72    # Applying to a deleted file doesn't throw exceptions. It just returns
73    # empty content.
74    # self.assertRaises(FileNotFoundError, self._ApplySingle,
75    #     'docs/templates/public/extensions/runtime.html')
76
77    # Apply to an unknown file.
78    self.assertRaises(FileNotFoundError, self._ApplySingle, 'not_existing')
79
80if __name__ == '__main__':
81  unittest.main()
82