2017-06-22 17:41:41 -07:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import unittest
|
2017-06-23 20:05:10 -07:00
|
|
|
from pathlib import Path
|
2017-07-02 15:15:48 -07:00
|
|
|
import logging
|
|
|
|
import ast
|
2017-06-23 19:10:26 -07:00
|
|
|
import json
|
2017-08-29 04:30:49 -07:00
|
|
|
|
|
|
|
import types
|
|
|
|
import importlib.machinery
|
|
|
|
loader = importlib.machinery.SourceFileLoader('generate_repository', 'generate_repository')
|
|
|
|
generate_repository = types.ModuleType(loader.name)
|
|
|
|
loader.exec_module(generate_repository)
|
2017-06-22 17:41:41 -07:00
|
|
|
|
2017-07-06 22:12:43 -07:00
|
|
|
logging.basicConfig(level=logging.ERROR,
|
2017-07-02 15:15:48 -07:00
|
|
|
format='%(levelname)8s: %(message)s')
|
|
|
|
|
2017-07-05 18:40:32 -07:00
|
|
|
class TestRepoGeneration(unittest.TestCase):
|
2017-06-22 17:41:41 -07:00
|
|
|
|
2017-06-23 20:05:10 -07:00
|
|
|
helper_path = Path('tests', 'test_helpers')
|
2017-06-29 02:11:43 -07:00
|
|
|
addon_path = helper_path / 'addons'
|
2017-06-22 17:47:38 -07:00
|
|
|
|
2017-06-22 18:00:29 -07:00
|
|
|
def test_extract_blinfo_from_nonexistent(self):
|
2017-06-23 18:53:07 -07:00
|
|
|
test_file = 'file_that_doesnt_exist'
|
2017-07-04 23:56:19 -07:00
|
|
|
with self.assertRaises(FileNotFoundError):
|
2017-08-29 04:30:49 -07:00
|
|
|
generate_repository.extract_blinfo(self.addon_path / test_file)
|
2017-06-22 17:41:41 -07:00
|
|
|
|
2017-08-29 04:30:49 -07:00
|
|
|
def test_generate_repository_from_nonexistent(self):
|
2017-07-02 16:55:17 -07:00
|
|
|
with self.assertRaises(FileNotFoundError):
|
2017-08-29 04:30:49 -07:00
|
|
|
generate_repository.make_repo(Path('in_a_galaxy_far_far_away'), "somename", "someurl")
|
2017-06-23 18:53:07 -07:00
|
|
|
|
2017-07-02 15:15:48 -07:00
|
|
|
# addons which should contain bl_infos
|
|
|
|
yes_blinfo = [
|
2017-07-05 18:40:32 -07:00
|
|
|
f for f in TestRepoGeneration.addon_path.iterdir()
|
2017-07-02 15:15:48 -07:00
|
|
|
if not f.match('*nonaddon*') and not f.match('*invalid_addon*')
|
|
|
|
]
|
|
|
|
# addons which should throw BadAddon because they have no blinfo
|
|
|
|
no_blinfo = [
|
2017-07-05 18:40:32 -07:00
|
|
|
f for f in TestRepoGeneration.addon_path.iterdir()
|
2017-07-02 15:15:48 -07:00
|
|
|
if f.match('*nonaddon*')
|
|
|
|
]
|
2017-06-23 18:53:07 -07:00
|
|
|
|
2017-07-02 15:15:48 -07:00
|
|
|
def generate_good_blinfo_test(test_file: Path):
|
2017-06-23 18:53:07 -07:00
|
|
|
def test(self):
|
2017-08-29 04:30:49 -07:00
|
|
|
reality = generate_repository.extract_blinfo(test_file)
|
2017-07-02 15:15:48 -07:00
|
|
|
with (self.helper_path / 'expected_blinfo').open("r") as f:
|
|
|
|
expectation = ast.literal_eval(f.read())
|
2017-06-23 18:53:07 -07:00
|
|
|
self.assertEqual(expectation, reality)
|
|
|
|
return test
|
|
|
|
|
2017-07-02 15:15:48 -07:00
|
|
|
def generate_bad_blinfo_test(test_file: Path):
|
|
|
|
def test(self):
|
2017-08-29 04:30:49 -07:00
|
|
|
with self.assertRaises(generate_repository.BadAddon):
|
|
|
|
generate_repository.extract_blinfo(test_file)
|
2017-07-02 15:15:48 -07:00
|
|
|
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)
|
|
|
|
|
2017-07-05 18:40:32 -07:00
|
|
|
add_generated_tests(generate_good_blinfo_test, yes_blinfo, TestRepoGeneration)
|
|
|
|
add_generated_tests(generate_bad_blinfo_test, no_blinfo, TestRepoGeneration)
|
2017-06-23 18:53:07 -07:00
|
|
|
|