Mercurial > piecrust2
comparison tests/conftest.py @ 1169:978ed6deea91
tests: Add ability to test different expected outputs based on Python version.
Similar to the previous change, there was also a change in what gets escaped
by `urllib.parse.quote` (as in: the tilde sign ('~') stopped being escaped).
So now we need to be able to test different outputs in the tests, yay again.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 04 Oct 2019 11:15:11 -0700 |
parents | cf6b2bf042fb |
children | 161cba5d031a |
comparison
equal
deleted
inserted
replaced
1168:10520472cc73 | 1169:978ed6deea91 |
---|---|
1 import io | 1 import io |
2 import re | |
2 import sys | 3 import sys |
3 import time | 4 import time |
4 import pprint | 5 import pprint |
5 import os.path | 6 import os.path |
6 import logging | 7 import logging |
160 actual = fs.getStructure('kitchen/_counter') | 161 actual = fs.getStructure('kitchen/_counter') |
161 error = _compare_dicts(expected_output_files, actual, cctx) | 162 error = _compare_dicts(expected_output_files, actual, cctx) |
162 if error: | 163 if error: |
163 raise error_type(error) | 164 raise error_type(error) |
164 | 165 |
166 pyversion = sys.hexversion | |
167 | |
165 expected_partial_files = spec.get('outfiles') | 168 expected_partial_files = spec.get('outfiles') |
166 if expected_partial_files: | 169 if expected_partial_files: |
167 keys = list(sorted(expected_partial_files.keys())) | 170 keys = list(sorted(expected_partial_files.keys())) |
168 for key in keys: | 171 for key in keys: |
172 | |
173 cleankey, valid = _eval_pyversion_condition(key) | |
174 if not valid: | |
175 logging.getLogger().warning( | |
176 "Skipping '%s' because the python version condition " | |
177 "was not met" % key) | |
178 continue | |
179 | |
169 try: | 180 try: |
170 actual = fs.getFileEntry('kitchen/_counter/' + | 181 actual = fs.getFileEntry('kitchen/_counter/' + |
171 key.lstrip('/')) | 182 cleankey.lstrip('/')) |
172 except Exception as e: | 183 except Exception as e: |
173 lines = print_fs_tree(fs.path('kitchen/_counter')) | 184 lines = print_fs_tree(fs.path('kitchen/_counter')) |
174 raise error_type([ | 185 raise error_type([ |
175 "Can't access output file %s: %s" % (key, e), | 186 "Can't access output file %s: %s" % (cleankey, e), |
176 "Got output directory:"] + | 187 "Got output directory:"] + |
177 lines) | 188 lines) |
178 | 189 |
179 expected = expected_partial_files[key] | 190 expected = expected_partial_files[key] |
180 # HACK because for some reason PyYAML adds a new line for | 191 # HACK because for some reason PyYAML adds a new line for |
181 # those and I have no idea why. | 192 # those and I have no idea why. |
182 actual = actual.rstrip('\n') | 193 actual = actual.rstrip('\n') |
183 expected = expected.rstrip('\n') | 194 expected = expected.rstrip('\n') |
184 cctx.path = key | 195 cctx.path = cleankey |
185 cmpres = _compare_str(expected, actual, cctx) | 196 cmpres = _compare_str(expected, actual, cctx) |
186 if cmpres: | 197 if cmpres: |
187 raise error_type(cmpres) | 198 raise error_type(cmpres) |
188 | 199 |
189 | 200 |
617 return ["Right is longer.", | 628 return ["Right is longer.", |
618 "Left '%s': " % ctx.path, left, | 629 "Left '%s': " % ctx.path, left, |
619 "Right '%s': " % ctx.path, right, | 630 "Right '%s': " % ctx.path, right, |
620 "Extra items: %r" % right[len(left):]] | 631 "Extra items: %r" % right[len(left):]] |
621 | 632 |
633 | |
634 re_pyver_cond = re.compile(r'^\{py(?P<cmp>[\<\>\=]\=?)(?P<ver>[\d\.]+)\}') | |
635 | |
636 _cur_pyver = (sys.version_info.major, sys.version_info.minor, | |
637 sys.version_info.micro) | |
638 | |
639 | |
640 def _eval_pyversion_condition(string): | |
641 m = re_pyver_cond.match(string) | |
642 if m is None: | |
643 return string, True | |
644 | |
645 | |
646 pyver_bits = m.group('ver').split('.') | |
647 while len(pyver_bits) < 3: | |
648 pyver_bits.append('0') | |
649 cmp_ver = tuple([int(b) for b in pyver_bits]) | |
650 | |
651 cmp_op = m.group('cmp') | |
652 if cmp_op == '>': | |
653 res = _cur_pyver > cmp_ver | |
654 elif cmp_op == '>=': | |
655 res = _cur_pyver >= cmp_ver | |
656 elif cmp_op == '<': | |
657 res = _cur_pyver < cmp_ver | |
658 elif cmp_op == '<=': | |
659 res = _cur_pyver <= cmp_ver | |
660 elif cmp_op == '==': | |
661 res = _cur_pyver == cmp_ver | |
662 else: | |
663 raise Exception("Unknown comparison operator: %s" % cmp_op) | |
664 | |
665 return string[m.end():], res |