From 1925fa1a3b3545f5d4e4a582684809e2647b1289 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Tue, 27 Jul 2021 13:08:39 -0400 Subject: [PATCH] Update Test to lazily parse generated content --- tools/generation/lib/template.py | 9 +++++---- tools/generation/lib/test.py | 19 ++++++++++++++----- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/tools/generation/lib/template.py b/tools/generation/lib/template.py index cdc5276e89..a0eb156037 100644 --- a/tools/generation/lib/template.py +++ b/tools/generation/lib/template.py @@ -210,9 +210,10 @@ class Template: return '\n'.join(lines) def expand(self, case_filename, case_name, case_values, encoding): - frontmatter = self._frontmatter(case_filename, case_values) - body = self.expand_regions(self.source, case_values) - assert encoding == 'utf-8' + def get_source(): + frontmatter = self._frontmatter(case_filename, case_values) + body = self.expand_regions(self.source, case_values) + return codecs.encode(frontmatter + '\n' + body, encoding) return Test(self.attribs['meta']['path'] + case_name + '.js', - source=codecs.encode(frontmatter + '\n' + body, encoding)) + dynamic_source=get_source) diff --git a/tools/generation/lib/test.py b/tools/generation/lib/test.py index a984ac887c..ad2eb3dcde 100644 --- a/tools/generation/lib/test.py +++ b/tools/generation/lib/test.py @@ -6,17 +6,23 @@ import os, re from .util.find_comments import find_comments from .util.parse_yaml import parse_yaml +def after_parse(fn): + def wrapper(self, *args, **kwargs): + if self.source is None and self.dynamic_source is not None: + self.source = self.dynamic_source() + self._parse() + return fn(self, *args, **kwargs) + return wrapper + class Test: """Representation of a generated test. Specifies a file location which may or may not exist.""" - def __init__(self, file_name, source=None): + def __init__(self, file_name, dynamic_source=None): self.file_name = file_name - self.source = source + self.dynamic_source = dynamic_source + self.source = None self.attribs = dict(meta=None) - if self.source: - self._parse() - def load(self, prefix = None): location = os.path.join(prefix or '', self.file_name) with open(location, 'r') as handle: @@ -30,6 +36,7 @@ class Test: self.attribs['meta'] = meta break + @after_parse def is_generated(self): if not self.attribs['meta']: return False @@ -40,6 +47,7 @@ class Test: return 'generated' in flags + @after_parse def to_string(self): return '\n'.join([ '/**', @@ -50,6 +58,7 @@ class Test: self.source, '\n']) + @after_parse def write(self, prefix, parents=False): location = os.path.join(prefix, self.file_name) path = os.path.dirname(location)