90 lines
4.0 KiB
Python
90 lines
4.0 KiB
Python
import copy
|
|
import importlib.util
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[3]
|
|
SCRIPT = ROOT / 'harness/agents/review/skills/gitea-code-review/scripts/native_review.py'
|
|
FIXTURES = ROOT / 'harness/tests/review/test_native_review.py'
|
|
|
|
def load(path, name):
|
|
spec = importlib.util.spec_from_file_location(name, path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
class CanonicalJournalTests(unittest.TestCase):
|
|
def test_binding_path_is_stable_and_head_scoped(self):
|
|
m = load(SCRIPT, 'native_review_canonical_test')
|
|
fixtures = load(FIXTURES, 'native_review_fixture_test')
|
|
plan = fixtures.plan_fixture()
|
|
first = m.canonical_journal_path(plan)
|
|
again = m.canonical_journal_path(copy.deepcopy(plan))
|
|
self.assertEqual(first, again)
|
|
changed = copy.deepcopy(plan)
|
|
changed['target']['head'] = 'b' * 40
|
|
changed['body'] = changed['body'].replace('head=' + plan['target']['head'], 'head=' + changed['target']['head'])
|
|
self.assertNotEqual(first, m.canonical_journal_path(changed))
|
|
changed = copy.deepcopy(plan)
|
|
changed['reviewer']['id'] = 43
|
|
self.assertNotEqual(first, m.canonical_journal_path(changed))
|
|
self.assertEqual(first.parts[-2], 'v1')
|
|
self.assertEqual(first.parts[-3], 'native-review-journals')
|
|
|
|
def test_missing_entry_with_presence_anchor_fails_closed(self):
|
|
m = load(SCRIPT, 'native_review_anchor_test')
|
|
fixtures = load(FIXTURES, 'native_review_anchor_fixture_test')
|
|
plan = fixtures.plan_fixture()
|
|
with tempfile.TemporaryDirectory() as td:
|
|
root = Path(td) / 'native-review-journals'
|
|
old = m._journal.root_for
|
|
m._journal.root_for = lambda anchor: root
|
|
try:
|
|
path = m.canonical_journal_path(plan)
|
|
entry = m._journal.open_binding(path, plan, anchor_file=str(SCRIPT),
|
|
origin=m.ORIGIN, validate=m.validate_plan)
|
|
self.assertTrue((entry / 'binding.json').exists())
|
|
import shutil
|
|
shutil.rmtree(entry)
|
|
with self.assertRaises(ValueError):
|
|
m._journal.open_binding(path, plan, anchor_file=str(SCRIPT),
|
|
origin=m.ORIGIN, validate=m.validate_plan)
|
|
finally:
|
|
m._journal.root_for = old
|
|
|
|
def test_deleted_root_with_persistent_anchor_fails_closed(self):
|
|
m = load(SCRIPT, 'native_review_root_loss_test')
|
|
fixtures = load(FIXTURES, 'native_review_root_loss_fixture_test')
|
|
plan = fixtures.plan_fixture()
|
|
with tempfile.TemporaryDirectory() as td:
|
|
root = Path(td) / 'native-review-journals'
|
|
old = m._journal.root_for
|
|
m._journal.root_for = lambda anchor: root
|
|
try:
|
|
path = m.canonical_journal_path(plan)
|
|
m._journal.open_binding(path, plan, anchor_file=str(SCRIPT),
|
|
origin=m.ORIGIN, validate=m.validate_plan)
|
|
import shutil
|
|
shutil.rmtree(root)
|
|
with self.assertRaises(ValueError):
|
|
m._journal.open_binding(path, plan, anchor_file=str(SCRIPT),
|
|
origin=m.ORIGIN, validate=m.validate_plan)
|
|
finally:
|
|
m._journal.root_for = old
|
|
|
|
def test_alternate_path_is_rejected_before_publication(self):
|
|
m = load(SCRIPT, 'native_review_alternate_test')
|
|
fixtures = load(FIXTURES, 'native_review_alternate_fixture_test')
|
|
plan, before, after, exact = fixtures.publication_fixture()
|
|
with tempfile.TemporaryDirectory() as td:
|
|
with self.assertRaises(m.ValidationError):
|
|
m.publish(plan, before, fixtures.FakeAPI(plan, before, after, exact),
|
|
fixtures.FakeLease(), td, declaration={'mode': 'none'})
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|