annotate tests/test_pipelines_asset.py @ 974:72f17534d58e

tests: First pass on making unit tests work again. - Fix all imports - Add more helper functions to work with mock file-systems - Simplify some code by running chef directly on the mock FS - Fix a couple tests
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 17 Oct 2017 01:07:30 -0700
parents
children 45ad976712ec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
974
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import time
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import os.path
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import shutil
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import inspect
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 import pytest
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 from piecrust.pipelines.asset import get_filtered_processors
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 from piecrust.pipelines.records import MultiRecord
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 from piecrust.processing.base import SimpleFileProcessor
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 from .mockutil import mock_fs, mock_fs_scope
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 class FooProcessor(SimpleFileProcessor):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 def __init__(self, exts=None, open_func=None):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 exts = exts or {'foo', 'foo'}
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 super(FooProcessor, self).__init__({exts[0]: exts[1]})
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 self.PROCESSOR_NAME = exts[0]
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 self.open_func = open_func or open
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 def _doProcess(self, in_path, out_path):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 with self.open_func(in_path, 'r') as f:
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 text = f.read()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 with self.open_func(out_path, 'w') as f:
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 f.write("%s: %s" % (self.PROCESSOR_NAME.upper(), text))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 return True
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 class NoopProcessor(SimpleFileProcessor):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 def __init__(self, exts):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 super(NoopProcessor, self).__init__({exts[0]: exts[1]})
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 self.PROCESSOR_NAME = exts[0]
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 self.processed = []
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 def _doProcess(self, in_path, out_path):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 self.processed.append(in_path)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 shutil.copyfile(in_path, out_path)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 return True
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 def _get_test_fs(processors=None):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 if processors is None:
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 processors = 'copy'
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 return (mock_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 .withDir('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 .withConfig({
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 'pipelines': {
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 'asset': {
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 'processors': processors
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 }
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 }
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 }))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 def _create_test_plugin(fs, *, foo_exts=None, noop_exts=None):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 src = [
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 'from piecrust.plugins.base import PieCrustPlugin',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 'from piecrust.processing.base import SimpleFileProcessor']
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 foo_lines = inspect.getsourcelines(FooProcessor)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 src += ['']
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 src += map(lambda l: l.rstrip('\n'), foo_lines[0])
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 noop_lines = inspect.getsourcelines(NoopProcessor)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 src += ['']
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 src += map(lambda l: l.rstrip('\n'), noop_lines[0])
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 src += [
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 '',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 'class FooNoopPlugin(PieCrustPlugin):',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 ' def getProcessors(self):',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 ' yield FooProcessor(%s)' % repr(foo_exts),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 ' yield NoopProcessor(%s)' % repr(noop_exts),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 '',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 '__piecrust_plugin__ = FooNoopPlugin']
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 fs.withFile('kitchen/plugins/foonoop.py', src)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 def _bake_assets(fs):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 fs.runChef('bake', '-p', 'asset')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 def test_empty():
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 fs = _get_test_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 with mock_fs_scope(fs):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 expected = {}
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 assert expected == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 _bake_assets(fs)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 expected = {}
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 assert expected == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 def test_one_file():
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 fs = (_get_test_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 .withFile('kitchen/assets/something.html', 'A test file.'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 with mock_fs_scope(fs):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 expected = {}
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 assert expected == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98 _bake_assets(fs)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 expected = {'something.html': 'A test file.'}
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 assert expected == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 def test_one_level_dirtyness():
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 fs = (_get_test_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 .withFile('kitchen/assets/blah.foo', 'A test file.'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 with mock_fs_scope(fs):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 _bake_assets(fs)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 expected = {'blah.foo': 'A test file.'}
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 assert expected == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 mtime = os.path.getmtime(fs.path('/counter/blah.foo'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 assert abs(time.time() - mtime) <= 2
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 time.sleep(1)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 _bake_assets(fs)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 assert expected == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116 assert mtime == os.path.getmtime(fs.path('/counter/blah.foo'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118 time.sleep(1)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119 fs.withFile('kitchen/assets/blah.foo', 'A new test file.')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 _bake_assets(fs)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 expected = {'blah.foo': 'A new test file.'}
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
122 assert expected == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123 assert mtime < os.path.getmtime(fs.path('/counter/blah.foo'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 def test_two_levels_dirtyness():
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127 fs = (_get_test_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 .withFile('kitchen/assets/blah.foo', 'A test file.'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 _create_test_plugin(fs, foo_exts=('foo', 'bar'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 with mock_fs_scope(fs):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131 _bake_assets(fs)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132 expected = {'blah.bar': 'FOO: A test file.'}
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133 assert expected == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 mtime = os.path.getmtime(fs.path('/counter/blah.bar'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135 assert abs(time.time() - mtime) <= 2
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137 time.sleep(1)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
138 _bake_assets(fs)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
139 assert expected == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 assert mtime == os.path.getmtime(fs.path('/counter/blah.bar'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142 time.sleep(1)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
143 fs.withFile('kitchen/assets/blah.foo', 'A new test file.')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
144 _bake_assets(fs)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
145 expected = {'blah.bar': 'FOO: A new test file.'}
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
146 assert expected == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
147 assert mtime < os.path.getmtime(fs.path('/counter/blah.bar'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
148
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
149
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
150 def test_removed():
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
151 fs = (_get_test_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
152 .withFile('kitchen/assets/blah1.foo', 'A test file.')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
153 .withFile('kitchen/assets/blah2.foo', 'Ooops'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
154 with mock_fs_scope(fs):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
155 expected = {
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
156 'blah1.foo': 'A test file.',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
157 'blah2.foo': 'Ooops'}
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
158 assert expected == fs.getStructure('kitchen/assets')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
159 _bake_assets(fs)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
160 assert expected == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
161
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
162 time.sleep(1)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
163 os.remove(fs.path('/kitchen/assets/blah2.foo'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
164 expected = {
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
165 'blah1.foo': 'A test file.'}
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
166 assert expected == fs.getStructure('kitchen/assets')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
167 _bake_assets(1)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
168 assert expected == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
169
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
170
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
171 def test_record_version_change():
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
172 fs = (_get_test_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
173 .withFile('kitchen/assets/blah.foo', 'A test file.'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
174 _create_test_plugin(fs, foo_exts=('foo', 'foo'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
175 with mock_fs_scope(fs):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
176 _bake_assets(fs)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
177 assert os.path.exists(fs.path('/counter/blah.foo')) is True
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
178 mtime = os.path.getmtime(fs.path('/counter/blah.foo'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
179
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
180 time.sleep(1)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
181 _bake_assets(fs)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
182 assert mtime == os.path.getmtime(fs.path('/counter/blah.foo'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
183
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
184 time.sleep(1)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
185 MultiRecord.RECORD_VERSION += 1
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
186 try:
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
187 _bake_assets(fs)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
188 assert mtime < os.path.getmtime(fs.path('/counter/blah.foo'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
189 finally:
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
190 MultiRecord.RECORD_VERSION -= 1
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
191
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
192
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
193 @pytest.mark.parametrize('patterns, expected', [
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
194 (['_'],
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
195 {'something.html': 'A test file.'}),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
196 (['html'],
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197 {}),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
198 (['/^_/'],
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
199 {'something.html': 'A test file.',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
200 'foo': {'_important.html': 'Important!'}})
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
201 ])
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
202 def test_ignore_pattern(patterns, expected):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
203 fs = (_get_test_fs()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
204 .withFile('kitchen/assets/something.html', 'A test file.')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
205 .withFile('kitchen/assets/_hidden.html', 'Shhh')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
206 .withFile('kitchen/assets/foo/_important.html', 'Important!'))
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
207 fs.withConfig({'pipelines': {'asset': {'ignore': patterns}}})
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208 with mock_fs_scope(fs):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
209 assert {} == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
210 _bake_assets(fs)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
211 assert expected == fs.getStructure('counter')
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
212
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
214 @pytest.mark.parametrize('names, expected', [
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
215 ('all', ['cleancss', 'compass', 'copy', 'concat', 'less', 'requirejs',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
216 'sass', 'sitemap', 'uglifyjs', 'pygments_style']),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
217 ('all -sitemap', ['cleancss', 'copy', 'compass', 'concat', 'less',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
218 'requirejs', 'sass', 'uglifyjs', 'pygments_style']),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
219 ('-sitemap -less -sass all', ['cleancss', 'copy', 'compass', 'concat',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
220 'requirejs', 'uglifyjs',
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
221 'pygments_style']),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
222 ('copy', ['copy']),
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
223 ('less sass', ['less', 'sass'])
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
224 ])
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
225 def test_filter_processor(names, expected):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
226 fs = mock_fs().withConfig()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
227 with mock_fs_scope(fs):
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
228 app = fs.getApp()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
229 processors = app.plugin_loader.getProcessors()
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
230 procs = get_filtered_processors(processors, names)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
231 actual = [p.PROCESSOR_NAME for p in procs]
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
232 assert sorted(actual) == sorted(expected)
72f17534d58e tests: First pass on making unit tests work again.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
233