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