Include the current addon, terrain integrations, tools, regression tests and recorded development evidence.
70 lines
4.9 KiB
Python
70 lines
4.9 KiB
Python
from pathlib import Path
|
|
r=Path('/tmp/erri-optional-identity-1509');s=(r/'baseline/tests/test_external_addons.py').read_text();s=s.replace('import copy','import ast\nimport importlib.util\nimport os\nimport re\nimport copy',1);s=s.replace('import external_addons as ext',"TOOLS=Path(os.environ.get('ERR_OPTIONAL_IDENTITY_TOOLS',Path(__file__).resolve().parents[1]/'tools'))\nsys.path.insert(0,str(TOOLS))\nimport resource_references as refs\nimport external_addons as ext",1);s=s.replace("ROOT=Path(__file__).resolve().parents[1]","ROOT=Path(os.environ.get('ERR_REPO',Path(__file__).resolve().parents[1]))",1)
|
|
new=r'''
|
|
class OptionalIdentityTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.name=refs.OPTIONAL_FILE
|
|
self.text=(ROOT/self.name).read_text()
|
|
self.guid='574AA64002B8E308';self.resource='Prefabs/Characters/REAPER_SpectatorCameraHelper.et'
|
|
def rejected_reference(self,name,text):
|
|
self.assertIn((self.guid,self.resource),list(refs.hard_resource_references(name,text)))
|
|
def test_exact_comparison_only_and_every_other_reference_retained(self):
|
|
all_refs=re.findall(r'\{([A-F0-9]{16})\}([^"\n]+)',self.text)
|
|
expected=[pair for pair in all_refs if pair!=(self.guid,self.resource)]
|
|
self.assertEqual(list(refs.hard_resource_references(self.name,self.text)),expected)
|
|
self.assertIn(('37578B1666981FCE','Prefabs/Characters/Core/Character_Base.et'),expected)
|
|
self.rejected_reference('addons/core/Scripts/Game/Other.c',self.text)
|
|
for changed in [
|
|
self.text+'\n', # even a benign source edit needs renewed audit
|
|
self.text.replace('return prefab ==', 'Resource.Load(prefab); return prefab =='),
|
|
self.text.replace('return prefab ==', 'return Resource.Load(').replace(refs.OPTIONAL_RESOURCE+'";',refs.OPTIONAL_RESOURCE+'");'),
|
|
self.text+'\nResource.Load("'+refs.OPTIONAL_RESOURCE+'");',
|
|
self.text+'\nSpawn("'+refs.OPTIONAL_RESOURCE+'");',
|
|
self.text+'\nResourceName extra = "'+refs.OPTIONAL_RESOURCE+'";',
|
|
self.text.replace(refs.OPTIONAL_METHOD,'/*'+refs.OPTIONAL_METHOD+'*/'),
|
|
self.text.replace('CameraSlot(BaseContainer slot)','DifferentMethod(BaseContainer slot)'),
|
|
self.text.replace('if (!ModsPresent()) return false;',''),
|
|
]: self.rejected_reference(self.name,changed)
|
|
unknown=self.text.replace(self.guid,'574AA64002B8E309')
|
|
self.assertIn(('574AA64002B8E309',self.resource),list(refs.hard_resource_references(self.name,unknown)))
|
|
# The optional addon never enters either resource policy or dependency closure.
|
|
value=ext.policy(ROOT)
|
|
self.assertNotIn('5EB139459EBF5C16',ext.ALLOWED)
|
|
self.assertFalse(ext.resource_allowed(value,set(ext.ALLOWED),self.guid,self.resource))
|
|
def repository_resource_loop(self,name,text):
|
|
# Execute the actual dependency-scanner loop without running the full suite.
|
|
source=Path(os.environ.get('ERR_OPTIONAL_CHECK_SOURCE',TOOLS/'check.py')).read_text()
|
|
tree=ast.parse(source)
|
|
loops=[node for node in ast.walk(tree) if isinstance(node,ast.For) and isinstance(node.target,ast.Tuple)
|
|
and [getattr(n,'id',None) for n in node.target.elts]==['g','path']]
|
|
self.assertEqual(len(loops),1)
|
|
code=compile(ast.fix_missing_locations(ast.Module(body=loops,type_ignores=[])),'actual_repository_resource_loop','exec')
|
|
env=dict(re=re,hard_resource_references=refs.hard_resource_references,p=ROOT/name,ROOT=ROOT,s=text,
|
|
resources={},owners={},vanilla=json.loads((ROOT/'docs/vanilla-resources.json').read_text()),
|
|
external=ext.policy(ROOT),visibility={'core':{'core',*ext.ALLOWED}},resource_allowed=ext.resource_allowed)
|
|
exec(code,env)
|
|
def test_actual_repository_resource_loop(self):
|
|
self.repository_resource_loop(self.name,self.text)
|
|
for name,text in [(self.name,self.text+'\nResource.Load("'+refs.OPTIONAL_RESOURCE+'");'),
|
|
('addons/core/Scripts/Game/Other.c',self.text),
|
|
(self.name,self.text.replace('return prefab ==','return Resource.Load(').replace(refs.OPTIONAL_RESOURCE+'";',refs.OPTIONAL_RESOURCE+'");'))]:
|
|
with self.assertRaisesRegex(AssertionError,'Unresolved/mismatched'):
|
|
self.repository_resource_loop(name,text)
|
|
def test_actual_bundle_validator(self):
|
|
path=Path(os.environ.get('ERR_OPTIONAL_BUNDLE_SOURCE',TOOLS/'source_bundle.py'))
|
|
spec=importlib.util.spec_from_file_location('optional_test_source_bundle',path)
|
|
bundle=importlib.util.module_from_spec(spec);spec.loader.exec_module(bundle)
|
|
payload={name:(ROOT/name).read_bytes() for name in bundle.inventory(ROOT)}
|
|
before=ext.policy(ROOT)
|
|
bundle.validate(payload,ROOT)
|
|
self.assertEqual(before,ext.policy(ROOT))
|
|
for changed in [self.text+'\nResource.Load("'+refs.OPTIONAL_RESOURCE+'");',
|
|
self.text+'\nSpawn("'+refs.OPTIONAL_RESOURCE+'");']:
|
|
altered=dict(payload);altered[self.name]=changed.encode()
|
|
with self.assertRaisesRegex(ValueError,'unresolved or nonvanilla'):
|
|
bundle.validate(altered,ROOT)
|
|
|
|
'''
|
|
s=s.replace("if __name__=='__main__':unittest.main()",new+"if __name__=='__main__':unittest.main()")
|
|
(r/'tests/test_external_addons.py').write_text(s)
|