From d46273563e684bf8445369bd03752ceebf26e1ec Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Thu, 13 Feb 2020 17:48:00 +0100 Subject: Add initial, very basic save/open & library linking blendfile tests. Do not do much for now, but would have been enough to catch the crash introduced the other day in linking code... --- tests/python/CMakeLists.txt | 14 +++++ tests/python/bl_blendfile_io.py | 110 +++++++++++++++++++++++++++++++++++ tests/python/bl_blendfile_liblink.py | 108 ++++++++++++++++++++++++++++++++++ 3 files changed, 232 insertions(+) create mode 100644 tests/python/bl_blendfile_io.py create mode 100644 tests/python/bl_blendfile_liblink.py (limited to 'tests') diff --git a/tests/python/CMakeLists.txt b/tests/python/CMakeLists.txt index f287ba5ed02..cf4438a8544 100644 --- a/tests/python/CMakeLists.txt +++ b/tests/python/CMakeLists.txt @@ -127,6 +127,20 @@ add_blender_test( --python ${CMAKE_CURRENT_LIST_DIR}/bl_id_management.py ) +# ------------------------------------------------------------------------------ +# BLEND IO & LINKING + +add_blender_test( + blendfile_io + --python ${CMAKE_CURRENT_LIST_DIR}/bl_blendfile_io.py -- + --output-dir ${TEST_OUT_DIR}/blendfile_io/ +) + +add_blender_test( + blendfile_liblink + --python ${CMAKE_CURRENT_LIST_DIR}/bl_blendfile_liblink.py +) + # ------------------------------------------------------------------------------ # MODELING TESTS add_blender_test( diff --git a/tests/python/bl_blendfile_io.py b/tests/python/bl_blendfile_io.py new file mode 100644 index 00000000000..493b16b4ab3 --- /dev/null +++ b/tests/python/bl_blendfile_io.py @@ -0,0 +1,110 @@ +# Apache License, Version 2.0 + +# ./blender.bin --background -noaudio --python tests/python/bl_blendfile_io.py +import bpy +import os +import pprint + + +class TestHelper: + + @staticmethod + def id_to_uid(id_data): + return (type(id_data).__name__, + id_data.name, + id_data.users, + id_data.library.filepath if id_data.library else None) + + @classmethod + def blender_data_to_tuple(cls, bdata): + return sorted(tuple((cls.id_to_uid(k), sorted(tuple(cls.id_to_uid(vv) for vv in v))) + for k, v in bdata.user_map().items())) + + @staticmethod + def ensure_path(path): + if not os.path.exists(path): + os.makedirs(path) + + def run_all_tests(self): + for inst_attr_id in dir(self): + if not inst_attr_id.startswith("test_"): + continue + inst_attr = getattr(self, inst_attr_id) + if callable(inst_attr): + inst_attr() + + +class TestBlendFileSaveLoadBasic(TestHelper): + + def __init__(self, args): + self.args = args + + def test_save_load(self): + bpy.ops.wm.read_factory_settings() + bpy.data.meshes.new("OrphanedMesh") + + output_dir = self.args.output_dir + self.ensure_path(output_dir) + output_path = os.path.join(output_dir, "blendfile.blend") + + orig_data = self.blender_data_to_tuple(bpy.data) + + bpy.ops.wm.save_as_mainfile(filepath=output_path, check_existing=False, compress=False) + bpy.ops.wm.open_mainfile(filepath=output_path, load_ui=False) + + read_data = self.blender_data_to_tuple(bpy.data) + + # We have orphaned data, which should be removed by file reading, so there should not be equality here. + assert(orig_data != read_data) + + bpy.data.orphans_purge() + + orig_data = self.blender_data_to_tuple(bpy.data) + + bpy.ops.wm.save_as_mainfile(filepath=output_path, check_existing=False, compress=False) + bpy.ops.wm.open_mainfile(filepath=output_path, load_ui=False) + + read_data = self.blender_data_to_tuple(bpy.data) + + assert(orig_data == read_data) + + + +TESTS = ( + TestBlendFileSaveLoadBasic, + ) + + +def argparse_create(): + import argparse + + # When --help or no args are given, print this help + description = "Test basic IO of blend file." + parser = argparse.ArgumentParser(description=description) + parser.add_argument( + "--output-dir", + dest="output_dir", + default=".", + help="Where to output temp saved blendfiles", + required=False, + ) + + return parser + + +def main(): + args = argparse_create().parse_args() + + for Test in TESTS: + Test(args).run_all_tests() + + +if __name__ == '__main__': + import sys + sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else []) + try: + main() + except: + import traceback + traceback.print_exc() + sys.exit(1) diff --git a/tests/python/bl_blendfile_liblink.py b/tests/python/bl_blendfile_liblink.py new file mode 100644 index 00000000000..51dd989ece1 --- /dev/null +++ b/tests/python/bl_blendfile_liblink.py @@ -0,0 +1,108 @@ +# Apache License, Version 2.0 + +# ./blender.bin --background -noaudio --python tests/python/bl_blendfile_liblink.py +import bpy +import os +import pprint + + +class TestHelper: + + @staticmethod + def id_to_uid(id_data): + return (type(id_data).__name__, + id_data.name, + id_data.users, + id_data.library.filepath if id_data.library else None) + + @classmethod + def blender_data_to_tuple(cls, bdata): + return sorted(tuple((cls.id_to_uid(k), sorted(tuple(cls.id_to_uid(vv) for vv in v))) + for k, v in bdata.user_map().items())) + + @staticmethod + def ensure_path(path): + if not os.path.exists(path): + os.makedirs(path) + + def run_all_tests(self): + for inst_attr_id in dir(self): + if not inst_attr_id.startswith("test_"): + continue + inst_attr = getattr(self, inst_attr_id) + if callable(inst_attr): + inst_attr() + + +class TestBlendLibLinkSaveLoadBasic(TestHelper): + + def __init__(self, args): + self.args = args + + def test_link_save_load(self): + bpy.ops.wm.read_factory_settings() + me = bpy.data.meshes.new("LibMesh") + me.use_fake_user = True + + output_dir = self.args.output_dir + self.ensure_path(output_dir) + output_path = os.path.join(output_dir, "blendlib.blend") + + bpy.ops.wm.save_as_mainfile(filepath=output_path, check_existing=False, compress=False) + + bpy.ops.wm.read_factory_settings() + bpy.data.orphans_purge() + + link_dir = os.path.join(output_path, "Mesh") + bpy.ops.wm.link(directory=link_dir, filename="LibMesh") + + orig_data = self.blender_data_to_tuple(bpy.data) + + output_path = os.path.join(output_dir, "blendfile.blend") + bpy.ops.wm.save_as_mainfile(filepath=output_path, check_existing=False, compress=False) + bpy.ops.wm.open_mainfile(filepath=output_path, load_ui=False) + + read_data = self.blender_data_to_tuple(bpy.data) + + assert(orig_data == read_data) + + + +TESTS = ( + TestBlendLibLinkSaveLoadBasic, + ) + + +def argparse_create(): + import argparse + + # When --help or no args are given, print this help + description = "Test basic IO of blend file." + parser = argparse.ArgumentParser(description=description) + parser.add_argument( + "--output-dir", + dest="output_dir", + default=".", + help="Where to output temp saved blendfiles", + required=False, + ) + + return parser + + +def main(): + args = argparse_create().parse_args() + + for Test in TESTS: + Test(args).run_all_tests() + + +if __name__ == '__main__': + import sys + sys.argv = [__file__] + (sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else []) + try: + main() + except: + import traceback + traceback.print_exc() + sys.exit(1) -- cgit v1.2.3