Inspired by Adam Johnson's test for pending migrations, and of course in conversation with my own love of weird tests, I offer a similar concept: a test for finding stray print statements in your codebase, with a ratchet array for stuff to ignore.

import glob

PATH = "**/*.py"

irrelevant_paths = (
    "/commands/",
    "node_modules",
)

def test_no_print_statements() -> None:
    all_files = glob.glob(PATH, recursive=True)
    relevant_files = [
        filename for filename in all_files
        if not any(
            irrelevant_path in filename
            for irrelevant_path in irrelevant_paths
        )
    ]
    files_with_print = [
        filename for filename in relevant_files
        if "print(" in open(filename).read()
    ]
    assert not files_with_print, f"Print statements found in {files_with_print}"

I anticipate a concern being "this is slow! you're opening thousands of files!", to which I reply: this test is faster than any other test you have that touches a database. That being said, I'm sure there are edge cases or ways to improve it, so please let me know!

Lightning bolt
Subscribe to my newsletter

I publish monthly roundups of everything I've written, plus pictures of my corgi.
© 2024 Justin Duke · All rights reserved · have a nice day.