derrickaw commented on code in PR #35914: URL: https://github.com/apache/beam/pull/35914#discussion_r2292582922
########## sdks/python/apache_beam/yaml/examples/testing/examples_test.py: ########## @@ -1175,6 +1251,51 @@ def _batch_log_analysis_test_preprocessor( return test_spec +@YamlExamplesTestSuite.register_test_preprocessor( + ['test_wordCountInclude_yaml']) +def _jinja_preprocessor(raw_spec_string: str): + """ + Preprocessor for Jinja-based YAML tests. + + This function takes a raw YAML string, which is treated as a Jinja2 + template, and renders it to produce the final pipeline specification. + It specifically handles templates that use the `{% include ... %}` + directive by manually loading the content of the included files from the + filesystem. + + The Jinja variables required for rendering are loaded from a predefined + data source. + + Args: + raw_spec_string: A string containing the raw YAML content, which is a + Jinja2 template. + + Returns: + A string containing the fully rendered YAML pipeline specification. + """ + + jinja_variables = json.loads(input_data.word_count_jinja_parameter_data()) + + test_file_dir = os.path.dirname(__file__) + sdk_root = os.path.abspath(os.path.join(test_file_dir, '../../../..')) + + include_files = input_data.word_count_jinja_template_data() + mock_templates = {'main_template': raw_spec_string} + for file_path in include_files: + full_path = os.path.join(sdk_root, file_path) + with open(full_path, 'r', encoding='utf-8') as f: + mock_templates[file_path] = f.read() + + # Can't use the standard expand_jinja method due to it not supporting + # `% include` jinja templization. + # TODO: Maybe update expand_jinja to handle this case. Review Comment: added todo link -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org