import hashlib import io from pathlib import Path import tarfile import tempfile import unittest import bundle class BundleTest(unittest.TestCase): def test_notice_superset_is_inert(self): p = bundle.NoticeText() p.feed('
Copyright & authors
Terms') result = ''.join(p.parts) self.assertIn('Copyright & authors', result) self.assertIn('https://example.org/license', result) self.assertNotIn('bad()', result) self.assertNotIn('hidden', result) def test_hash_rejects_changed_cached_source(self): with tempfile.TemporaryDirectory() as name: cache = Path(name) (cache / 'source').write_bytes(b'changed') with self.assertRaisesRegex(ValueError, 'checksum'): bundle.obtain(cache, 'source', 'https://invalid.test', '0' * 64, True) self.assertEqual(bundle.obtain(cache, 'source', '', hashlib.sha256(b'changed').hexdigest(), True), cache / 'source') def test_archive_is_read_not_extracted(self): with tempfile.TemporaryDirectory() as name: path = Path(name) / 'source.tar.gz' with tarfile.open(path, 'w:gz') as archive: member = tarfile.TarInfo('../../LICENSE') data = b'Copyright owner. Permission notice.' member.size = len(data) archive.addfile(member, io.BytesIO(data)) member = tarfile.TarInfo('src/license.html') data = b'#ifdef OPTIONAL\nOptional terms
\n#else\nOther terms
\n#endif' member.size = len(data) archive.addfile(member, io.BytesIO(data)) notice = bundle.notices(path) self.assertIn('Copyright owner', notice) self.assertIn('Optional terms', notice) self.assertIn('Other terms', notice) self.assertNotIn('#ifdef', notice) self.assertEqual(list(Path(name).iterdir()), [path]) if __name__ == '__main__': unittest.main()