48 lines
1.9 KiB
Python
48 lines
1.9 KiB
Python
"""Self-contained tests for Code4History scaffold's Python tooling."""
|
|
from __future__ import annotations
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
|
|
def load(name:str):
|
|
path=ROOT/'tools'/f'{name}.py'
|
|
spec=importlib.util.spec_from_file_location(name,path)
|
|
module=importlib.util.module_from_spec(spec);sys.modules[name]=module
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
new_item=load('new_item'); validate=load('validate')
|
|
|
|
class ScaffoldTests(unittest.TestCase):
|
|
def test_real_scaffold_structure(self):
|
|
errors,_=validate.check(ROOT)
|
|
self.assertEqual(errors,[])
|
|
def test_creation_numbering_and_references(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
p=Path(tmp)
|
|
(p/'10_templates').mkdir()
|
|
shutil.copy(ROOT/'10_templates/primary.md',p/'10_templates/primary.md')
|
|
shutil.copy(ROOT/'10_templates/evidence.md',p/'10_templates/evidence.md')
|
|
id1, path1=new_item.create(p,'primary','first-source')
|
|
id2, _=new_item.create(p,'primary','second-source')
|
|
self.assertEqual((id1,id2),('SRC-P-0001','SRC-P-0002'))
|
|
self.assertTrue(path1.read_text().startswith('---\nid: SRC-P-0001'))
|
|
with self.assertRaises(ValueError): new_item.create(p,'evidence','../bad')
|
|
def test_invalid_link_is_detected(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
p=Path(tmp)
|
|
shutil.copytree(ROOT,p/'code4history')
|
|
q=p/'code4history'
|
|
_,path=new_item.create(q,'evidence','unverifiable-note')
|
|
text=path.read_text().replace('source_ids: []','source_ids: [SRC-P-9999]')
|
|
path.write_text(text)
|
|
errors,_=validate.check(q)
|
|
self.assertTrue(any('SRC-P-9999' in e for e in errors))
|
|
|
|
if __name__=='__main__':unittest.main()
|