|
| 1 | +"""Frozen snapshots retain byte identities and POSIX lock keys across checkouts.""" |
| 2 | +import hashlib |
| 3 | +import json |
| 4 | +import os |
| 5 | +from pathlib import Path, PureWindowsPath |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +import tempfile |
| 10 | +import unittest |
| 11 | +from unittest.mock import patch |
| 12 | + |
| 13 | +ROOT = Path(__file__).resolve().parents[1] |
| 14 | +sys.path.insert(0, str(ROOT / 'scripts')) |
| 15 | +from vendor_skills import files |
| 16 | + |
| 17 | + |
| 18 | +class SnapshotPortabilityTests(unittest.TestCase): |
| 19 | + def test_windows_relative_paths_use_posix_lock_keys(self): |
| 20 | + # Keep actual enumeration and hashing; simulate only Windows path spelling |
| 21 | + # so this platform-dependent failure is reproducible on Linux as well. |
| 22 | + relative_to = Path.relative_to |
| 23 | + def windows_relative(path, *other, **kwargs): |
| 24 | + relative = relative_to(path, *other, **kwargs) |
| 25 | + return PureWindowsPath(*relative.parts) |
| 26 | + expected = json.loads((ROOT / 'skills.lock.json').read_text(encoding='utf-8'))['files'] |
| 27 | + with patch.object(Path, 'relative_to', new=windows_relative): |
| 28 | + observed = files(ROOT) |
| 29 | + self.assertEqual(observed, expected) |
| 30 | + self.assertTrue(all('\\' not in key for key in observed)) |
| 31 | + |
| 32 | + def test_autocrlf_checkout_preserves_exact_frozen_bytes(self): |
| 33 | + env = {**os.environ, 'GIT_CONFIG_NOSYSTEM': '1', 'GIT_CONFIG_GLOBAL': os.devnull} |
| 34 | + with tempfile.TemporaryDirectory(prefix='snapshot-checkout-') as temp: |
| 35 | + source = Path(temp) / 'source';source.mkdir() |
| 36 | + checkout = Path(temp) / 'checkout' |
| 37 | + for directory in ('skills', 'profiles'): |
| 38 | + shutil.copytree(ROOT / directory, source / directory, ignore=shutil.ignore_patterns('__pycache__', '*.pyc')) |
| 39 | + for name in ('sources.json', 'skills.lock.json'): |
| 40 | + shutil.copyfile(ROOT / name, source / name) |
| 41 | + if (ROOT / '.gitattributes').exists(): |
| 42 | + shutil.copyfile(ROOT / '.gitattributes', source / '.gitattributes') |
| 43 | + (source / 'unprotected.txt').write_bytes(b'one\ntwo\n') |
| 44 | + def git(*args, cwd=source): |
| 45 | + result = subprocess.run(['git', '-C', str(cwd), *map(str, args)], env=env, capture_output=True, timeout=30) |
| 46 | + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) |
| 47 | + return result.stdout |
| 48 | + git('init', '-b', 'main') |
| 49 | + git('config', 'core.autocrlf', 'false') |
| 50 | + git('add', '.') |
| 51 | + git( '-c', 'user.name=Fixture', '-c', '[email protected]', 'commit', '-m', 'snapshot fixture') |
| 52 | + git('-c', 'core.autocrlf=true', 'clone', '--no-local', source, checkout) |
| 53 | + # Control confirms the checkout actually converted ordinary text. |
| 54 | + self.assertEqual((checkout / 'unprotected.txt').read_bytes(), b'one\r\ntwo\r\n') |
| 55 | + expected = json.loads((source / 'skills.lock.json').read_text(encoding='utf-8'))['files'] |
| 56 | + self.assertEqual(files(checkout), expected) |
| 57 | + for relative, digest in expected.items(): |
| 58 | + with self.subTest(path=relative): |
| 59 | + self.assertEqual((checkout / relative).read_bytes(), (source / relative).read_bytes()) |
| 60 | + self.assertEqual(hashlib.sha256((checkout / relative).read_bytes()).hexdigest(), digest) |
0 commit comments