Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: tests/test_multi_source.py

Issue 29555839: Issue 5336 - Allow additional include, page, and template paths using CMS (Closed)
Patch Set: Created Sept. 25, 2017, 7:12 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH
3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation.
7 #
8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15
16 from __future__ import unicode_literals
17
18 import pytest
19
20 from cms.sources import Source, MultiSource
21
22
23 class FakeSource(Source):
24 """Fake source object used for testing."""
25
26 def __init__(self, name, files, cache_dir='cache', version=None):
27 self.log = []
28 self._name = name
29 self._files = files
30 self._cache_dir = cache_dir
31 if version is not None:
32 self.version = version
33
34 def __enter__(self):
35 self.log.append('enter')
36 return self
37
38 def __exit__(self, exc_type, exc_value, tb):
39 self.log.append('exit: {} {}'.format(exc_type, exc_value))
40 return False
41
42 def close(self):
43 self.log.append('close')
44
45 def has_file(self, filename):
46 return filename in self._files
47
48 def read_file(self, filename, binary=False):
49 data = self._files[filename]
50 if binary:
51 data = data.encode('utf-8')
52 return data, '{}:{}'.format(self._name, filename)
53
54 def list_files(self, subdir):
55 if subdir and not subdir.endswith('/'):
56 subdir += '/'
57 return [fn for fn in self._files if fn.startswith(subdir)]
58
59 def get_cache_dir(self):
60 return self._cache_dir
61
62
63 @pytest.fixture
64 def base_sources():
65 return [
66 FakeSource('foo', {'a': 'b', 'c': 'd', 'a/b/c': 'c'}, version='42'),
67 FakeSource('bar', {'b': 'b', 'c': 'c', 'a/d': 'd'}, cache_dir='oops'),
68 ]
69
70
71 @pytest.fixture
72 def multi_source(base_sources):
73 return MultiSource(base_sources)
74
75
76 def test_lifecycle(multi_source, base_sources):
77 with multi_source as ms:
78 ms.close()
79 for bs in base_sources:
80 assert bs.log == ['enter', 'close', 'exit: None None']
81
82
83 def test_lifecycle_error(multi_source, base_sources):
84 with pytest.raises(Exception):
85 with multi_source:
86 raise Exception('42')
87 for bs in base_sources:
88 assert bs.log[-1] == "exit: <type 'exceptions.Exception'> 42"
89
90
91 def test_has_file(multi_source):
92 assert multi_source.has_file('a')
93 assert multi_source.has_file('b')
94 assert not multi_source.has_file('d')
95
96
97 def test_read_file(multi_source):
98 assert multi_source.read_file('a') == ('b', 'foo:a')
99 assert multi_source.read_file('b') == ('b', 'bar:b')
100 assert multi_source.read_file('c') == ('d', 'foo:c')
101
102
103 def test_read_binary(multi_source):
104 assert isinstance(multi_source.read_file('a', binary=True)[0], type(b'b'))
105
106
107 def test_list_files(multi_source):
108 assert sorted(multi_source.list_files('')) == ['a', 'a/b/c', 'a/d', 'b',
109 'c']
110 assert sorted(multi_source.list_files('a')) == ['a/b/c', 'a/d']
111 assert sorted(multi_source.list_files('a/')) == ['a/b/c', 'a/d']
112
113
114 def test_version(base_sources):
115 assert MultiSource(base_sources).version == '42'
116 with pytest.raises(AttributeError):
117 MultiSource(list(reversed(base_sources))).version
118
119
120 def test_cache_dir(multi_source):
121 assert multi_source.get_cache_dir() == 'cache'
OLDNEW
« cms/sources.py ('K') | « tests/test_additional_paths.py ('k') | tests/test_page_outputs.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld