Mercurial > piecrust2
annotate tests/conftest.py @ 347:76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 15 Apr 2015 16:39:35 -0700 |
parents | 1cd67680c38c |
children | 1f22d4b10fef |
rev | line source |
---|---|
86
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
1 import sys |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
2 import pprint |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
3 import os.path |
86
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
4 import logging |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
5 import pytest |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
6 import yaml |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
7 from piecrust.configuration import merge_dicts |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
8 from .mockutil import mock_fs, mock_fs_scope |
86
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
10 |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 def pytest_runtest_setup(item): |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 pass |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 def pytest_addoption(parser): |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
16 parser.addoption( |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
17 '--log-debug', |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
18 action='store_true', |
86
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
19 help="Sets the PieCrust logger to output debug info to stdout.") |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 def pytest_configure(config): |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 if config.getoption('--log-debug'): |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 hdl = logging.StreamHandler(stream=sys.stdout) |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 logging.getLogger('piecrust').addHandler(hdl) |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 logging.getLogger('piecrust').setLevel(logging.DEBUG) |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
28 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
29 def pytest_collect_file(parent, path): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
30 if path.ext == ".bake" and path.basename.startswith("test"): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
31 return BakeTestFile(path, parent) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
32 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
33 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
34 class BakeTestFile(pytest.File): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
35 def collect(self): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
36 spec = yaml.load_all(self.fspath.open()) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
37 for i, item in enumerate(spec): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
38 name = '%s_%d' % (self.fspath.basename, i) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
39 if 'test_name' in item: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
40 name += '_%s' % item['test_name'] |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
41 yield BakeTestItem(name, self, item) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
42 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
43 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
44 class BakeTestItem(pytest.Item): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
45 def __init__(self, name, parent, spec): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
46 super(BakeTestItem, self).__init__(name, parent) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
47 self.spec = spec |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
48 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
49 def runtest(self): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
50 fs = mock_fs() |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
51 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
52 # Website config. |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
53 config = { |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
54 'site': { |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
55 'default_format': 'none', |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
56 'default_page_layout': 'none', |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
57 'default_post_layout': 'none'} |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
58 } |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
59 test_config = self.spec.get('config') |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
60 if test_config is not None: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
61 merge_dicts(config, test_config) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
62 fs.withConfig(config) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
63 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
64 # Input file-system. |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
65 input_files = self.spec.get('in') |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
66 if input_files is not None: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
67 _add_mock_files(fs, '/kitchen', input_files) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
68 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
69 # Output file-system. |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
70 expected_output_files = self.spec.get('out') |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
71 expected_partial_files = self.spec.get('outfiles') |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
72 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
73 # Bake! |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
74 from piecrust.baking.baker import Baker |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
75 with mock_fs_scope(fs): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
76 out_dir = fs.path('kitchen/_counter') |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
77 app = fs.getApp() |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
78 baker = Baker(app, out_dir) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
79 baker.bake() |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
80 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
81 if expected_output_files: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
82 actual = fs.getStructure('kitchen/_counter') |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
83 error = _compare_dicts(actual, expected_output_files) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
84 if error: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
85 raise ExpectedBakeOutputError(error) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
86 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
87 if expected_partial_files: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
88 for key, content in expected_partial_files.items(): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
89 try: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
90 actual = fs.getStructure('kitchen/_counter/' + |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
91 key.lstrip('/')) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
92 except Exception: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
93 raise ExpectedBakeOutputError([ |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
94 "Missing expected output file: %s" % key]) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
95 if not isinstance(actual, str): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
96 raise ExpectedBakeOutputError([ |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
97 "Expected output file is a directory: %s" % key]) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
98 if actual != content: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
99 raise ExpectedBakeOutputError([ |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
100 "Unexpected output file contents:", |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
101 "%s: %s" % (key, content), |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
102 "%s: %s" % (key, actual)]) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
103 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
104 def reportinfo(self): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
105 return self.fspath, 0, "bake: %s" % self.name |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
106 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
107 def repr_failure(self, excinfo): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
108 if isinstance(excinfo.value, ExpectedBakeOutputError): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
109 return ('\n'.join(excinfo.value.args[0])) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
110 return super(BakeTestItem, self).repr_failure(excinfo) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
111 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
112 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
113 class ExpectedBakeOutputError(Exception): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
114 pass |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
115 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
116 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
117 def _add_mock_files(fs, parent_path, spec): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
118 for name, subspec in spec.items(): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
119 path = os.path.join(parent_path, name) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
120 if isinstance(subspec, str): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
121 fs.withFile(path, subspec) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
122 elif isinstance(subspec, dict): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
123 _add_mock_files(fs, path, subspec) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
124 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
125 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
126 def _compare_dicts(left, right, basepath=''): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
127 key_diff = set(left.keys()) ^ set(right.keys()) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
128 if key_diff: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
129 extra_left = set(left.keys()) - set(right.keys()) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
130 if extra_left: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
131 return (["Left contains more items: "] + |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
132 ['- %s/%s' % (basepath, k) for k in extra_left]) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
133 extra_right = set(right.keys()) - set(left.keys()) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
134 if extra_right: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
135 return (["Right contains more items: "] + |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
136 ['- %s/%s' % (basepath, k) for k in extra_right]) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
137 return ["Unknown difference"] |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
138 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
139 for key in left.keys(): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
140 lv = left[key] |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
141 rv = right[key] |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
142 childpath = basepath + '/' + key |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
143 if type(lv) != type(rv): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
144 return (["Different items: ", |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
145 "%s/%s: %s" % (basepath, key, pprint.pformat(lv)), |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
146 "%s/%s: %s" % (basepath, key, pprint.pformat(rv))]) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
147 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
148 if isinstance(lv, dict): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
149 r = _compare_dicts(lv, rv, childpath) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
150 if r: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
151 return r |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
152 elif isinstance(lv, list): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
153 r = _compare_lists(lv, rv, childpath) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
154 if r: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
155 return r |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
156 elif lv != rv: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
157 return (["Different items: ", |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
158 "%s/%s: %s" % (basepath, key, pprint.pformat(lv)), |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
159 "%s/%s: %s" % (basepath, key, pprint.pformat(rv))]) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
160 return None |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
161 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
162 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
163 def _compare_lists(left, right): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
164 for i in range(min(len(left), len(right))): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
165 l = left[i] |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
166 r = right[i] |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
167 if type(l) != type(r): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
168 return ['Different items at index %d:' % i, |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
169 pprint.pformat(l), |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
170 pprint.pformat(r)] |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
171 if isinstance(l, dict): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
172 r = _compare_dicts(l, r) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
173 if r: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
174 return r |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
175 elif isinstance(l, list): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
176 r = _compare_lists(l, r) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
177 if r: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
178 return r |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
179 elif l != r: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
180 return ['Different items at index %d:' % i, |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
181 pprint.pformat(l), |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
182 pprint.pformat(r)] |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
183 return None |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
184 |