import importlib.util from pathlib import Path import tempfile import unittest SPEC = importlib.util.spec_from_file_location( 'public_repository', Path(__file__).resolve().parents[1] / 'check_public_repository.py') CHECKER = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(CHECKER) class PublicRepositoryTest(unittest.TestCase): def setUp(self): self.temporary = tempfile.TemporaryDirectory() self.addCleanup(self.temporary.cleanup) self.root = Path(self.temporary.name) def write(self, name, text='local value'): path = self.root / name path.parent.mkdir(parents=True, exist_ok=True) path.write_text(text) def test_credential_report_does_not_echo_secret(self): secret = 'gh' + 'p_' + 'a' * 40 self.write('config.txt', 'token=' + secret) errors = CHECKER.check_files(self.root, ['config.txt']) self.assertEqual(len(errors), 1) self.assertIn('config.txt:1', errors[0]) self.assertNotIn(secret, errors[0]) def test_local_inputs_are_rejected_but_examples_are_allowed(self): names = ['.env', '.env.production', 'android/key.properties', '.local/config.json', 'nested/build/output.txt', 'example.cutdex/project.json', 'signing.p12'] for name in names + ['.env.example', '.env.production.sample']: self.write(name) self.assertEqual(len(CHECKER.check_files(self.root, names)), len(names)) self.assertEqual(CHECKER.check_files( self.root, ['.env.example', '.env.production.sample']), []) def test_links_allow_urls_fragments_and_relative_files(self): self.write('docs/guide.md', '[file](../LICENSE) [section](#intro) ' '[web](https://example.com) [missing](missing.md)') self.write('LICENSE') errors = CHECKER.check_files(self.root, ['docs/guide.md']) self.assertEqual(errors, ['docs/guide.md: missing or external local link missing.md']) def test_symlinks_cannot_publish_external_files(self): (self.root / 'external').symlink_to(self.root.parent) self.assertEqual(CHECKER.check_files(self.root, ['external']), ['external: symlink escapes repository']) def test_deleted_worktree_file_is_not_published(self): self.assertEqual(CHECKER.check_files(self.root, ['deleted.txt']), []) if __name__ == '__main__': unittest.main()