def run(
    *,
    script_file: str,
    tmpdir_factory: pytest.TempdirFactory,
    arg: str = '',
    setup: str = '',
    additional_intercepts: Iterable[str] = (),
) -> subprocess.CompletedProcess:
    """Invokes the given script within a temporary test environment."""

    with open(script_file, encoding="utf8") as f:
        script_lines = f.readlines()

    # Create a unique temporary directory
    dir_path = tmpdir_factory.mktemp('tmp', numbered=True)
    file_path = os.path.join(dir_path, 'test-script.sh')

    intercepted = [
        'python',
        'python3',
        'pylint',
        'env',
        'pytest',
        'mypy',
        'black',
        *additional_intercepts,
    ]
    assert script_lines[0] == '#!/usr/bin/env bash\n'
    for e in intercepted:
        script_lines.insert(1, e + '() {\n  echo INTERCEPTED ' + e + ' $@\n}\n')

    with open(file_path, 'w', encoding="utf8") as f:
        f.writelines(script_lines)

    cmd = f"""
export GIT_CONFIG_GLOBAL=/dev/null
export GIT_CONFIG_SYSTEM=/dev/null
dir=$(git rev-parse --show-toplevel)
cd {dir_path}
git init --quiet --initial-branch main
git config --local user.name 'Me'
git config --local user.email '<>'
git commit -m init --allow-empty --quiet --no-gpg-sign
{setup}
mkdir -p dev_tools
touch dev_tools/pypath
chmod +x ./test-script.sh
./test-script.sh {arg}
"""
    return shell_tools.run(
        cmd, log_run_to_stderr=False, shell=True, check=False, capture_output=True
    )
Invokes the given script within a temporary test environment.
dev_tools/bash_scripts_test.py