From 3bb273564e0f0b3666217ba087f319ad5c8d27d7 Mon Sep 17 00:00:00 2001 From: Angelos Evripiotis Date: Wed, 20 Mar 2019 14:03:22 +0000 Subject: test: Add yaml_file_get_provenance Add a new helper - testutils.yaml_file_get_provenance, this will let us make less fragile assertions about the presence of provenance in BuildStream output. --- tests/testutils/__init__.py | 1 + tests/testutils/yaml.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 tests/testutils/yaml.py diff --git a/tests/testutils/__init__.py b/tests/testutils/__init__.py index 173cc7c3e..67378c959 100644 --- a/tests/testutils/__init__.py +++ b/tests/testutils/__init__.py @@ -29,3 +29,4 @@ from .element_generators import create_element_size, update_element_size from .junction import generate_junction from .runner_integration import wait_for_cache_granularity from .python_repo import setup_pypi_repo +from .yaml import yaml_file_get_provenance diff --git a/tests/testutils/yaml.py b/tests/testutils/yaml.py new file mode 100644 index 000000000..4706a27bb --- /dev/null +++ b/tests/testutils/yaml.py @@ -0,0 +1,28 @@ +from buildstream import _yaml + + +# yaml_file_get_provenance() +# +# Load a yaml file and return it's _yaml.Provenance object. +# +# This is useful for checking the provenance in BuildStream output is as +# expected. +# +# Args: +# path (str): The path to the file to be loaded +# shortname (str): How the path should appear in the error +# key (str): Optional key to look up in the loaded file +# indices (list of indexes): Optional index path, in the case of list values +# +# Returns: +# The Provenance of the dict, member or list element +# +def yaml_file_get_provenance(path, shortname, key=None, indices=None): + with open(path) as data: + element_yaml = _yaml.load_data( + data, + _yaml.ProvenanceFile(path, shortname, project=None), + ) + provenance = _yaml.node_get_provenance(element_yaml, key, indices) + assert provenance is not None + return provenance -- cgit v1.2.1