changeset 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 10520472cc73
children 5a404aa35971
files tests/bakes/test_special_root.yaml tests/conftest.py
diffstat 2 files changed, 63 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/tests/bakes/test_special_root.yaml	Fri Oct 04 11:13:33 2019 -0700
+++ b/tests/bakes/test_special_root.yaml	Fri Oct 04 11:15:11 2019 -0700
@@ -6,5 +6,19 @@
     pages/about.html: 'URL: {{page.url}}, LINK: {{pcurl("missing")}}'
     pages/_index.html: 'URL: {{page.url}}'
 outfiles:
-    about.html: 'URL: /%7Ejohn/public/about.html, LINK: /%7Ejohn/public/missing.html'
-    index.html: 'URL: /%7Ejohn/public/'
+    "{py>=3.7}about.html": 'URL: /~john/public/about.html, LINK: /~john/public/missing.html'
+    "{py>=3.7}index.html": 'URL: /~john/public/'
+    "{py<3.7}about.html": 'URL: /%7Ejohn/public/about.html, LINK: /%7Ejohn/public/missing.html'
+    "{py<3.7}index.html": 'URL: /%7Ejohn/public/'
+---
+config:
+    site:
+        pretty_urls: true
+in:
+    pages/users/~john.html: 'URL: {{page.url}}'
+    pages/users/~john/bio.html: 'URL: {{page.url}}'
+outfiles:
+    "{py>=3.7}users/~john/index.html": 'URL: /users/~john'
+    "{py>=3.7}users/~john/bio/index.html": 'URL: /users/~john/bio'
+    "{py<3.7}users/~john/index.html": 'URL: /users/%7Ejohn'
+    "{py<3.7}users/~john/bio/index.html": 'URL: /users/%7Ejohn/bio'
--- a/tests/conftest.py	Fri Oct 04 11:13:33 2019 -0700
+++ b/tests/conftest.py	Fri Oct 04 11:15:11 2019 -0700
@@ -1,4 +1,5 @@
 import io
+import re
 import sys
 import time
 import pprint
@@ -162,17 +163,27 @@
         if error:
             raise error_type(error)
 
+    pyversion = sys.hexversion
+
     expected_partial_files = spec.get('outfiles')
     if expected_partial_files:
         keys = list(sorted(expected_partial_files.keys()))
         for key in keys:
+
+            cleankey, valid = _eval_pyversion_condition(key)
+            if not valid:
+                logging.getLogger().warning(
+                    "Skipping '%s' because the python version condition "
+                    "was not met" % key)
+                continue
+
             try:
                 actual = fs.getFileEntry('kitchen/_counter/' +
-                                         key.lstrip('/'))
+                                         cleankey.lstrip('/'))
             except Exception as e:
                 lines = print_fs_tree(fs.path('kitchen/_counter'))
                 raise error_type([
-                    "Can't access output file %s: %s" % (key, e),
+                    "Can't access output file %s: %s" % (cleankey, e),
                     "Got output directory:"] +
                     lines)
 
@@ -181,7 +192,7 @@
             # those and I have no idea why.
             actual = actual.rstrip('\n')
             expected = expected.rstrip('\n')
-            cctx.path = key
+            cctx.path = cleankey
             cmpres = _compare_str(expected, actual, cctx)
             if cmpres:
                 raise error_type(cmpres)
@@ -619,3 +630,36 @@
                 "Right '%s': " % ctx.path, right,
                 "Extra items: %r" % right[len(left):]]
 
+
+re_pyver_cond = re.compile(r'^\{py(?P<cmp>[\<\>\=]\=?)(?P<ver>[\d\.]+)\}')
+
+_cur_pyver = (sys.version_info.major, sys.version_info.minor,
+             sys.version_info.micro)
+
+
+def _eval_pyversion_condition(string):
+    m = re_pyver_cond.match(string)
+    if m is None:
+        return string, True
+
+
+    pyver_bits = m.group('ver').split('.')
+    while len(pyver_bits) < 3:
+        pyver_bits.append('0')
+    cmp_ver = tuple([int(b) for b in pyver_bits])
+
+    cmp_op = m.group('cmp')
+    if cmp_op == '>':
+        res = _cur_pyver > cmp_ver
+    elif cmp_op == '>=':
+        res = _cur_pyver >= cmp_ver
+    elif cmp_op == '<':
+        res = _cur_pyver < cmp_ver
+    elif cmp_op == '<=':
+        res = _cur_pyver <= cmp_ver
+    elif cmp_op == '==':
+        res = _cur_pyver == cmp_ver
+    else:
+        raise Exception("Unknown comparison operator: %s" % cmp_op)
+
+    return string[m.end():], res