From 9f0ff11421edda6973b7b6884b46cbfaadaf6405 Mon Sep 17 00:00:00 2001 From: gandalf3 Date: Sun, 2 Jul 2017 16:55:17 -0700 Subject: [PATCH] Get all tests for make_repo.py passing --- make_repo.py | 37 ++++---- tests/test_helpers/repo.json | 176 +++++++++++++++++++++++++++++++++++ tests/test_make_repo.py | 21 ++--- 3 files changed, 202 insertions(+), 32 deletions(-) create mode 100644 tests/test_helpers/repo.json diff --git a/make_repo.py b/make_repo.py index f13128a..cad71af 100755 --- a/make_repo.py +++ b/make_repo.py @@ -18,15 +18,14 @@ class BadAddon(Exception): def iter_addons(path: Path) -> (Path, dict): """ - Generator, yields (path, bl_info) of blender addons in `path` (non recursive). + Generator, yields (path, bl_info) of blender addons in `path`. """ pass - # for item in addons_dir.iterdir(): - # base = item.name - # - # yield (base, fname, '.zip') - # else: - # yield (base, item.path, '.py') + for item in path.iterdir(): + try: + yield(item, extract_blinfo(item)) + except BadAddon as err: + log.debug("Skipping '{}': {}".format(item.name, err)) def parse_blinfo(source: str) -> dict: """Parse a python file and return its bl_info dict, if there is one (else return None)""" @@ -80,16 +79,19 @@ def extract_blinfo(item: Path) -> dict: for fname in z.namelist(): # TODO: zips with multiple bl_infos might be a problem, # not sure how such cases should be handled (if at all) + # for now we just break after the first one if fname.endswith('__init__.py'): try: blinfo = parse_blinfo(z.read(fname)) break except BadAddon: continue - raise BadAddon("Zipfile '%s' doesn't contain any readable bl_info dict" % item) + if blinfo is None: + raise BadAddon("Zipfile '%s' doesn't contain a readable bl_info dict" % item) except zipfile.BadZipFile: - # Assume file is + # If it's not a valid zip, assume file is just a normal file + # TODO: this probably blows up inelegantly on corrupted zips with item.open() as f: blinfo = parse_blinfo(f.read()) @@ -101,24 +103,17 @@ def extract_blinfo(item: Path) -> dict: -def make_repo(repopath: Path): - """Make repo.json for files in directory 'repopath'""" +def make_repo(path: Path): + """Make repo.json for files in directory 'path'""" repo_data = {} package_data = [] - if not repopath.is_dir(): - raise FileNotFoundError(repopath) + if not path.is_dir(): + raise FileNotFoundError(path) - for addon_path in repopath.iterdir(): + for addon, bl_info in iter_addons(path): package_datum = {} - addon = addon_path.name - - try: - bl_info = extract_blinfo(addon_path) - except BadAddon as err: - log.warning('Could not extract bl_info from {}: {}'.format(addon_path, err)) - continue if not REQUIRED_KEYS.issubset(set(bl_info)): log.warning( diff --git a/tests/test_helpers/repo.json b/tests/test_helpers/repo.json new file mode 100644 index 0000000..9e8b2b0 --- /dev/null +++ b/tests/test_helpers/repo.json @@ -0,0 +1,176 @@ +{ + "packages": [ + { + "bl_info": { + "author": "Multiple Authors", + "blender": [ + 2, + 76, + 0 + ], + "category": "Add Curve", + "description": "Add extra curve object types", + "location": "View3D > Add > Curve > Extra Objects", + "name": "Extra Objects", + "version": [ + 0, + 1, + 2 + ], + "warning": "", + "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Curve/Curve_Objects" + }, + "type": "addon" + }, + { + "bl_info": { + "author": "testscreenings, PKHG, TrumanBlending", + "blender": [ + 2, + 59, + 0 + ], + "category": "Add Curve", + "description": "Adds generated ivy to a mesh object starting at the 3D cursor", + "location": "View3D > Add > Curve", + "version": [ + 0, + 1, + 2 + ], + "warning": "", + "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Curve/Ivy_Gen" + }, + "type": "addon" + }, + { + "bl_info": { + "author": "testscreenings, PKHG, TrumanBlending", + "blender": [ + 2, + 59, + 0 + ], + "category": "Add Curve", + "description": "Adds generated ivy to a mesh object starting at the 3D cursor", + "location": "View3D > Add > Curve", + "version": [ + 0, + 1, + 2 + ], + "warning": "", + "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Curve/Ivy_Gen" + }, + "type": "addon" + }, + { + "bl_info": { + "author": "Multiple Authors", + "blender": [ + 2, + 76, + 0 + ], + "category": "Add Curve", + "description": "Add extra curve object types", + "location": "View3D > Add > Curve > Extra Objects", + "name": "Extra Objects", + "version": [ + 0, + 1, + 2 + ], + "warning": "", + "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Curve/Curve_Objects" + }, + "type": "addon" + }, + { + "bl_info": { + "author": "testscreenings, PKHG, TrumanBlending", + "blender": [ + 2, + 59, + 0 + ], + "category": "Add Curve", + "description": "Adds generated ivy to a mesh object starting at the 3D cursor", + "location": "View3D > Add > Curve", + "version": [ + 0, + 1, + 2 + ], + "warning": "", + "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Curve/Ivy_Gen" + }, + "type": "addon" + }, + { + "bl_info": { + "author": "testscreenings, PKHG, TrumanBlending", + "blender": [ + 2, + 59, + 0 + ], + "category": "Add Curve", + "description": "Adds generated ivy to a mesh object starting at the 3D cursor", + "location": "View3D > Add > Curve", + "version": [ + 0, + 1, + 2 + ], + "warning": "", + "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Curve/Ivy_Gen" + }, + "type": "addon" + }, + { + "bl_info": { + "author": "Multiple Authors", + "blender": [ + 2, + 76, + 0 + ], + "category": "Add Curve", + "description": "Add extra curve object types", + "location": "View3D > Add > Curve > Extra Objects", + "name": "Extra Objects", + "version": [ + 0, + 1, + 2 + ], + "warning": "", + "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Curve/Curve_Objects" + }, + "type": "addon" + }, + { + "bl_info": { + "author": "Multiple Authors", + "blender": [ + 2, + 76, + 0 + ], + "category": "Add Curve", + "description": "Add extra curve object types", + "location": "View3D > Add > Curve > Extra Objects", + "name": "Extra Objects", + "version": [ + 0, + 1, + 2 + ], + "warning": "", + "wiki_url": "https://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Curve/Curve_Objects" + }, + "type": "addon" + } + ] +} \ No newline at end of file diff --git a/tests/test_make_repo.py b/tests/test_make_repo.py index 254d69b..6584459 100644 --- a/tests/test_make_repo.py +++ b/tests/test_make_repo.py @@ -26,18 +26,20 @@ class test_make_repo(unittest.TestCase): def test_make_repo_valid(self): make_repo.make_repo(self.helper_path / 'addons') repojson = Path.cwd() / 'repo.json' + reference_repojson = self.helper_path / 'repo.json' try: - with repojson.open('r') as f: - json.loads(f.read()) + with repojson.open('r') as repolist_f: + with reference_repojson.open('r') as ref_repolist_f: + repolist = json.loads(repolist_f.read()) + ref_repolist = json.loads(ref_repolist_f.read()) + self.assertEqual(repolist, ref_repolist) finally: repojson.unlink() - self.fail('unfinished test') - def test_make_repo_from_nonexistent(self): - make_repo.make_repo(self.helper_path / 'addons') - self.fail('unfinished test') + with self.assertRaises(FileNotFoundError): + make_repo.make_repo(Path('in_a_galaxy_far_far_away')) # addons which should contain bl_infos yes_blinfo = [ @@ -60,11 +62,8 @@ def generate_good_blinfo_test(test_file: Path): def generate_bad_blinfo_test(test_file: Path): def test(self): - self.assertRaises( - make_repo.BadAddon, - make_repo.extract_blinfo, - test_file - ) + with self.assertRaises(make_repo.BadAddon): + make_repo.extract_blinfo(test_file) return test # Add test method retur