Skip to content

Reorganize res://test/samples#829

Open
Curtis-Barnhart wants to merge 4 commits intobitwes:mainfrom
Curtis-Barnhart:reorganize-sample-tests
Open

Reorganize res://test/samples#829
Curtis-Barnhart wants to merge 4 commits intobitwes:mainfrom
Curtis-Barnhart:reorganize-sample-tests

Conversation

@Curtis-Barnhart
Copy link
Copy Markdown
Contributor

@Curtis-Barnhart Curtis-Barnhart commented Mar 22, 2026

In #821 you said

Many of these tests have been moved over time. I think this script could actually be deleted. Maybe most/all of the stuff in test/samples.

I was curious how many scripts/tests could be safely deleted, so I wrote a python script that uses regex to search for references to these scripts/tests in the rest of the repository. After manually reviewing the results (there were a few false positives) I removed the scripts/tests that were no longer actual sample tests anywhere in the documentation (some of the scripts are used in gut unit tests as well and cannot be removed). I also renamed one of the files from "test_readme_examples" to "test_guttest_docstring_examples" since the examples it had were moved from the readme to GutTest docstrings a while back.

If you'd like to clean up the test/samples directory in this way, I can just close 821 since the fixes it provides are to tests that no longer exist as actual samples.

Here is the python script (run from the root of the repository):

from os import walk, path, listdir
from dataclasses import dataclass
from re import findall, MULTILINE


@dataclass
class SourceFile:
    name: str  # name of file in test/samples
    file_references: list[str]  # list of places that refer to this filename
    function_references: dict[str, list[str]]  # list of places that refer to each function in this file


# Finds references to the filename/functions of sf in referrer_content
# and populates sf with the data
def find_references_single(sf: SourceFile, referrer_name: str, referrer_content: str):
    referrer_refs = findall(f"^(\\d+):.*{sf.name}", referrer_content, MULTILINE)
    sf.file_references.extend(f"{referrer_name}:{ln}" for ln in referrer_refs)

    # we are only looking for function references in docstrings, not other tests
    if not referrer_name.startswith("./test"):
        for func in sf.function_references.keys():
            func_refs = findall(f"^(\\d+):.*{func}\\(", referrer_content, MULTILINE)
            sf.function_references[func].extend(f"{referrer_name}:{ln}" for ln in func_refs)


# Finds references to the filename/functions of sources in the file with path
# referrer_path and populates each source with that data
def find_references_multiple(sources: list[SourceFile], referrer_path: str):
    with open(referrer_path) as file:
        content = "".join(f"{i + 1}:{l}" for i, l in enumerate(file.readlines()))

    for tf in sources:
        find_references_single(tf, referrer_path, content)


# Loads test functions of all files identified in source_paths.
# Recursively searches repository for references to functions/filenames of those
# source files in other files in the repository and prints results
def find_references_all(source_paths: list[str]):
    sources: list[SourceFile] = []
    for sp in source_paths:
        with open(sp) as file:
            functions = findall("^func (test_\\w+)\\(", file.read(), MULTILINE)
            sources.append(SourceFile(path.basename(sp), [], {f: [] for f in functions}))

    for dirname, _, fnames in walk("."):
        for fname in fnames:
            # we only want to check docstrings or human writen docs (not html, rst, etc)
            if fname.endswith(".gd") or fname.endswith(".md"):
                find_references_multiple(sources, path.join(dirname, fname))

    for i, s in enumerate(sources):
        s.function_references = {k: v for k, v in s.function_references.items() if len(v) > 0}

        if len(s.file_references) != 0 or len(s.function_references) != 0:
            if i != 0:
                print()
            print(s.name)

            if len(s.file_references) != 0:
                print("\tFile References")
                for fref in s.file_references:
                    print("\t\t" + fref)

            if len(s.function_references) != 0:
                print("\tFunc References")
                for func, refs in s.function_references.items():
                    print("\t\t" + func)
                    for r in refs:
                        print("\t\t\t" + r)


if __name__ == "__main__":
    SAMPLE_DIR = "./test/samples/"
    sample_files = listdir("./test/samples/")
    find_references_all([SAMPLE_DIR + sf for sf in sample_files if sf.endswith(".gd")])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant