tests/test_no_leaked_terms.py — Data Room Toolkit

From version 0.1.5 of Data Room Toolkit, shown publicly by its creator. 4.2 KiB · SHA-256 a5343bba651c76d6dc080181fff148ec2754317bb820c175b2c35799fbff3708.

# By Zero Width Lab (zerowidthlab.com)
"""Publication hygiene checks for shipped files."""
from __future__ import annotations
import ast
import re
import unicodedata
import unittest
from pathlib import Path
__author__ = "Zero Width Lab"
ROOT =Path(__file__).resolve().parent.parent
SHIPPED_SUFFIXES ={'.py','.md','.json','.txt'}
GUID =r'\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b'
EMAIL =r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}'
HOME_PATH ='(?<![a-z])/(?:Users|home)/[A-Za-z]'
TENANT_HOSTS =('onmicrosoft.com','.sharepoint.com')
PLACEHOLDER_HOST_SUFFIXES =('example.com','example.org','example.net')
PLACEHOLDER_TENANT_HOSTS =('<tenant>.sharepoint.com','<tenant>-my.sharepoint.com')
SYNTHETIC_TEST_TENANT_HOSTS =('contoso-my.sharepoint.com','contoso.sharepoint.com','a.b.sharepoint.com')

def shipped_files() -> list[Path]:
 return sorted((path for path in ROOT.rglob('*') if path.is_file() and path.suffix.lower() in SHIPPED_SUFFIXES and ('.git' not in path.parts) and ('__pycache__' not in path.parts) and ('packaging' not in path.relative_to(ROOT).parts) and (path != Path(__file__).resolve())))

class PublicationHygieneTests(unittest.TestCase):

 def setUp(self) -> None:
  self.files =shipped_files()
  self.assertTrue(self.files)

 def test_signatures_and_safe_source(self) -> None:
  signature ='By Zero Width Lab (zerowidthlab.com)'
  forbidden ={'eval','exec','compile','os.system','pickle','ctypes','re.compile'}
  for path in self.files:
   text =path.read_text(encoding='utf-8')
   with self.subTest(path=path):
    self.assertFalse(any((unicodedata.category(c) =='Cf' for c in text)))
    if path.suffix =='.md':
     self.assertEqual(text.rstrip().splitlines()[-1],signature)
    if path.suffix =='.py':
     self.assertIn(signature,'\n'.join(text.splitlines()[:5]))
     self.assertIn('__author__ = "Zero Width Lab"', text)
     tree =ast.parse(text)
     for node in ast.walk(tree):
      if isinstance(node,ast.Call):
       self.assertNotIn(ast.unparse(node.func),forbidden)
      if isinstance(node,(ast.Import,ast.ImportFrom)):
       names =[n.name for n in node.names] if isinstance(node,ast.Import) else [node.module]
       self.assertFalse(set(names) & {'pickle','ctypes'})

 def test_scope_and_local_operations_documented(self):
  import json
  self.assertIn('"/" or empty means the whole library',(ROOT / 'references/config-schema.md').read_text())
  operations =json.loads((ROOT / 'source-package.json').read_text())['operations']
  self.assertTrue(any(o['kind'] =='read' and 'local source files' in o['description'] for o in operations))
  self.assertTrue(any(o['kind'] =='create' and '--out' in o['description'] and 'nothing is overwritten' in o['description'] for o in operations))

 def test_hygiene_patterns(self):
  for pattern,yes,no in ((GUID,'12345678-1234-1234-1234-123456789abc','x12345678-1234-1234-1234-123456789abcx'),(EMAIL,'reader@example.com','reader@exampleXcom'),(HOME_PATH,'/'+ 'Users/Example/file','/'+ 'Users/123/file')):
   self.assertIsNotNone(re.search(pattern,yes))
   self.assertIsNone(re.search(pattern,no))

 def test_no_guids_outside_tests(self) -> None:
  for path in self.files:
   if 'tests' in path.relative_to(ROOT).parts:
    continue
   with self.subTest(path=path):
    self.assertIsNone(re.search(GUID,path.read_text(encoding='utf-8'),re.IGNORECASE))

 def test_only_placeholder_email_addresses(self) -> None:
  for path in self.files:
   for match in re.findall(EMAIL,path.read_text(encoding='utf-8')):
    with self.subTest(path=path,address=match):
     self.assertTrue(match.lower().endswith(PLACEHOLDER_HOST_SUFFIXES))

 def test_no_tenant_hosts_or_home_paths(self) -> None:
  for path in self.files:
   text =path.read_text(encoding='utf-8')
   lowered =text.lower()
   for host in TENANT_HOSTS:
    with self.subTest(path=path,host=host):
     stripped =lowered
     for placeholder in PLACEHOLDER_TENANT_HOSTS:
      stripped =stripped.replace(placeholder,'')
     if 'tests' in path.relative_to(ROOT).parts:
      for synthetic in SYNTHETIC_TEST_TENANT_HOSTS:
       stripped =stripped.replace(synthetic,'')
     self.assertNotIn(host,stripped)
   with self.subTest(path=path,pattern='home path'):
    self.assertIsNone(re.search(HOME_PATH,text))
if __name__ =='__main__':
 unittest.main()

License: MIT (read it).

MITMIT License

Copyright (c) 2026 Zero Width Lab

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Back to the listing