def test_whitespace(self): # pragma: no cover """Checks for problematic trailing whitespace and missing ending newlines.""" EXCLUDE_EXTS = (".gif", ".ico", ".ics", ".jpg", ".lock", ".svg", ".png") repo = Repo(REPO_ROOT) errors = set() prog = re.compile(r"^.*[ \t]+$") # Some sources from beautiful-jekyll have persistent whitespace issues. WHITESPACE_EXCEPTIONS = "docs/_includes/head.html" def paths(tree: Tree, path: Path): for blob in tree.blobs: yield path / blob.name for t in tree.trees: yield from paths(t, path / t.name) def check(path: Path): if path.suffix.lower() in EXCLUDE_EXTS: return rels = str(path.relative_to(REPO_ROOT)) if "__snapshots__" in rels: return with open(path, encoding="utf-8") as file: lastline = None key = None for i, line in enumerate(file.readlines()): rel_path = f"{path.relative_to(REPO_ROOT)}" key = f"{rel_path}:{i + 1}" if prog.match( line) and rel_path not in WHITESPACE_EXCEPTIONS: errors.add(f"\t{key} - trailing whitespace") lastline = line if not rels.endswith("CNAME"): if lastline and not lastline.endswith("\n"): errors.add(f"\t{key} - missing endline") for path in paths(repo.tree(), REPO_ROOT): check(path) for change in repo.index.diff(None): check(REPO_ROOT / change.a_path) if errors: print("Files with trailing whitespace:", file=sys.stderr) # noqa: T001 for error in sorted(errors): print(error, file=sys.stderr) # noqa: T001 assert False, "Trailing whitespace is not allowed."