annotate tests/conftest.py @ 452:55026b7bb1bf

tests: Fix Jinja2 test. Looks like Jinja is stripping rendered output, so now that we actually skip Jinja altogether when there's no `{` in the string, we need to adjust the tests.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 07 Jul 2015 19:58:07 -0700
parents c0700c6d9545
children 456db44dcc53
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
385
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
1 import io
86
1cd67680c38c Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import sys
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
3 import time
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
4 import pprint
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
5 import os.path
86
1cd67680c38c Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 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
7 import pytest
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
8 import yaml
385
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
9 import colorama
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
10 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
11 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
12
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 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
15 pass
1cd67680c38c Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16
1cd67680c38c Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17
1cd67680c38c Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 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
19 parser.addoption(
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
20 '--log-debug',
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
21 action='store_true',
86
1cd67680c38c Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 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
23
1cd67680c38c Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24
1cd67680c38c Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 def pytest_configure(config):
1cd67680c38c Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 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
27 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
28 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
29 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
30
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
31
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
32 def pytest_collect_file(parent, path):
397
879b6b5647a8 tests: Move all bakes/cli/servings tests files to have a YAML extension.
Ludovic Chabant <ludovic@chabant.com>
parents: 391
diff changeset
33 if path.ext == '.yaml' and path.basename.startswith("test"):
879b6b5647a8 tests: Move all bakes/cli/servings tests files to have a YAML extension.
Ludovic Chabant <ludovic@chabant.com>
parents: 391
diff changeset
34 category = os.path.basename(path.dirname)
879b6b5647a8 tests: Move all bakes/cli/servings tests files to have a YAML extension.
Ludovic Chabant <ludovic@chabant.com>
parents: 391
diff changeset
35 if category == 'bakes':
391
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
36 return BakeTestFile(path, parent)
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
37 elif category == 'procs':
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
38 return PipelineTestFile(path, parent)
397
879b6b5647a8 tests: Move all bakes/cli/servings tests files to have a YAML extension.
Ludovic Chabant <ludovic@chabant.com>
parents: 391
diff changeset
39 elif category == 'cli':
391
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
40 return ChefTestFile(path, parent)
397
879b6b5647a8 tests: Move all bakes/cli/servings tests files to have a YAML extension.
Ludovic Chabant <ludovic@chabant.com>
parents: 391
diff changeset
41 elif category == 'servings':
391
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
42 return ServeTestFile(path, parent)
347
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
385
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
45 class YamlTestFileBase(pytest.File):
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
46 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
47 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
48 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
49 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
50 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
51 name += '_%s' % item['test_name']
385
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
52 yield self.__item_class__(name, self, item)
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
53
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
54
385
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
55 class YamlTestItemBase(pytest.Item):
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
56 def __init__(self, name, parent, spec):
385
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
57 super(YamlTestItemBase, self).__init__(name, parent)
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
58 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
59
385
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
60 def _prepareMockFs(self):
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
61 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
62
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
63 # Website config.
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
64 config = {
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
65 'site': {
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
66 '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
67 '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
68 '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
69 }
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
70 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
71 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
72 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
73 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
74
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
75 # 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
76 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
77 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
78 _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
79
385
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
80 return fs
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
81
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
82
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
83 def check_expected_outputs(spec, fs, error_type):
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
84 cctx = CompareContext()
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
85 expected_output_files = spec.get('out')
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
86 if expected_output_files:
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
87 actual = fs.getStructure('kitchen/_counter')
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
88 error = _compare_dicts(expected_output_files, actual, cctx)
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
89 if error:
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
90 raise error_type(error)
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
91
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
92 expected_partial_files = spec.get('outfiles')
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
93 if expected_partial_files:
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
94 keys = list(sorted(expected_partial_files.keys()))
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
95 for key in keys:
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
96 try:
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
97 actual = fs.getFileEntry('kitchen/_counter/' +
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
98 key.lstrip('/'))
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
99 except Exception as e:
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
100 raise error_type([
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
101 "Can't access output file %s: %s" % (key, e)])
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
102
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
103 expected = expected_partial_files[key]
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
104 # HACK because for some reason PyYAML adds a new line for
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
105 # those and I have no idea why.
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
106 actual = actual.rstrip('\n')
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
107 expected = expected.rstrip('\n')
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
108 cctx.path = key
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
109 cmpres = _compare_str(expected, actual, cctx)
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
110 if cmpres:
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
111 raise error_type(cmpres)
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
112
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
113
385
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
114 class ChefTestItem(YamlTestItemBase):
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
115 __initialized_logging__ = False
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
116
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
117 def runtest(self):
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
118 if not ChefTestItem.__initialized_logging__:
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
119 colorama.init()
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
120 hdl = logging.StreamHandler(stream=sys.stdout)
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
121 logging.getLogger().addHandler(hdl)
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
122 logging.getLogger().setLevel(logging.INFO)
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
123 ChefTestItem.__initialized_logging__ = True
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
124
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
125 fs = self._prepareMockFs()
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
126
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
127 argv = self.spec['args']
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
128 if isinstance(argv, str):
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
129 argv = argv.split(' ')
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
130
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
131 expected_code = self.spec.get('code', 0)
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
132 expected_out = self.spec.get('out', '')
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
133
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
134 with mock_fs_scope(fs):
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
135 memstream = io.StringIO()
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
136 hdl = logging.StreamHandler(stream=memstream)
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
137 logging.getLogger().addHandler(hdl)
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
138 try:
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
139 from piecrust.main import PreParsedChefArgs, _run_chef
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
140 pre_args = PreParsedChefArgs(
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
141 root=fs.path('/kitchen'))
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
142 exit_code = _run_chef(pre_args, argv)
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
143 finally:
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
144 logging.getLogger().removeHandler(hdl)
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
145
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
146 assert expected_code == exit_code
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
147 assert expected_out == memstream.getvalue()
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
148
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
149 def reportinfo(self):
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
150 return self.fspath, 0, "bake: %s" % self.name
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
151
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
152 def repr_failure(self, excinfo):
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
153 if isinstance(excinfo.value, ExpectedChefOutputError):
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
154 return ('\n'.join(
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
155 ['Unexpected command output. Left is expected output, '
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
156 'right is actual output'] +
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
157 excinfo.value.args[0]))
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
158 return super(ChefTestItem, self).repr_failure(excinfo)
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
159
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
160
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
161 class ExpectedChefOutputError(Exception):
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
162 pass
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
163
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
164
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
165 class ChefTestFile(YamlTestFileBase):
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
166 __item_class__ = ChefTestItem
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
167
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
168
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
169 class BakeTestItem(YamlTestItemBase):
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
170 def runtest(self):
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
171 fs = self._prepareMockFs()
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
172
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
173 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
174 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
175 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
176 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
177 baker = Baker(app, out_dir)
398
af17c143b9ab tests: Fail bake tests with a proper error message when bake fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 397
diff changeset
178 record = baker.bake()
af17c143b9ab tests: Fail bake tests with a proper error message when bake fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 397
diff changeset
179
411
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 399
diff changeset
180 if not record.success:
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 399
diff changeset
181 errors = []
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 399
diff changeset
182 for e in record.entries:
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 399
diff changeset
183 errors += e.getAllErrors()
e7b865f8f335 bake: Enable multiprocess baking.
Ludovic Chabant <ludovic@chabant.com>
parents: 399
diff changeset
184 raise BakeError(errors)
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
185
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
186 check_expected_outputs(self.spec, fs, ExpectedBakeOutputError)
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
187
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
188 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
189 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
190
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
191 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
192 if isinstance(excinfo.value, ExpectedBakeOutputError):
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
193 return ('\n'.join(
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
194 ['Unexpected bake output. Left is expected output, '
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
195 'right is actual output'] +
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
196 excinfo.value.args[0]))
398
af17c143b9ab tests: Fail bake tests with a proper error message when bake fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 397
diff changeset
197 elif isinstance(excinfo.value, BakeError):
af17c143b9ab tests: Fail bake tests with a proper error message when bake fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 397
diff changeset
198 return ('\n'.join(
af17c143b9ab tests: Fail bake tests with a proper error message when bake fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 397
diff changeset
199 ['Errors occured during bake:'] +
af17c143b9ab tests: Fail bake tests with a proper error message when bake fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 397
diff changeset
200 excinfo.value.args[0]))
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
201 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
202
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
203
398
af17c143b9ab tests: Fail bake tests with a proper error message when bake fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 397
diff changeset
204 class BakeError(Exception):
af17c143b9ab tests: Fail bake tests with a proper error message when bake fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 397
diff changeset
205 pass
af17c143b9ab tests: Fail bake tests with a proper error message when bake fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 397
diff changeset
206
af17c143b9ab tests: Fail bake tests with a proper error message when bake fails.
Ludovic Chabant <ludovic@chabant.com>
parents: 397
diff changeset
207
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
208 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
209 pass
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
210
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
211
385
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
212 class BakeTestFile(YamlTestFileBase):
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
213 __item_class__ = BakeTestItem
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
214
2d5f2289885a tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 365
diff changeset
215
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
216 class PipelineTestItem(YamlTestItemBase):
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
217 def runtest(self):
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
218 fs = self._prepareMockFs()
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
219
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
220 from piecrust.processing.pipeline import ProcessorPipeline
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
221 with mock_fs_scope(fs):
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
222 out_dir = fs.path('kitchen/_counter')
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
223 app = fs.getApp()
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
224 pipeline = ProcessorPipeline(app, out_dir)
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
225
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
226 proc_names = self.spec.get('processors')
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
227 if proc_names:
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
228 pipeline.enabled_processors = proc_names
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
229
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
230 record = pipeline.run()
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
231
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
232 if not record.success:
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
233 errors = []
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
234 for e in record.entries:
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
235 errors += e.errors
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
236 raise PipelineError(errors)
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
237
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
238 check_expected_outputs(self.spec, fs, ExpectedPipelineOutputError)
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
239
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
240 def reportinfo(self):
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
241 return self.fspath, 0, "pipeline: %s" % self.name
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
242
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
243 def repr_failure(self, excinfo):
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
244 if isinstance(excinfo.value, ExpectedPipelineOutputError):
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
245 return ('\n'.join(
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
246 ['Unexpected pipeline output. Left is expected output, '
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
247 'right is actual output'] +
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
248 excinfo.value.args[0]))
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
249 elif isinstance(excinfo.value, PipelineError):
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
250 return ('\n'.join(
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
251 ['Errors occured during processing:'] +
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
252 excinfo.value.args[0]))
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
253 return super(PipelineTestItem, self).repr_failure(excinfo)
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
254
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
255
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
256 class PipelineError(Exception):
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
257 pass
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
258
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
259
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
260 class ExpectedPipelineOutputError(Exception):
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
261 pass
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
262
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
263
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
264 class PipelineTestFile(YamlTestFileBase):
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
265 __item_class__ = PipelineTestItem
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
266
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
267
391
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
268 class ServeTestItem(YamlTestItemBase):
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
269 class _TestApp(object):
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
270 def __init__(self, server):
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
271 self.server = server
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
272
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
273 def __call__(self, environ, start_response):
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
274 return self.server._try_run_request(environ, start_response)
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
275
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
276 def runtest(self):
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
277 fs = self._prepareMockFs()
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
278
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
279 url = self.spec.get('url')
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
280 if url is None:
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
281 raise Exception("Missing URL in test spec.")
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
282
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
283 expected_status = self.spec.get('status', 200)
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
284 expected_headers = self.spec.get('headers')
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
285 expected_output = self.spec.get('out')
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
286 expected_contains = self.spec.get('out_contains')
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
287
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
288 from werkzeug.test import Client
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
289 from werkzeug.wrappers import BaseResponse
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
290 from piecrust.serving.server import Server
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
291 with mock_fs_scope(fs):
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
292 server = Server(fs.path('/kitchen'))
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
293 test_app = self._TestApp(server)
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
294 client = Client(test_app, BaseResponse)
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
295 resp = client.get(url)
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
296 assert expected_status == resp.status_code
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
297
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
298 if expected_headers:
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
299 for k, v in expected_headers.items():
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
300 assert v == resp.headers.get(k)
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
301
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
302 actual = resp.data.decode('utf8').rstrip()
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
303 if expected_output:
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
304 assert expected_output.rstrip() == actual
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
305
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
306 if expected_contains:
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
307 assert expected_contains.rstrip() in actual
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
308
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
309 def reportinfo(self):
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
310 return self.fspath, 0, "serve: %s" % self.name
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
311
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
312
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
313 class ServeTestFile(YamlTestFileBase):
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
314 __item_class__ = ServeTestItem
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
315
3e4bb57d8506 tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents: 385
diff changeset
316
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
317 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
318 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
319 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
320 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
321 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
322 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
323 _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
324
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
325
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
326 class CompareContext(object):
439
c0700c6d9545 tests: Fix crash in processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 436
diff changeset
327 def __init__(self, path=None, t=None):
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
328 self.path = path or ''
439
c0700c6d9545 tests: Fix crash in processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 436
diff changeset
329 self.time = t or time.time()
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
330
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
331 def createChildContext(self, name):
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
332 ctx = CompareContext(
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
333 path='%s/%s' % (self.path, name),
439
c0700c6d9545 tests: Fix crash in processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 436
diff changeset
334 t=self.time)
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
335 return ctx
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
336
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
337
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
338 def _compare(left, right, ctx):
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
339 if type(left) != type(right):
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
340 return (["Different items: ",
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
341 "%s: %s" % (ctx.path, pprint.pformat(left)),
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
342 "%s: %s" % (ctx.path, pprint.pformat(right))])
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
343 if isinstance(left, str):
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
344 return _compare_str(left, right, ctx)
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
345 elif isinstance(left, dict):
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
346 return _compare_dicts(left, right, ctx)
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
347 elif isinstance(left, list):
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
348 return _compare_lists(left, right, ctx)
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
349 elif left != right:
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
350 return (["Different items: ",
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
351 "%s: %s" % (ctx.path, pprint.pformat(left)),
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
352 "%s: %s" % (ctx.path, pprint.pformat(right))])
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
353
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
354
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
355 def _compare_dicts(left, right, ctx):
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
356 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
357 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
358 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
359 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
360 return (["Left contains more items: "] +
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
361 ['- %s/%s' % (ctx.path, k) for k in extra_left])
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
362 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
363 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
364 return (["Right contains more items: "] +
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
365 ['- %s/%s' % (ctx.path, k) for k in extra_right])
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
366 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
367
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
368 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
369 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
370 rv = right[key]
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
371 child_ctx = ctx.createChildContext(key)
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
372 cmpres = _compare(lv, rv, child_ctx)
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
373 if cmpres:
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
374 return cmpres
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
375 return None
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
376
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
377
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
378 def _compare_lists(left, right, ctx):
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
379 for i in range(min(len(left), len(right))):
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
380 l = left[i]
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
381 r = right[i]
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
382 cmpres = _compare(l, r, ctx)
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
383 if cmpres:
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
384 return cmpres
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
385 if len(left) > len(right):
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
386 return (["Left '%s' contains more items. First extra item: " %
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
387 ctx.path, left[len(right)]])
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
388 if len(right) > len(left):
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
389 return (["Right '%s' contains more items. First extra item: " %
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
390 ctx.path, right[len(left)]])
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
391 return None
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
392
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
393
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
394 def _compare_str(left, right, ctx):
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
395 if left == right:
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
396 return None
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
397
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
398 test_time_iso8601 = time.strftime('%Y-%m-%dT%H:%M:%SZ',
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
399 time.gmtime(ctx.time))
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
400 replacements = {
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
401 '%test_time_iso8601%': test_time_iso8601}
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
402 for token, repl in replacements.items():
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
403 left = left.replace(token, repl)
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
404 right = right.replace(token, repl)
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
405
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
406 for i in range(min(len(left), len(right))):
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
407 if left[i] != right[i]:
365
a9929e0b8f66 tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents: 351
diff changeset
408 start = max(0, i - 15)
399
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
409 l_end = min(len(left), i + 15)
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
410 r_end = min(len(right), i + 15)
365
a9929e0b8f66 tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents: 351
diff changeset
411
399
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
412 l_str = ''
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
413 l_offset = 0
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
414 for j in range(start, l_end):
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
415 c = repr(left[j]).strip("'")
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
416 l_str += c
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
417 if j < i:
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
418 l_offset += len(c)
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
419
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
420 r_str = ''
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
421 r_offset = 0
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
422 for j in range(start, r_end):
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
423 c = repr(right[j]).strip("'")
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
424 r_str += c
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
425 if j < i:
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
426 r_offset += len(c)
365
a9929e0b8f66 tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents: 351
diff changeset
427
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
428 return ["Items '%s' differ at index %d:" % (ctx.path, i), '',
365
a9929e0b8f66 tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents: 351
diff changeset
429 "Left:", left, '',
a9929e0b8f66 tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents: 351
diff changeset
430 "Right:", right, '',
a9929e0b8f66 tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents: 351
diff changeset
431 "Difference:",
399
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
432 l_str, (' ' * l_offset + '^'),
a0724af26c12 tests: More accurate marker position for diff'ing strings.
Ludovic Chabant <ludovic@chabant.com>
parents: 398
diff changeset
433 r_str, (' ' * r_offset + '^')]
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
434 if len(left) > len(right):
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
435 return ["Left is longer.",
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
436 "Left '%s': " % ctx.path, left,
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
437 "Right '%s': " % ctx.path, right,
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
438 "Extra items: %r" % left[len(right):]]
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
439 if len(right) > len(left):
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
440 return ["Right is longer.",
436
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
441 "Left '%s': " % ctx.path, left,
2aa879d63133 tests: Add pipeline processing tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 411
diff changeset
442 "Right '%s': " % ctx.path, right,
351
1f22d4b10fef tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents: 347
diff changeset
443 "Extra items: %r" % right[len(left):]]
347
76c838453dbe tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents: 86
diff changeset
444