This repository has been archived on 2023-02-07. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-package-manager-addon/tests/test_make_repo.py
2017-07-02 16:55:17 -07:00

81 lines
2.6 KiB
Python

#!/usr/bin/env python3
import unittest
from pathlib import Path
import logging
import ast
import json
import make_repo
logging.basicConfig(level=logging.DEBUG,
format='%(levelname)8s: %(message)s')
class test_make_repo(unittest.TestCase):
helper_path = Path('tests', 'test_helpers')
addon_path = helper_path / 'addons'
def test_extract_blinfo_from_nonexistent(self):
test_file = 'file_that_doesnt_exist'
self.assertRaises(
FileNotFoundError,
make_repo.extract_blinfo,
self.addon_path / test_file
)
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 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()
def test_make_repo_from_nonexistent(self):
with self.assertRaises(FileNotFoundError):
make_repo.make_repo(Path('in_a_galaxy_far_far_away'))
# addons which should contain bl_infos
yes_blinfo = [
f for f in test_make_repo.addon_path.iterdir()
if not f.match('*nonaddon*') and not f.match('*invalid_addon*')
]
# addons which should throw BadAddon because they have no blinfo
no_blinfo = [
f for f in test_make_repo.addon_path.iterdir()
if f.match('*nonaddon*')
]
def generate_good_blinfo_test(test_file: Path):
def test(self):
reality = make_repo.extract_blinfo(test_file)
with (self.helper_path / 'expected_blinfo').open("r") as f:
expectation = ast.literal_eval(f.read())
self.assertEqual(expectation, reality)
return test
def generate_bad_blinfo_test(test_file: Path):
def test(self):
with self.assertRaises(make_repo.BadAddon):
make_repo.extract_blinfo(test_file)
return test
# Add test method retur
def add_generated_tests(test_generator, params, destclass):
"""
Add a test method (as returned by 'test_generator') to destclass for every param
"""
for param in params:
test_func = test_generator(param)
setattr(destclass, 'test_{}'.format(param), test_func)
add_generated_tests(generate_good_blinfo_test, yes_blinfo, test_make_repo)
add_generated_tests(generate_bad_blinfo_test, no_blinfo, test_make_repo)