generate_folders.py


Overview

`generate_folders.py` is a utility script designed to automate the creation of nested folder structures populated with Python test files. It generates directories and test files according to user-specified numeric arguments, supporting multi-level folder hierarchies with customizable breadth and depth.

The script reads a predefined test file template (`template_test.py`) from the same directory and replicates its content into newly created test files or `__init__.py` files inside generated folders. This setup is useful for creating a scaffold of test modules for large Python projects, facilitating bulk test organization and initial setup.


Detailed Explanation

Constants and Globals


Function: generate_folders

def generate_folders(root: pathlib.Path, elements: int, *more_numbers: int) -> None:

Purpose:

Recursively generates a folder hierarchy under the specified `root` path based on the numeric parameters provided.

Parameters:

Behavior:

Return Value:

Usage Example:

from pathlib import Path

# Generate 5 folders under "./tests", each containing 10 test files
generate_folders(Path("./tests"), 5, 10)

This will create:

./tests/
    foo_0/
        test_0.py
        test_1.py
        ...
        test_9.py
        __init__.py
    foo_1/
        ...
    ...
    foo_4/
        ...

Script Entry Point

if __name__ == "__main__":
    args = parser.parse_args()
    generate_folders(HERE, *(args.numbers or (10, 100)))

**Example CLI Usage:**

python generate_folders.py 3 5 2

This generates 3 folders at the root level, each containing 5 subfolders, each of those containing 2 test files.


Implementation Details and Algorithms


Interaction with Other Parts of the System


Mermaid Diagram: Flowchart of Main Functions and Workflow

flowchart TD
    A[Start: Script Execution] --> B{Parse CLI Args}
    B --> |Args Provided| C[Call generate_folders(HERE, *args.numbers)]
    B --> |No Args| D[Call generate_folders(HERE, 10, 100)]
    C --> E[generate_folders Function]
    D --> E
    E --> F{more_numbers Present?}
    F --> |Yes| G[Create 'foo_i' folders with __init__.py]
    G --> H[Recursive call generate_folders(subfolder, *more_numbers)]
    H --> F
    F --> |No| I[Create 'test_i.py' files]
    I --> J[Write TEST_CONTENT into files]
    J --> K[End]

Summary

`generate_folders.py` is a recursive folder and test file generator that simplifies the setup of nested Python test module structures. It leverages a template test file and supports flexible multi-level generation defined by command-line numeric arguments. This utility is a practical aid for developers managing large test suites or needing automated scaffolding of test directories.