Files
FolderShare/scripts/tests/test_testtask.py
T

79 lines
3.7 KiB
Python

"""Safety checks: python3 -m unittest discover -s scripts/tests -v."""
import importlib.util
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[2]
os.environ.update(ROBORTO_PROJECT_ROOT=str(ROOT), ROBORTO_RUN_DIR='/tmp/testtask-unit-run', ROBORTO_TASK_ID='002', ROBORTO_WORKTREE=str(ROOT / '.test-worktrees/task-002'))
spec = importlib.util.spec_from_file_location('adapter', ROOT / 'scripts/testtask_adapter.py')
adapter = importlib.util.module_from_spec(spec)
spec.loader.exec_module(adapter)
class AdapterTests(unittest.TestCase):
def test_snapshot_excludes_local_data_and_links(self):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
subprocess.run(['git', 'init', '-q', str(root)], check=True)
for name in ['api/source.py', 'api/new_test.py', 'api/.env', 'api/accounts.sqlite', 'api/share/movie.mp4', 'react/node_modules/package/code.js', 'docs/snapshots/old.zip', 'api/id_rsa', '.roborto/testtask.conf']:
path = root / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text('fixture')
(root / 'api/link.py').symlink_to(root / 'api/source.py')
subprocess.run(['git', '-C', str(root), 'add', 'api/source.py'], check=True)
(root / 'api/source.py').write_text('unstaged change')
files = adapter.safe_files(root)
self.assertEqual(set(files), {'api/source.py', 'api/new_test.py', '.roborto/testtask.conf'})
(root / 'api/new_test.py').write_text('updated')
self.assertNotEqual(files, adapter.safe_files(root))
def test_cleanup_preserves_foreign_snapshot(self):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
snapshot = root / '.test-worktrees/task-002'
snapshot.mkdir(parents=True)
marker = snapshot / adapter.MARKER
marker.write_text('/other/session')
with patch.object(adapter, 'ROOT', root), patch.object(adapter, 'SNAPSHOT', snapshot):
adapter.cleanup()
self.assertTrue(snapshot.exists())
marker.write_text(str(adapter.RUN))
adapter.cleanup()
adapter.cleanup()
self.assertFalse(snapshot.exists())
def test_cleanup_rejects_unexpected_path(self):
with patch.object(adapter, 'SNAPSHOT', Path('/tmp/unowned')):
with self.assertRaises(RuntimeError):
adapter.cleanup()
def test_existing_snapshot_is_never_replaced(self):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
snapshot = root / '.test-worktrees/task-002'
snapshot.mkdir(parents=True)
with patch.object(adapter, 'ROOT', root), patch.object(adapter, 'SNAPSHOT', snapshot):
with self.assertRaisesRegex(RuntimeError, 'already exists'):
adapter.validate()
def test_malformed_task_rejected(self):
with patch.object(adapter, 'TASK', '../002'):
with self.assertRaises(RuntimeError):
adapter.validate()
def test_task_discovery(self):
output = b'worktree /tmp/TASK-002\0HEAD abc\0branch refs/heads/task/TASK-002-authorization\0\0'
with patch.object(adapter.subprocess, 'check_output', return_value=output):
self.assertEqual(adapter.source(), Path('/tmp/TASK-002'))
with patch.object(adapter, 'TASK', '999'):
with self.assertRaisesRegex(RuntimeError, 'found 0'):
adapter.source()
if __name__ == '__main__':
unittest.main()