annotate tests/mock.py @ 476:71114096433c

core: Add support for Markdown extensions, add header anchor extension. - New configuration option to specify Markdown extensions. - Enable some extensions by default. - Add CSS to make tables pretty. - Add extension to generate anchors next to each HTML heading. - Provide CSS to show those anchors on mouse-hover.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 11 Oct 2018 23:24:06 -0700
parents 2027ab79f006
children 0bfd648aca6a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
1 import os
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import codecs
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import logging
336
03e3e793fa22 Convert project to Python 3.4.
Ludovic Chabant <ludovic@chabant.com>
parents: 225
diff changeset
5 import io
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
6 from collections import deque
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
7 from contextlib import closing
336
03e3e793fa22 Convert project to Python 3.4.
Ludovic Chabant <ludovic@chabant.com>
parents: 225
diff changeset
8 from configparser import SafeConfigParser
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
9 from wikked.fs import FileSystem
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
10 from wikked.db.base import Database
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
11 from wikked.indexer.base import WikiIndex
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
12 from wikked.scm.base import SourceControl
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
13 from wikked.wiki import WikiParameters, passthrough_formatter
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
16 logger = logging.getLogger(__name__)
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
19 class MockWikiParameters(WikiParameters):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
20 def __init__(self, root=None):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
21 super(MockWikiParameters, self).__init__(root)
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
22 self.config_text = ""
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
23 self.mock_fs = None
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
24 self.mock_index = None
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
25 self.mock_db = None
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
26 self.mock_scm = None
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
28 def fs_factory(self):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
29 if self.mock_fs is False:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
30 return super(MockWikiParameters, self).fs_factory()
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
31 return self.mock_fs or MockFileSystem(self.root, self.config)
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
33 def index_factory(self):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
34 if self.mock_index is False:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
35 return super(MockWikiParameters, self).index_factory()
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
36 return self.mock_index or MockWikiIndex()
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
37
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
38 def db_factory(self):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
39 if self.mock_db is False:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
40 return super(MockWikiParameters, self).db_factory()
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
41 return self.mock_db or MockDatabase()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
43 def scm_factory(self, for_init=False):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
44 if self.mock_scm is False:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
45 return super(MockWikiParameters, self).scm_factory(for_init)
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
46 return self.mock_scm or MockSourceControl()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
48 def getFormatters(self):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
49 formatters = {
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
50 passthrough_formatter: ['txt', 'html']
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
51 }
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
52 return formatters
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
54 def getPageUpdater(self):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
55 return lambda wiki, url: wiki.update(url, cache_ext_data=True)
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
57 def _loadConfig(self):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
58 default_config_path = os.path.join(
339
64acfb58e023 Cleanup import and code.
Ludovic Chabant <ludovic@chabant.com>
parents: 336
diff changeset
59 os.path.dirname(__file__), '..',
64acfb58e023 Cleanup import and code.
Ludovic Chabant <ludovic@chabant.com>
parents: 336
diff changeset
60 'wikked', 'resources', 'defaults.cfg')
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
62 config = SafeConfigParser()
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
63 config.readfp(open(default_config_path))
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
64 config.set('wiki', 'root', '/fake/root')
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
65 if self.config_text:
336
03e3e793fa22 Convert project to Python 3.4.
Ludovic Chabant <ludovic@chabant.com>
parents: 225
diff changeset
66 with closing(io.StringIO(self.config_text)) as conf:
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
67 config.readfp(conf)
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
69 return config
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
72 def mock_os_walk(root_dir, root_node):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
73 queue = deque()
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
74 queue.appendleft((root_dir, root_node))
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
75 while len(queue) > 0:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
76 cur_dir, cur_node = queue.pop()
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
77
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
78 dirnames = []
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
79 filenames = []
336
03e3e793fa22 Convert project to Python 3.4.
Ludovic Chabant <ludovic@chabant.com>
parents: 225
diff changeset
80 for name, child in cur_node.items():
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
81 if isinstance(child, dict):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
82 dirnames.append(name)
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
83 else:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
84 filenames.append(name)
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
85 yield cur_dir, dirnames, filenames
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
86 for name in dirnames:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
87 fullname = os.path.join(cur_dir, name)
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
88 queue.appendleft((fullname, cur_node[name]))
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
89
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
90
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
91 class MockFileSystem(FileSystem):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
92 def __init__(self, root, config, structure=None):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
93 super(MockFileSystem, self).__init__(root, config)
462
2027ab79f006 tests: Fix exclusion of built-in endpoints in tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 448
diff changeset
94 self.include_builtin_endpoints = False
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 if not structure:
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
96 self.structure = {}
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
97 else:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
98 self.structure = MockFileSystem.flat_to_nested(structure)
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 def getPageInfos(self, subdir=None):
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
101 def tmp_walk(path):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
102 node = self._getNode(path)
448
c78eaebced8b fs: Add support for builtin namespaces.
Ludovic Chabant <ludovic@chabant.com>
parents: 339
diff changeset
103 if node is None:
c78eaebced8b fs: Add support for builtin namespaces.
Ludovic Chabant <ludovic@chabant.com>
parents: 339
diff changeset
104 raise Exception("No node at %s" % path)
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
105 return mock_os_walk(path, node)
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
107 orig_walk = os.walk
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
108 os.walk = tmp_walk
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
109 try:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
110 gen = super(MockFileSystem, self).getPageInfos(subdir)
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
111 return list(gen)
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
112 finally:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
113 os.walk = orig_walk
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
115 def setPage(self, url, content):
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
116 raise NotImplementedError()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
118 def _getPageInfo(self, path):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
119 pi = super(MockFileSystem, self)._getPageInfo(path)
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
120 node = self._getNode(path)
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
121 if node is not None:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
122 pi._content = node
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
123 else:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
124 raise Exception("Can't find node: %s" % path)
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
125 return pi
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 def _getNode(self, path):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 node = self.structure
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
129 path = path.lstrip('/')
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
130 if path != '':
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131 for n in path.split('/'):
51
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
132 if n not in node:
2733871775cd More unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 49
diff changeset
133 return None
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 node = node[n]
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
135 return node
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
136
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
137 def _getPhysicalPath(self, url, is_file=True, make_new=False):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
138 def tmp_walk(path):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
139 node = self._getNode(path)
448
c78eaebced8b fs: Add support for builtin namespaces.
Ludovic Chabant <ludovic@chabant.com>
parents: 339
diff changeset
140 if node is None:
c78eaebced8b fs: Add support for builtin namespaces.
Ludovic Chabant <ludovic@chabant.com>
parents: 339
diff changeset
141 raise Exception("No node at %s" % path)
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
142 return mock_os_walk(path, node)
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
143
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
144 orig_walk = os.walk
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
145 os.walk = tmp_walk
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
146 try:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
147 return super(MockFileSystem, self)._getPhysicalPath(url, is_file,
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
148 make_new)
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
149 finally:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
150 os.walk = orig_walk
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
151
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
152 @staticmethod
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
153 def flat_to_nested(flat):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
154 nested = {}
336
03e3e793fa22 Convert project to Python 3.4.
Ludovic Chabant <ludovic@chabant.com>
parents: 225
diff changeset
155 for k, v in flat.items():
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
156 bits = k.lstrip('/').split('/')
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
157 cur = nested
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
158 for i, b in enumerate(bits):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
159 if i < len(bits) - 1:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
160 if b not in cur:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
161 cur[b] = {}
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
162 cur = cur[b]
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
163 else:
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
164 cur[b] = v
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
165 return nested
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
166
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
167 @staticmethod
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
168 def save_structure(path, structure):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
169 if not os.path.isdir(path):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
170 os.makedirs(path)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
171 for node in structure:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
172 node_path = os.path.join(path, node)
336
03e3e793fa22 Convert project to Python 3.4.
Ludovic Chabant <ludovic@chabant.com>
parents: 225
diff changeset
173 if isinstance(structure[node], str):
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
174 with codecs.open(node_path, 'w', encoding='utf-8') as f:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
175 f.write(structure[node])
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
176 else:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
177 MockFileSystem.save_structure(node_path, structure[node])
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
178
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
179
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
180 class MockDatabase(Database):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
181 def __init__(self, content=None):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
182 super(MockDatabase, self).__init__()
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
183 self.content = content
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
184
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
185 def getPageUrls(self, subdir=None, uncached_only=False):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
186 return []
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
187
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
188 def getPages(self, subdir=None, meta_query=None, uncached_only=False,
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
189 fields=None):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
190 return []
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
191
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
192 def isCacheValid(self, page):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
193 return False
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
194
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
195 def pageExists(self, url=None, path=None):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
196 return False
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
197
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
198 def getLinksTo(self, url):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
199 return []
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
200
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
201 def _getPageByUrl(self, url, fields):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
202 return None
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
203
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
204 def _getPageByPath(self, path, fields):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
205 return None
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
206
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
207
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
208 class MockWikiIndex(WikiIndex):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
209 def __init__(self):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
210 super(MockWikiIndex, self).__init__()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
211
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
212 def search(self, query):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213 # url, title, content_highlights
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
214 return None
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
215
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
216
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
217 class MockSourceControl(SourceControl):
225
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
218 def __init__(self):
ebb12ff21cb2 Updated unit tests to be able to run.
Ludovic Chabant <ludovic@chabant.com>
parents: 100
diff changeset
219 super(MockSourceControl, self).__init__()
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
220
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
221 def getSpecialFilenames(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
222 return []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
223
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
224 def getHistory(self, path=None):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
225 return []
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
226
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
227 def getState(self, path):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
228 raise NotImplementedError()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
229
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
230 def getRevision(self, path, rev):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
231 raise NotImplementedError()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
232
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
233 def diff(self, path, rev1, rev2):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
234 raise NotImplementedError()