diff options
author | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-10-23 15:25:26 +0900 |
---|---|---|
committer | Tristan Van Berkom <tristan.vanberkom@codethink.co.uk> | 2017-10-23 15:28:07 +0900 |
commit | 8f8009e3c9b02324622f1e4d3460faace5e6e942 (patch) | |
tree | eca69a6f63f764915f605c85d9649a731e2af3c7 /tests/cachekey/update.py | |
parent | 12dc790ea5b94c758ffa0897a0df19dde0dcf584 (diff) | |
download | buildstream-8f8009e3c9b02324622f1e4d3460faace5e6e942.tar.gz |
tests/cachekey: Refactoring to now include an automatic updater
When cache key tests fail, and when it is intentional that
the keys have changed, then run ./tests/cachekey/update.py
to update the keys.
This is also useful whenever adding new tests.
Diffstat (limited to 'tests/cachekey/update.py')
-rwxr-xr-x | tests/cachekey/update.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/cachekey/update.py b/tests/cachekey/update.py new file mode 100755 index 000000000..09cf19657 --- /dev/null +++ b/tests/cachekey/update.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 +# +# Automatically create or update the .expected files in the +# cache key test directory. +# +# Simply run without any arguments, from anywhere, e.g.: +# +# ./tests/cachekey/update.py +# +# After this, add any files which were newly created and commit +# the result in order to adjust the cache key test to changed +# keys. +# +import os +import tempfile +from tests.testutils.runcli import Cli + +# This weird try / except is needed, because this will be imported differently +# when pytest runner imports them vs when you run the updater directly from +# this directory. +try: + from cachekey import element_filename, parse_output_keys, load_expected_keys +except ImportError: + from .cachekey import element_filename, parse_output_keys, load_expected_keys + +# Project directory +PROJECT_DIR = os.path.join( + os.path.dirname(os.path.realpath(__file__)), + "project", +) + + +def write_expected_key(element_name, actual_key): + expected_file = element_filename(PROJECT_DIR, element_name, 'expected') + with open(expected_file, 'w') as f: + f.write(actual_key) + + +def update_keys(): + + with tempfile.TemporaryDirectory(dir=PROJECT_DIR) as tmpdir: + directory = os.path.join(tmpdir, 'cache') + os.makedirs(directory) + cli = Cli(directory, verbose=False) + + # Run bst show + result = cli.run(project=PROJECT_DIR, silent=True, args=[ + '--no-colors', + 'show', '--format', '%{name}::%{full-key}', + 'target.bst' + ]) + + # Load the actual keys, and the expected ones if they exist + actual_keys = parse_output_keys(result.output) + expected_keys = load_expected_keys(PROJECT_DIR, actual_keys, raise_error=False) + + for element_name in actual_keys: + expected = element_filename(PROJECT_DIR, element_name, 'expected') + + if actual_keys[element_name] != expected_keys[element_name]: + if not expected_keys[element_name]: + print("Creating new expected file: {}".format(expected)) + else: + print("Updating expected file: {}".format(expected)) + + write_expected_key(element_name, actual_keys[element_name]) + +if __name__ == '__main__': + update_keys() |