Mercurial > piecrust2
annotate tests/mockutil.py @ 380:f33712c4cfab
routing: Fix bugs with matching URLs with correct route but missing metadata.
When matching a route like `/foo/%slug%` against an URL like `/foo`, the route
will (correctly) return a match, but it will be completely missing the `slug`
metadata, resulting in problems elsewhere. This change makes it so that any
missing route metadata will be filled in with an empty string.
And because this means generated URLs may differ from the incoming URL when
using trailing slashes (`/foo/` _vs._ `/foo`), we make the assert in the
chef server handle those discrepancies.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 10 May 2015 00:34:21 -0700 |
parents | 4b1019bb2533 |
children | 65db6df28120 |
rev | line source |
---|---|
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
1 import io |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
2 import time |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
3 import random |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
4 import codecs |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
5 import shutil |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
6 import os.path |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 import mock |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
8 import yaml |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 from piecrust.app import PieCrust, PieCrustConfiguration |
181
d356f6178623
tests: Add help functions to get and render a simple page.
Ludovic Chabant <ludovic@chabant.com>
parents:
120
diff
changeset
|
10 from piecrust.page import Page |
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
349
diff
changeset
|
11 from piecrust.rendering import QualifiedPage, PageRenderingContext, render_page |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
14 resources_path = os.path.abspath( |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
15 os.path.join( |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
16 os.path.dirname(__file__), |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
17 '..', 'piecrust', 'resources')) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
18 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
19 |
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 def get_mock_app(config=None): |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 app = mock.MagicMock(spec=PieCrust) |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 app.config = PieCrustConfiguration() |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 return app |
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
25 |
181
d356f6178623
tests: Add help functions to get and render a simple page.
Ludovic Chabant <ludovic@chabant.com>
parents:
120
diff
changeset
|
26 def get_simple_page(app, rel_path): |
d356f6178623
tests: Add help functions to get and render a simple page.
Ludovic Chabant <ludovic@chabant.com>
parents:
120
diff
changeset
|
27 source = app.getSource('pages') |
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
349
diff
changeset
|
28 metadata = {'slug': os.path.splitext(rel_path)[0]} |
181
d356f6178623
tests: Add help functions to get and render a simple page.
Ludovic Chabant <ludovic@chabant.com>
parents:
120
diff
changeset
|
29 return Page(source, metadata, rel_path) |
d356f6178623
tests: Add help functions to get and render a simple page.
Ludovic Chabant <ludovic@chabant.com>
parents:
120
diff
changeset
|
30 |
d356f6178623
tests: Add help functions to get and render a simple page.
Ludovic Chabant <ludovic@chabant.com>
parents:
120
diff
changeset
|
31 |
369
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
349
diff
changeset
|
32 def render_simple_page(page, route, route_metadata): |
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
349
diff
changeset
|
33 qp = QualifiedPage(page, route, route_metadata) |
4b1019bb2533
serve: Giant refactor to change how we handle data when serving pages.
Ludovic Chabant <ludovic@chabant.com>
parents:
349
diff
changeset
|
34 ctx = PageRenderingContext(qp) |
181
d356f6178623
tests: Add help functions to get and render a simple page.
Ludovic Chabant <ludovic@chabant.com>
parents:
120
diff
changeset
|
35 rp = render_page(ctx) |
d356f6178623
tests: Add help functions to get and render a simple page.
Ludovic Chabant <ludovic@chabant.com>
parents:
120
diff
changeset
|
36 return rp.content |
d356f6178623
tests: Add help functions to get and render a simple page.
Ludovic Chabant <ludovic@chabant.com>
parents:
120
diff
changeset
|
37 |
d356f6178623
tests: Add help functions to get and render a simple page.
Ludovic Chabant <ludovic@chabant.com>
parents:
120
diff
changeset
|
38 |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
39 class _MockFsEntry(object): |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
40 def __init__(self, contents): |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
41 self.contents = contents |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
42 self.metadata = {'mtime': time.time()} |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
43 |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
44 |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
45 class _MockFsEntryWriter(object): |
182
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
46 def __init__(self, entry, mode='rt'): |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
47 self._entry = entry |
182
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
48 self._mode = mode |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
49 |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
50 if 'b' in mode: |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
51 data = entry.contents |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
52 if isinstance(data, str): |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
53 data = data.encode('utf8') |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
54 self._stream = io.BytesIO(data) |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
55 else: |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
56 self._stream = io.StringIO(entry.contents) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
57 |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
58 def __getattr__(self, name): |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
59 return getattr(self._stream, name) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
60 |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
61 def __enter__(self): |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
62 return self |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
63 |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
64 def __exit__(self, exc_type, exc_value, exc_tb): |
182
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
65 if 'w' in self._mode: |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
66 if 'a' in self._mode: |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
67 self._entry.contents += self._stream.getvalue() |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
68 else: |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
69 self._entry.contents = self._stream.getvalue() |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
70 self._entry.metadata['mtime'] = time.time() |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
71 self._stream.close() |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
72 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
73 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
74 class mock_fs(object): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
75 def __init__(self, default_spec=True): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
76 self._root = 'root_%d' % random.randrange(1000) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
77 self._fs = {self._root: {}} |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
78 if default_spec: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
79 self.withDir('counter') |
36
485682a6de50
New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents:
35
diff
changeset
|
80 self.withFile('kitchen/config.yml', |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
81 "site:\n title: Mock Website\n") |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
82 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
83 def path(self, p): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
84 p = p.replace('\\', '/') |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
85 if p in ['/', '', None]: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
86 return '/%s' % self._root |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
87 return '/%s/%s' % (self._root, p.lstrip('/')) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
88 |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
89 def getApp(self, cache=True): |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
90 root_dir = self.path('/kitchen') |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
91 return PieCrust(root_dir, cache=cache, debug=True) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
92 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
93 def withDir(self, path): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
94 path = path.replace('\\', '/') |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
95 path = path.lstrip('/') |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
96 path = '/%s/%s' % (self._root, path) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
97 self._createDir(path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
98 return self |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
99 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
100 def withFile(self, path, contents): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
101 path = path.replace('\\', '/') |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
102 path = path.lstrip('/') |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
103 path = '/%s/%s' % (self._root, path) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
104 self._createFile(path, contents) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
105 return self |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
106 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
107 def withAsset(self, path, contents): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
108 return self.withFile('kitchen/' + path, contents) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
109 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
110 def withAssetDir(self, path): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
111 return self.withDir('kitchen/' + path) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
112 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
113 def withConfig(self, config): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
114 return self.withFile( |
36
485682a6de50
New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents:
35
diff
changeset
|
115 'kitchen/config.yml', |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
116 yaml.dump(config)) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
117 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
118 def withThemeConfig(self, config): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
119 return self.withFile( |
36
485682a6de50
New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents:
35
diff
changeset
|
120 'kitchen/theme/theme_config.yml', |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
121 yaml.dump(config)) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
122 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
123 def withPage(self, url, config=None, contents=None): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
124 config = config or {} |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
125 contents = contents or "A test page." |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
126 text = "---\n" |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
127 text += yaml.dump(config) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
128 text += "---\n" |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
129 text += contents |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
130 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
131 name, ext = os.path.splitext(url) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
132 if not ext: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
133 url += '.md' |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
134 url = url.lstrip('/') |
36
485682a6de50
New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents:
35
diff
changeset
|
135 return self.withAsset(url, text) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
136 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
137 def withPageAsset(self, page_url, name, contents=None): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
138 contents = contents or "A test asset." |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
139 url_base, ext = os.path.splitext(page_url) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
140 dirname = url_base + '-assets' |
36
485682a6de50
New site layout support.
Ludovic Chabant <ludovic@chabant.com>
parents:
35
diff
changeset
|
141 return self.withAsset('%s/%s' % (dirname, name), |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
142 contents) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
143 |
322
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
144 def withPages(self, num, url_factory, config_factory=None, |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
145 contents_factory=None): |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
146 for i in range(num): |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
147 if isinstance(url_factory, str): |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
148 url = url_factory.format(idx=i, idx1=(i + 1)) |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
149 else: |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
150 url = url_factory(i) |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
151 |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
152 config = None |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
153 if config_factory: |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
154 config = config_factory(i) |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
155 |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
156 contents = None |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
157 if contents_factory: |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
158 contents = contents_factory(i) |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
159 |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
160 self.withPage(url, config, contents) |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
161 return self |
7544c03b6bab
tests: Add utility function to create multiple mock pages in one go.
Ludovic Chabant <ludovic@chabant.com>
parents:
198
diff
changeset
|
162 |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
163 def getStructure(self, path=None): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
164 root = self._fs[self._root] |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
165 if path: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
166 root = self._getEntry(self.path(path)) |
328
2a5996e0d3ec
tests: Raise an exception instead of crashing rudely.
Ludovic Chabant <ludovic@chabant.com>
parents:
322
diff
changeset
|
167 if root is None: |
2a5996e0d3ec
tests: Raise an exception instead of crashing rudely.
Ludovic Chabant <ludovic@chabant.com>
parents:
322
diff
changeset
|
168 raise Exception("No such path: %s" % path) |
349
c484e1ec9150
tests: Add more utility functions to the mock file-system.
Ludovic Chabant <ludovic@chabant.com>
parents:
346
diff
changeset
|
169 if not isinstance(root, dict): |
c484e1ec9150
tests: Add more utility functions to the mock file-system.
Ludovic Chabant <ludovic@chabant.com>
parents:
346
diff
changeset
|
170 raise Exception("Path is not a directory: %s" % path) |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
171 |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
172 res = {} |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
173 for k, v in root.items(): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
174 self._getStructureRecursive(v, res, k) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
175 return res |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
176 |
349
c484e1ec9150
tests: Add more utility functions to the mock file-system.
Ludovic Chabant <ludovic@chabant.com>
parents:
346
diff
changeset
|
177 def getFileEntry(self, path): |
c484e1ec9150
tests: Add more utility functions to the mock file-system.
Ludovic Chabant <ludovic@chabant.com>
parents:
346
diff
changeset
|
178 entry = self._getEntry(self.path(path)) |
c484e1ec9150
tests: Add more utility functions to the mock file-system.
Ludovic Chabant <ludovic@chabant.com>
parents:
346
diff
changeset
|
179 if entry is None: |
c484e1ec9150
tests: Add more utility functions to the mock file-system.
Ludovic Chabant <ludovic@chabant.com>
parents:
346
diff
changeset
|
180 raise Exception("No such file: %s" % path) |
c484e1ec9150
tests: Add more utility functions to the mock file-system.
Ludovic Chabant <ludovic@chabant.com>
parents:
346
diff
changeset
|
181 if not isinstance(entry, _MockFsEntry): |
c484e1ec9150
tests: Add more utility functions to the mock file-system.
Ludovic Chabant <ludovic@chabant.com>
parents:
346
diff
changeset
|
182 raise Exception("Path is not a file: %s" % path) |
c484e1ec9150
tests: Add more utility functions to the mock file-system.
Ludovic Chabant <ludovic@chabant.com>
parents:
346
diff
changeset
|
183 return entry.contents |
c484e1ec9150
tests: Add more utility functions to the mock file-system.
Ludovic Chabant <ludovic@chabant.com>
parents:
346
diff
changeset
|
184 |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
185 def _getStructureRecursive(self, src, target, name): |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
186 if isinstance(src, _MockFsEntry): |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
187 target[name] = src.contents |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
188 return |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
189 |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
190 e = {} |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
191 for k, v in src.items(): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
192 self._getStructureRecursive(v, e, k) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
193 target[name] = e |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
194 |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
195 def _getEntry(self, path): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
196 cur = self._fs |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
197 path = path.replace('\\', '/').lstrip('/') |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
198 bits = path.split('/') |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
199 for p in bits: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
200 try: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
201 cur = cur[p] |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
202 except KeyError: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
203 return None |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
204 return cur |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
205 |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
206 def _createDir(self, path): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
207 cur = self._fs |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
208 path = path.replace('\\', '/').strip('/') |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
209 bits = path.split('/') |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
210 for b in bits: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
211 if b not in cur: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
212 cur[b] = {} |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
213 cur = cur[b] |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
214 return self |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
215 |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
216 def _createFile(self, path, contents): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
217 cur = self._fs |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
218 path = path.replace('\\', '/').lstrip('/') |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
219 bits = path.split('/') |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
220 for b in bits[:-1]: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
221 if b not in cur: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
222 cur[b] = {} |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
223 cur = cur[b] |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
224 cur[bits[-1]] = _MockFsEntry(contents) |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
225 return self |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
226 |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
227 def _deleteEntry(self, path): |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
228 parent = self._getEntry(os.path.dirname(path)) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
229 assert parent is not None |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
230 name = os.path.basename(path) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
231 assert name in parent |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
232 del parent[name] |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
233 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
234 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
235 class mock_fs_scope(object): |
182
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
236 def __init__(self, fs, open_patches=None): |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
237 self.open_patches = open_patches or [] |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
238 self._fs = fs |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
239 self._patchers = [] |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
240 self._originals = {} |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
241 |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
242 @property |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
243 def root(self): |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
244 return self._fs._root |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
245 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
246 def __enter__(self): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
247 self._startMock() |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
248 return self |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
249 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
250 def __exit__(self, type, value, traceback): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
251 self._endMock() |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
252 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
253 def _startMock(self): |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
254 # TODO: sadly, there seems to be no way to replace `open` everywhere? |
182
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
255 modules = self.open_patches + ['__main__', 'piecrust.records'] |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
256 for m in modules: |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
257 self._createMock('%s.open' % m, open, self._open, create=True) |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
258 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
259 self._createMock('codecs.open', codecs.open, self._codecsOpen) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
260 self._createMock('os.listdir', os.listdir, self._listdir) |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
261 self._createMock('os.makedirs', os.makedirs, self._makedirs) |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
262 self._createMock('os.remove', os.remove, self._remove) |
335
8511137d1b62
tests: Add `os.rename` to the mocked functions.
Ludovic Chabant <ludovic@chabant.com>
parents:
328
diff
changeset
|
263 self._createMock('os.rename', os.rename, self._rename) |
182
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
264 self._createMock('os.path.exists', os.path.exists, self._exists) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
265 self._createMock('os.path.isdir', os.path.isdir, self._isdir) |
24
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
266 self._createMock('os.path.isfile', os.path.isfile, self._isfile) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
267 self._createMock('os.path.islink', os.path.islink, self._islink) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
268 self._createMock('os.path.getmtime', os.path.getmtime, self._getmtime) |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
269 self._createMock('shutil.copyfile', shutil.copyfile, self._copyfile) |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
270 self._createMock('shutil.rmtree', shutil.rmtree, self._rmtree) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
271 for p in self._patchers: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
272 p.start() |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
273 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
274 def _endMock(self): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
275 for p in self._patchers: |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
276 p.stop() |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
277 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
278 def _createMock(self, name, orig, func, **kwargs): |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
279 self._originals[name] = orig |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
280 self._patchers.append(mock.patch(name, func, **kwargs)) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
281 |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
282 def _doOpen(self, orig_name, path, mode, *args, **kwargs): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
283 path = os.path.normpath(path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
284 if path.startswith(resources_path): |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
285 return self._originals[orig_name](path, mode, *args, **kwargs) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
286 |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
287 if 'r' in mode: |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
288 e = self._getFsEntry(path) |
182
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
289 elif 'w' in mode or 'x' in mode or 'a' in mode: |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
290 e = self._getFsEntry(path) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
291 if e is None: |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
292 contents = '' |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
293 if 'b' in mode: |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
294 contents = bytes() |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
295 self._fs._createFile(path, contents) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
296 e = self._getFsEntry(path) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
297 assert e is not None |
182
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
298 elif 'x' in mode: |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
299 raise OSError("File '%s' already exists" % path) |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
300 else: |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
301 raise OSError("Unsupported open mode: %s" % mode) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
302 |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
303 if e is None: |
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
304 raise OSError("No such file: %s" % path) |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
305 if not isinstance(e, _MockFsEntry): |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
306 raise OSError("'%s' is not a file %s" % (path, e)) |
182
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
307 |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
308 return _MockFsEntryWriter(e, mode) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
309 |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
310 def _open(self, path, mode, *args, **kwargs): |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
311 return self._doOpen('__main__.open', path, mode, *args, **kwargs) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
312 |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
313 def _codecsOpen(self, path, mode, *args, **kwargs): |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
314 return self._doOpen('codecs.open', path, mode, *args, **kwargs) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
315 |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
316 def _listdir(self, path): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
317 path = os.path.normpath(path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
318 if path.startswith(resources_path): |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
319 return self._originals['os.listdir'](path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
320 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
321 e = self._getFsEntry(path) |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
322 if e is None: |
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
323 raise OSError("No such directory: %s" % path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
324 if not isinstance(e, dict): |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
325 raise OSError("'%s' is not a directory." % path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
326 return list(e.keys()) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
327 |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
328 def _makedirs(self, path, mode=0o777): |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
329 if not path.replace('\\', '/').startswith('/' + self.root): |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
330 raise Exception("Shouldn't create directory: %s" % path) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
331 self._fs._createDir(path) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
332 |
120
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
333 def _remove(self, path): |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
334 path = os.path.normpath(path) |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
335 self._fs._deleteEntry(path) |
133845647083
Better error management and removal support in baking/processing.
Ludovic Chabant <ludovic@chabant.com>
parents:
85
diff
changeset
|
336 |
182
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
337 def _exists(self, path): |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
338 path = os.path.normpath(path) |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
339 if path.startswith(resources_path): |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
340 return self._originals['os.path.isdir'](path) |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
341 e = self._getFsEntry(path) |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
342 return e is not None |
a54d3c0b5f4a
tests: Patch `os.path.exists` and improve patching for `open`.
Ludovic Chabant <ludovic@chabant.com>
parents:
181
diff
changeset
|
343 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
344 def _isdir(self, path): |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
345 path = os.path.normpath(path) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
346 if path.startswith(resources_path): |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
347 return self._originals['os.path.isdir'](path) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
348 e = self._getFsEntry(path) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
349 return e is not None and isinstance(e, dict) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
350 |
24
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
351 def _isfile(self, path): |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
352 path = os.path.normpath(path) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
353 if path.startswith(resources_path): |
24
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
354 return self._originals['os.path.isfile'](path) |
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
355 e = self._getFsEntry(path) |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
356 return e is not None and isinstance(e, _MockFsEntry) |
24
644869022b6e
Mock `os.path.isfile`, and fix a few other test utilities.
Ludovic Chabant <ludovic@chabant.com>
parents:
11
diff
changeset
|
357 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
358 def _islink(self, path): |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
359 path = os.path.normpath(path) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
360 if path.startswith(resources_path): |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
361 return self._originals['os.path.islink'](path) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
362 return False |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
363 |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
364 def _getmtime(self, path): |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
365 path = os.path.normpath(path) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
366 if path.startswith(resources_path): |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
367 return self._originals['os.path.getmtime'](path) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
368 e = self._getFsEntry(path) |
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
369 if e is None: |
11
617191dec18e
Fixes for Windows, make `findPagePath` return a ref path.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
370 raise OSError("No such file: %s" % path) |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
371 return e.metadata['mtime'] |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
372 |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
373 def _copyfile(self, src, dst): |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
374 src = os.path.normpath(src) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
375 if src.startswith(resources_path): |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
376 with self._originals['__main__.open'](src, 'r') as fp: |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
377 src_text = fp.read() |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
378 else: |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
379 e = self._getFsEntry(src) |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
380 src_text = e.contents |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
381 if not dst.replace('\\', '/').startswith('/' + self.root): |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
382 raise Exception("Shouldn't copy to: %s" % dst) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
383 self._fs._createFile(dst, src_text) |
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
384 |
335
8511137d1b62
tests: Add `os.rename` to the mocked functions.
Ludovic Chabant <ludovic@chabant.com>
parents:
328
diff
changeset
|
385 def _rename(self, src, dst): |
8511137d1b62
tests: Add `os.rename` to the mocked functions.
Ludovic Chabant <ludovic@chabant.com>
parents:
328
diff
changeset
|
386 src = os.path.normpath(src) |
8511137d1b62
tests: Add `os.rename` to the mocked functions.
Ludovic Chabant <ludovic@chabant.com>
parents:
328
diff
changeset
|
387 if src.startswith(resources_path) or dst.startswith(resources_path): |
8511137d1b62
tests: Add `os.rename` to the mocked functions.
Ludovic Chabant <ludovic@chabant.com>
parents:
328
diff
changeset
|
388 raise Exception("Shouldn't rename files in the resources path.") |
8511137d1b62
tests: Add `os.rename` to the mocked functions.
Ludovic Chabant <ludovic@chabant.com>
parents:
328
diff
changeset
|
389 self._copyfile(src, dst) |
8511137d1b62
tests: Add `os.rename` to the mocked functions.
Ludovic Chabant <ludovic@chabant.com>
parents:
328
diff
changeset
|
390 self._remove(src) |
8511137d1b62
tests: Add `os.rename` to the mocked functions.
Ludovic Chabant <ludovic@chabant.com>
parents:
328
diff
changeset
|
391 |
85
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
392 def _rmtree(self, path): |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
393 if not path.replace('\\', '/').startswith('/' + self.root): |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
394 raise Exception("Shouldn't delete trees from: %s" % path) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
395 e = self._fs._getEntry(os.path.dirname(path)) |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
396 del e[os.path.basename(path)] |
3471ffa059b2
Add a `BakeScheduler` to handle build dependencies. Add unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
36
diff
changeset
|
397 |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
398 def _getFsEntry(self, path): |
35
e4c345dcf33c
More unit tests, fix a bug with the skip patterns.
Ludovic Chabant <ludovic@chabant.com>
parents:
32
diff
changeset
|
399 return self._fs._getEntry(path) |
6
f5ca5c5bed85
More Python 3 fixes, modularization, and new unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
400 |